Creating a single-node Kubernetes cluster using kubeadm on an Ubuntu server is straightforward. Here are the detailed steps:
Step 1: Prepare the Environment
-
Update and upgrade the system:
sudo apt update && sudo apt upgrade -y -
Install required dependencies:
sudo apt install -y apt-transport-https ca-certificates curl -
Disable swap (required by Kubernetes):
bash1sudo swapoff -a 2sudo sed -i '/swap/d' /etc/fstab -
Configure kernel modules and sysctl settings:
bash1sudo modprobe overlay 2sudo modprobe br_netfilter 3 4cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf 5overlay 6br_netfilter 7EOF 8 9cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf 10net.bridge.bridge-nf-call-iptables = 1 11net.bridge.bridge-nf-call-ip6tables = 1 12net.ipv4.ip_forward = 1 13EOF 14 15sudo sysctl --system
Step 2: Install Docker or Container Runtime
-
Install Docker (most common runtime):
bash1sudo apt install -y docker.io 2sudo systemctl enable docker 3sudo systemctl start docker -
Set up the Docker daemon for Kubernetes:
bash1cat <<EOF | sudo tee /etc/docker/daemon.json 2{ 3 "exec-opts": ["native.cgroupdriver=systemd"], 4 "log-driver": "json-file", 5 "log-opts": { 6 "max-size": "100m" 7 }, 8 "storage-driver": "overlay2" 9} 10EOF 11 12sudo systemctl restart docker
Step 3: Install kubeadm, kubelet, and kubectl
-
Add the Kubernetes repository:
bash1echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list 2curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg -
Install Kubernetes components:
bash1sudo apt update 2sudo apt install -y kubeadm kubelet kubectl 3sudo apt-mark hold kubeadm kubelet kubectl
Step 4: Initialize the Single-Node Cluster
-
Run the
kubeadm initcommand:sudo kubeadm init --pod-network-cidr=192.168.0.0/16- Use the
--pod-network-cidrflag if you're using a specific CNI (e.g., Calico). - This command will output a
kubeadm joincommand—save it in case you add more nodes later.
- Use the
-
Set up the kubeconfig for the user:
bash1mkdir -p $HOME/.kube 2sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config 3sudo chown $(id -u):$(id -g) $HOME/.kube/config -
Allow the master node to schedule pods:
kubectl taint nodes --all node-role.kubernetes.io/control-plane-
Step 5: Install a Pod Network Add-on
- Install a network plugin (e.g., Calico):
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
Step 6: Verify the Cluster
-
Check the status of the nodes:
kubectl get nodes -
Check the status of pods in all namespaces:
bash1# Check the status of pods in all namespaces 2kubectl get pods -A