最終更新 1706077627

Install K8S on your home lab!

修正履歴 ce8c73ee6ccee6c70d749183637ab818d4cae547

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# Reference: https://www.cherryservers.com/blog/install-kubernetes-on-ubuntu#step-2-set-up-hostnames
6
7DEBIAN_FRONTEND=noninteractive sudo apt update
8DEBIAN_FRONTEND=noninteractive sudo apt install curl gnupg2 software-properties-common apt-transport-https ca-certificates -y
9
10echo "Disable swap..."
11sudo swapoff -a
12sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
13
14echo "Uninstall Docker.ce..."
15aiur() { arg="$( cut -d ' ' -f 2- <<< "$@" )" && curl -sL https://gitlab.aiursoft.cn/aiursoft/aiurscript/-/raw/master/$1.sh | sudo bash -s $arg; }
16aiur uninstall/docker
17sudo apt autoremove -y
18
19echo "Install Docker.io..."
20DEBIAN_FRONTEND=noninteractive sudo apt install docker.io -y
21
22echo "Prepare network..."
23cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
24overlay
25br_netfilter
26EOF
27sudo modprobe overlay
28sudo modprobe br_netfilter
29# sysctl params required by setup, params persist across reboots
30cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
31net.bridge.bridge-nf-call-iptables = 1
32net.bridge.bridge-nf-call-ip6tables = 1
33net.ipv4.ip_forward = 1
34EOF
35sudo sysctl --system
36
37echo "Prepare runtime to setup containerd..."
38sudo mkdir /etc/containerd > /dev/null 2>&1
39sudo sh -c "containerd config default > /etc/containerd/config.toml"
40sudo sed -i 's/ SystemdCgroup = false/ SystemdCgroup = true/' /etc/containerd/config.toml
41sudo systemctl restart containerd.service
42sudo systemctl restart kubelet.service > /dev/null 2>&1
43
44echo "Install K8S..."
45curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes.gpg --yes
46echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/kubernetes.gpg] http://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list
47sudo apt update
48DEBIAN_FRONTEND=noninteractive sudo apt install kubeadm kubelet kubectl kubernetes-cni -y
49
50# Init (Only on Master)
51echo "Init K8S..."
52sudo kubeadm config images pull
53sudo kubeadm init --pod-network-cidr=10.244.0.0/16
54read -p "Please copy the kubeadm join command above and run it on worker nodes. Press any key to continue..."
55
56# Config (Only on Master)
57echo "Config K8S..."
58mkdir -p $HOME/.kube
59sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
60sudo chown $(id -u):$(id -g) $HOME/.kube/config
61
62# Calico (Only on Master)
63echo "Install Calico..."
64kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/tigera-operator.yaml
65curl https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/custom-resources.yaml -O
66sed -i 's/cidr: 192\.168\.0\.0\/16/cidr: 10\.244\.0\.0\/16/' custom-resources.yaml
67kubectl create -f custom-resources.yaml
68sudo systemctl restart kubelet.service
69
70# Test (Only on Master)
71echo "Test K8S..."
72kubectl get nodes -o wide
73kubectl get pods --all-namespaces