Are you an Ubuntu user and want to try out Kubernetes?
Are you wondering how to install kubectl on Ubuntu?
Kubernetes is a powerful open-source platform used to deploy, manage, and scale containerized applications in a clustered environment. It has revolutionized how developers build and deploy applications.
With Kubernetes, you also get two command-line tools, kubeadm and kubectl. The latter is used for managing a cluster and deploying applications on a cluster.
Installing kubectl on Ubuntu can be difficult for beginners, but once you get the hang of it, you can easily set it for your Kubernetes installation.
In this article, we will discuss what Kubectl is, how it works, and the steps needed to install Kubectl on Ubuntu. We’ll also discuss some tips to get the most out of your installation and make it as efficient as possible.
Table Of Contents
What is Kubectl?
Kubectl is a command-line tool that is used to interact with Kubernetes clusters. It is a client-side utility that communicates with the Kubernetes API server, allowing users to manage and deploy applications on a Kubernetes cluster.
With kubectl, users can perform a variety of tasks, such as creating and managing pods, services, and deployments, checking the status of nodes and clusters, scaling applications up or down, and updating configurations.
Kubectl is an essential tool for developers, system administrators, and DevOps engineers who work with Kubernetes. It is open-source and can be used on any platform that supports Kubernetes, including Linux, macOS, and Windows.
How Kubectl Works
Kubectl communicates with the Kubernetes API server using a RESTful API. The API server runs on the Kubernetes control plane, which is responsible for managing the state of the Kubernetes cluster.
When a user runs a kubectl command, the following happens:
- The kubectl command is parsed by the Kubernetes API client, which converts it into an API request.
- The API request is sent to the Kubernetes API server.
- The Kubernetes API server receives the API request and validates it. If the request is valid, the Kubernetes API server updates the desired state of the Kubernetes cluster.
- The Kubernetes API server sends a response to the kubectl command, indicating whether the operation was successful.
Following this process, kubectl allows users to interact with the Kubernetes control plane and manage the state of the Kubernetes cluster. Users can create, modify, or delete Kubernetes resources (including pods, services, and deployments) by issuing kubectl commands.
Kubectl also supports several useful features, such as auto-completion, context switching, and configuration management, which make it easier for users to work with Kubernetes clusters. The command uses a config file stored in the .kube directory under the user’s home path to perform tasks according to API requests against a specific cluster.
How to Install kubectl on Ubuntu 20.04 LTS
As mentioned earlier, we’ll now go through the steps involved in setting up kubectl on Ubuntu 20.04 LTS.
Prerequisites
The following are the prerequisites of the process:
- A server running Ubuntu 20.04 LTS.
- Root access to the server or a user with sudo privileges.
- apt or apt-get utility should be installed on the server.
Now that you have understood the prerequisites of the process. Let’s dive into how to install kubectl on Ubuntu 20.04 LTS.
Step # 1: Update Server Packages
It is always a good idea to update the server so that the latest versions of the packages are installed and available for the kubectl installation. On our Ubuntu 20.04 LTS system, we use the apt-update command to sync all packages.
# apt-get update
Step # 2: Install Kubectl
There are various ways to install the kubectl utility.
a) Install it from Snap Store as a Snap Package.
Since we’re on Ubuntu, we can get the latest package from the Snap Store. For this, use the following command:
# snap install kubectl --classic
b) Manually Install the Utility
Like all utilities, you can install kubectl manually on the Ubuntu server. Start by downloading the latest release with the command:
# curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
Next, change the file permissions to make the kubectl binary executable.
# chmod +x ./kubectl
Move the binary into the PATH.
# sudo mv ./kubectl /usr/local/bin/kubectl
At this point, the utility has been installed on your Ubuntu machine.
Step # 3: Check kubectl Version
Once installed, you can verify the current version by using the kubectl version command, as shown below.
# kubectl version --output=yaml
Step # 4: Check all the Available Options
You can also check all the options available for the kubectl command by using kubectl --help
command, as shown below.
Now that you have installed Kubectl on Ubuntu server, the next logical move is to get to know the command in some detail.
Kubectl Command Syntax
The syntax of kubectl commands is as follows.
$ kubectl [command] [TYPE] [NAME] [flags]
Let’s break down these four components.
Command
The command portion describes what sort of operation you want to perform – create, describe, get, apply, and delete. You can execute these commands on one or multiple resources since it’s possible to designate multiple resources within a single command.
- create generates new resources from files or standard input devices.
- describe retrieves details about a resource or resource group.
- get fetches cluster data from various sources.
- delete erases resources as required.
- apply pushes changes based on your configuration files
Type
This component characterizes the type of resource. This is case-insensitive and used in a single, plural, or as abbreviated manner.
Name
This denotes the identity of a resource and is case-sensitive.
Flags
A flag sets options on a command.
Some Basic Kubectl Commands
kubectl uses a kubeconfig file to locate and access a Kubernetes cluster. This file is automatically generated when you create a cluster using kube-up.sh or deploy a Minikube cluster successfully.
The kubectl configuration is typically found at ~/.kube/config by default. Once the cluster setup is ready, you can try following basic commands.
Get the Cluster State
Use the following command to get the cluster state:
# kubectl cluster-info
Get The Cluster Information
Use the following command to get the information about the server:
# kubectl config view
The following command displayed the list of available API resources.
# kubectl api-resources
List Deployment
The following command will list the deployments
# kubectl get deployment
List all Available Services
The following command will list all available commands in the current namespace:
# kubectl get services
Get the Status of Node
The following command will get the status of the nodes (including the node’s name, status, roles, age, and Kubernetes version.
# kubectl get nodes
Create a New Namespace
The following command will create a new namespace.
# kubectl create ns my-space
Uninstall Kubectl
There are scenarios where you might want to remove kubectl from the Ubuntu server. For this, you need to remove the kubectl package.
An easy way is to use the snap remove kubectl command as shown below.
# snap remove kubectl
If you installed the package manually, you could use the following command:
# sudo rm -f /usr/local/bin/kubectl
Conclusion
This article covered the essential aspects of kubectl, including installation, configuration, and basic command usage. With its many helpful features, Kubernetes is an outstanding tool for managing your Kubernetes clusters.
Let us know how you installed kubectl on your Ubuntu machine.
FAQ – Install kubectl on Ubuntu
Q: What are some common uses of kubectl?
A: Some common uses of kubectl include creating and managing Kubernetes resources, inspecting cluster resources, troubleshooting, and deploying applications.
Q: How do I use kubectl to deploy an application to a Kubernetes cluster?
A: You can use kubectl to deploy an application to a Kubernetes cluster by creating a deployment manifest in YAML format, then running the kubectl apply command to create or update the deployment.
Q: What is a Kubernetes resource?
A: A Kubernetes resource is an object that represents a piece of the cluster’s state, such as a pod, a deployment, or a service.
Q: How can I view the logs of a pod using kubectl?
A: You can view the logs of a pod using the kubectl logs command followed by the name of the pod you want to view the logs for.
Q: How can I check the status of a deployment using kubectl?
A: You can check the status of a deployment using the kubectl rollout status command followed by the name of the deployment you want to check the status of.