Skip to main content

Command Palette

Search for a command to run...

Creating Kubernetes Cluster using Kubeadm

Published
5 min read
Creating Kubernetes Cluster using Kubeadm

Before starting with our handson let's get some introduction about Kubeadm in Kubernetes.

What is Kubeadm?

Kubeadm is a tool used for automating the process of setting up and managing Kubernetes clusters. It is a command-line tool that can be used to bootstrap a cluster, configure the control plane, and add worker nodes to the cluster. Kubeadm is part of the Kubernetes project and is a recommended way of creating and managing production-grade clusters.

Kubeadm performs several tasks during the cluster creation process. These include:

  1. Setting up the Kubernetes control plane: This includes initializing the etcd cluster, generating certificates and keys, and configuring the Kubernetes API server, scheduler, and controller manager.

  2. Joining worker nodes to the cluster: Kubeadm generates a token that is used by worker nodes to join the cluster. Worker nodes are required to have the token, as well as the cluster's certificate authority (CA) and key.

  3. Installing network plugins: In order for pods to communicate with each other in the Kubernetes cluster, you need to install a network plugin. Kubeadm supports several plugins, including Flannel, Calico, and Weave.

Kubeadm is designed to be flexible and configurable, allowing users to customize their Kubernetes cluster based on their specific requirements. It supports various configuration options, such as specifying the network pod CIDR, enabling or disabling certain features, and configuring the API server's service IP address.

Overall, Kubeadm simplifies the process of creating and managing Kubernetes clusters, making it easier for developers and DevOps teams to deploy and manage containerized applications.

Now let's get started with practical.

Firstly I will create one AWS Server with 2 CPU and 4 GB RAM for Master.

  • A minimum of two servers running Ubuntu 18.04 or later, with at least 2GB of RAM and 2 CPUs. One server will act as the master node, and the others will be worker nodes.

  • SSH access to all servers.

Also, I will create another instance with free tier eligibility for worker node.

and will connect them.

Master:

Worker:

Step 1: Install Docker and Kubeadm

First, we need to install Docker and Kubeadm on all of the servers. Run the following commands on each server:

sudo apt-get update
sudo apt-get install -y docker.io
sudo systemctl enable docker
sudo systemctl start docker
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 -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update

Step 2: Install kubeadm,cubectl and cubelet with following commands on both serveres.

 sudo apt-get install kubeadm=1.20.0-00
 sudo apt-get install kubectl=1.20.0-00
 sudo apt-get install kubelet=1.20.0-00

Note: Cubectl is not required on worker node so you can skip cubectl command on worker node.

Now our Master and Worker nodes are individually ready but not connected to each other right now.

So, let's connect them to each other.

Now, let's come to the master node and switch to root user with below command.

sudo -i

Now, let's initialise Kubeadm on Master and setting up cluster with following command.

kubeadm init

This command will pull etcd, schedular,controller manager,cube api server etc.

Now, you will below instructions where it will ask to you run some set of commands.

Here I am a root user so I am using below command.

export KUBECONFIG=/etc/kubernetes/admin.conf

and if you want to see what is in this admin.conf file just do cat and path of this file.

cat /etc/kubernetes/admin.conf

You will get below details with user details and key.

I will use same key to connect my worker node to this Master node.

Now on Master node Kubectl finish the setup and network will be created with below command

kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml

Below result we got.

serviceaccount/weave-net created clusterrole.rbac.authorization.k8s.io/weave-net created clusterrolebinding.rbac.authorization.k8s.io/weave-net created role.rbac.authorization.k8s.io/weave-net created rolebinding.rbac.authorization.k8s.io/weave-net created daemonset.apps/weave-net created

Now, We will generate a connection-token that will be used to connect the Master node to the Worker node.

Now, Let's create a token with the help of the below command on the Master node.

kubeadm token create --print-join-command

You will get the result as below.

Any worker who has this token will be able to my cluster.

Now, I will jump to the Worker server and switch to the root user.

I will reset all checks so that we can join any server freshly and after running this command your worker node will be disabled for running "kubeadm init" command this node will remain the Worker node always and kubeadm will not be installed.

kubeadm reset pre-flight checks

Here is the result.

Now we will make a connection between the Master and the Worker node but before that, we need to allow a port(6443) that is mentioned in your token if not allowed.

Now copy that token and add version flag after token as "--v=5" as below.

Worker Node:

kubeadm join 172.31.5.164:6443 --token 7rr46i.o73q0cjzaaw3kv5i     --discovery-token-ca-cert-hash sha256:561eeaaad24b0cb1df9b6a9ce1aee6dc0694e613109434cbac99fa --v=5

Currently, in our Master node, we have only one node but after running the above command on the Worker node it will be show our worker node.

Before joining Master :

After the connection done, we have now the worker node available...

on Master:

Your cluster is ready!!!!!

Now I want to run the nginx pode on my Worker node.

Very easy to go to your Master and run the below command.

 kubectl run nginx --image=nginx --restart=Never

The magic is here .... your Nginx is running on your Worker node.

see below.

Worker Node:

Master Node: you can see here with below command.

kubectl get pod

Master:

It is so easy wooooohoooooooooooooooo!!!

If you enjoyed it, hit the heart button and follow me here and on Linkedln...