Install and Set Up Kubernetes Kubectl on Linux
Installing and Setting Up Kubernetes Kubectl on Linux
Kubectl is a command-line tool available on Linux and Mac systems for managing Kubernetes clusters. You can easily manage configurations, configure environmental variables, and do much more with Kubectl. It simplifies the process of deploying applications on Kubernetes clusters or inspecting their resources, by offering the most powerful commands on your system. Additionally, it allows you to view Kubernetes cluster logs. The following tutorial will teach you how to install and configure Kubectl for Linux on your computer.
What is Kubectl?
By using Kubectl you can easily manage Kubernetes files by setting up the –kubeconfig flag or the KUBECONFIG environment variable. The Kubectl syntax explains how commands should be executed from the command-line, and it gives relevant examples of how the commands should be executed. Below is a brief description of the Kubectl syntax:
In your terminal window, you can utilise the Kubectl syntax to run the kubectl commands:
kubectl [command] [TYPE] [NAME] [flags]
The command, type, name, and flags represent the following:
Command: Command means the operations that you require to operate on Kubernetes cluster resources. The operations may include create, get, describe, delete, etc.
TYPE: This refers to the type of sources that are case-sensitive. Types allow you to specify single, plural, and abbreviated patterns like this output:
kubectl get pod pod1
kubectl get pods pod1
kubectl get po pod1
NAME: In name, you will find the title of the resource and these titles are case-sensitive. When you run kubectl get pods, the resources’ details will appear on screen. If you are dealing with different resources, you will have to specify the resource type, names, etc. for one or more files. The resource types and names can be defined as follows:
- Use TYPE1 name1 name2 name<#>.to organize resources in case those types are the same for example kubectl get pod example-pod1 example-pod2
- Type separately TYPE1/name1 TYPE1/name2 TYPE2/name3 TYPE<#>/name<#> to specify names of multiple resources such as kubectl get pod/example-pod1 replicationcontroller/example-rc1
- Use -f file1 -f file2 -f file<#> to specify resources that have multiple files. You can use YAML files rather than JSON files because the first one is more user friendly. For example: kubectl get -f ./pod.yaml
flags: These will specify optional flags such as -s or –server flags to display the address and port of the API server in Kubernetes.
This is the overview of Kubernetes Kubectl and syntax. Check out the other sections of this post if you want to install it on your Linux computers.
To Install and Set up Kubernetes Kubectl on Linux
Make sure you have the prerequisites before installing Kubectl on Linux. As a starting point, you must use a kubectl version that differs from the one in your cluster. For example, a v1.22 client will communicate with v1.21, v1.22, and v1.23. Making sure you’re always using the latest version of a tool will help you avoid performance issues as well as security issues. Let’s start installing Kubectl on Linux after you’ve comprehended this section.
There are three methods for installing Kubectl on Linux, but right here we are sharing the easiest one, to begin with. When installing Kubectl on Linux, you can use native package management tools or other package management tools.
How to Install Kubectl Binary with Curl on Linux-
Download the latest version of Kubectl with the following command:
curl -LO “https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl”
If you want to download a specific version of kubectl, you need to replace the $(curl -L -s https://dl.k8s.io/release/stable.txt) section with the exact version. Like, if you want to download v1.22.0, use the following command:
Now you can validate the binary by following the steps below. Download the kubectl checksum file first with the following command:
curl -LO “https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256”
Now validate the kubectl binary with the checksum file:
echo “$(<kubectl.sha256) kubectl” | sha256sum –check
If the output is validated, it will show this: kubectl: OK
If the output fails, then sha256 will leave with a nonzero status and will show you the following output:
sha256sum: WARNING: 1 computed checksum did NOT match
Now, you can install kubectl: sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
If you do not have access to root user credentials, install the tool on the ~/.local/bin directory:
chmod +x kubectl
mkdir -p ~/.local/bin/kubectl
mv ./kubectl ~/.local/bin/kubectl
# and then add ~/.local/bin/kubectl to $PATH
Now test the installed version to see if it is up to date:
kubectl version –client
Install Kubectl on Linux Using Native Package Management
If it is a debian-based Linux distribution, follow the steps below.
- Update apt package index as well as install packages you need to access the Kubernetes apt repository with this command: sudo apt-get update sudo apt-get install -y apt-transport-https ca-certificates curl.
- Download the Google Cloud public signing key on your system: sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
- Add apt repository in Kubernetes: 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
- Install kubectl by updating the apt package index with the new repository: sudo apt-get update sudo apt-get install -y kubectl
That should be enough to install Kubectl on Linux, but you should also verify that Kubectl is installed.
How to Verify Kubernetes Kubectl Configuration-
Kubectl needs a kubeconfig file to find and enter a Kubernetes cluster. This file is created by default when you build a cluster with kube-up.sh or when you deploy a Minikube cluster. This file is located at ~/.kube/config but if you want to verify if the Kubernetes tool is completely verified, use this command: kubectl cluster-info.
A URL response will appear, telling you that kubectl is set up to use on your computer.
There are various plugins available in Kubectl such as shell autocompletion that you can install by adding source /usr/share/bash-completion/bash_completion to your ~/.bashrc file. To enable the autocompletion, source the script with this command: echo ‘source <(kubectl completion bash)’ >>~/.bashrc and enable it using kubectl completion bash >/etc/bash_completion.d/kubectl.
This autocompletion plugin will help you with Bash and Zsh commands that won’t require you to do much typing for configuring Kubernetes clusters.
There is another plugin named kubectl convert that you can download with curl -LO https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl-convert. It’s the latest release of the plugin that will help you convert manifests within different Kubernetes API versions.
That’s all you need to know about installing and setting up Kubernetes kubectl on Linux but you can check out our other articles where we have talked about Kubernetes kubectl for Mac and Windows.