Last active
November 29, 2023 12:04
-
-
Save ashupednekar/ba785189fd0963ca69338966f2af7eae to your computer and use it in GitHub Desktop.
Kubeadm setup
This file contains 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 | |
# Function to print messages with emojis | |
print_message() { | |
echo -e "$1 $2" | |
} | |
# Take input for fresh install or reset | |
read -p "Is this a fresh install or reset? (fresh/reset): " install_type | |
# Reset Kubernetes if specified | |
if [ "$install_type" == "reset" ]; then | |
print_message "π§ Resetting Kubernetes..." | |
sudo rm -rf /etc/kubernetes ~/.kube | |
sudo kubeadm reset | |
else | |
# Install net-tools and check specific ports | |
print_message "π Installing net-tools..." | |
sudo apt install net-tools | |
print_message "π Checking open ports..." | |
netstat -tulpn | grep "6443\|2379\|2380\|10250\|10259\|10257" | |
# Disable UFW (Uncomplicated Firewall) and show status | |
print_message "π¨ Disabling UFW..." | |
sudo ufw disable | |
print_message "π₯ UFW Status:" | |
sudo ufw status | |
# Update and upgrade system packages | |
print_message "π Updating system packages..." | |
sudo apt update | |
sudo apt -y full-upgrade | |
# Install and configure systemd-timesyncd for time synchronization | |
print_message "π°οΈ Installing systemd-timesyncd for time synchronization..." | |
sudo apt install systemd-timesyncd | |
sudo timedatectl set-ntp true | |
print_message "β° Time synchronization status:" | |
timedatectl status | |
# Disable swap | |
print_message "π« Disabling swap..." | |
sudo swapoff -a | |
sudo sed -i.bak -r 's/(.+ swap .+)/#\1/' /etc/fstab | |
print_message "π½ Checking swap status:" | |
free -m | |
cat /etc/fstab | grep swap | |
# Load kernel modules for Kubernetes | |
print_message "π Adding kernel modules for Kubernetes..." | |
echo "overlay" | sudo tee -a /etc/modules-load.d/k8s.conf | |
echo "br_netfilter" | sudo tee -a /etc/modules-load.d/k8s.conf | |
sudo modprobe overlay | |
sudo modprobe br_netfilter | |
print_message "π§ Loaded kernel modules:" | |
lsmod | grep "overlay\|br_netfilter" | |
# Configure sysctl settings for Kubernetes | |
print_message "π§ Configuring sysctl settings for Kubernetes..." | |
echo "net.bridge.bridge-nf-call-ip6tables = 1" | sudo tee -a /etc/sysctl.d/k8s.conf | |
echo "net.bridge.bridge-nf-call-iptables = 1" | sudo tee -a /etc/sysctl.d/k8s.conf | |
echo "net.ipv4.ip_forward = 1" | sudo tee -a /etc/sysctl.d/k8s.conf | |
sudo sysctl --system | |
# Add Kubernetes repository and install Kubernetes components | |
print_message "π Adding Kubernetes repository and installing components..." | |
sudo apt-get install -y apt-transport-https ca-certificates curl gpg gnupg2 software-properties-common | |
sudo mkdir /etc/apt/keyrings || true | |
sudo chmod 755 /etc/apt/keyrings | |
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/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:/v1.28/deb/ /' | \ | |
sudo tee /etc/apt/sources.list.d/kubernetes.list | |
sudo apt update | |
sudo apt-get install -y kubelet kubeadm kubectl | |
sudo apt-mark hold kubelet kubeadm kubectl | |
# Install containerd and related tools | |
print_message "π³ Installing containerd and related tools..." | |
sudo wget https://github.com/containerd/containerd/releases/download/v1.6.8/containerd-1.6.8-linux-amd64.tar.gz | |
sudo tar Cxzvf /usr/local containerd-1.6.8-linux-amd64.tar.gz | |
sudo wget https://github.com/opencontainers/runc/releases/download/v1.1.3/runc.amd64 | |
sudo install -m 755 runc.amd64 /usr/local/sbin/runc | |
sudo wget https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-amd64-v1.1.1.tgz | |
sudo mkdir -p /opt/cni/bin | |
sudo tar Cxzvf /opt/cni/bin cni-plugins-linux-amd64-v1.1.1.tgz | |
sudo mkdir /etc/containerd | |
# Configure containerd | |
print_message "π οΈ Configuring containerd..." | |
sudo containerd config default | sudo tee /etc/containerd/config.toml | |
sudo sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml | |
sudo curl -L https://raw.githubusercontent.com/containerd/containerd/main/containerd.service -o /etc/systemd/system/containerd.service | |
sudo systemctl daemon-reload | |
sudo systemctl enable --now containerd | |
print_message "π Containerd status:" | |
sudo systemctl status containerd | |
print_message "βΉοΈ Change SystemCgroup to true" | |
# Configure crictl settings | |
print_message "π§ Configuring crictl settings..." | |
echo "runtime-endpoint: unix:///run/containerd/containerd.sock" | sudo tee -a /etc/crictl.yaml | |
echo "image-endpoint: unix:///run/containerd/containerd.sock" | sudo tee -a /etc/crictl.yaml | |
echo "timeout: 2" | sudo tee -a /etc/crictl.yaml | |
echo "debug: false # <- if you don't want to see debug info you can set this to false" | sudo tee -a /etc/crictl.yaml | |
echo "pull-image-on-create: false" | sudo tee -a /etc/crictl.yaml | |
# Enable kubelet and check containerd images | |
print_message "π Enabling kubelet..." | |
sudo systemctl enable kubelet | |
print_message "π Checking containerd images..." | |
sudo crictl images | |
# Pull Kubernetes container images using containerd | |
print_message "π¦ Pulling Kubernetes container images using containerd..." | |
sudo kubeadm config images pull --cri-socket unix:///var/run/containerd/containerd.sock | |
print_message "π Checking containerd images after pulling Kubernetes images..." | |
sudo crictl images | |
# Enable network forwarding and initialize Kubernetes master | |
print_message "π Enabling network forwarding and initializing Kubernetes master..." | |
sudo modprobe br_netfilter | |
echo '1' | sudo tee /proc/sys/net/ipv4/ip_forward | |
fi | |
sudo kubeadm init \ | |
--pod-network-cidr=10.244.0.0/16 \ | |
--cri-socket unix:///var/run/containerd/containerd.sock \ | |
--v=5 | |
# Configure kubeconfig | |
print_message "π Configuring kubeconfig..." | |
sudo mkdir -p $HOME/.kube | |
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config | |
sudo chown $(id -u):$(id -g) $HOME/.kube/config | |
print_message "β¨ Kubernetes setup complete! You can now deploy your pods. β¨" | |
# Weave | |
print_message "π Deploying Weave CNI..." | |
kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml | |
# MetalLB | |
print_message "ποΈ Configuring MetalLB..." | |
kubectl get configmap kube-proxy -n kube-system -o yaml | \ | |
sed -e "s/strictARP: false/strictARP: true/" | \ | |
kubectl apply -f - -n kube-system | |
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.13.12/config/manifests/metallb-native.yaml | |
echo "apiVersion: metallb.io/v1beta1" | sudo tee -a metallb-ip-pool.yaml | |
echo "kind: IPAddressPool" | sudo tee -a metallb-ip-pool.yaml | |
echo "metadata:" | sudo tee -a metallb-ip-pool.yaml | |
echo " name: first-pool" | sudo tee -a metallb-ip-pool.yaml | |
echo " namespace: metallb-system" | sudo tee -a metallb-ip-pool.yaml | |
echo "spec:" | sudo tee -a metallb-ip-pool.yaml | |
echo " addresses:" | sudo tee -a metallb-ip-pool.yaml | |
echo " - 192.168.10.0/24" | sudo tee -a metallb-ip-pool.yaml | |
echo " - 192.168.9.1-192.168.9.5" | sudo tee -a metallb-ip-pool.yaml | |
echo " - fc00:f853:0ccd:e799::/124" | sudo tee -a metallb-ip-pool.yaml | |
echo "---" | sudo tee -a metallb-ip-pool.yaml | |
kubectl apply -f metallb-ip-pool.yaml | |
# Untaint master | |
print_message "π Untainting master node..." | |
# Use "kubectl get no -o name" to get the node name | |
kubectl taint node <node_name> node-role.kubernetes.io/control-plane- | |
# Istio | |
print_message "π Installing Istio..." | |
helm repo add istio https://istio-release.storage.googleapis.com/charts | |
helm repo update | |
kubectl create ns istio-system | |
helm install istio-base istio/base -n istio-system --set defaultRevision=default | |
helm install istiod istio/istiod -n istio-system --wait | |
kubectl create ns istio-ingress | |
helm install istio-ingress istio/gateway -n istio-ingress --wait | |
kubectl label ns dev istio-injection=enabled | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment