Skip to content

Docker - Working of Kubernetes

Docker - Kubernetes Installation with kubeadm

Section titled “Docker - Kubernetes Installation with kubeadm”

kubeadm is a tool designed to simplify the process of bootstrapping a secure Kubernetes cluster. It automates many of the steps involved in setting up the control plane and joining worker nodes. This chapter provides a high-level overview of installing a basic cluster using kubeadm on Linux (e.g., Ubuntu).

Note: Setting up a production-ready Kubernetes cluster involves many considerations (high availability, security, networking choices, etc.). This guide demonstrates a basic setup suitable for learning or testing.

  • One or more machines running a compatible Linux distribution (e.g., Ubuntu 20.04/22.04, CentOS 7/8, RHEL 7/8).
  • Sufficient resources (e.g., 2 GB+ RAM, 2+ CPUs per machine).
  • Full network connectivity between all machines in the cluster (public or private network).
  • Unique hostname, MAC address, and product_uuid for every node.
  • Certain ports need to be open on your nodes. Refer to the Kubernetes documentation for the exact list (e.g., 6443, 2379-2380, 10250-10252).
  • Swap disabled on all nodes: sudo swapoff -a (and remove swap entry from /etc/fstab).
  • A compatible container runtime (like Docker or containerd) installed and running on all nodes.

Installation Steps Overview (Commands may require sudo)

Section titled “Installation Steps Overview (Commands may require sudo)”

Step 1: Install Container Runtime (Docker/containerd)

Follow the official Docker Engine installation guide (covered in a previous chapter) or the containerd installation guide on all nodes (control plane and workers).

Step 2: Install kubeadm, kubelet, and kubectl

On all nodes, install the Kubernetes components. The following example is for Ubuntu/Debian. Refer to Kubernetes docs for other distributions.

# Update apt package index and install required packages
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl
# Download the Google Cloud public signing key
curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-archive-keyring.gpg
# Add the Kubernetes apt repository
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
# Update apt package index and install components
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
# Prevent them from being automatically updated
sudo apt-mark hold kubelet kubeadm kubectl

Note: Ensure the installed version of kubelet and kubeadm match your desired Kubernetes cluster version.

Step 3: Initialize the Control Plane Node

On the machine designated as the control plane node, run:

sudo kubeadm init --pod-network-cidr=10.244.0.0/16 # Example CIDR for Flannel

The --pod-network-cidr is required, and its value depends on the CNI (Container Network Interface) plugin you plan to use. The example 10.244.0.0/16 is commonly used with the Flannel CNI.

After successful initialization, kubeadm init will output:

  • Commands to set up kubectl for the regular user (copying the admin config file).
  • A kubeadm join command containing a token and hash. Save this command securely – it’s needed to add worker nodes to the cluster.

Follow the instructions to configure kubectl:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Step 4: Install a Pod Network Add-on (CNI)

A CNI plugin is required for Pods to communicate across nodes. Apply the manifest for your chosen CNI. Example using Flannel:

kubectl apply -f https://github.com/coreos/flannel/raw/master/Documentation/kube-flannel.yml

Other popular options include Calico, Weave Net, etc. Ensure the CNI’s configuration matches the CIDR specified in kubeadm init.

Step 5: Join Worker Nodes (Optional)

On each worker node, run the kubeadm join command that was outputted by kubeadm init on the control plane node. It will look something like:

sudo kubeadm join <control-plane-host>:<control-plane-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>

After a short time, you should see the worker nodes listed when running kubectl get nodes on the control plane node.

This provides a basic Kubernetes cluster setup. Consult the official Kubernetes documentation for advanced configurations and troubleshooting.