Install.sh
· 4.1 KiB · Bash
Bruto
#/bin/bash
# This script is to install Kubernetes on Ubuntu 22.04 LTS, with Calico as CNI
# This script used 10.244.0.0/16 as pod network CIDR. This network should not be used in your physical network.
# This script used Calico v3.27.0. You can change it to the latest version.
# Reference: https://www.cherryservers.com/blog/install-kubernetes-on-ubuntu
DEBIAN_FRONTEND=noninteractive sudo apt update
DEBIAN_FRONTEND=noninteractive sudo apt install curl gnupg2 software-properties-common apt-transport-https ca-certificates -y
echo "Disable swap..."
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
echo "Uninstall Docker.ce..."
aiur() { arg="$( cut -d ' ' -f 2- <<< "$@" )" && curl -sL https://gitlab.aiursoft.cn/aiursoft/aiurscript/-/raw/master/$1.sh | sudo bash -s $arg; }
aiur uninstall/docker
sudo apt autoremove -y
echo "Install Docker.io..."
DEBIAN_FRONTEND=noninteractive sudo apt install docker.io -y
echo "Prepare network..."
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
# sysctl params required by setup, params persist across reboots
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
sudo sysctl --system
echo "Prepare runtime to setup containerd..."
sudo mkdir /etc/containerd > /dev/null 2>&1
sudo sh -c "containerd config default > /etc/containerd/config.toml"
sudo sed -i 's/ SystemdCgroup = false/ SystemdCgroup = true/' /etc/containerd/config.toml
sudo systemctl restart containerd.service
sudo systemctl restart kubelet.service > /dev/null 2>&1
echo "Install K8S..."
curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes.gpg --yes
echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/kubernetes.gpg] http://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list
sudo apt update
DEBIAN_FRONTEND=noninteractive sudo apt install kubeadm kubelet kubectl kubernetes-cni -y
# Init (Only on Master)
echo "Init K8S..."
sudo kubeadm config images pull
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
read -p "Please copy the kubeadm join command above and run it on worker nodes. Press any key to continue..."
# Config (Only on Master)
echo "Config K8S..."
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# Calico (Only on Master)
echo "Install Calico..."
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/tigera-operator.yaml
curl https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/custom-resources.yaml -O
sed -i 's/cidr: 192\.168\.0\.0\/16/cidr: 10\.244\.0\.0\/16/' custom-resources.yaml
kubectl create -f custom-resources.yaml
sudo systemctl restart kubelet.service
# Test (Only on Master)
echo "Test K8S..."
while [ "$(kubectl get nodes | grep -v "Ready" | wc -l)" -gt 1 ]; do
echo "Waiting for all nodes to be ready..."
kubectl get nodes -o wide
sleep 5
done
while [ "$(kubectl get pods --all-namespaces | grep -v "Running" | wc -l)" -gt 1 ]; do
echo "Waiting for all pods to be running..."
kubectl get pods --all-namespaces -o wide
sleep 5
done
# Install Dashboard (Only on Master)
echo "Install dashboard..."
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
kubectl create serviceaccount -n kubernetes-dashboard admin-user
cat << EOF > rbac.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: admin-user
namespace: kubernetes-dashboard
EOF
kubectl apply -f rbac.yaml
rm rbac.yaml
token=$(kubectl -n kubernetes-dashboard create token admin-user)
echo "Dashboard token: $token"
kubectl port-forward -n kubernetes-dashboard service/kubernetes-dashboard --address 0.0.0.0 10443:443
hostname=$(hostname)
echo "Dashboard URL: https://$hostname:10443"
| 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 |
| 6 | |
| 7 | DEBIAN_FRONTEND=noninteractive sudo apt update |
| 8 | DEBIAN_FRONTEND=noninteractive sudo apt install curl gnupg2 software-properties-common apt-transport-https ca-certificates -y |
| 9 | |
| 10 | echo "Disable swap..." |
| 11 | sudo swapoff -a |
| 12 | sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab |
| 13 | |
| 14 | echo "Uninstall Docker.ce..." |
| 15 | aiur() { arg="$( cut -d ' ' -f 2- <<< "$@" )" && curl -sL https://gitlab.aiursoft.cn/aiursoft/aiurscript/-/raw/master/$1.sh | sudo bash -s $arg; } |
| 16 | aiur uninstall/docker |
| 17 | sudo apt autoremove -y |
| 18 | |
| 19 | echo "Install Docker.io..." |
| 20 | DEBIAN_FRONTEND=noninteractive sudo apt install docker.io -y |
| 21 | |
| 22 | echo "Prepare network..." |
| 23 | cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf |
| 24 | overlay |
| 25 | br_netfilter |
| 26 | EOF |
| 27 | sudo modprobe overlay |
| 28 | sudo modprobe br_netfilter |
| 29 | # sysctl params required by setup, params persist across reboots |
| 30 | cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf |
| 31 | net.bridge.bridge-nf-call-iptables = 1 |
| 32 | net.bridge.bridge-nf-call-ip6tables = 1 |
| 33 | net.ipv4.ip_forward = 1 |
| 34 | EOF |
| 35 | sudo sysctl --system |
| 36 | |
| 37 | echo "Prepare runtime to setup containerd..." |
| 38 | sudo mkdir /etc/containerd > /dev/null 2>&1 |
| 39 | sudo sh -c "containerd config default > /etc/containerd/config.toml" |
| 40 | sudo sed -i 's/ SystemdCgroup = false/ SystemdCgroup = true/' /etc/containerd/config.toml |
| 41 | sudo systemctl restart containerd.service |
| 42 | sudo systemctl restart kubelet.service > /dev/null 2>&1 |
| 43 | |
| 44 | echo "Install K8S..." |
| 45 | curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes.gpg --yes |
| 46 | echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/kubernetes.gpg] http://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list |
| 47 | sudo apt update |
| 48 | DEBIAN_FRONTEND=noninteractive sudo apt install kubeadm kubelet kubectl kubernetes-cni -y |
| 49 | |
| 50 | # Init (Only on Master) |
| 51 | echo "Init K8S..." |
| 52 | sudo kubeadm config images pull |
| 53 | sudo kubeadm init --pod-network-cidr=10.244.0.0/16 |
| 54 | read -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) |
| 57 | echo "Config K8S..." |
| 58 | mkdir -p $HOME/.kube |
| 59 | sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config |
| 60 | sudo chown $(id -u):$(id -g) $HOME/.kube/config |
| 61 | |
| 62 | # Calico (Only on Master) |
| 63 | echo "Install Calico..." |
| 64 | kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/tigera-operator.yaml |
| 65 | curl https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/custom-resources.yaml -O |
| 66 | sed -i 's/cidr: 192\.168\.0\.0\/16/cidr: 10\.244\.0\.0\/16/' custom-resources.yaml |
| 67 | kubectl create -f custom-resources.yaml |
| 68 | sudo systemctl restart kubelet.service |
| 69 | |
| 70 | # Test (Only on Master) |
| 71 | echo "Test K8S..." |
| 72 | while [ "$(kubectl get nodes | grep -v "Ready" | wc -l)" -gt 1 ]; do |
| 73 | echo "Waiting for all nodes to be ready..." |
| 74 | kubectl get nodes -o wide |
| 75 | sleep 5 |
| 76 | done |
| 77 | |
| 78 | while [ "$(kubectl get pods --all-namespaces | grep -v "Running" | wc -l)" -gt 1 ]; do |
| 79 | echo "Waiting for all pods to be running..." |
| 80 | kubectl get pods --all-namespaces -o wide |
| 81 | sleep 5 |
| 82 | done |
| 83 | |
| 84 | # Install Dashboard (Only on Master) |
| 85 | echo "Install dashboard..." |
| 86 | kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml |
| 87 | kubectl create serviceaccount -n kubernetes-dashboard admin-user |
| 88 | cat << EOF > rbac.yaml |
| 89 | apiVersion: rbac.authorization.k8s.io/v1 |
| 90 | kind: ClusterRoleBinding |
| 91 | metadata: |
| 92 | name: admin-user |
| 93 | roleRef: |
| 94 | apiGroup: rbac.authorization.k8s.io |
| 95 | kind: ClusterRole |
| 96 | name: cluster-admin |
| 97 | subjects: |
| 98 | - kind: ServiceAccount |
| 99 | name: admin-user |
| 100 | namespace: kubernetes-dashboard |
| 101 | EOF |
| 102 | kubectl apply -f rbac.yaml |
| 103 | rm rbac.yaml |
| 104 | token=$(kubectl -n kubernetes-dashboard create token admin-user) |
| 105 | echo "Dashboard token: $token" |
| 106 | kubectl port-forward -n kubernetes-dashboard service/kubernetes-dashboard --address 0.0.0.0 10443:443 |
| 107 | hostname=$(hostname) |
| 108 | echo "Dashboard URL: https://$hostname:10443" |
| 109 |