🕸 Setting Up a Kubernetes Cluster Using Kubeadm
In this guide, we will walk you through setting up a Kubernetes cluster with kubeadm
using one master node and one worker node on a Linux environment. We’ll also deploy a simple application at the end to demonstrate how the cluster works.
Kubeadm is a tool that makes the process of bootstrapping a Kubernetes cluster simple and straightforward. This guide covers:
- Prerequisites and environment setup
- Configuring the master node
- Joining a worker node to the cluster
- Deploying a sample application
Let’s get started.
Step 1: Prerequisites
To set up a Kubernetes cluster, you will need:
- Two Linux machines (or virtual machines) running a modern Linux distribution such as Ubuntu 20.04.
- Master Node
- Worker Node
- Each machine should have:
- At least 2 CPU cores
- 2 GB RAM or more
- Access to the internet
Ensure you have root or sudo privileges on both machines.
1.1 Update the System
First, make sure your system is up to date. Run the following commands on both the master and worker nodes:
sudo apt-get update && sudo apt-get upgrade -y
1.2 Install Docker
Docker is the default container runtime for Kubernetes. Run the following commands to install Docker on both the master and worker nodes.
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
Enable Docker to start automatically:
sudo systemctl enable docker
sudo systemctl start docker
1.3 Install kubeadm, kubelet, and kubectl
Kubernetes requires kubeadm
, kubelet
, and kubectl
to manage the cluster. Install them on both the master and worker nodes.
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 -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | 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: Set Up the Master Node
2.1 Disable Swap
Kubernetes requires swap to be disabled. Disable it by running the following commands on the master node:
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
2.2 Initialize the Master Node
To initialize the Kubernetes control plane on the master node, run:
sudo kubeadm init --pod-network-cidr=192.168.0.0/16
This command will set up the master node with the necessary components. Once the initialization is complete, you will see a message that includes a command like this:
kubeadm join <MASTER_IP>:6443 --token <TOKEN> --discovery-token-ca-cert-hash sha256:<HASH>
Save this command. You will need it to join the worker node to the cluster.
2.3 Configure kubectl for the Master Node
In order to run Kubernetes commands from the master node, set up the kubectl
command-line tool:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
2.4 Install a Pod Network Add-On
Kubernetes needs a networking solution to manage communication between pods. Here, we’ll use Calico as the pod network add-on:
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
Ensure the nodes are ready:
kubectl get nodes
At this point, the master node should be Ready.
Step 3: Join the Worker Node
3.1 Disable Swap on the Worker Node
Run the following commands to disable swap on the worker node:
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
3.2 Generate the Join Command
If you didn’t save the join command during the master node setup, you can generate it again by running:
sudo kubeadm token create --print-join-command
This will output a command that looks like this:
kubeadm join <MASTER_IP>:6443 --token <TOKEN> --discovery-token-ca-cert-hash sha256:<HASH>
Use this command to join the worker node to the master node.
3.3 Join the Worker Node
Run the kubeadm join
command provided above on the worker node:
sudo kubeadm join <MASTER_IP>:6443 --token <TOKEN> --discovery-token-ca-cert-hash sha256:<HASH>
Once the worker node has successfully joined, verify the nodes on the master node:
kubectl get nodes
You should see both the master and worker nodes listed as Ready.
Step 4: Deploy a Sample Application
Now that your cluster is ready, let’s deploy a simple application to test everything.
4.1 Deploy Nginx
We’ll use the popular Nginx web server for this example:
kubectl create deployment nginx --image=nginx
4.2 Expose the Deployment
Expose the Nginx deployment as a service to make it accessible:
kubectl expose deployment nginx --port=80 --type=NodePort
4.3 Check the Service
To view the service and obtain the NodePort:
kubectl get svc
Take note of the NodePort
, which should be a number between 30000-32767. You can use this port to access Nginx from the outside.
4.4 Access the Nginx Application
Visit the Nginx application by opening a browser and going to:
http://<WORKER_NODE_IP>:<NODE_PORT>
You should see the Nginx welcome page, confirming that your Kubernetes cluster and sample application are functioning correctly.
Conclusion
In this guide, we’ve walked through setting up a Kubernetes cluster with kubeadm
, using one master node and one worker node. We also deployed a simple Nginx application to demonstrate the cluster’s capabilities.
This basic setup can now be expanded by adding more worker nodes or deploying more complex applications. Kubernetes gives you a highly flexible and scalable platform for containerized workloads, and kubeadm
simplifies the setup process.