Setting up Kubernetes Multi Node Cluster on-premise in Ubuntu

Vamsi Krishna Gutta
4 min readMay 26, 2021

When it comes to open source deployment tool which can scale the application, kubernetes is no brainer.

This post guides you to create a basic kubernetes cluster using kubeadm in your local machine

The kubeadm tool is good if you need:

  • A simple way for you to try out Kubernetes, possibly for the first time.
  • A way for existing users to automate setting up a cluster and test their application.
  • A building block in other ecosystem and/or installer tools with a larger scope.

Environment Setup

Generally it is recommended that we have at-least 2 control-nodes along with other worker nodes. For simplicity we will create a single control and multiple worker nodes.

The minimum requirement for a vm to setup as node in kubernetes is

  • 2 GiB or more of RAM per machine — any less leaves little room for your apps.
  • At least 2 CPUs on the machine that you use as a control-plane node.
  • Full network connectivity among all machines in the cluster. You can use either a public or a private network.

Considering that we have created a Ubuntu vm with the specification above installed let us proceed to the next step

Installing the container runtime

Container runtime is a software that is responsible for running the containers. We have many popular container runtimes, like containerd,crio and docker. Let us use docker for this setup

If the docker setup is not done already, let us perform it by using the script below in the command prompt

curl -fsSL https://get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh

To check for other methods of installing docker check the official documentation

Installing the kubernetes using kubeadm

Once the container runtime setup is done, we move on to install the kubernetes tools. kubeadm, kubelet, kubectl.

kubeadm : tool used to initiate the cluster

kubelet: A service which runs in all the nodes including the master and worker nodes. This supports the things like starting containers and pods.

kubectl : command line tool to communicate with the cluster

# Update the sources and install packages to support kubeadm #installation
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl

# Download the google cloud public signing key
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg

# Add kubernetes to apt repository
echo "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

# update the package sources and install the kubernetes tools
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

Duplicating the environment for multi nodes

Considering the above setup is done in a vm, we can simply duplicate this vm for the worker nodes as well. If the setup is done in a bare metal machine, we need to follow the steps until now for all control and worker machines

Assigning Control Node and Joining Worker Nodes

Once the both the control and worker vms are setup, we need to assign a vm as control-node.

Creating Cluster:

Run the commands shown below to create the cluster in the control-node.

kubeadm init

The above command download docker images and creates a cluster with the vm in which it ran as the control-node. Once the setup is done the following text appears

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:
/docs/concepts/cluster-administration/addons/

You can now join any number of machines by running the following on each node
as root:

kubeadm join <control-plane-host>:<control-plane-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>

To make the kubectl available for non root users we run below commands

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

Joining Nodes:

Once the control-node setup is done, we need to get the join command which when executed on the worker nodes joins them to the cluster. This command is found at the ending of the kubeadm init command which looks like

kubeadm join <control-plane-host>:<control-plane-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>

or this command can be retrieved using the following command on the master node

kubeadm token create --print-join-command

Installing the CNI(Container Network Interface)

After the cluster is initialized and the worker nodes are joined. The nodes will be in NotReady state as the container network interface is not defined.

Container network interface is the way in which kubernetes implements the networking between pod-pod, pod-service, service-external etc. Many cloud providers provide their own Container network interface. while there are many third party container interfaces. We are going to using Calico as the network interface.

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

The comparison of various CNIs is present in here

You should be able to see all the control and worker nodes as ready executing the command to initiate CNI. Now you can deploy your applications,

Happy Deploying :)

References

  1. https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
  2. https://docs.docker.com/engine/install/
  3. https://docs.projectcalico.org/getting-started/kubernetes/self-managed-onprem/onpremises
  4. https://stackoverflow.com/a/51126179/9091771
  5. https://rancher.com/blog/2019/2019-03-21-comparing-kubernetes-cni-providers-flannel-calico-canal-and-weave/

--

--

Vamsi Krishna Gutta

I am a software engineer, who loves to solve problems and learn new technologies.