How to creating a single control plane cluster with Kubeadm a powerful open-source platform for managing containerized workloads & services. It has become a staple in modern software development, offering robust features like automated deployment, scaling, and management of applications. Setting up a Kubernetes cluster is one of the foundational steps to harness its capabilities. This article will guide you through how to creating a single control plane cluster with Kubeadm, a tool designed to streamline the cluster bootstrapping process.
How to Creating a Single Control Plane Cluster with Kubeadm? Step-by-Step Guide
Prerequisites
Before diving into the set-up, make sure you have the following:
- Hardware Requirements:
- At least 2 GB of RAM per node.
- A minimum of 2 CPUs.
- 20 GB of disk space.
- Operating System:
- A Linux distribution such as Ubuntu 20.04, Debian 10, or CentOS 7.
- Network Setup:
- Proper hostname resolution (e.g., DNS or /etc/hosts entries).
- Disable swap (sudo swapoff -a) and remove any references to swap in /etc/fstab.
- Installed Tools:
- Docker or another container runtime (e.g., containerd, CRI-O).
- kubeadm, kubelet, and kubectl installed on all nodes.
Step 1: Prepare the Nodes
1.1 Update System Packages
Update all your system packages to ensure compatibility and security.
bash Copy code
sudo apt update && sudo apt upgrade -y
1.2 Install Container Runtime
Install Docker or an alternative container runtime. For Docker:
bash Copy code
sudo apt install -y docker.io
sudo systemctl enable docker
sudo systemctl start docker
For containerd:
bash Copy code
sudo apt install -y containerd
sudo systemctl enable containerd
sudo systemctl start containerd
1.3 Install Kubernetes Tools
Install kubeadm, kubelet, and kubectl:
bash Copy code
sudo apt-get update
sudo apt-get 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-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
Step 2: Initialize the Control Plane Node
The Control plane (CP) node is brain of your Kubernetes cluster. It runs components like the API server, controller manager, and scheduler.
2.1 Initialize the Cluster
Run the follow command on the control plane (CP) node:
bash Copy code
sudo kubeadm init –pod-network-cidr=192.168.0.0/16
- –pod-network-cidr: Specifies the CIDR block for the pod network. Use a CIDR that matches your desired network plugin.
The output will include a kubeadm join command, which you’ll use later to add worker nodes.
2.2 Configure kubectl
To start using the cluster, set up the kubeconfig file:
bash Copy code
mkdir -p $HOME/.kube
sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
2.3 Verify Cluster Status
Run the following command to ensure everything is running as expected:
bash Copy code
kubectl get nodes
Step 3: Deploy a Pod Network
A pod network enables communication between pods across different nodes. You can choose from several network plugins, such as Calico, Flannel, or Weave Net.
Example: Deploying Calico
Run the following command to deploy Calico:
bash Copy code
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
Verify that the Calico pods are running:
bash Copy code
kubectl get pods –all-namespaces
Step 4: Add Worker Nodes
To add worker nodes to your cluster, following these steps:
4.1 Execute the Join Command
On each worker node, run the kubeadm join command that was output during the control plane initialization. For example:
bash Copy code
sudo kubeadm join 192.168.1.100:6443 –token <token> –discovery-token-ca-cert-hash sha256:<hash>
If you lost the join command, you can regenerate it on the control plane node:
bash Copy code
kubeadm token create –print-join-command
4.2 Verify Node Addition
After running the join command, check the nodes in the cluster from the control plane:
bash Copy code
kubectl get nodes
The newly added nodes should appear as Ready.
Step 5: Deploy a Test Application
To validate the cluster setup, deploy a test application. For example, deploy an Nginx pod:
bash Copy code
kubectl create deployment nginx –image=nginx
kubectl expose deployment nginx –port=80 –type=NodePort
Retrieve the NodePort assigned to Nginx:
bash Copy code
kubectl get svc nginx
Access the application using http://<node-ip>:<node-port> in your browser.
Step 6: Configure High Availability (Optional)
Although this guide focuses on a single control plane cluster, you can configure high availability (HA) for production environments. This involves:
- Setting up additional control plane nodes.
- Using an external load balancer to distribute API server traffic.
Troubleshooting Tips
- Cluster Initialization Fails:
- Check logs: journalctl -u kubelet -f.
- Ensure network configurations are correct.
- Worker Node Join Issues:
- Verify token validity using kubeadm token list.
- Check that the worker node can reach the control plane (CP).
- Pods Stuck in Pending State:
- Ensure the pod network plugin is correctly deployed.
Conclusion
How to creating a single control plane cluster with Kubeadm is a straightforward process when you follow a structured approach. From preparing nodes and initializing the control plane to deploying a pod network and adding worker nodes, kubeadm simplifies the Kubernetes cluster setup. While a single control plane cluster is suitable for testing and development, consider setting up high availability for production workloads.
By following this guide, you now have a functional Kubernetes cluster ready for containerized applications. Dive into Kubernetes’ extensive features and begin building robust, scalable solutions!