|
| 1 | +--- |
| 2 | +title: Build a Kubernetes Cluster on Ubuntu 24 |
| 3 | +image: /assets/img/default-banner.jpg |
| 4 | +author: jack |
| 5 | +date: 2025-05-27 20:55:00 +0800 |
| 6 | +categories: [blog, linux] |
| 7 | +tags: [linux] |
| 8 | +math: false |
| 9 | +pin: false |
| 10 | +--- |
| 11 | + |
| 12 | +In this tutorial, we’ll set up a two-node Kubernetes cluster on Ubuntu 24.04 Server, consisting of one control plane node and one worker nodes. |
| 13 | + |
| 14 | +A Kubernetes cluster consists of a control plane and worker nodes. The control plane manages and orchestrates the cluster, while the worker nodes actually run the application containers (Pods). This guide will walk you through every step, from OS installation and node configuration to deploying basic workloads. |
| 15 | + |
| 16 | +We’ll start by preparing two Ubuntu 24.04 virtual machines as the Master and Worker Nodes. Each VM should have at least 2GB of RAM and 2 CPU cores. |
| 17 | + |
| 18 | + |
| 19 | +| Node Type | CPU | RAM | Disk | OS | NAT DHCP IP | |
| 20 | +| --------- | --- | --- | ---- | -- | ------------ | |
| 21 | +| Master | 2 | 2GB | 15GB | Ubuntu 24.04 | 192.168.122.11 | |
| 22 | +| Worker | 2 | 2GB | 15GB | Ubuntu 24.04 | 192.168.122.12 | |
| 23 | + |
| 24 | +## Master Node Setup |
| 25 | + |
| 26 | +### System Update and Basic Settings |
| 27 | + |
| 28 | +First, log into the Master Node and update the system: |
| 29 | + |
| 30 | +```sh |
| 31 | +$ sudo apt update |
| 32 | +``` |
| 33 | + |
| 34 | +Disable swap: |
| 35 | + |
| 36 | +```sh |
| 37 | +$ sudo swapoff -a |
| 38 | +``` |
| 39 | + |
| 40 | +Enable Kernel IP Forwarding: |
| 41 | + |
| 42 | +```sh |
| 43 | +$ echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf |
| 44 | +$ sudo sysctl -p |
| 45 | +``` |
| 46 | + |
| 47 | +Enable the `overlay` and `br_netfilter` kernel modules and verify they are loaded: |
| 48 | + |
| 49 | +```sh |
| 50 | +$ cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf |
| 51 | +overlay |
| 52 | +br_netfilter |
| 53 | +EOF |
| 54 | + |
| 55 | +$ sudo modprobe overlay |
| 56 | +$ sudo modprobe br_netfilter |
| 57 | +$ cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf |
| 58 | +net.bridge.bridge-nf-call-iptables = 1 |
| 59 | +net.bridge.bridge-nf-call-ip6tables = 1 |
| 60 | +net.ipv4.ip_forward = 1 |
| 61 | +EOF |
| 62 | + |
| 63 | +$ sudo sysctl -p |
| 64 | +$ lsmod | grep br_netfilter |
| 65 | +$ lsmod | grep overlay |
| 66 | +``` |
| 67 | + |
| 68 | +## Install kubelet, kubeadm, kubectl |
| 69 | + |
| 70 | +Follow the [official Kubernetes guide](https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/) to install tools using APT. (The version used here is v1.33—this may change, so check the official guide for the latest `curl -fsSL` URL.) |
| 71 | + |
| 72 | +```sh |
| 73 | +$ sudo apt-get install -y apt-transport-https ca-certificates curl gnupg |
| 74 | +$ curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.33/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg |
| 75 | +$ sudo chmod 644 /etc/apt/keyrings/kubernetes-apt-keyring.gpg |
| 76 | +$ echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.33/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list |
| 77 | +$ sudo chmod 644 /etc/apt/sources.list.d/kubernetes.list |
| 78 | +``` |
| 79 | + |
| 80 | +Then update APT and install the tools: |
| 81 | + |
| 82 | +```sh |
| 83 | +$ sudo apt-get update |
| 84 | +$ sudo apt-get install -y kubelet kubeadm kubectl |
| 85 | +``` |
| 86 | + |
| 87 | +## Install containerd |
| 88 | + |
| 89 | +We'll use containerd as the container runtime. Follow the [official Docker documentation](https://docs.docker.com/engine/install/ubuntu/) to add the Docker APT repository: |
| 90 | + |
| 91 | +```sh |
| 92 | +$ sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc |
| 93 | +$ sudo chmod a+r /etc/apt/keyrings/docker.asc |
| 94 | +$ echo \ |
| 95 | + "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \ |
| 96 | + $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}") stable" | \ |
| 97 | + sudo tee /etc/apt/sources.list.d/docker.list > /dev/null |
| 98 | +``` |
| 99 | + |
| 100 | +Then update and install containerd: |
| 101 | + |
| 102 | +```sh |
| 103 | +$ sudo apt-get update |
| 104 | +$ sudo apt-get install -y containerd.io |
| 105 | +``` |
| 106 | + |
| 107 | +Edit `/etc/containerd/config.toml` to enable SystemdCgroup and comment `out disabled_plugins = ["cri"]`: |
| 108 | + |
| 109 | +```sh |
| 110 | +$ sudo vim /etc/containerd/config.toml |
| 111 | +``` |
| 112 | + |
| 113 | +```toml |
| 114 | +# disabled_plugins = ["cri"] |
| 115 | +SystemdCgroup = true |
| 116 | +``` |
| 117 | + |
| 118 | +Restart containerd: |
| 119 | + |
| 120 | +```sh |
| 121 | +$ sudo systemctl restart containerd |
| 122 | +``` |
| 123 | + |
| 124 | +## Initialize Kubernetes with kubeadm |
| 125 | + |
| 126 | +Use `kubeadm` to initialize the Kubernetes cluster. The `--pod-network-cidr` flag specifies the IP range for the pod network. If `10.100.0.0/16` conflicts with your network setup, choose another CIDR block. |
| 127 | + |
| 128 | +```sh |
| 129 | +$ sudo kubeadm init --pod-network-cidr=10.100.0.0/16 |
| 130 | +$ mkdir -p $HOME/.kube |
| 131 | +$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config |
| 132 | +$ sudo chown $(id -u):$(id -g) $HOME/.kube/config |
| 133 | +``` |
| 134 | + |
| 135 | +> If you run `sudo crictl ps` and see etcd or kube-apiserver constantly restarting, try switching from cgroup v2 to v1. This resolved issues in my QEMU/KVM VMs. |
| 136 | +> ```sh |
| 137 | +> $ sudo vim /etc/default/grub |
| 138 | +> ``` |
| 139 | +> Modify the `GRUB_CMDLINE_LINUX_DEFAULT`: |
| 140 | +> ```ini |
| 141 | +> GRUB_CMDLINE_LINUX_DEFAULT="systemd.unified_cgroup_hierarchy=0" |
| 142 | +> ``` |
| 143 | +> Then update GRUB and reboot: |
| 144 | +> ```sh |
| 145 | +> $ sudo update-grub |
| 146 | +> ``` |
| 147 | +
|
| 148 | +Install Flannel as the pod network plugin. Flannel is a lightweight CNI plugin suitable for Kubernetes. |
| 149 | +
|
| 150 | +```sh |
| 151 | +$ kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml |
| 152 | +``` |
| 153 | +
|
| 154 | +## Worker Node Setup |
| 155 | + |
| 156 | +Follow the same steps as the Master Node to update the system, disable swap, enable IP forwarding, install kubelet, kubeadm, kubectl, and containerd. **Do not run** `kubeadm init`. |
| 157 | + |
| 158 | +On the Master Node, run the following command to get the join command: |
| 159 | + |
| 160 | +```sh |
| 161 | +$ kubeadm token create --print-join-command |
| 162 | +``` |
| 163 | + |
| 164 | +Run the printed command on the Worker Node to join the cluster. |
| 165 | + |
| 166 | +## Verify Cluster Status |
| 167 | + |
| 168 | +Run the following on the Master Node to verify cluster health: |
| 169 | + |
| 170 | +```sh |
| 171 | +$ kubectl get nodes |
| 172 | +``` |
0 commit comments