Creating a production-quality Kubernetes Cluster in Ubuntu/Debian- step by step guide
When learning kubernetes, we often use tools like minikube to deploy our cluster. But, it abstract away lot of things from us about creating a kubernetes cluster. In real world scenario, we are not going to deploy our application using minikube or other such tools. To deploy a production-quality kubernetes cluster, we need tools like Kubeadm to achieve that goal.
In this article, I will walk you through deploying a production ready kubernetes cluster with the help of Kubeadm in Ubuntu/Debain server.
If you want to learn the nitty-gritty of creating a Kubernetes cluster and want to do it the hard way, consider going through this tutorial.
Requirements
- At least two Ubuntu/Debian based system. For this I used Google Compute Engine.
- The master node or control-plane should have at least 2Gi of RAM and 2 CPU.
- If you are using a Cloud Platform to run your machine, place your machine in the same VPC.
Part 0
For every node/system of your kubernetes cluster perform the below steps -
Perform all the steps as a non-root user to avoid errors.
- Update your repository
sudo apt update
2. We will use Docker as a container runtime for our kubernetes cluster. To install docker and start it automatically, every time our system restarts, use -
sudo apt install docker.iosudo systemctl enable dockersudo systemctl start docker
3. Run the following commands to add the kubernetes gpg key and repository to the system-
For Ubuntu -
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
For Debian -
sudo apt-get install -y apt-transport-https ca-certificates curlsudo curl -fsSLo /usr/share/keyrings/kubernetes-archive- keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpgecho"deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main"| sudo tee /etc/apt/sources.list.d/kubernetes.list
4. Download and install the Kubeadm, Kubelet and Kubectl tool to bootstrap the cluster -
sudo apt update
sudo apt install kubeadm kubelet kubectl
sudo apt-mark hold kubeadm kubelet kubectl
Go through this to learn about Kubeadm.
5. By default Ubuntu create a swap area for your system, which can lead to stability issue within kubernetes. So, we need to disable the swap memory, to do that, use-
sudo swapoff -a
To make sure after every restart, the swap memory stays disabled, run the following-
sudo sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab
For Debian OS, swap memory is disabled by default, so no need to perform the above step unless you already enabled the swap memory.
Part 1
For the master node/Control-plane of your Cluster, perform the below steps-
- Make a directory for docker runtime if it doesn’t exist already -
sudo mkdir /etc/docker
2. Create a file inside the folder and add the following lines of code to the file -
sudo nano /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
To save the file and exit nano, press ctrl+o
and then ctrl+x
. To understand what the file is doing for your cluster, click here.
3. Reload your background services and restart docker -
sudo systemctl daemon-reload
sudo systemctl restart docker
4. Initialize the Kubernetes master node using Kubeadm and define a pod network for the cluster -
sudo kubeadm init --pod-network-cidr=10.240.0.0/16
This will take a minute or two to complete the execution. If successfully initialized, it will tell you to execute the following commands. From the output, copy the kubeadm join command with the token and note it down.
5. Add the kube admin config file to your non-root user. To do that execute the following-
mkdir -p $HOME/.kubesudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/config
6. Our pods needs to talk to each other across all our nodes. We will use the Flannel plugin to create our pod network inside the cluster. To install the flannel plugin -
sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
You are all done with the master node setup. To confirm that the master node is working fine, execute the following -
kubectl get pods --all-namespaces
Or to see the nodes, run -
kubectl get nodes
Part 2
For every worker/slave node of your cluster, perform the below steps -
Before beginning with the following steps, don’t forget to complete the steps mentioned in Part 0 .
- Add config file -
sudo nano /etc/docker/daemon.json
2. Copy, paste and save the file -
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver": "overlay2"
}
3. Reload and Restart Docker -
sudo systemctl enable docker
sudo systemctl daemon-reload
sudo systemctl restart docker
4. Now, copy and paste the kubeadm join command which you obtained from the master node initialization. It should look something like this -
Do the above steps for every worker node, and your kubernetes cluster should be up and running.
To check whether the node has joined the cluster, SSH into master node and execute -
kubectl get nodes
If you still got an error, let me know in the comments. I will try to resolve it.
If you learned something from this article, please don’t forget to share and give it a Like.
Keep Learning :)