#!/bin/bash set -euo pipefail exec > >(tee /var/log/kubeadm-setup.log) 2>&1 K8S_VERSION="1.34" K8S_PATCH="1.34.1" POD_CIDR="10.244.0.0/16" echo "=== [1/7] Disable swap and SELinux enforcement ===" swapoff -a sed -i '/swap/d' /etc/fstab setenforce 0 || true sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config echo "=== [2/7] Kernel modules and sysctl ===" cat < /etc/modules-load.d/k8s.conf overlay br_netfilter EOF modprobe overlay modprobe br_netfilter cat < /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 sysctl --system echo "=== [3/7] Install containerd ===" dnf install -y yum-utils dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo dnf install -y containerd.io mkdir -p /etc/containerd containerd config default > /etc/containerd/config.toml sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml systemctl enable --now containerd echo "=== [4/7] Install kubeadm, kubelet, kubectl ===" cat < /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://pkgs.k8s.io/core:/stable:/v${K8S_VERSION}/rpm/ enabled=1 gpgcheck=1 gpgkey=https://pkgs.k8s.io/core:/stable:/v${K8S_VERSION}/rpm/repodata/repomd.xml.key EOF dnf install -y kubelet-${K8S_PATCH} kubeadm-${K8S_PATCH} kubectl-${K8S_PATCH} systemctl enable kubelet echo "=== [5/7] Initialize kubeadm ===" INTERNAL_IP=$(curl -s -H "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/ip) kubeadm init \ --pod-network-cidr=${POD_CIDR} \ --apiserver-advertise-address=${INTERNAL_IP} \ --control-plane-endpoint=${INTERNAL_IP} echo "=== [6/7] Configure kubectl ===" mkdir -p /root/.kube cp /etc/kubernetes/admin.conf /root/.kube/config echo "=== [7/7] Install Calico CNI ===" kubectl --kubeconfig=/root/.kube/config apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.29.3/manifests/calico.yaml echo "=== Generate join command ===" kubeadm token create --print-join-command > /root/kubeadm-join-command.txt cat /root/kubeadm-join-command.txt echo "=== Generate SSH key pair for worker access ===" ssh-keygen -t rsa -b 4096 -f /root/.ssh/id_rsa -N '' -q cat /root/.ssh/id_rsa.pub echo "=== Control plane setup complete ==="