Screenshot from the article

Setting Up Kubernetes with kubeadm: A Complete Guide

kubeadm is a powerful tool that simplifies Kubernetes cluster setup. It is ideal for production-grade clusters as well as local testing.

In this guide, we’ll cover:

  • Installing kubeadm (install guide)
  • Setting up a Kubernetes cluster
  • Managing nodes and networking
  • Deploying applications
  • Advanced configurations
  • FAQs for troubleshooting and interview prep

Prerequisites

Ensure you have:

  • At least two machines (one for the control plane, others as worker nodes)
  • Ubuntu, CentOS, or Debian-based OS
  • Minimum 2 CPUs, 2GB RAM per node
  • Docker installed
  • Firewall and Swap disabled

Step 1: Installing kubeadm, kubelet, and kubectl

On all nodes (control plane & workers):

sudo apt update && sudo apt install -y apt-transport-https ca-certificates curl
curl -fsSL 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 update
sudo apt install -y kubelet kubeadm kubectl
sudo systemctl enable --now kubelet

Step 2: Initializing the Kubernetes Cluster (Control Plane)

On the control plane node, run:

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

After initialization, configure kubectl:

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

Verify the cluster:

kubectl get nodes

Step 3: Configuring Networking

A network plugin is required. Install Calico:

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

Check pod status:

kubectl get pods -n kube-system

Step 4: Adding Worker Nodes

On each worker node, join the cluster using the command given by kubeadm init. If lost, recreate it from the control plane:

kubeadm token create --print-join-command

Run the command on each worker node:

sudo kubeadm join <control-plane-ip>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>

Verify on the control plane:

kubectl get nodes

Step 5: Deploying Applications

Deploy a sample Nginx application:

kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort

Retrieve the service:

kubectl get services nginx

Step 6: Managing the Cluster

List nodes:

kubectl get nodes

Drain a node before maintenance:

kubectl drain <node-name> --ignore-daemonsets --delete-local-data

Bring the node back:

kubectl uncordon <node-name>

FAQs:

Q1: How do I reset the cluster?

sudo kubeadm reset
rm -rf ~/.kube

Q2: How do I troubleshoot worker node connection issues?

Check the kubelet logs:

journalctl -u kubelet -f

Q3: How do I upgrade Kubernetes?

sudo apt update && sudo apt install -y kubeadm
sudo kubeadm upgrade plan
sudo kubeadm upgrade apply <version>

Q4: How do I remove a node from the cluster?

Drain and delete the node:

kubectl drain <node-name> --ignore-daemonsets --delete-local-data
kubectl delete node <node-name>

Q5: How do I enable the Kubernetes dashboard?

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.2.0/aio/deploy/recommended.yaml
kubectl proxy

Access via:

http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/

Conclusion

With kubeadm, you can efficiently set up and manage Kubernetes clusters. In this guide, we covered:

✅ Installing kubeadm and dependencies

✅ Setting up a Kubernetes cluster

✅ Adding worker nodes and networking

✅ Deploying applications and managing nodes

✅ FAQs for troubleshooting

Mastering kubeadm will help you manage production-ready Kubernetes environments effectively. 🚀

📢 Have questions or feedback? Drop a comment below or connect with me on Twitter/X@spysood!

Originally published on Medium.