8.9 KiB
reviewers | title | content_type | weight | card | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
Install and Set Up kubectl on Linux | task | 10 |
|
{{% heading "prerequisites" %}}
You must use a kubectl version that is within one minor version difference of your cluster. For example, a v{{< skew currentVersion >}} client can communicate with v{{< skew currentVersionAddMinor -1 >}}, v{{< skew currentVersionAddMinor 0 >}}, and v{{< skew currentVersionAddMinor 1 >}} control planes. Using the latest compatible 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
- Install using native package management
- Install using other package management
Install kubectl binary with curl on Linux
-
Download the latest release with the command:
{{< tabs name="download_binary_linux" >}} {{< tab name="x86-64" codelang="bash" >}} curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" {{< /tab >}} {{< tab name="ARM64" codelang="bash" >}} curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/arm64/kubectl" {{< /tab >}} {{< /tabs >}}
{{< 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 {{< skew currentPatchVersion >}} on Linux x86-64, type:
curl -LO https://dl.k8s.io/release/v{{< skew currentPatchVersion >}}/bin/linux/amd64/kubectl
And for Linux ARM64, type:
curl -LO https://dl.k8s.io/release/{{< param "fullversion" >}}/bin/linux/arm64/kubectl
{{< /note >}}
-
Validate the binary (optional)
Download the kubectl checksum file:
{{< tabs name="download_checksum_linux" >}} {{< tab name="x86-64" codelang="bash" >}} curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256" {{< /tab >}} {{< tab name="ARM64" codelang="bash" >}} curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/arm64/kubectl.sha256" {{< /tab >}} {{< /tabs >}}
Validate the kubectl binary against the checksum file:
echo "$(cat kubectl.sha256) kubectl" | sha256sum --check
If valid, the output is:
kubectl: OK
If the check fails,
sha256
exits with nonzero status and prints output similar to:kubectl: FAILED sha256sum: WARNING: 1 computed checksum did NOT match
{{< note >}} Download the same version of the binary and checksum. {{< /note >}}
-
Install kubectl
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:chmod +x kubectl mkdir -p ~/.local/bin mv ./kubectl ~/.local/bin/kubectl # and then append (or prepend) ~/.local/bin to $PATH
{{< /note >}}
-
Test to ensure the version you installed is up-to-date:
kubectl version --client
{{< note >}} The above command will generate a warning:
WARNING: This version information is deprecated and will be replaced with the output from kubectl version --short.
You can ignore this warning. You are only checking the version of
kubectl
that you have installed.{{< /note >}}
Or use this for detailed view of version:
kubectl version --client --output=yaml
Install using native package management
{{< tabs name="kubectl_install" >}} {{% tab name="Debian-based distributions" %}}
-
Update the
apt
package index and install packages needed to use the Kubernetesapt
repository:sudo apt-get update sudo apt-get install -y ca-certificates curl
If you use Debian 9 (stretch) or earlier you would also need to install
apt-transport-https
:sudo apt-get install -y apt-transport-https
-
Download the Google Cloud public signing key:
curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-archive-keyring.gpg
-
Add the Kubernetes
apt
repository:echo "deb [signed-by=/etc/apt/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
-
Update
apt
package index with the new repository and install kubectl:sudo apt-get update sudo apt-get install -y kubectl
{{< note >}}
In releases older than Debian 12 and Ubuntu 22.04, /etc/apt/keyrings
does not exist by default.
You can create this directory if you need to, making it world-readable but writeable only by admins.
{{< /note >}}
{{% /tab %}}
{{% tab name="Red Hat-based distributions" %}}
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearch
enabled=1
gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
sudo yum install -y kubectl
{{% /tab %}} {{< /tabs >}}
Install using other package management
{{< tabs name="other_kubectl_install" >}} {{% tab name="Snap" %}} If you are on Ubuntu or another Linux distribution that supports the snap package manager, kubectl is available as a snap application.
snap install kubectl --classic
kubectl version --client
{{% /tab %}}
{{% tab name="Homebrew" %}} If you are on Linux and using Homebrew package manager, kubectl is available for installation.
brew install kubectl
kubectl version --client
{{% /tab %}}
{{< /tabs >}}
Verify kubectl configuration
{{< include "included/verify-kubectl.md" >}}
Optional kubectl configurations and plugins
Enable shell autocompletion
kubectl provides autocompletion support for Bash, Zsh, Fish, and PowerShell, which can save you a lot of typing.
Below are the procedures to set up autocompletion for Bash, Fish, and Zsh.
{{< tabs name="kubectl_autocompletion" >}} {{< tab name="Bash" include="included/optional-kubectl-configs-bash-linux.md" />}} {{< tab name="Fish" include="included/optional-kubectl-configs-fish.md" />}} {{< tab name="Zsh" include="included/optional-kubectl-configs-zsh.md" />}} {{< /tabs >}}
Install kubectl convert
plugin
{{< include "included/kubectl-convert-overview.md" >}}
-
Download the latest release with the command:
{{< tabs name="download_convert_binary_linux" >}} {{< tab name="x86-64" codelang="bash" >}} curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl-convert" {{< /tab >}} {{< tab name="ARM64" codelang="bash" >}} curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/arm64/kubectl-convert" {{< /tab >}} {{< /tabs >}}
-
Validate the binary (optional)
Download the kubectl-convert checksum file:
{{< tabs name="download_convert_checksum_linux" >}} {{< tab name="x86-64" codelang="bash" >}} curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl-convert.sha256" {{< /tab >}} {{< tab name="ARM64" codelang="bash" >}} curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/arm64/kubectl-convert.sha256" {{< /tab >}} {{< /tabs >}}
Validate the kubectl-convert binary against the checksum file:
echo "$(cat kubectl-convert.sha256) kubectl-convert" | sha256sum --check
If valid, the output is:
kubectl-convert: OK
If the check fails,
sha256
exits with nonzero status and prints output similar to:kubectl-convert: FAILED sha256sum: WARNING: 1 computed checksum did NOT match
{{< note >}} Download the same version of the binary and checksum. {{< /note >}}
-
Install kubectl-convert
sudo install -o root -g root -m 0755 kubectl-convert /usr/local/bin/kubectl-convert
-
Verify plugin is successfully installed
kubectl convert --help
If you do not see an error, it means the plugin is successfully installed.
-
After installing the plugin, clean up the installation files:
rm kubectl-convert kubectl-convert.sha256
{{% heading "whatsnext" %}}
{{< include "included/kubectl-whats-next.md" >}}