SnapShooter Learning Center

How to Setup Kubernetes Cluster on Debian 11

How to Setup Kubernetes Cluster on Debian 11

Simon Bennett]
Simon Bennett
Last Updated: Oct 20, 2021
Table of Contents

# Introduction

Kubernetes also known as k8s is a free and open-source container orchestration tool used for managing containerized applications. It is developed by Google and allows you to orchestrate containers across multiple hosts. With Kubernetes, you can set up load balancing between containers and run multiple containers across multiple systems. It can run on-premises bare metal, OpenStack, public clouds Google, Azure, AWS, etc.

Features

  • Automated deployment and scheduling
  • Automated rollouts & rollback
  • Horizontal Scaling & Load Balancing
  • Self-Healing Capabilities
  • Provide an in-built tool for logging and monitoring

In this guide, we will explain how to set up Kubernetes cluster on Debian 11.


# Requirements

  • Two server running Debian 11 operating system.
  • A root password is set up on your server.

# Getting Started

Before starting, Swap space must be disabled in your system. You can disable it by editing the /etc/fstab file:

nano /etc/fstab

Comment the following line:

# swap was on /dev/sda6 during installation
# UUID=18b43798-486f-499d-9edf-2c551b34b5a1 none            swap    sw              0       0

Save and close the file then disable the Swap space on the fly using the following command:

swapoff -a

Next, you will also need to enable IP forwarding. You can do it by editing /etc/sysctl.conf file.

nano /etc/sysctl.conf

Uncomment the following line:

net.ipv4.ip_forward=1

Save and close the file when you are finished.


# Install Docker CE on both Master and Worker Node

Next, you will need to install Docker CE on both the Master and Worker nodes. First, install all the required dependencies using the following command:

apt-get install apt-transport-https ca-certificates curl gnupg2 software-properties-common

Next, add the Docker CE repository with the following command:

curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list

Next, update the repository and install the Docker CE with the following command:

apt-get update -y
apt-get install docker-ce docker-ce-cli containerd.io -y

Once Docker CE has been installed, verify the installed version of Docker CE with the following command:

docker -v

You will get the following output:

Docker version 20.10.9, build c2ea9bc

# Install Kubernetes Components on Both Master and Worker Node

Next, you will also need to install the Kubernetes component on both nodes. First, add the Kubernetes repository using the following command:

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add
apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"

Next, update the repository and install all required components using the following command:

apt-get update -y
apt-get install kubelet kubeadm kubectl -y

Once all the Kubernetes packages are installed, you can proceed to the next step.


# Initialize Kubernetes Cluster

Next, you will need to initialize the Kubernetes cluster on the Master node. You can initialize it with the following command:

kubeadm init

You will get the following output:

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

Alternatively, if you are the root user, you can run:

export KUBECONFIG=/etc/kubernetes/admin.conf

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 69.28.91.245:6443 --token 3kwruw.iz4wiwbnr1l31o6t \
--discovery-token-ca-cert-hash sha256:c199b45b1abffe99f8ec539d5c6681a8857aac51b423a4e4efebf9ad86b86705 

Next, run the following command to activate the kubernetes configuration:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Note: Remember the command kubeadm join from the above output. You will need to run it on the Worker node to join the cluster.


# Join Worker Node to the Kubernetes Cluster

Next, go to the Worker node and run the following command to join the Kubernetes cluster:

kubeadm join 69.28.91.245:6443 --token 3kwruw.iz4wiwbnr1l31o6t \
--discovery-token-ca-cert-hash sha256:c199b45b1abffe99f8ec539d5c6681a8857aac51b423a4e4efebf9ad86b86705 

You will get the following output:

This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.

Run 'kubectl get nodes' on the control-plane to see this node join the cluster.

# Verify the Cluster Status

Now, go to the Master node and run the following command verify the Worker node.

kubectl get nodes

You should see the following output:

NAME     STATUS   ROLES                  AGE     VERSION
master   Ready    control-plane,master   8m30s   v1.22.2
worker   Ready    <none>                 6m12s   v1.22.2

As you can see, both worker and master node have joined the cluster.


# Deploy Calico Pod Network Add-on

Next, you will need to install the Calico pod network add-on on the Master node. You can install it using the following command:

kubectl apply -f https://docs.projectcalico.org/v3.16/manifests/calico.yaml

Now, verify the status of pods from all namespaces using the following command:

kubectl get pods --all-namespaces

You will get the following output:

NAMESPACE     NAME                                      READY   STATUS    RESTARTS      AGE
kube-system   calico-kube-controllers-677965bf5-8nqxn   0/1     Error     2 (34s ago)   76s
kube-system   calico-node-6qhqm                         1/1     Running   0             80s
kube-system   calico-node-x7sm8                         1/1     Running   0             80s
kube-system   coredns-78fcd69978-8zllm                  1/1     Running   0             8m42s
kube-system   coredns-78fcd69978-hxzfw                  1/1     Running   0             8m42s
kube-system   etcd-master                               1/1     Running   0             8m48s
kube-system   kube-apiserver-master                     1/1     Running   0             8m45s
kube-system   kube-controller-manager-master            1/1     Running   0             8m45s
kube-system   kube-proxy-78pck                          1/1     Running   0             6m31s
kube-system   kube-proxy-sgqz5                          1/1     Running   0             8m43s
kube-system   kube-scheduler-master                     1/1     Running   0             8m45s

# Verify Kubernetes Cluster

Next, you will need to test whether the Kubernetes cluster is working or not.

To test it, let's create an Nginx deployment using the following command:

kubectl create deployment nginx-web --image=nginx --port=80

Next, verify your Nginx deployment using the following command:

kubectl get deployments.apps  -o wide

You will get the following output:

NAME        READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES   SELECTOR
nginx-web   0/1     1            0           6s    nginx        nginx    app=nginx-web

Next, scale your deployment with 3 replica using the following command:

kubectl scale --replicas=3 deployment nginx-web

Now verify the status of your deployment using the following commands,

kubectl get deployments.apps nginx-web

Sample output:

NAME        READY   UP-TO-DATE   AVAILABLE   AGE
nginx-web   3/3     3            3           40s

Next, run the following command to create a pod for Nginx deployment.

kubectl run nginx-web --image=httpd --port=80

Next, create an Nginx service and expose it on port 80.

kubectl expose pod nginx-web --name=http-service --port=80 --type=NodePort

You can now check the Nginx service using the following command:

kubectl get service http-service

Sample output:

NAME           TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
http-service   NodePort   10.103.75.66   <none>        80:30844/TCP   36s

Next, get detailed information about the Nginx service using the command below:

kubectl describe service http-service

You will get the following output:

Name:                     http-service
Namespace:                default
Labels:                   run=nginx-web
Annotations:              <none>
Selector:                 run=nginx-web
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.103.75.66
IPs:                      10.103.75.66
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  30844/TCP
Endpoints:                192.168.171.75:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

You can check your running Nginx pods using the following command:

kubectl get pods nginx-web -o wide

Sample output:

NAME        READY   STATUS    RESTARTS   AGE    IP               NODE     NOMINATED NODE   READINESS GATES
nginx-web   1/1     Running   0          106s   192.168.171.75   worker   <none>           <none>

Now, you can verify the Nginx server by specifying the Worker node's IP address as shown below:

curl http://worker-node-ip:30844

If everything is fine, you will get the following output:

<html><body><h1>It works!</h1></body></html>

# Conclusion

In the above guide, you learned how to set up a Kubernetes cluster on Debian 11. You can now add more Worker nodes to the Kubernetes cluster and scale your application as per your requirement.


Did you find this article helpful?