Last updated on August 7, 2024
Setting up a Kubernetes cluster on Debian 12 involves multiple steps, from configuring hostnames to installing necessary tools. Below is a comprehensive guide to help you get started.
Prerequisites
- Three Debian 12 machines with the following IP addresses:
- Control Node (k8s-control) – 10.0.0.1
- Worker Node 1 (k8s-compute01) – 10.0.0.2
- Worker Node 2 (k8s-compute02) – 10.0.0.3
Step 1: Set Hostnames and Update Hosts File
Login to each node and set their hostname using the hostnamectl
command:
sudo hostnamectl set-hostname "k8s-control.mycluster.local" # Run on control node
sudo hostnamectl set-hostname "k8s-compute01.mycluster.local" # Run on 1st worker node
sudo hostnamectl set-hostname "k8s-compute02.mycluster.local" # Run on 2nd worker node
Add the following entries to the /etc/hosts
file on all the nodes:
10.0.0.1 k8s-control.mycluster.local k8s-control
10.0.0.2 k8s-compute01.mycluster.local k8s-compute01
10.0.0.3 k8s-compute02.mycluster.local k8s-compute02
Step 1.1: Update system on All Nodes
apt update && apt upgrade -y
Step 2: Disable Swap on All Nodes
To ensure kubelet functions properly, disable swap:
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
Step 3: Configure Firewall Rules for Kubernetes
If your Debian systems have the firewall enabled, open the necessary ports.
On Control Node:
sudo ufw allow 6443/tcp
sudo ufw allow 2379/tcp
sudo ufw allow 2380/tcp
sudo ufw allow 10250/tcp
sudo ufw allow 10251/tcp
sudo ufw allow 10252/tcp
sudo ufw allow 10255/tcp
sudo ufw reload
On Worker Nodes:
sudo ufw allow 10250/tcp
sudo ufw allow 30000:32767/tcp
sudo ufw reload
Step 4: Install Containerd on All Nodes
Containerd is a standard container runtime supported by Kubernetes.
Configure Kernel Parameters:
cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
sudo sysctl --system
Install and Configure Containerd:
sudo apt update
sudo apt -y install containerd
containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
sudo systemctl restart containerd
sudo systemctl enable containerd
Step 5: Add Kubernetes Package Repository
Add the Kubernetes repository to your Debian systems:
sudo mkdir -p /etc/apt/keyrings
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
Step 6: Install Kubernetes Tools
Install kubeadm, kubelet, and kubectl on all nodes:
sudo apt update
sudo apt install kubelet kubeadm kubectl -y
sudo apt-mark hold kubelet kubeadm kubectl
Step 7: Set Up the Kubernetes Cluster with Kubeadm
Create a kubelet configuration file:
cat <<EOF | sudo tee kubelet.yaml
apiVersion: kubeadm.k8s.io/v1beta3
kind: InitConfiguration
---
apiVersion: kubeadm.k8s.io/v1beta3
kind: ClusterConfiguration
kubernetesVersion: "1.30.0" # Replace with your desired version
---
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
EOF
Initialize the Kubernetes cluster on the control node:
sudo kubeadm init --config kubelet.yaml
Set up kubectl on the control node:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Verify the cluster:
kubectl get nodes
kubectl cluster-info
Join worker nodes to the cluster using the command displayed during kubeadm init
. Example:
sudo kubeadm join k8s-control:6443 --token <token> \
--discovery-token-ca-cert-hash <hash>
Step 8: Install Calico Network Plugin
On the control node, install Calico:
kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/calico.yaml
Allow Calico ports in the firewall on all nodes:
sudo ufw allow 179/tcp
sudo ufw allow 4789/udp
sudo ufw allow 51820/udp
sudo ufw allow 51821/udp
sudo ufw reload
Verify Calico pods status:
kubectl get pods -n kube-system
Step 9: Test the Kubernetes Cluster
Deploy a sample Nginx application to verify the cluster:
kubectl create deployment nginx-app --image=nginx --replicas 2
kubectl expose deployment nginx-app --name=nginx-web-svc --type NodePort --port 80 --target-port 80
kubectl describe svc nginx-web-svc
By following these steps, you should have a fully functional Kubernetes cluster running on Debian 12. Enjoy managing your new cluster!
**You might face warnings from crictl like these below:
crictl images WARN[0000] image connect using default endpoints: [unix:///run/containerd/containerd.sock unix:///run/crio/crio.sock unix:///var/run/cri-dockerd.sock]. As the default settings are now deprecated, you should set the endpoint instead.
To fix this, just update crictl config with:
crictl config --set runtime-endpoint=unix:///run/containerd/containerd.sock