Last active 1706077627

Install K8S on your home lab!

Revision dd5884aec791a18105561a8ff19e1a610655d565

Install.sh Raw
1#/bin/bash
2# This script is to install Kubernetes on Ubuntu 22.04 LTS, with Calico as CNI
3# This script used 10.244.0.0/16 as pod network CIDR. This network should not be used in your physical network.
4# This script used Calico v3.27.0. You can change it to the latest version.
5
6# Prerequisites
7DEBIAN_FRONTEND=noninteractive sudo apt update
8DEBIAN_FRONTEND=noninteractive sudo apt install curl gnupg2 software-properties-common apt-transport-https ca-certificates -y
9
10# Docker
11echo "Install Docker..."
12DEBIAN_FRONTEND=noninteractive sudo apt install docker.io -y
13
14# Network
15echo "Prepare network..."
16cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
17overlay
18br_netfilter
19EOF
20sudo modprobe overlay
21sudo modprobe br_netfilter
22# sysctl params required by setup, params persist across reboots
23cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
24net.bridge.bridge-nf-call-iptables = 1
25net.bridge.bridge-nf-call-ip6tables = 1
26net.ipv4.ip_forward = 1
27EOF
28sudo sysctl --system
29
30# Runtime
31echo "Prepare runtime to setup containerd..."
32sudo mkdir /etc/containerd
33sudo sh -c "containerd config default > /etc/containerd/config.toml"
34sudo sed -i 's/ SystemdCgroup = false/ SystemdCgroup = true/' /etc/containerd/config.toml
35sudo systemctl restart containerd.service
36sudo systemctl restart kubelet.service
37
38# K8S
39curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes.gpg
40echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/kubernetes.gpg] http://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list
41sudo apt update
42DEBIAN_FRONTEND=noninteractive sudo apt install kubeadm kubelet kubectl kubernetes-cni -y
43
44# Init (Only on Master)
45sudo kubeadm config images pull
46sudo kubeadm init --pod-network-cidr=10.244.0.0/16
47read -p "Please copy the kubeadm join command above and run it on worker nodes. Press any key to continue..."
48
49# Config (Only on Master)
50mkdir -p $HOME/.kube
51sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
52sudo chown $(id -u):$(id -g) $HOME/.kube/config
53
54# Calico (Only on Master)
55kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/tigera-operator.yaml
56curl https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/custom-resources.yaml -O
57sed -i 's/cidr: 192\.168\.0\.0\/16/cidr: 10\.244\.0\.0\/16/' custom-resources.yaml
58kubectl create -f custom-resources.yaml
59sudo systemctl restart kubelet.service
60
61# Test (Only on Master)
62kubectl get nodes -o wide
63kubectl get pods --all-namespaces