How to set Kubernetes using kubeadm

Creating a Kubernetes (K8s) cluster can seem daunting at first, but with kubeadm, the process is significantly simplified. Kubeadm is a tool built to provide kubeadm init and kubeadm join as best practices for creating Kubernetes clusters. It performs the actions necessary to get a minimum viable, secure cluster up and running in a user-friendly way. This blog post will guide you through setting up a Kubernetes cluster using kubeadm.

Prerequisites

Before you start, you will need:

  • Two or more machines for your cluster. One machine will be designated as the master node, and the others will be your worker nodes.
  • A compatible Linux distribution (Ubuntu 20.04, CentOS 7, Fedora 31, etc.) is installed on each machine.
  • Full network connectivity between all machines in the cluster (public or private network).
  • Unique hostname, MAC address, and product_uuid for every node. You can check the product_uuid with the command sudo cat /sys/class/dmi/id/product_uuid.
  • Certain ports are open on your machines. For example, the Kubernetes API server is accessed through port 6443.
  • Swap disabled. It would help if you disabled swap on each server because Kubernetes does not support swap memory.

Step 1: Install kubeadm, kubelet, and kubectl

Run this command to install docker if you haven’t installed yet.

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update -y
sudo apt-get install docker-ce docker-ce-cli containerd.io -y

sudo docker run hello-world

You need CRI support enabled to use containerd with Kubernetes. Make sure that cri is not included in thedisabled_plugins list within /etc/containerd/config.toml; if you made changes to that file, also restart containerd

sudo systemctl restart containerd

Install kubeadm, kubelet, kubectl

sudo apt-get update -y
# apt-transport-https may be a dummy package; if so, you can skip that package
sudo apt-get install -y apt-transport-https ca-certificates curl

curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.27/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.27/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

Step 2: Create a Kubernetes Cluster with kubeadm

On the master node, initialize the cluster:

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

Note: The --pod-network-cidr flag specifies the network CIDR for pod IPs. Adjust it according to your network provider requirements.

After initialization, to start using your cluster, you need to run 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

Apply a pod network to the cluster so that your pods can communicate with each other. The following command is an example of applying the Calico network plugin:

curl https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/calico.yaml -O

kubectl apply -f calico.yaml

Control plane node isolation

By default, your cluster will not schedule Pods on the control plane nodes for security reasons. If you want to be able to schedule Pods on the control plane nodes, for example for a single machine Kubernetes cluster, run:

kubectl taint nodes --all node-role.kubernetes.io/control-plane-

Step 3: Join Nodes in Your Cluster

On each worker node, join the cluster by using the command provided at the end of the kubeadm init output on the master node. It looks something like this:

Verify the cluster is fully operational:

kubectl get nodes

You can add a shortcut for your ‘kubectl’ command by adding an alias k in .bashrc file

nano ~/.bashrc
source ~/.bashrc

You should see all your nodes listed, and after a short time, they should all be in the Ready state.

Step 4: Deploy Applications

Now that your cluster is up and running, you can start deploying applications using Kubernetes manifests or Helm charts.

k run nginx-pod --image=nginx:alpine

Troubleshooting

If you encounter issues during the setup, consult the official Kubernetes documentation or the troubleshooting section of the kubeadm documentation.

Conclusion

Setting up a Kubernetes cluster with kubeadm is a straightforward process that involves installing the necessary tools, initializing the master node, joining worker nodes, and applying a network plugin. By following these steps, you can get a Kubernetes cluster up and running with minimal hassle. Once your cluster is set up, you can deploy applications and explore the powerful features Kubernetes offers

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

Leave a Reply