How to Install Kubectl on Debian 9|10|11|12

Posted March 4, 2024
How to Install Kubectl on Debian 9|10|11|12

This guide teaches you how to perfectly install a Kubectl command-line tool on Debian version 9|10|11|12. You will learn the two main approaches to getting Kubectl ready on Debian. Once installed you add a Kubernetes cluster and use Kubectl to run the Kubernetes pod on Debian.

Kubectl is a Command line client for controlling a Kubernetes cluster. You will learn:

  • Installing Kubectl on Debian using Snap.
  • How to use Kubernetes apt Repository to create a Kubectl Debian binary.
  • Installing Kubernetes and running the Kubectl command to run Pods on Debian.
  • How to Uninstall a Kubectl tool on Debian.

RelatedInstall and Run Kubectl on Linux|Ubuntu 20.04|22.04|18.04

Ready? Now dive in and Install Kubectl on Debian like a Pro.

Step 1: What you Need to Install Kubectl on Debian

These steps work for Debian 9|10|11|12. If there is a difference in versions, a comment will be highlighted on what you need to do.

Kubectl works with Kubectl this means your Debian machine must have resources capable of running Kubernetes, i.e. 2GB RAM one available core, and at least 20GM space.

Step 2: Installing Kubectl on Debian with Snaps

Linux systems like Debian have access to Snap. Snap manages Linux-based tools like Kubectl. This means in your Snap command, you will have discoverable and installable Kubectl from the Snap Store. First, Update the Debian Package index

sudo apt update
sudo apt upgrade -y

Check the following steps to get Kubectl ready on Debian using Snap:

  • Ensure you have snap installed.

In most cases, Debian will come with Snap. However, to make sure you are not missing anything Snap-related, use the following Snap commands:

  1. Install Snapd to manage Snap packages on Debian:
sudo apt install snapd

Installing Kubectl on Debian with Snaps

  1. 1nstalls the core Snap package:
sudo snap install core

Installing Kubectl on Debian with Snaps

  • Next, use Snap and a --classic to get Kubectl on Debian as follows:
sudo snap install kubectl --classic

Installing Kubectl on Debian with Snaps

Finally, check the version of the installed Debian Kubectl tool. First update the packages again:

sudo apt update
sudo apt upgrade -y

Check the installed Kubectl with this command:

kubectl version --client --output=yaml

Installing Kubectl on Debian with Snaps

Step 3: Installing Kubectl on Debian using Kubernetes APT Repository

Kubernetes apt repository has binary and checksum to get Kubectl running on Debian. This appt the main Linux Kubectl binary. You will add to the Debian source and Install Kubectl as follows:

  • Install Kubectl Debian necessary packages:
sudo apt-get install -y apt-transport-https ca-certificates curl

Install Kubectl on Debian using Kubernetes APT Repository

  • Generate a valid signed key. Debian will use this key to make sure Kubernetes package repositories add Kubectl to the source securely:
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

If you are using versions older than Debian 12, you don’t have a /etc/apt/keyrings folder ready. This means you need to create a /etc/apt/keyrings manually before adding Kubectl to Debian source:

sudo mkdir -p -m 755 /etc/apt/keyrings

Install Kubectl on Debian using Kubernetes APT Repository

  • Next, add the Kubernetes Kubectl repository to the Debian source:
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.list

APT repositories are configured in files under /etc/apt/sources.list.d/. It is the echo command that will add the repository URL along with the GPG key for signature verification.

  • Finally, update the new Debian package index and install Kubectl
sudo apt-get update
sudo apt-get install -y kubectl

Install Kubectl on Debian using Kubernetes APT Repository

You now have the honor of checking the installed Kubectl on your Debian machine:

kubectl version --client

Install Kubectl on Debian using Kubernetes APT Repository

Step 4: Uninstalling Kubectl from Debian

If you have Kubectl instilled used with Snap, binaries are saved on /snap/bin/kubectl or /snap/local/bin/kubectl. Run the following command to uninstall it:

sudo snap remove kubectl

When using apt, Debian will save the Kubectl binary and checksum on either /usr/local/bin/kubectl or /usr/bin/kubectl. Uninstall Kubectl as such:

sudo apt-get remove kubectl

Step 5: How to Add Kubernetes and Kubectl to Debian

This step will require you to have Docker installed on your computer. Check the following commands and run them each time:

  1. Install Docker to Debian:
# Install necessary packages for adding repositories over HTTPS
sudo apt install apt-transport-https ca-certificates curl gnupg2 software-properties-common -y

# Download Docker's official GPG key and add it to the keyring
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add -

# Add the Docker repository to the system list of software sources
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"

# Update the package index
sudo apt update

# Install Docker Engine, Docker CLI, containerd.io, Docker Buildx plugin, and Docker Compose plugin
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

Once done, check the installed Docker version:

# installed Docker version
sudo docker --version

Install Kubectl on Debian using Kubernetes APT Repository

Also, ensure Docker is up and running:

# Check Docker status
sudo systemctl status docker

Install Kubectl on Debian using Kubernetes APT Repository

  1. Disable all swap spaces. Swap space is used by the system as virtual memory when physical RAM is full.
# Disable all swap space
sudo swapoff -a

# Cremove swap in the /etc/fstab file
sudo sed -i '/ swap / s/^/#/' /etc/fstab
  1. Run the following command to Install Kubernetes Components:
# Install kubelet, kubeadm, and kubectl
sudo apt install -y kubelet kubeadm kubectl

# Enable the kubelet service to start automatically on boot
sudo systemctl enable kubelet

You should note that kubeadm is the command-line tool for bootstrapping Kubernetes clusters.

Install Kubectl on Debian using Kubernetes APT Repository

  • Go ahead and initialize kubeadm on Debian:
sudo rm /etc/containerd/config.toml
sudo systemctl restart containerd
sudo kubeadm init

Install Kubectl on Debian using Kubernetes APT Repository

  • Now, set up and Kube config for Kubectl:
# Create the directory for storing Kubernetes configuration files
mkdir -p $HOME/.kube
# Copy the Kubernetes admin configuration file to .kube/config
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
# Change ownership of the copied configuration file to the current user
sudo chown $(id -u):$(id -g) $HOME/.kube/config
  • Create the Pod Network:
# Apply the Calico networking manifest
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

Install Kubectl on Debian using Kubernetes APT Repository

  • Check if the Main Node of Kubernetes is running:
# list nodes in the Kubernetes cluster
sudo kubectl get nodes

# Get  pods across all namespaces
sudo kubectl get pods --all-namespaces

Conclusion

You have successfully Installed Kubectl on Debian and learned:

  • Installing Kubectl on Debian using Snap.
  • How to use Kubernetes apt Repository to create a Kubectl Debian binary.
  • Installing Kubernetes and running the Kubectl command to run Pods on Debian.
  • How to Uninstall a Kubectl tool on Debian.
How to Install Kubectl on Debian 9|10|11|12

Written By:

Joseph Chege