In this guide, we’ll walk through setting up a Kubernetes cluster with one master and two worker nodes using Kubeadm on Ubuntu servers.
Ensure that all three nodes (one master and two workers) have Docker installed with version locking to prevent automated updates.
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo -E add-apt-repository "deb https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install -y docker-ce=5:19.03.12~3-0~ubuntu-bionic
sudo apt-mark hold docker-ce
Kubelet requires swap to be disabled. After disabling swap, remove it from the fstab file to prevent it from being enabled after a reboot.
sudo swapoff -a
# Remove swap from fstab
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo -E add-apt-repository "deb https://apt.kubernetes.io/ kubernetes-xenial main"
sudo apt-get update
sudo apt-get install -y kubelet=1.18.4-01 kubeadm=1.18.4-01 kubectl=1.18.4-01
sudo apt-mark hold kubelet kubeadm kubectl
Create a configuration file named clusterconfig.yaml
with the following content:
apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
Then, initialize the cluster:
sudo kubeadm init --config clusterconfig.yaml
Set up the kubeconfig file for cluster interaction:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get nodes
At this point, the nodes may show a NotReady
status because the network addon hasn’t been installed yet.
kubectl apply -f https://docs.projectcalico.org/v3.14/manifests/calico.yaml
Generate the join command on the master node:
kubeadm token create --print-join-command
Execute the printed command on each worker node to join them to the cluster.
After all nodes have joined, check their status:
kubectl get nodes
All nodes should display a Ready
status.
For more detailed information, refer to the official Kubernetes documentation: Creating a single control-plane cluster with kubeadm
I hope this guide helps you set up your Kubernetes cluster successfully.