Maab S.
5min Read

Install Kubernetes cluster on Debian 9

Install Kubernetes cluster on Debian 9

Kubernetes (k8s) is an open-source, cloud-native, container orchestration and management platform. It’s the go-to way to automate the deployment, scaling, and maintenance of containerised applications across different nodes. From service discovery to auto-restarts, and from resource allocation tracking to compute utilisation and scaling; a well-configured k8s cluster can manage a lot on its own.


What is a Kubernetes Cluster?

A Kubernetes cluster consists of a Master and at least one to several worker node(s). The Master is the virtual machine (VM) that administers all activities on your cluster. A node is a VM that serves as a worker machine in your k8s cluster to host running applications. We strongly recommend you only use VMs aka Cloud Servers to run Kubernetes, not system containers aka VPS, as these can cause issues with k8s.

HOSTAFRICA Kubernetes Cluster Diagram – Debian

 

A node is comprised of the Kubelet, a container runtime, and the kube-proxy. The k8s installation’s three core modules: Kubelet, kubeadm, and kubectl are agents that control the node and communicate with the Kubernetes Master. Once they have been installed and other configurations done, you will be able to create your first k8s cluster. You can manage this cluster from the command line on your kubemaster node.

Every Kubernetes instance runs on top of a container runtime, which is software responsible for managing container operations. Containers in this case are not virtualised servers but rather a solution that packages code and dependencies to run a single application (service) in an isolated (containerised) environment, essentially disassociating applications from the host machine. The most popular and recommended one is Docker, and it’s the one we will use for the purpose of this guide. However, if you want to install a different underlying container runtime, you can harness the power of the Container Runtime Interface and use basically any runtime you want.

Kubernetes groups containers into pods, its most basic operational unit, which are basically just groups of containers running on the same node. Pods are connected over a network and share storage resources.

In order to connect your nodes or VMs and make them private, make sure to choose a hosting company who provides a Virtual Local Area Network (VLAN) with their VMs. We offer a VLAN add-on to our Cloud Servers for R200 per month.

Prerequisites
• Multiple Debian 9 VMs (Cloud Servers) to house the Master and worker nodes.
• Docker or any other container runtime.
• User with sudo or root privileges on every server.


How to install Kubernetes on Debian 9

Step 1. Install Docker on all Debian 9 VMs

Use our guide on How to install Docker on Debian 9 and 10

Your final output should be as shown below:

Docker output

Step 2. Install Curl and https

First, we need to install curl and https on every machine. Use the following commands:

sudo apt install curl
sudo apt-get install -y apt-transport-https

Step 3. Add Kubernetes public key

Now get and add the key

sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -

command output

Step 4. Add an apt repository

Next, we need to create a file for our new apt repository. Use the following command:

cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF

command output

Step 5. Update the system and install Kubernetes modules

To update your system, enter:

sudo apt-get update

After the command finishes execution, install the Kubernetes modules using:

apt-get install -y kubelet kubeadm kubectl

Step 6. Set hostnames

Set the appropriate hostnames for your Master and worker nodes:

sudo hostnamectl set-hostname "master-node"
exec bash

And

sudo hostnamectl set-hostname "w-node1"
exec bash

Now open the /etc/hosts file and edit the hostnames for your worker nodes:

sudo cat <<EOF>> /etc/hosts
10.168.10.207 master-node
10.168.10.208 node1 W-node1
10.168.10.209 node2 W-node2
EOF

Step 7. Configure Firewall

For seamless communication across multiple nodes, we need to define rules in firewall. Use the following commands on your Master node to do so:

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

We also need to execute these commands on each of the worker nodes:

sudo ufw allow 10251/tcp
sudo ufw allow 10255/tcp
sudo ufw reload

Step 8. Turn swap off

For Kubelet to work, we also need to disable swap on all of our VMs. Use the following command to turn swap off:

sudo swapoff -a

Step 9. Enable Kubelet

Finally, we need to enable the kubelet service. Use this command:

sudo systemctl enable kubelet

This concludes our installation and configuration of Kubernetes on Debian 9. We will now share the steps for deploying a k8s cluster.


Deploying a Kubernetes Cluster on Debian 9

Step 1. kubeadm initialisation

To launch a new Kubernetes cluster instance, you need to initialise kubeadm. Use the following command:

sudo kubeadm init

This command may take several minutes to execute. Upon success, you should get logs similar to those in this screenshot:

command output

You will also get an auto-generated command at the end of the output. Copy the text following the line Then you can join any number of worker nodes by running the following on each as root: as highlighted in the above screenshot and save it somewhere safe. We will use this to add worker nodes to our cluster.

Note: If you forgot to copy the command, or have misplaced it, don’t worry. You can retrieve it again by entering the following command:

sudo kubeadm token create --print-join-command

Step 2. Create required directories and start managing Kubernetes cluster

In order to start managing your cluster, you need to create a directory and assume ownership. Run the following commands 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

Step 3. Set up Pod network for the Cluster

Pods within a cluster are connected via the pod network. At this point, it’s not working. This can be verified by entering the following two commands:

sudo kubectl get nodes
sudo kubectl get pods --all-namespaces

command output

As you can see, the status of masternode is NotReady. The CoreDNS service is also not running. To fix this, run the following commands:

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

You should get the following output:

command output

And now, if you verify the statuses of your node and CoreDNS service, you should get Ready and Running like seen below:

command output

Step 4. Add nodes to your cluster

As a final step, you need to add worker nodes to your cluster. We will use the kubeadm join auto-generated token in Step 1. here.

Run your own version of the following command on all of the worker node VMs, meaning you have to replace 102.130.117.206:6443with your own machine’s IP address.

kubeadm join 102.130.117.206:6443 --token v9qxex.i6jant8m2r0zxhhk     --discovery-token-ca-cert-hash sha256:b0cda6d2e64a8a65ad5f439e06c3cb489c3d7f6f4e0c094ebb2556037153dc4b

On successful addition, you should get the following output:

command output

 

Running the following command on the master-node should show your newly added node.

sudo kubectl get nodes

command output

To set the role for your worker node, use the following command:

sudo kubectl label node w-node1 node-role.kubernetes.io/worker=worker

command output

Now you’re all set up.

Happy Hosting!


The Author

Maab S.

Maab is an experienced software engineer who specializes in explaining technical topics to a wider audience.

More posts from Maab