How to Install Kubectl on Ubuntu 22.04

How to Install Kubectl on Ubuntu 22.04
How to Install Kubectl on Ubuntu 22.04

What is Kubectl?

Kubectl is a Kubernetes-specific command-line tool that lets you communicate and control Kubernetes clusters. It enables efficient control and management of Kubernetes resources. With Kubectl, you can deploy applications, inspect and update cluster resources at will, scale deployments, and even troubleshoot issues that surface.

Prerequisites

  • Basic knowledge of the Linux command-line interface (CLI)
  • A server with a fresh installation of the latest Ubuntu version

Step-by-Step Process to Install Kubectl on Ubuntu 22.04

For the purpose of this tutorial, we will use a CPU-powered Virtual Machine offered by NodeShift; however, you can replicate the same steps with any other cloud provider of your choice.

Step 1: Sign Up and Set Up a NodeShift Cloud Account

  1. Visit the NodeShift Cloud website and create an account.
  2. Once you've signed up, log into your account.
  3. Follow the account setup process and provide the necessary details and information.

Step 2: Create a CPU Virtual Machine

NodeShift CPUs offer flexible and scalable on-demand resources like NodeShift Virtual Machines (VMs), which are easily deployed and come with general-purpose, CPU-powered, or storage-optimized nodes.

  1. Navigate to the menu on the left side.
  2. Select the "Compute VMs" option.
  3. Click the "Create Compute VM" button in the Dashboard to make your first deployment.

Step 3: Select a Region and Choose VM Configuration

  1. In the "CPU Deployment" tab, select a geographical region where you want to launch the VM (e.g., the United States).
  1. In the "Choose VM Configuration" section, select the number of cores, amount of memory, boot disk type, and size that best suits your needs.

Step 4: Choose an Image
Next, you will need to choose an image for your VM. We will deploy the Ubuntu on the Virtual Machine, but you can choose an OS according to your preference. Other options like CentOS and Debian are also available.

Step 5: Select Billing and Authentication Method

Select the authentication method. There are two options: Password and SSH Key. SSH keys are a more secure option. To create them, refer to our official documentation.

Step 6: Additional Details

Open your terminal, paste the IP with SSH, and give the password.

Step 7: Complete Deployment

After finalizing the details, click the ‘Create’ button, and your VM will be deployed.

Step 8: Virtual Machine Successfully Deployed

You will get visual confirmation that your machine is up and running.

Step 9: Connect to Compute VMs using SSH

  1. NodeShift Compute VMs can be connected and controlled through a terminal and SSH. 
  2. Open your terminal paste the IP with SSH and give password.

Step 10: Install Kubectl on Ubuntu 22.04

There are three common ways to install Kubectl on your Ubuntu 22.04 system:

  1. Installing the Kubectl binary using curl
  2. Using the apt package manager
  3. Using the snap package manager

Method 1 - Install Kubectl binary using curl
Step 1:
Download the latest release with the command:

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

Validate the binary (optional)

Step 2: Download the Kubectl 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.

Step 3: Install kubectl

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

Step 4: Test to ensure the version you installed is up-to-date:

kubectl version --client

Or use this for a detailed view of the version:

kubectl version --client --output=yaml

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

Step 1 - Download the Kubectl binary
First, you need to download the Kubectl binary from the official Kubernetes release repository. To do this, run the following command in your terminal:

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

Step 2 - Make the Kubectl binary executable
Next, you need to make the downloaded binary executable by running the following command:

chmod +x kubectl

Step 3 - Move Kubectl binary to PATH

Run the following command to move the binary to a directory included in the system's PATH environment variable:

sudo mv kubectl /usr/local/bin

This will make Kubectl accessible from anywhere in the terminal.

Method 2 - Install Kubectl using apt

Another method to install Kubectl on Ubuntu 22.04 is using the apt package manager.

Step 1 - Add Kubernetes repository's GPG (GNU Privacy Guard) key

First, download the GPG key for the Kubernetes repository by running:

curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-archive-keyring.gpg

Running this command will download the GPG key, convert it to a binary format, and save it as a GPG keyring file.

Step 2 - Add the Kubernetes repository to your system's sources.list

Next, create a repository entry for the Kubernetes packages by executing the following in the terminal:

Step 3 - Update system packages

After adding the repository, update the system packages:

sudo apt update

Step 4 - Install Kubectl using the apt package manager

Now, install Kubectl using apt by running the following command:

sudo apt install kubectl

Method 3 - Install Kubectl using Snap

Snap is another package manager that can install Kubectl on Ubuntu 22.04. This method is suitable if you prefer a simple installation process.

Step 1 - Update system packages

Run the following command to update the system packages:

sudo apt update

Step 2 - Install Kubectl

Next, to install Kubectl using Snap, run the command below:

sudo snap install kubectl --classic

Conclusion

Installing Kubectl on Ubuntu 22.04 is straightforward and can be achieved through several methods, including using curl, the apt package manager, or the snap package manager. This guide walks you through setting up a NodeShift Cloud account, deploying a CPU-powered VM, and the detailed steps for installing Kubectl. With these instructions, you can efficiently manage your Kubernetes clusters on Ubuntu 22.04.

For more information about NodeShift:

Read more