Install and Set Up kubectl on Linux

Before you begin

You must use a kubectl version that is within one minor version difference of your cluster. For example, a v1.21 client can communicate with v1.20, v1.21, and v1.22 control planes. Using the latest version of kubectl helps avoid unforeseen issues.

Install kubectl on Linux

The following methods exist for installing kubectl on Linux:

Install kubectl binary with curl on Linux

  1. Download the latest release with the command:

    1. curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"

    Note:

    To download a specific version, replace the $(curl -L -s https://dl.k8s.io/release/stable.txt) portion of the command with the specific version.

    For example, to download version v1.21.0 on Linux, type:

    1. curl -LO https://dl.k8s.io/release/v1.21.0/bin/linux/amd64/kubectl
  2. Validate the binary (optional)

    Download the kubectl checksum file:

    1. curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"

    Validate the kubectl binary against the checksum file:

    1. echo "$(<kubectl.sha256) kubectl" | sha256sum --check

    If valid, the output is:

    1. kubectl: OK

    If the check fails, sha256 exits with nonzero status and prints output similar to:

    1. kubectl: FAILED
    2. sha256sum: WARNING: 1 computed checksum did NOT match

    Note: Download the same version of the binary and checksum.

  3. Install kubectl

    1. sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

    Note:

    If you do not have root access on the target system, you can still install kubectl to the ~/.local/bin directory:

    1. mkdir -p ~/.local/bin/kubectl
    2. mv ./kubectl ~/.local/bin/kubectl
    3. # and then add ~/.local/bin/kubectl to $PATH
  4. Test to ensure the version you installed is up-to-date:

    1. kubectl version --client

Install using native package management

  1. Update the apt package index and install packages needed to use the Kubernetes apt repository:

    1. sudo apt-get update
    2. sudo apt-get install -y apt-transport-https ca-certificates curl
  2. Download the Google Cloud public signing key:

    1. sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
  3. Add the Kubernetes apt repository:

    1. echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
  4. Update apt package index with the new repository and install kubectl:

    1. sudo apt-get update
    2. sudo apt-get install -y kubectl
  1. cat <<EOF > /etc/yum.repos.d/kubernetes.repo
  2. [kubernetes]
  3. name=Kubernetes
  4. baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
  5. enabled=1
  6. gpgcheck=1
  7. repo_gpgcheck=1
  8. gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
  9. EOF
  10. yum install -y kubectl

Install using other package management

If you are on Ubuntu or another Linux distribution that support snap package manager, kubectl is available as a snap application.

  1. snap install kubectl --classic
  2. kubectl version --client

If you are on Linux and using Homebrew package manager, kubectl is available for installation.

  1. brew install kubectl
  2. kubectl version --client

Install on Linux as part of the Google Cloud SDK

You can install kubectl as part of the Google Cloud SDK.

  1. Install the Google Cloud SDK.

  2. Run the kubectl installation command:

    1. gcloud components install kubectl
  3. Test to ensure the version you installed is up-to-date:

    1. kubectl version --client

Verify kubectl configuration

In order for kubectl to find and access a Kubernetes cluster, it needs a kubeconfig file, which is created automatically when you create a cluster using kube-up.sh or successfully deploy a Minikube cluster. By default, kubectl configuration is located at ~/.kube/config.

Check that kubectl is properly configured by getting the cluster state:

  1. kubectl cluster-info

If you see a URL response, kubectl is correctly configured to access your cluster.

If you see a message similar to the following, kubectl is not configured correctly or is not able to connect to a Kubernetes cluster.

  1. The connection to the server <server-name:port> was refused - did you specify the right host or port?

For example, if you are intending to run a Kubernetes cluster on your laptop (locally), you will need a tool like Minikube to be installed first and then re-run the commands stated above.

If kubectl cluster-info returns the url response but you can’t access your cluster, to check whether it is configured properly, use:

  1. kubectl cluster-info dump

Optional kubectl configurations

Enable shell autocompletion

kubectl provides autocompletion support for Bash and Zsh, which can save you a lot of typing.

Below are the procedures to set up autocompletion for Bash and Zsh.

Introduction

The kubectl completion script for Bash can be generated with the command kubectl completion bash. Sourcing the completion script in your shell enables kubectl autocompletion.

However, the completion script depends on bash-completion, which means that you have to install this software first (you can test if you have bash-completion already installed by running type _init_completion).

Install bash-completion

bash-completion is provided by many package managers (see here). You can install it with apt-get install bash-completion or yum install bash-completion, etc.

The above commands create /usr/share/bash-completion/bash_completion, which is the main script of bash-completion. Depending on your package manager, you have to manually source this file in your ~/.bashrc file.

To find out, reload your shell and run type _init_completion. If the command succeeds, you’re already set, otherwise add the following to your ~/.bashrc file:

  1. source /usr/share/bash-completion/bash_completion

Reload your shell and verify that bash-completion is correctly installed by typing type _init_completion.

Enable kubectl autocompletion

You now need to ensure that the kubectl completion script gets sourced in all your shell sessions. There are two ways in which you can do this:

  • Source the completion script in your ~/.bashrc file:

    1. echo 'source <(kubectl completion bash)' >>~/.bashrc
  • Add the completion script to the /etc/bash_completion.d directory:

    1. kubectl completion bash >/etc/bash_completion.d/kubectl

If you have an alias for kubectl, you can extend shell completion to work with that alias:

  1. echo 'alias k=kubectl' >>~/.bashrc
  2. echo 'complete -F __start_kubectl k' >>~/.bashrc

Note: bash-completion sources all completion scripts in /etc/bash_completion.d.

Both approaches are equivalent. After reloading your shell, kubectl autocompletion should be working.

The kubectl completion script for Zsh can be generated with the command kubectl completion zsh. Sourcing the completion script in your shell enables kubectl autocompletion.

To do so in all your shell sessions, add the following to your ~/.zshrc file:

  1. source <(kubectl completion zsh)

If you have an alias for kubectl, you can extend shell completion to work with that alias:

  1. echo 'alias k=kubectl' >>~/.zshrc
  2. echo 'complete -F __start_kubectl k' >>~/.zshrc

After reloading your shell, kubectl autocompletion should be working.

If you get an error like complete:13: command not found: compdef, then add the following to the beginning of your ~/.zshrc file:

  1. autoload -Uz compinit
  2. compinit

What’s next