Deploy Kubernetes on Linode with the k8s-alpha CLI

Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Deprecated

This guide has been deprecated and is no longer being maintained.

Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

Caution

This guide’s example instructions will create several billable resources on your Linode account. If you do not want to keep using the example cluster that you create, be sure to delete it when you have finished the guide.

If you remove the resources afterward, you will only be billed for the hour(s) that the resources were present on your account. Consult the Billing and Payments guide for detailed information about how hourly billing works and for a table of plan pricing.

What is the k8s-alpha CLI?

Caution

The k8s-alpha CLI is deprecated. On March 31st, 2020, it will be removed from the linode-cli. After March 31, 2020, you will no longer be able to create or manage clusters using the k8s-alpha CLI plugin.

However, you will still be able to create and manage these clusters using Terraform. The Terraform module used is a public project officially supported by Linode, and is currently used to power the k8s-alpha CLI.

Other alternatives for creating and managing clusters include:

The Linode k8s-alpha CLI is a plugin for the Linode CLI that offers quick, single-command deployments of Kubernetes clusters on your Linode account. When you have it installed, creating a cluster can be as simple as:

linode-cli k8s-alpha create example-cluster

The clusters that it creates are pre-configured with useful Linode integrations, like our CCM, CSI, and ExternalDNS plugins. As well, the Kubernetes metrics-server is pre-installed, so you can run kubectl top. Nodes in your clusters will also be labeled with the Linode Region and Linode Type, which can also be used by Kubernetes controllers for the purposes of scheduling pods.

The CCM (Cloud Controller Manager), CSI (Container Storage Interface), and ExternalDNS plugins are Kubernetes addons published by Linode. You can use them to create NodeBalancers, Block Storage Volumes, and DNS records through your Kubernetes manifests.

The k8s-alpha CLI will create two kinds of nodes on your account:

  • Master nodes will run the components of your Kubernetes control plane, and will also run etcd.

  • Worker nodes will run your workloads.

These nodes will all exist as billable services on your account. You can specify how many master and worker nodes are created and also your nodes’ Linode plan and the data center they are located in.

Alternatives for Creating Clusters

Rancher

Another easy way to create clusters is with Rancher. Rancher is a web application that provides a GUI interface for cluster creation and for management of clusters. Rancher also provides easy interfaces for deploying and scaling apps on your clusters, and it has a built-in catalog of curated apps to choose from.

To get started with Rancher, review our How to Deploy Kubernetes on Linode with Rancher guide. Rancher is capable of importing clusters that were created outside of it, so you can still use it even if you create your clusters through the k8s-alpha CLI or some other means.

Linode Kubernetes Engine (LKE)

The Linode Kubernetes Engine (LKE) is a fully-managed container orchestration engine for deploying and managing containerized applications and workloads. LKE combines Linode’s ease of use and simple pricing with the infrastructure efficiency of Kubernetes.

When you deploy an LKE cluster, you receive a Kubernetes Master at no additional cost; you only pay for the Linodes (worker nodes), NodeBalancers (load balancers), and Block Storage Volumes. Your LKE cluster’s Master node runs the Kubernetes control plane processes – including the API, scheduler, and resource controllers. To get started with LKE, review our Deploy a Cluster with Linode Kubernetes Engine guide.

Beginners Resources

If you haven’t used Kubernetes before, we recommend reading through our introductory guides on the subject:

Before You Begin

  1. You will need to have a personal access token for Linode’s API. If you don’t have one already, follow the Get an Access Token section of our API guide and create a token with read/write permissions.

  2. If you do not already have a public-private SSH key pair, you will need to generate one. Follow the Generate a Key Pair section of our Public Key Authentication guide for instructions.

    Note
    If you’re unfamiliar with the concept of public-private key pairs, the introduction to our Public Key Authentication guide explains what they are.

Install the k8s-alpha CLI

The k8s-alpha CLI is bundled with the Linode CLI, and using it requires the installation and configuration of a few dependencies:

  • Terraform: The k8s-alpha CLI creates clusters by defining a resource plan in Terraform and then having Terraform create those resources. If you’re interested in how Terraform works, you can review our Beginner’s Guide to Terraform, but doing so is not required to use the k8s-alpha CLI.

    Note
    The k8s-alpha CLI requires Terraform version 0.12.0+.
  • kubectl: kubectl is the client software for Kubernetes, and it is used to interact with your Kubernetes cluster’s API.

  • SSH agent: Terraform will rely on public-key authentication to connect to the Linodes that it creates, and you will need to configure your SSH agent on your computer with the keys that Terraform should use.

Install the Linode CLI

Follow the Install the CLI section of our CLI guide to install the Linode CLI. If you already have the CLI, upgrade it to the latest version available:

pip install --upgrade linode-cli

Install Terraform

Follow the instructions in the Install Terraform section of our Use Terraform to Provision Linode Environments guide.

Install kubectl

macOS:

Install via Homebrew:

brew install kubectl

If you don’t have Homebrew installed, visit the Homebrew home page for instructions. Alternatively, you can manually install the binary; visit the Kubernetes documentation for instructions.

Linux:

  1. Download the latest kubectl release:

    curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
    
  2. Make the downloaded file executable:

    chmod +x ./kubectl
    
  3. Move the command into your PATH:

    sudo mv ./kubectl /usr/local/bin/kubectl
    
Note
You can also install kubectl via your package manager; visit the Kubernetes documentation for instructions.

Windows:

Visit the Kubernetes documentation for a link to the most recent Windows release.

Configure your SSH Agent

Your SSH key pair is stored in your home directory (or another location), but the k8s-alpha CLI’s Terraform implementation will not be able to reference your keys without first communicating your keys to Terraform. To communicate your keys to Terraform, you’ll first start the ssh-agent process. ssh-agent will cache your private keys for other processes, including keys that are passphrase-protected.

Linux: Run the following command; if you stored your private key in another location, update the path that’s passed to ssh-add accordingly:

eval $(ssh-agent) && ssh-add ~/.ssh/id_rsa
Note
You will need to run all of your k8s-alpha CLI commands from the terminal that you start the ssh-agent process in. If you start a new terminal, you will need to run the commands in this step again before using the k8s-alpha CLI.

macOS: macOS has an ssh-agent process that persists across all of your terminal sessions, and it can store your private key passphrases in the operating system’s Keychain Access service.

  1. Update your ~/.ssh/config SSH configuration file. This configuration will add keys to the persistent agent and store passphrases in the OS keychain:

    File: ~/.ssh/config
    1
    2
    3
    4
    
    Host *
      AddKeysToAgent yes
      UseKeychain yes
      IdentityFile ~/.ssh/id_rsa
Note
Although kubectl should be used in all cases possible to interact with nodes in your cluster, the key pair cached in the ssh-agent process will enable you to access individual nodes via SSH as the core user.
  1. Add your key to the ssh-agent process:

    ssh-add -K ~/.ssh/id_rsa
    

Create a Cluster

  1. To create your first cluster, run:

    linode-cli k8s-alpha create example-cluster
    
  2. Your terminal will show output related to the Terraform plan for your cluster. The output will halt with the following messages and prompt:

    Plan: 5 to add, 0 to change, 0 to destroy.
    
    Do you want to perform these actions in workspace "example-cluster"?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value:
    
    Note
    Your Terraform configurations will be stored under ~/.k8s-alpha-linode/
  3. Enter yes at the Enter a value: prompt. The Terraform plan will be applied over the next few minutes.

    Note

    You may see an error like the following:

    Error creating a Linode Instance: [400] Account Limit reached. Please open a support ticket.

    If this appears, then you have run into a limit on the number of resources allowed on your Linode account. If this is the case, or if your nodes do not appear in the Linode Cloud Manager as expected, contact Linode Support. This limit also applies to Block Storage Volumes and NodeBalancers, which some of your cluster app deployments may try to create.

  4. When the operation finishes, you will see options like the following:

    Apply complete! Resources: 5 added, 0 changed, 0 destroyed.
    Switched to context "example-cluster-4-kacDTg9RmZK@example-cluster-4".
    Your cluster has been created and your kubectl context updated.
    
    Try the following command:
    kubectl get pods --all-namespaces
    
    Come hang out with us in #linode on the Kubernetes Slack! http://slack.k8s.io/
    
  5. If you visit the Linode Cloud Manager, you will see your newly created cluster nodes on the Linodes page. By default, your Linodes will be created under the region and Linode plan that you have set as the default for your Linode CLI. To set new defaults for your Linode CLI, run:

    linode-cli configure
    

    The k8s-alpha CLI will conform to your CLI defaults, with the following exceptions:

    • If you set a default plan size smaller than Linode 4GB, the k8s-alpha CLI will create your master node(s) on the Linode 4GB plan, which is the minimum recommended for master nodes. It will still create your worker nodes using your default plan.

    • The k8s-alpha CLI will always create nodes running CoreOS (instead of the default distribution that you set).

  6. The k8s-alpha CLI will also update your kubectl client’s configuration (the kubeconfig file) to allow immediate access to the cluster. Review the Manage your Clusters with kubectl section for further instructions.

Cluster Creation Options

The following optional arguments are available:

linode-cli k8s-alpha create example-cluster-2 --node-type g6-standard-1 --nodes 6 --master-type g6-standard-4 --region us-east --ssh-public-key $HOME/.ssh/id_rsa.pub
Argument                                          Description
--node-type TYPEThe Linode Type ID for cluster worker nodes (which you can retrieve by running linode-cli linodes types).
--nodes COUNTThe number of Linodes to deploy as Nodes in the cluster (default 3).
--master-type TYPEThe Linode Type ID for cluster master nodes (which you can retrieve by running linode-cli linodes types).
--region REGIONThe Linode Region ID in which to deploy the cluster (which you can retrieve by running linode-cli regions list).
--ssh-public-key KEYPATHThe path to your public key file which will be used to access Nodes during initial provisioning only! The keypair must be added to an ssh-agent (default $HOME/.ssh/id_rsa.pub).

Delete a Cluster

  1. To delete a cluster, run the delete command with the name of your cluster:

    linode-cli k8s-alpha delete example-cluster
    
  2. Your terminal will show output from Terraform that describes the deletion operation. The output will halt with the following messages and prompt:

    Plan: 0 to add, 0 to change, 5 to destroy.
    
    Do you really want to destroy all resources in workspace "example-cluster"?
      Terraform will destroy all your managed infrastructure, as shown above.
      There is no undo. Only 'yes' will be accepted to confirm.
    
      Enter a value:
    
  3. Enter yes at the Enter a value: prompt. The nodes in your cluster will be deleted over the next few minutes.

  4. You should also login to the Linode Cloud Manager and confirm that any Volumes and NodeBalancers created by any of your cluster app deployments.

  5. Deleting the cluster will not remove the kubectl client configuration that the CLI inserted into your kubeconfig file. Review the Remove a Cluster’s Context section if you’d like to remove this information.

Manage your Clusters with kubectl

The k8s-alpha CLI will automatically configure your kubectl client to connect to your cluster. Specifically, this connection information is stored in your kubeconfig file. The path for this file is normally ~/.kube/config.

Use the kubectl client to interact with your cluster’s Kubernetes API. This will work in the same way as with any other cluster. For example, you can get all the pods in your cluster:

kubectl get pods --all-namespaces

Review the Kubernetes documentation for more information about how to use kubectl.

Switch between Cluster Contexts

If you have more than one cluster set up, you can switch your kubectl client between them. To list all of your cluster contexts:

kubectl config get-contexts

An asterisk will appear before the current context:

CURRENT   NAME                                                      CLUSTER                 AUTHINFO                            NAMESPACE
*         example-cluster-kat7BqBBgU8@example-cluster               example-cluster         example-cluster-kat7BqBBgU8
          example-cluster-2-kacDTg9RmZK@example-cluster-2           example-cluster-2       example-cluster-2-kacDTg9RmZK

To switch to another context, use the use-context subcommand and pass the value under the NAME column:

kubectl config use-context example-cluster-2-kacDTg9RmZK@example-cluster-2

All kubectl commands that you issue will now apply to the cluster you chose.

Remove a Cluster’s Context

When you delete a cluster with the k8s-alpha CLI, its connection information will persist in your local kubeconfig file, and it will still appear when you run kubectl config get-contexts. To remove this connection data, run the following commands:

kubectl config delete-cluster example-cluster
kubectl config delete-context example-cluster-kat7BqBBgU8@example-cluster
kubectl config unset users.example-cluster-kat7BqBBgU8
  • For the delete-cluster subcommand, supply the value that appears under the CLUSTER column in the output from get-contexts.

  • For the delete-context subcommand, supply the value that appears under the NAME column in the output from get-contexts.

  • For the unset subcommand, supply users.<AUTHINFO>, where <AUTHINFO> is the value that appears under the AUTHINFO column in the output from get-contexts.

Next Steps

Now that you have a cluster up and running, you’re ready to start deploying apps to it. Review our other Kubernetes guides for help with deploying software and managing your cluster:

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.