#!/bin/bash # Kubernetes Cluster Setup Script for Controller Node # Disable Swap echo "Disabling swap..." sudo swapoff -a sudo sed -i '/\/swap.img/ s/^/#/' /etc/fstab # Load Kernel Modules echo "Loading kernel modules..." cat < /dev/null sudo apt-get update sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin # Configure containerd echo "Configuring containerd..." sudo systemctl enable --now containerd sudo containerd config default | sudo tee /etc/containerd/config.toml > /dev/null sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml sudo systemctl restart containerd # Install Kubernetes echo "Installing Kubernetes components..." sudo apt-get update curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | sudo 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:/v1.30/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list sudo apt-get update sudo apt-get install -y kubelet kubeadm kubectl sudo apt-mark hold kubelet kubeadm kubectl sudo systemctl enable --now kubelet # Initialize Kubernetes Cluster echo "Initialising Kubernetes cluster..." sudo kubeadm init --pod-network-cidr=10.244.0.0/16 # Configure kubectl for the current user echo "Setting up kubectl..." mkdir -p $HOME/.kube sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config # Install Flannel Network Plugin echo "Installing Flannel network plugin..." kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml # Verify Setup echo "Kubernetes cluster setup completed successfully!" kubectl get nodes kubectl get pods -n kube-system