Building a Kubernetes cluster in Linux requires coordinating multiple nodes with kubectl. If you are looking for a hands-on guide on how to create kubernetes cluster in linux, you have come to the right place. This tutorial will walk you through every step, from setting up prerequisites to verifying a fully functional cluster.
Kubernetes is the industry standard for container orchestration. It helps you deploy, scale, and manage containerized applications. While managed services exist, running your own cluster on Linux gives you full control and deeper understanding.
In this article, we will use kubeadm, a tool designed to simplify cluster creation. We will set up one control plane node and one worker node, but the process scales easily to more nodes. Let’s get started.
Prerequisites For Creating A Kubernetes Cluster In Linux
Before we begin, ensure your Linux systems meet the following requirements. These steps apply to both the control plane and worker nodes.
- Two or more Linux machines (physical or virtual) running Ubuntu 20.04 or CentOS 7/8.
- Each machine should have at least 2 GB RAM and 2 CPU cores.
- Full network connectivity between all nodes. The nodes must be able to reach each other over the network.
- Root or sudo access on every machine.
- A unique hostname, MAC address, and product_uuid for each node. Check with
sudo cat /sys/class/dmi/id/product_uuid. - Port 6443, 10250, 10251, 10252, and 30000-32767 must be open on the control plane. Worker nodes need 10250 and 30000-32767 open.
If you are using a cloud provider, ensure firewall rules allow these ports. For local VMs, disable firewalld or configure it properly.
Step 1: Install Container Runtime
Kubernetes needs a container runtime to run pods. We will use containerd, which is lightweight and stable. Install it on all nodes.
- Update the package index:
sudo apt update(Ubuntu) orsudo yum update(CentOS). - Install dependencies:
sudo apt install -y containerdorsudo yum install -y containerd. - Configure containerd:
sudo mkdir -p /etc/containerdandcontainerd config default | sudo tee /etc/containerd/config.toml. - Restart containerd:
sudo systemctl restart containerdand enable it:sudo systemctl enable containerd.
Verify containerd is running: sudo systemctl status containerd. You should see “active (running)”.
Step 2: Install Kubernetes Components
Now install kubeadm, kubelet, and kubectl on all nodes. These are the core tools for cluster management.
- Add the Kubernetes repository. For Ubuntu:
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -andecho "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list. - For CentOS:
cat < - Install the packages:
sudo apt install -y kubelet kubeadm kubectlorsudo yum install -y kubelet kubeadm kubectl. - Hold the packages to prevent automatic updates:
sudo apt-mark hold kubelet kubeadm kubectlorsudo yum versionlock kubelet kubeadm kubectl. - Enable and start kubelet:
sudo systemctl enable kubelet && sudo systemctl start kubelet.
Kubelet will restart several times during setup. That is normal.
How To Create Kubernetes Cluster In Linux
Now we reach the core of this guide. This section explains exactly how to create kubernetes cluster in linux using kubeadm. We will initialize the control plane first, then join worker nodes.
Step 3: Initialize The Control Plane
On the control plane node, run the following command. This sets up the master components.
- Initialize the cluster:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16. The CIDR range is for the pod network. Adjust if needed. - The command will output a join token. Save it carefully. It looks like:
kubeadm join 192.168.1.100:6443 --token ... --discovery-token-ca-cert-hash sha256:... - Configure kubectl for your user:
mkdir -p $HOME/.kubeandsudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configandsudo chown $(id -u):$(id -g) $HOME/.kube/config. - Verify the control plane is running:
kubectl get nodes. You should see the control plane node with status "NotReady". That is expected until we install a pod network.
If you see an error about swap, disable swap: sudo swapoff -a and remove swap entries from /etc/fstab.
Step 4: Install A Pod Network
A pod network is required for pods to communicate. We will use Flannel, which is simple and works well with the CIDR we set.
- Apply the Flannel manifest:
kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml. - Wait for the Flannel pods to start:
kubectl get pods -n kube-flannel. They should show "Running". - Check the node status again:
kubectl get nodes. The control plane should now show "Ready".
If Flannel does not work, you can try Calico or Weave. The process is similar.
Step 5: Join Worker Nodes
On each worker node, run the join command you saved earlier. If you lost it, generate a new token on the control plane.
- On the control plane, create a new token:
kubeadm token create --print-join-command. - Copy the full command. It looks like:
kubeadm join 192.168.1.100:6443 --token ... --discovery-token-ca-cert-hash sha256:... - On each worker node, run the command with sudo:
sudo kubeadm join .... - Wait a few seconds, then on the control plane run:
kubectl get nodes. You should see all nodes listed with status "Ready".
If a node shows "NotReady", check the kubelet logs: sudo journalctl -u kubelet -f. Common issues include network misconfiguration or container runtime problems.
Post-Installation Verification
After joining nodes, verify the cluster is fully functional. Run these commands on the control plane.
kubectl get nodes– All nodes should be "Ready".kubectl get pods -n kube-system– Core system pods like coredns should be running.kubectl run test-pod --image=nginx --restart=Never– Create a test pod. Check it runs:kubectl get pods.- Delete the test pod:
kubectl delete pod test-pod.
If everything works, your cluster is ready. You can now deploy applications.
Step 6: Deploy A Sample Application
To confirm the cluster works end-to-end, deploy a simple Nginx deployment.
- Create a deployment:
kubectl create deployment nginx --image=nginx. - Expose it as a service:
kubectl expose deployment nginx --port=80 --type=NodePort. - Get the service details:
kubectl get svc nginx. Note the NodePort (e.g., 30080). - Access the application from any node's IP:
curl http://. You should see the Nginx welcome page.:
This proves the cluster is working correctly. You can scale the deployment: kubectl scale deployment nginx --replicas=3.
Troubleshooting Common Issues
Even with careful setup, issues can arise. Here are solutions to frequent problems.
Node Not Ready
If a node stays "NotReady", check the kubelet status: sudo systemctl status kubelet. Look for errors like "container runtime is down" or "network plugin is not ready". Restart kubelet: sudo systemctl restart kubelet.
Join Command Fails
If the join command fails with "error execution phase preflight", ensure the token is valid. Tokens expire after 24 hours. Generate a new one: kubeadm token create and use the full join command from kubeadm token create --print-join-command.
DNS Not Working
If CoreDNS pods are crash looping, check the logs: kubectl logs -n kube-system -l k8s-app=kube-dns. Often, the pod network CIDR mismatch causes this. Ensure the CIDR in kubeadm init matches the pod network plugin.
Port Conflicts
If you see "port 6443 is already in use", another process is using it. Check with sudo netstat -tulpn | grep 6443. Stop the conflicting service or change the port.
Securing Your Cluster
Security is critical for production clusters. Here are basic steps to harden your setup.
- Enable RBAC: Kubernetes enables Role-Based Access Control by default. Use it to restrict permissions.
- Use network policies: Limit pod-to-pod communication. Install a network policy controller like Calico.
- Regularly update components: Keep kubeadm, kubelet, and kubectl updated. Use
apt upgradeoryum update. - Secure etcd: etcd stores cluster data. Use TLS encryption and restrict access to the control plane.
- Disable anonymous access: Edit the kube-apiserver manifest to set
--anonymous-auth=false.
For a single-node test cluster, these steps are optional. But for multi-node setups, implement them.
Scaling Your Cluster
Adding more worker nodes is straightforward. Follow the same steps as joining the first worker node. Ensure the new node meets prerequisites and has network access to the control plane.
- On the new node, install containerd, kubelet, kubeadm, and kubectl.
- Run the join command from the control plane.
- Verify the node appears:
kubectl get nodes.
You can also add more control plane nodes for high availability. This requires setting up a load balancer and using kubeadm init --control-plane-endpoint.
Frequently Asked Questions
Q: Can I create a Kubernetes cluster on a single Linux machine?
A: Yes, you can use Minikube or kubeadm with a single node. For kubeadm, remove the taint on the control plane: kubectl taint nodes --all node-role.kubernetes.io/master-. This allows pods to run on the master.
Q: What is the easiest way to create a Kubernetes cluster in Linux?
A: Using kubeadm is the most straightforward method for production-like clusters. For testing, Minikube is simpler. Both are valid depending on your needs.
Q: How do I reset a Kubernetes cluster created with kubeadm?
A: On each node, run sudo kubeadm reset. Then clean up network configs: sudo rm -rf /etc/cni/net.d. Finally, remove the kubelet state: sudo rm -rf /var/lib/kubelet.
Q: Why does my worker node show "NotReady" after joining?
A: Common causes include missing container runtime, network plugin not installed, or firewall blocking ports. Check kubelet logs and ensure containerd is running.
Q: Can I use Docker as the container runtime instead of containerd?
A: Kubernetes deprecated Docker as a runtime in v1.24. Use containerd or CRI-O for compatibility. Docker images still work with containerd.
Conclusion
You now know how to create kubernetes cluster in linux from scratch. We covered installing prerequisites, initializing the control plane, joining worker nodes, and verifying the setup. The process is repeatable and scalable for larger environments.
Remember to secure your cluster if it goes into production. Start with small applications to test functionality. Kubernetes has a learning curve, but hands-on practice is the best teacher.
If you encounter issues, refer to the troubleshooting section or the official Kubernetes documentation. Building your own cluster gives you invaluable experience. Good luck with your Kubernetes journey.