Created
October 15, 2024 12:31
-
-
Save markhc/21b54367b23c5cdfd1481fad1bb7eb60 to your computer and use it in GitHub Desktop.
Install kubernetes using kubeadm on Debian systems
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# Run as root | |
if [ "$EUID" -ne 0 ] | |
then echo "Please run as root" | |
exit | |
fi | |
CRICTL_VERSION="v1.30.0" | |
KUBERNETES_VERSION=1.30 | |
POSITIONAL_ARGS=() | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
-k|--kubernetes-version) | |
KUBERNETES_VERSION="$2" | |
shift # past argument | |
shift # past value | |
;; | |
-m|--mode) | |
INSTALL_MODE="$2" | |
shift # past argument | |
shift # past value | |
;; | |
-*|--*) | |
echo "Unknown option $1" | |
exit 1 | |
;; | |
*) | |
POSITIONAL_ARGS+=("$1") # save positional arg | |
shift # past argument | |
;; | |
esac | |
done | |
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters | |
if [ -z "$INSTALL_MODE" ]; then | |
echo "Please provide the install mode" | |
exit 1 | |
fi | |
if [ "$INSTALL_MODE" != "master" ] && [ "$INSTALL_MODE" != "worker" ]; then | |
echo "Invalid install mode" | |
exit 1 | |
fi | |
# Step 1: Enable iptables Bridged Traffic on all the Nodes | |
cat <<EOF | tee /etc/modules-load.d/k8s.conf | |
overlay | |
br_netfilter | |
EOF | |
modprobe overlay | |
modprobe br_netfilter | |
# sysctl params required by setup, params persist across reboots | |
cat <<EOF | 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 | |
# Apply sysctl params without reboot | |
sysctl --system | |
# Setp 2. Disable swap | |
swapoff -a | |
(crontab -l 2>/dev/null; echo "@reboot /sbin/swapoff -a") | crontab - || true | |
# Step 3. Install CRI-O | |
apt-get update -y && apt-get install -y software-properties-common gpg curl apt-transport-https ca-certificates | |
curl -fsSL https://pkgs.k8s.io/addons:/cri-o:/prerelease:/main/deb/Release.key | gpg --dearmor -o /etc/apt/keyrings/cri-o-apt-keyring.gpg | |
echo "deb [signed-by=/etc/apt/keyrings/cri-o-apt-keyring.gpg] https://pkgs.k8s.io/addons:/cri-o:/prerelease:/main/deb/ /" | tee /etc/apt/sources.list.d/cri-o.list | |
apt-get update -y && apt-get install -y cri-o | |
systemctl daemon-reload | |
systemctl enable crio --now && systemctl start crio.service | |
# install crictl | |
wget https://github.com/kubernetes-sigs/cri-tools/releases/download/$CRICTL_VERSION/crictl-$CRICTL_VERSION-linux-amd64.tar.gz | |
tar zxvf crictl-$CRICTL_VERSION-linux-amd64.tar.gz -C /usr/local/bin | |
rm -f crictl-$CRICTL_VERSION-linux-amd64.tar.gz | |
# Step 4: Install Kubeadm & Kubelet & Kubectl on all Nodes | |
mkdir -p /etc/apt/keyrings | |
curl -fsSL https://pkgs.k8s.io/core:/stable:/v$KUBERNETES_VERSION/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:/v$KUBERNETES_VERSION/deb/ /" | tee /etc/apt/sources.list.d/kubernetes.list | |
apt-get update -y && apt-get install -y kubelet kubeadm kubectl && apt-mark hold kubelet kubeadm kubectl | |
apt-get install -y jq | |
local_ip="$(ip --json addr show eth0 | jq -r '.[0].addr_info[] | select(.family == "inet") | .local')" | |
cat > /etc/default/kubelet << EOF | |
KUBELET_EXTRA_ARGS=--node-ip=$local_ip | |
EOF | |
if [ "$INSTALL_MODE" == "master" ]; then | |
# On Master Node, Initialize the Control Plane | |
IPADDR=$(curl ifconfig.me && echo "") | |
NODENAME=$(hostname -s) | |
POD_CIDR="192.168.0.0/16" | |
kubeadm init --control-plane-endpoint=$IPADDR --apiserver-cert-extra-sans=$IPADDR --pod-network-cidr=$POD_CIDR --node-name $NODENAME | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment