Kubernetes Cluster Setup Guide

Quick Start

To run this script directly, use the following command:

curl -fsSL https://raw.githubusercontent.com/alex938/docker-with-kubernetes-swarm/refs/heads/main/Kubernetes/install_k8s.sh | sh

You can also view the script here.

Introduction

This guide provides a script to set up a Kubernetes cluster on the controller node. It automates the process of disabling swap, installing Docker, configuring Kubernetes components, and initialising the cluster.

Step 1: Disable Swap

Kubernetes requires swap to be disabled. Use the following commands:

sudo swapoff -a
sudo sed -i '/\/swap.img/ s/^/#/' /etc/fstab

Step 2: Load Kernel Modules

Load the required kernel modules for Kubernetes:

cat < overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter

Step 3: Configure sysctl

Enable required sysctl settings:

cat < net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
sudo sysctl --system

Step 4: Install Docker

Install and configure Docker:

sudo apt-get update
sudo apt-get install -y ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Step 5: Install Kubernetes Components

Install kubelet, kubeadm, and kubectl:

curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/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.30/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

Step 6: Initialise Kubernetes Cluster

Initialise the cluster with a pod network CIDR:

sudo kubeadm init --pod-network-cidr=10.244.0.0/16

Set up kubectl for the current user:

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

Step 7: Install Flannel Network Plugin

Apply the Flannel CNI plugin to enable networking:

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

Step 8: Verify Setup

Verify the cluster is running correctly:

kubectl get nodes
kubectl get pods -n kube-system