Set up a development grade kubernetes cluster

Nithish Raja.G
cverse-ai
Published in
3 min readJan 12, 2020

This article will detail the steps taken to install a development grade kubernetes cluster on ubuntu servers. This article will detail steps to set up a single master and single worker cluster, however the same steps can be followed for adding any number of workers.

If you wish to change hostnames of each server, do it before installing and initialising kubernetes. Changing it later might cause problems with DNS resolution. To change hostname run the following command.

sudo hostnamectl set-hostname <hostname>

Installing Kubernetes

Steps 1 to 5 should be performed on all nodes. Step 6 to 7 should be done only on master node and step 8 should only be done on all worker nodes.

Step 1: Install docker

Docker needs to be installed on all the nodes. It can be installed by running the following command.

sudo apt-get -y install docker.io # Install docker

Once docker is installed check its version and enable it.

sudo docker version # Get docker versionsudo systemctl enable docker # Enable docker

Step 2: Add Kubernetes signing key

Run the following command to install curl and get kubernetes signing key.

sudo apt-get -y install curl # Install curlcurl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add # Get signing key

Step 3: Add Kubernetes repository

Once the signing key is added, add the kubernetes repository.

sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main" # Add kubernetes repository

Step 4: Install kubeadm

Once the repository had been added, install kubeadm package.

sudo apt-get update # Update dependenciessudo apt-get install -y kubeadm

The above command installs the latest version of kubernetes. However, if a specific version of kubernetes is required then run the command provided below.

export KUBE_VERSION=<specify version here> # Set kubernetes versionsudo apt-get install -y kubelet=$KUBE_VERSION kubectl=$KUBE_VERSION kubeadm=$KUBE_VERSION # Install version specific packagessudo kubeadm version # Check kubeadm version

Step 5: Disable swap memory

sudo swapoff -a

Step 6: Initialise master node

Before initialising kubernetes on the master node, decision on which CNI to use must be made. Refer kubernetes documentation to view all available CNI. Based upon the chosen CNI, the pod-network-cidr flag value changes during initialisation. Use this link to get the correct init command for the chosen CNI. In this article, steps to setup a flannel CNI are mentioned.

sudo kubeadm init --pod-network-cidr=10.244.0.0/16

The init command takes a few minutes to complete and outputs three new commands to run. These commands need to be run to avoid running all kubectl command as a superuser.

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

A join command is also printed after initialisation is complete. Worker nodes can be connected to the cluster by running it. However, if you missed the join command, run the following command to get a new join command.

sudo kubeadm token create --print-join-command

Step 7: Deploy a CNI

To deploy the latest version of flannel CNI run the following command.

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

If you have installed a different version of kubernetes or a different CNI, refer this to get the command for that CNI. Once the deployment of CNI is over, worker nodes can be added to the cluster.

Step 8: Add worker nodes

Run the join command in all worker nodes to join them to the cluster. The join command looks something similar to the following.

sudo kubeadm join 192.168.100.6:6443 --token 06tl4c.oqn35jzecidg0r0m --discovery-token-ca-cert-hash sha256:c40f5fa0aba6ba311efcdb0e8cb637ae0eb8ce27b7a03d47be6d966142f2204c

Run the following command to check if all nodes have joined the cluster and have reached the ready state.

kubectl get nodes -w

Uninstall Kubernetes

Often things go wrong while installing Kubernetes and there are many ways to fix it. However, the simplest way is to uninstall and then reinstall Kubernetes. No matter what the reason for uninstalling kubernetes may be, the following steps will help with uninstalling kubernetes.

Run the following commands on master node to drain worker node

kubectl drain <node name> --delete-local-data --force --ignore-daemonsetskubectl delete node <node name>

Run the following commands on worker node to remove the kubernetes packages.

sudo kubeadm reset 

sudo apt-get purge kubeadm kubectl kubelet kubernetes-cni kube* -y
sudo apt-get autoremove

sudo rm -rf ~/.kube
sudo rm -rf /opt/cni/binsudo rm -rf /etc/cni/net.d

After all worker nodes are removed, run the above commands on master node to remove kubernetes package in master node as well.

--

--