peter (admin) -

Installing kubectl on Linux
We have now installed the main prerequisites, installed Docker, and installed Minikube.
Docker allows us to work with containers, while Minikube allows us to create and run a local Kubernetes cluster.
However, one very important tool is still missing:
kubectl
kubectl is the main command-line tool used to communicate with and manage a Kubernetes cluster.
In this section, we will download, install, and verify kubectl on our Linux system.
What Is kubectl?
kubectl is the Kubernetes command-line interface.
It allows us to send commands to the Kubernetes API server and interact with resources inside the cluster.
For example, we can use kubectl to:
-
Create resources
-
View Pods
-
View Deployments
-
View Services
-
Delete resources
-
Scale applications
-
Examine logs
-
Troubleshoot Pods
-
Apply YAML configuration files
-
Inspect cluster information
Some common commands include:
kubectl get pods
kubectl get nodes
kubectl get services
kubectl get deployments
kubectl is therefore one of the tools we will use most frequently when working with Kubernetes.
How kubectl Communicates with Kubernetes
kubectl does not directly communicate with individual worker nodes or containers.
Instead, it communicates with the Kubernetes API server.
The basic relationship looks like this:
User
|
v
kubectl
|
v
Kubernetes API Server
|
+----------------------+
| | |
v v v
Pods Services Deployments
When we enter a kubectl command, kubectl sends an API request to the Kubernetes API server.
Kubernetes processes the request and returns the appropriate response.
Download kubectl
The recommended approach is to download kubectl from the official Kubernetes download location:
dl.k8s.io
We can use curl to determine the current stable Kubernetes version and download the appropriate kubectl executable.
For a 64-bit Intel/AMD Linux system, run:
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
The command contains two parts.
First:
curl -L -s https://dl.k8s.io/release/stable.txt
retrieves the current stable Kubernetes version.
For example, it may return something similar to:
v1.xx.x
The outer curl command then uses that version to construct the download URL for kubectl.
Understanding linux/amd64
The download path contains:
linux/amd64
linux tells us that the executable is intended for the Linux operating system.
amd64 refers to the common 64-bit x86 architecture used by most Intel and AMD desktop and server processors.
You can check your system architecture with:
uname -m
On an AMD64-compatible system, you will normally see:
x86_64
If you are using an ARM-based machine, you would need the appropriate ARM version instead.
Check the Download
After the download has completed, check that the file exists:
ls -lh kubectl
You should see a file called:
kubectl
You can also check what type of file it is:
file kubectl
The output should identify it as a Linux executable.
Install kubectl
Now install the downloaded executable into:
/usr/local/bin
Run:
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
This command both copies the file and sets the appropriate ownership and permissions.
Understanding the Permissions
The important part of the command is:
-m 0755
This sets the permissions of the kubectl executable.
0755 means:
Owner: read + write + execute
Group: read + execute
Others: read + execute
This allows users on the system to execute kubectl while only the owner can modify the file.
The permissions can also be represented as:
rwxr-xr-x
You can check them after installation with:
ls -l /usr/local/bin/kubectl
Why Install into /usr/local/bin?
We install kubectl into:
/usr/local/bin
because this directory is normally included in the Linux system's PATH.
You can check your PATH with:
echo $PATH
Because /usr/local/bin is in the PATH, we can simply enter:
kubectl
instead of:
/usr/local/bin/kubectl
This makes kubectl available from any directory in the terminal.
Verify the Installation Location
We can check where Linux finds kubectl with:
which kubectl
The expected result is:
/usr/local/bin/kubectl
You can also use:
command -v kubectl
Again, you should see:
/usr/local/bin/kubectl
This confirms that kubectl has been installed correctly and is available through the system PATH.
Check the kubectl Version
We can now verify that kubectl works correctly.
Run:
kubectl version --client
The --client option is important at this stage.
It tells kubectl:
Display information about the kubectl client itself without requiring information from a Kubernetes cluster.
The output will contain the installed kubectl version.
Depending on the version, you may also see information relating to Kustomize, which is built into kubectl and can be used to customize Kubernetes configuration.
What Is Kustomize?
Modern versions of kubectl include support for Kustomize.
Kustomize allows Kubernetes YAML configurations to be customized without directly modifying the original YAML files.
For example, you may eventually encounter:
kubectl apply -k .
For now, we do not need to understand Kustomize in detail.
The important point is that if the kubectl version output includes something such as:
Client Version: ...
Kustomize Version: ...
this is normal.
Test kubectl
We can also display the available commands:
kubectl --help
This will show the large number of operations available through kubectl.
Some of the commands we will frequently use include:
kubectl get
kubectl describe
kubectl apply
kubectl create
kubectl delete
kubectl logs
kubectl exec
kubectl scale
These commands will become very familiar as we begin working with Kubernetes.
kubectl and Minikube
Once we start our Minikube cluster, kubectl will become our main way of interacting with it.
The relationship between the tools will look something like this:
Linux System
|
+---------+---------+
| |
v v
Minikube kubectl
| |
v |
Kubernetes Cluster <-------+
|
+---- Pods
|
+---- Deployments
|
+---- Services
|
+---- ConfigMaps
|
+---- Secrets
Minikube creates and manages our local Kubernetes environment.
kubectl allows us to communicate with the Kubernetes API and manage resources inside that environment.
Example kubectl Commands
Once our cluster is running, we can check the nodes with:
kubectl get nodes
We can view Pods with:
kubectl get pods
We can see all Pods across all namespaces with:
kubectl get pods -A
We can inspect a particular Pod with:
kubectl describe pod
We can view container logs with:
kubectl logs
And one of the most important commands when working with YAML files is:
kubectl apply -f application.yaml
This submits our YAML configuration to Kubernetes.
Our Kubernetes Toolkit
At this stage, we have installed three important tools:
Docker
|
+---- Build and run containers
|
v
Minikube
|
+---- Create a local Kubernetes cluster
|
v
kubectl
|
+---- Communicate with Kubernetes
|
v
Kubernetes Resources
Each tool has a different responsibility.
| Tool | Purpose |
|---|---|
| Docker | Build, download, and run containers |
| Minikube | Create and manage a local Kubernetes cluster |
| kubectl | Communicate with and manage the Kubernetes cluster |
kubectl Installation Complete
We have now successfully:
-
Downloaded kubectl from the official Kubernetes source
-
Installed it into
/usr/local/bin -
Configured the correct executable permissions
-
Verified its installation location
-
Checked the kubectl client version
-
Confirmed that the command is ready to use
Our basic Kubernetes toolset is now in place.
With Docker, Minikube, and kubectl installed, we are ready to start our local Kubernetes cluster and begin working with Kubernetes resources directly.
Comments (0)
There are no comments yet. Be the first to comment.
Log in or register to leave a comment. all comments are subject to admin approval