Kubernetes is an open-source container orchestration platform, and kubeadm is a tool provided by the Kubernetes team for quickly setting up a Kubernetes cluster. This article walks through the process of creating a Kubernetes cluster on Ubuntu using kubeadm, covering prerequisites, installing components, initializing the cluster, and adding worker nodes.
Environment Preparation
1. System Requirements
- Ubuntu 20.04 or 18.04
- Minimum 2 GB RAM
- Minimum 2 CPU cores
- Unique hostname, MAC address, and product UUID for each machine
- Swap disabled
2. Update System and Disable Swap
sudo apt-get update
sudo apt-get upgrade -y
sudo swapoff -a
sudo sed -i '/ swap / s/^(.*)$/#\1/g' /etc/fstab
3. Set Hostnames and Static IPs
Insure each machine has a unique hostname and a static IP address.
sudo hostnamectl set-hostname <hostname>
4. Modify Hosts File
Add all node IP addresses and hostnames to /etc/hosts on each machine.
sudo nano /etc/hosts
Add the following entries:
<master-ip> master
<worker1-ip> worker1
<worker2-ip> worker2
5. Install Required Tools
sudo apt-get install -y apt-transport-https ca-certificates curl
Installing Docker
Docker is used as the container runtime in Kubernetes. Follow these steps to install Docker:
sudo apt-get update
sudo apt-get install -y docker.io
sudo systemctl enable docker
sudo systemctl start docker
Installing kubeadm, kubelet, and kubectl
1. Add Kubernetes Repository
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
2. Install Kubernetes Components
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
Initializing the Kubernetes Cluster
Run the following command on the master node to initialize the cluster:
1. Initialize the Cluster with kubeadm
sudo kubeadm init --pod-network-cidr=192.168.0.0/16
2. Configure kubectl
After initialization, follow the instructions displayed. Configure kubectl on the master node:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
3. Install a Network Plugin
Install the Calico network plugin:
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
Adding Worker Nodes
On each worker node, run the kubeadm join command obtained after initializing the master node:
sudo kubeadm join <master-ip>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>
Verifying the Cluster
On the master node, use the following command to check the cluster status:
kubectl get nodes
If all nodes show a status of Ready, the setup was successful.
Managing and Using the Kubernetes Cluster
1. Deploy a Sample Application
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
2. View Pods and Services
kubectl get pods
kubectl get svc
3. Delete Application and Service
kubectl delete svc nginx
kubectl delete deployment nginx
Common Issues and Solutions
1. Issue: Node Status is NotReady
Solution:
- Confirm that the network plugin is installed correctly.
- Check if kubelet and Docker are running properly on the node.
2. Issue: Cannot Join Worker Nodes
Solution:
- Verify that the token and hash used in the
kubeadm joincommand are correct. - Ensure that port 6443 is open on the master node's firewall.