Albert Einstein Installing Kubernetes

Install kubectl for interacting with k8s: A Quick Guide

Introduction

The kubectl tool is the official Kubernetes CLI. The kubectl command allows you to run commands against your Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, view logs, and more. This article explains the steps to install kubectl on a Ubuntu machine. However, the steps are applicable for a majority of Linux machines.

Estimated reading time: 3 minutes

Before installing kubectl

Always install a version of kubectl that is within one minor version difference of your cluster. For example, if your Kubernetes cluster is running 1.28, then you can install either of 1.27, 1.28 or 1.29 versions of kubectl.

The easiest option to install kubectl is to download the latest release of the utility from the Kubernetes repository. First, identify if you are running an x86_64 architecture system or arm64 architecture system. If you already know your system’s architecture, skip to the next section.

To identify your computer’s architecture, run:

$ uname -m

Example output:

daddy@5c1f1412bd9f:~$ uname -m
x86_64
daddy@5c1f1412bd9f:~$ 

Installing the latest stable version of kubectl

Let’s look at the easiest way to install the latest and stable version of kubectl. We assume at this point you should know if you are running an x86_64 or an arm64 computer.

Download the latest binary

If you are running an x86_64 computer, run:

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

Alternatively, if you are running an arm64 computer, run:

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

Installing a specific version of kubectl

If you are looking to install a specific version of kubectl, then you can replace the $(curl -L -s https://dl.k8s.io/release/stable.txt) portion of the command with the version you are trying to install. You can get a list of supported versions of Kubernetes and their URLs from downloadkubernetes.com.

Example:

$ curl -LO "https://dl.k8s.io/release/v1.28.4/bin/linux/arm64/kubectl"

Verify the checksum of the binary (optional)

For x86_64 computers, run:

$ echo "$(curl -L -s https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256) kubectl" | sha256sum --check 

For arm64 computers, run:

$ echo "$(curl -L -s https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/arm64/kubectl.sha256) kubectl" | sha256sum --check 

If the binary is valid, and the checksum passes, then the output is:

kubectl: OK

If it fails, then sha256 exits with a FAILEd message similar to:

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

Installing kubectl

Run the following command to install kubectl on Ubuntu:

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

If you do not have root access on your system, you can still use the binary you downloaded by adding execute permissions to it. For example:

chmod +x kubectl
mkdir -p ~/.apps/bin
mv ./kubectl ~/.apps/bin/kubectl
# and then append (or prepend) ~/.apps/bin to $PATH

Test your installation

Test your installation by running the version command. If the installation was successful, you should see an output that prints the kubectl version.

$ kubectl version --client --output=yaml

For example:

root@5c1f1412bd9f:~# kubectl version --client -o yaml
clientVersion:
  buildDate: "2023-12-13T08:51:44Z"
  compiler: gc
  gitCommit: 3f7a50f38688eb332e2a1b013678c6435d539ae6
  gitTreeState: clean
  gitVersion: v1.29.0
  goVersion: go1.21.5
  major: "1"
  minor: "29"
  platform: linux/arm64
kustomizeVersion: v5.0.4-0.20230601165947-6ce0bf390ce3

Installation using the native package managers

It is also possible to install kubectl on supported platforms with the native package managers such as apt and yum.

Installing kubectl on Ubuntu and other Debian-based systems

Update the apt package index and install the required packages from the Kubernetes apt repository:

sudo apt-get update 
sudo apt-get install -y apt-transport-https ca-certificates curl

Download the public signing key for the Kubernetes package repositories. This will be same for all versions of the kubernetes repositories so you can ignore the version in the URL.

curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

Add the relevant Kubernetes apt repository, and if you want to install a version other than v1.29, substitute v1.29 with the minor version you want in the following command:

echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

Update the apt package index and install kubectl:

sudo apt-get update
sudo apt-get install -y kubectl

Installing kubectl on CentOS and Redhat-based systems

Add the Kubernetes yum repository, and if you want to install a version other than v1.29, substitute v1.29 with the minor version you want in the following command:

# This will overwrite any existing configuration in /etc/yum.repos.d/kubernetes.repo
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.29/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.29/rpm/repodata/repomd.xml.key
EOF

Install kubectl using yum:

sudo yum install -y kubectl

References

Read Also:

Leave a Reply

Your email address will not be published. Required fields are marked *