Kubernetes on Linux: Installation Guide
Kubernetes is a container orchestration platform widely used to manage cloud-native applications. Installing it on Linux requires careful preparation and sequential steps to ensure a functional cluster. Below is a structured guide covering prerequisites, component installation, and cluster setup.
Before starting, verify the following:
Perform these steps on every node (Master and Workers) to ensure consistency:
Kubernetes requires swap to be disabled for proper pod scheduling. Run the following commands:
sudo swapoff -a # Disable swap temporarily
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab # Disable swap permanently in fstab
Enable IP forwarding and bridge networking (critical for pod communication):
cat <<EOF | sudo tee /etc/modules-load.d/kubernetes.conf
overlay
br_netfilter
EOF
cat <<EOF | sudo tee /etc/sysctl.d/kubernetes.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sudo sysctl --system # Apply settings immediately
Kubernetes uses a container runtime to manage containers. Docker is the most common choice:
sudo apt update
sudo apt install -y docker.io
sudo systemctl enable --now docker
sudo yum install -y docker
sudo systemctl enable --now docker
These tools manage the cluster lifecycle:
sudo apt update && sudo apt 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 update
sudo apt install -y kubelet kubeadm kubectl
sudo systemctl enable --now kubelet
sudo yum install -y epel-release
sudo yum update -y
sudo yum install -y kubelet kubeadm kubectl
sudo systemctl enable --now kubelet
The Master node orchestrates the cluster. Run this command on the designated Master:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
kubeadm join command (e.g., kubeadm join 192.168.1.100:6443 --token ...). Save this—you’ll need it to add Worker nodes.Set up kubectl (the Kubernetes CLI) to communicate with the cluster:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
On each Worker node, run the kubeadm join command copied from the Master initialization step. For example:
sudo kubeadm join 192.168.1.100:6443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
This command installs necessary components (kubelet, kube-proxy) and registers the node with the Master.
Kubernetes requires a network plugin to enable pod-to-pod communication. Popular choices include:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
Verify network connectivity with:
kubectl get pods -n kube-system # Ensure all pods are "Running"
Check the status of nodes and core components:
kubectl get nodes # Should show all nodes in "Ready" state
kubectl get pods --all-namespaces # Core components (e.g., kube-apiserver, kube-controller-manager) should be "Running"
For local development or testing, use microk8s (a lightweight, single-node Kubernetes cluster):
sudo snap install microk8s --classic
sudo usermod -aG microk8s $USER # Add current user to microk8s group
newgrp microk8s # Apply group changes
microk8s status --wait-ready # Verify cluster is running
Enable additional features (e.g., dashboard, DNS) with:
microk8s enable dashboard dns
By following these steps, you’ll have a functional Kubernetes cluster on Linux. Adjust commands based on your distribution (Ubuntu/CentOS) and hardware requirements. Always refer to the official Kubernetes documentation for version-specific updates.