Creating Kubernetes Cluster using Kubeadm

·

4 min read

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.

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

  • kubeadm – a CLI tool that will install and configure the various components of a cluster in a standard way.

  • kubelet – a system service/program that runs on all nodes and handles node-level operations.

  • kubectl – a CLI tool used for issuing commands to the cluster through its API Server.

Also, I will create two instances of Master & Worker Node

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

Install kubeadm, kubectl and kubelet with the following commands on both servers.

Found some error

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 kubelet kubeadm kubectl -y

Finally, install the kubelet, kubeadm, kubectl

Note: Kubectl is not required on the worker node so you can skip the Kubectl command on a worker node.

Now our Master and Worker nodes are individually ready but not connected now. So, let's connect them.

So, let's connect them.

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

sudo -i

Now, let's initialize Kubeadm on Master and set up a cluster with the following command.

kubeadm init

$ export KUBECONFIG=/etc/kubernetes/admin.conf

/etc/kubernetes/admin.conf - this admin.conf file has a key that is used to connect to the worker node.

cat /etc/kubernetes/admin.conf

You will get the below details with user details and key.

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

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

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

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

kubeadm token create --print-join-command

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

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

Reset pre-flight checks to join cluster freshely

$ kubeadm reset pre-flight checks

Important: Don't initialize kubeadm in the worker node by mistake otherwise your worker node will become the master node.

Copy the token from the Master node and paste it to the worker node, also don't forget to expose the port by adding it to the security group.

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

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

Now check Nginx runs or not kubectl get pod

The pod status is pending insufficient resources of one type or another that prevent scheduling.

Check logs in the master node. journalctl -u kubelet

Kubelet service not yet started you restart the service

systemctl restart kubelet

kubectl get nodes

Finally created a cluster using kubeadm...!!!