K8s Cluster using kubeadm and calico networking

kunal borse
4 min readNov 1, 2020

Goal:

Create on premise Kubernetes cluster using kubeadm.

This article shows how to install a Kubernetes cluster with multiple nodes on ubuntu 16.04. You’ll run the nodes inside virtual machines through VirtualBox, but you can also use a different virtualization tool.

VM Creation:

First, Create a Virtual machine for k8s master.

Perform the below steps on the master VM.

Installation of Docker:

  1. Log into the machine as root. First, you need to disable swap.
  2. Disabling swap : Run command swapoff -a && sed -i ‘/ swap / s/^/#/’ /etc/fstab
  3. Now you’re ready to install all the packages required for docker:

apt-get update -y && apt-get install apt-transport-https ca-certificates curl gnupg-agent cifs-utils nfs-client software-properties-common -y && curl -fsSL https://download.docker.com/linux/ubuntu/gpg|apt-key add — && sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable” && sudo apt-get update -y && sudo apt-get install docker-ce docker-ce-cli containerd.io -y
Please note above command will install the latest version of docker.

Installation of kubelet,kubeadm and kubectl:

  1. Run below commands

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

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

cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list

deb https://apt.kubernetes.io/ kubernetes-xenial main

EOF

sudo apt-get update

sudo apt-get install -y kubelet kubeadm kubectl

sudo apt-mark hold kubelet kubeadm kubectl

Clone VM:

  1. Everything you’ve done up to this point must be done on every machine you plan to use in your cluster
  2. To clone the machine in VirtualBox, first shut down the VM by running the shutdown command: shutdown now
  3. Clone master VM and create new VM’s as per y number of nodes required for your cluster.
  4. Because you created two clones from your master VM, all three VMs have the same hostname configured. Therefore, you need to change the hostnames of the two clones. To do that, log into each of the two nodes (as root) and run the following command:

hostnamectl — static set-hostname node1.k8s

hostnamectl — static set-hostname node2.k8s

  1. You need to ensure that all three nodes are resolvable either by adding records to a DNS server or by editing the /etc/hosts file on all of them. For example, you need to add the following three lines to the hosts file (replace the IPs with those of your VMs), as shown in the following listing.

Entries to be added in /etc/host file of each node

172.16.83.20 master

172.16.83.28 node1.k8s

172.16.83.29 node2.k8s

Configuring the master with kubeadm:

  1. all you need to do to initialize the master is run a single command, as shown in the following listing.

kubeadm — init — pod-network-cidr=192.168.0.0/16

[bootstrap-token] Creating the “cluster-info” ConfigMap in the “kube-public” namespace

[kubelet-finalize] Updating “/etc/kubernetes/kubelet.conf” to point to a rotatable kubelet client certificate and key

[addons] Applied essential addon: CoreDNS

[addons] Applied essential addon: kube-proxy

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

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 172.16.83.20:6443 — token 2me71h.pceuuwi7uzwz8b9w \

— discovery-token-ca-cert-hash sha256:178b5585ceef0e03dfb9f604f1989206b130fdea9bcd0df78175dfbd1ec0a701

kubeadm join 172.16.83.20:6443 — token 2me71h.pceuuwi7uzwz8b9w \

Write down the command shown in the last line of kubeadm init’s output. You’ll need it later.

  1. 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

  1. You can check nodes status using the below command

kubectl get nodes

Setting up the container network , we are using calico for networking
Run below command
kubectl apply -f https://docs.projectcalico.org/v3.8/manifests/calico.yaml

Configuring worker nodes with kubeadm:

  1. when you ran the kubeadm init command to set up your master, it already told you
  2. How to configure worker nodes.

All you need to do is run the kubeadm join command with the specified token and the master’s IP address/port on both of your nodes. It then takes less than a minute for the nodes to register themselves with the master. You can confirm they’re registered by running the kubectl get node command on the master again:

kubeadm join 172.16.83.20:6443 — token 2me71h.pceuuwi7uzwz8b9w \

— discovery-token-ca-cert-hash sha256:178b5585ceef0e03dfb9f604f1989206b130fdea9bcd0df78175dfbd1ec0a701

3. Run kubectl get nodes to verify the registration of worker nodes.

4. You will get final result as below :
root@ubuntu:/home/ubuntu# kubectl get nodes

NAME STATUS ROLES AGE VERSION

nodeone Ready <none> 4d21h v1.19.3

nodetwo Ready <none> 4d21h v1.19.3

ubuntu Ready master 4d21h v1.19.3

--

--