Kubectl
kubectl is a command-line tool for controlling Kubernetes clusters. This guide covers installation, configuration, and basic usage.
Installation on Debian-based systemsβ
Follow these steps to install kubectl on Debian-based Linux distributions.
-
Update the
aptpackage index and install necessary packages:sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curlnoteThe
apt-transport-httpspackage may not be required on some newer distributions. -
Download the public signing key for the Kubernetes package repositories:
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpgtipIf the directory
/etc/apt/keyringsdoes not exist, you can create it with the following command:sudo mkdir -p -m 755 /etc/apt/keyrings -
Add the Kubernetes
aptrepository: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.listinfoTo use a different Kubernetes version, replace
v1.29with the desired minor version. -
Install
kubectl:sudo apt-get update
sudo apt-get install -y kubectl -
Verify the installation by checking the version:
kubectl version --client
Configurationβ
The default configuration file for kubectl is located at ~/.kube/config. This file is typically generated automatically when you create a cluster with tools like minikube or k3s.
To verify that kubectl is configured correctly, run the following command to get the cluster status:
kubectl cluster-info
Managing a Remote Kubernetes Clusterβ
You can manage a Kubernetes cluster running on a different machine by updating your local ~/.kube/config file.
Exampleβ
This example demonstrates how to connect to a k3s cluster running on a Raspberry Pi.
-
Copy the
k3s.yamlfile from the remote machine to your local~/.kube/directory and rename it toconfig:scp user@remote-ip:/etc/rancher/k3s/k3s.yaml ~/.kube/config -
Edit the
~/.kube/configfile and change theserverIP address to the remote machine's IP address:server: https://<REMOTE_IP_ADDRESS>:6443 -
(Optional) You can rename the default cluster, context, and user for clarity.
-
Verify the connection to the remote cluster:
kubectl cluster-infoA successful connection will display the running services, similar to the output below:
Kubernetes control plane is running at https://<REMOTE_IP_ADDRESS>:6443
CoreDNS is running at https://<REMOTE_IP_ADDRESS>:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Metrics-server is running at https://<REMOTE_IP_ADDRESS>:6443/api/v1/namespaces/kube-system/services/https:metrics-server:https/proxy