Table of contents
Open Table of contents
Introduction
Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It is widely used in the industry to manage containerized workloads and services. In this post, i will guide you through the pre-installation environment setup for Kubernetes cluster deployment. I will cover the installation of Docker, Kubectl, Kubeadm, Kubelet, and other necessary tools on Ubuntu 22.04 LTS.
Prerequisites
Before you begin, you should have the following prerequisites:
- A fresh Ubuntu 22.04 LTS server with a minimum of 2 CPU cores, 2GB of RAM, and 20GB of free disk space.
- A user account with sudo privileges.
- A stable internet connection.
Step 1: Update the Package Repository And Disable Swap
First, you will need to update the package repository and disable the swap on your Ubuntu server. You can do this by running the following commands:
sudo apt update
sudo apt install -y curl \
gnupg2 \
software-properties-common \
apt-transport-https \
ca-certificates
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
Step 2: Setup Firewall Rules
Next, you will need to configure the firewall rules to allow the necessary ports for Kubernetes cluster.
For the Kubernetes control plane nodes, you will need to allow the following ports:
- TCP port 6443: Kubernetes API server
- TCP port 2379-2380: etcd server client API
- TCP port 10250: Kubelet API
- TCP port 10259: kube-scheduler
- TCP port 10257: kube-controller-manager
For the worker nodes, you will need to allow the following ports:
- TCP port 10250: Kubelet API
- TCP port 30000-32767: NodePort Services (default port range)
- TCP port 10256: Kube-proxy
You can configure the firewall rules by running the following commands:
sudo ufw allow 6443/tcp
sudo ufw allow 2379:2380/tcp
sudo ufw allow 10250/tcp
sudo ufw allow 10259/tcp
sudo ufw allow 10257/tcp
sudo ufw allow 30000:32767/tcp
sudo ufw allow 10256/tcp
sudo ufw reload
sudo ufw enable
You can also disable the firewall if you are only using the Kubernetes cluster for testing purposes:
sudo ufw disable
systemctl disable --now ufw
Step 3: Load Necessary Kernel Modules
You will need to load the necessary kernel modules on your Ubuntu server. You can do this by running the following commands:
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
Step 4: Setup Iptables
You also need to configure the net bridge to allow the traffic to pass through the iptables rules. You can do this by running the following commands:
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system
Step 5: Install Docker
Docker is a containerization platform that allows you to package, distribute, and run applications in containers. You can install Docker on your Ubuntu server by running the following commands:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | \
sudo tee /etc/apt/sources.list.d/docker.list
sudo apt update
sudo apt install -y containerd.io
sudo containerd config default | sudo tee /etc/containerd/config.toml
sudo sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' \
/etc/containerd/config.toml
sudo systemctl restart containerd
sudo systemctl enable containerd
Step 6: Install Kubectl/Kubeadm/Kubelet
Kubectl is a command-line tool that allows you to run commands against Kubernetes clusters. Kubeadm is a tool that helps you bootstrap a Kubernetes cluster. Kubelet is an agent that runs on each node in the cluster and ensures that containers are running in a pod.
You can install Kubectl, Kubeadm, and Kubelet on your Ubuntu server by running the following commands:
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | \
sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo "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
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
Step 7: Verify the Installation
You can verify the installation of Docker, Kubectl, Kubeadm, and Kubelet by running the following commands:
containerd -v
kubectl version --client
kubeadm version
kubelet --version
Conclusion
Accroding to the above steps, you have successfully set up the pre-installation environment for Kubernetes cluster deployment. If you want to make it automatic, you can use the following script to install all the necessary tools(run as root):
#!/bin/bash
echo "Installing Docker, Kubectl, Kubeadm, Kubelet, and other necessary tools on Ubuntu 22.04 LTS..."
echo "Updating the package repository..."
apt update
apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates
echo "Disabling swap..."
swapoff -a
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
echo "Setting up firewall rules..."
ufw allow 6443/tcp
ufw allow 2379:2380/tcp
ufw allow 10250/tcp
ufw allow 10259/tcp
ufw allow 10257/tcp
ufw allow 30000:32767/tcp
ufw allow 10256/tcp
ufw reload
ufw enable
echo "Loading necessary kernel modules..."
cat <<EOF | tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
echo "Setting up iptables..."
cat <<EOF | tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
echo "Installing Docker..."
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list
apt update
apt install -y containerd.io
containerd config default | tee /etc/containerd/config.toml
sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
systemctl restart containerd
systemctl enable containerd
echo "Installing Kubectl, Kubeadm, and Kubelet..."
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /" | tee /etc/apt/sources.list.d/kubernetes.list
apt update
apt install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl
echo "Verifying the installation..."
containerd -v
kubectl version --client
kubeadm version
kubelet --version