From 40a5d0be0371784988d69c19865476415a8b3df3 Mon Sep 17 00:00:00 2001 From: Bimo Fransiscus Asisi Date: Fri, 16 May 2025 20:27:14 +0800 Subject: [PATCH] script to install k8s Change-Id: I802c6896bec8f18c91487b4fbc221b074970944b Signed-off-by: Bimo Fransiscus Asisi --- tools/setup_k8s/setup_k8s.sh | 294 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 294 insertions(+) create mode 100755 tools/setup_k8s/setup_k8s.sh diff --git a/tools/setup_k8s/setup_k8s.sh b/tools/setup_k8s/setup_k8s.sh new file mode 100755 index 00000000..282a37c7 --- /dev/null +++ b/tools/setup_k8s/setup_k8s.sh @@ -0,0 +1,294 @@ +#!/bin/bash + +# Capture start time +start_time=$(date +%s) + +workspace=$(pwd) + +# Define default values +DEFAULT_KUBEVERSION="1.32.3-1.1" +DEFAULT_HELMVERSION="3.14.2" +DEFAULT_POD_CIDR="10.244.0.0/16" + +# Parse command-line arguments +IP_ADDR="" +KUBEVERSION="$DEFAULT_KUBEVERSION" +HELMVERSION="$DEFAULT_HELMVERSION" +POD_CIDR="$DEFAULT_POD_CIDR" + +while [[ "$#" -gt 0 ]]; do + case "$1" in + --ip-address) + IP_ADDR="$2" + shift 2 + ;; + --kube-version) + KUBEVERSION="$2" + shift 2 + ;; + --helm-version) + HELMVERSION="$2" + shift 2 + ;; + --pod-cidr) + POD_CIDR="$2" + shift 2 + ;; + *) + echo "Unknown parameter: $1" + exit 1 + ;; + esac +done + +command_exists() { + command -v "$1" >/dev/null 2>&1 +} + +# Function to check internet connectivity +check_internet() { + echo "Checking internet connection..." + if ping -q -c 1 -W 1 google.com >/dev/null 2>&1; then + echo "Internet connection is active." + else + echo "Error: No internet connection. Please ensure your system has access to the internet." + exit 1 + fi +} + +# Function to check if curl is installed and install if not +check_and_install_curl() { + echo "Checking if curl is installed..." + if ! command_exists curl; then + echo "curl is not installed. Installing now..." + apt update + apt install -y curl + if command_exists curl; then + echo "curl has been successfully installed." + else + echo "Error: Failed to install curl. Please check your internet connection and try again." + exit 1 + fi + else + echo "curl is already installed." + fi +} + +# Function to check for existing Kubernetes cluster and prompt for removal +check_existing_cluster() { + read -p "This will attempt to remove any existing cluster. Do you want to proceed? (y/N): " remove_cluster + if [[ "$remove_cluster" != "y" && "$remove_cluster" != "Y" ]]; then + echo "Kubernetes cluster removal skipped by user. Exiting script." + exit 0 + fi + + echo "Attempting to remove existing Kubernetes components..." + + # Attempt kubeadm reset if kubeadm is found + if command_exists kubeadm; then + echo "Running 'kubeadm reset'..." + if ! kubeadm reset -f; then + echo "Warning: 'kubeadm reset' failed or encountered issues. Proceeding with other cleanup steps." + fi + else + echo "Info: 'kubeadm' command not found. Skipping 'kubeadm reset'." + fi + + # Attempt to purge Kubernetes packages + echo "Purging Kubernetes packages..." + if ! apt-get -y purge kubeadm kubectl kubelet kubernetes-cni kube* containerd; then + echo "Warning: Failed to purge all Kubernetes packages. This might be due to them not being fully installed or other issues." + fi + + # Attempt autoremove for leftover dependencies + echo "Running 'apt-get autoremove'..." + if ! apt-get -y autoremove; then + echo "Warning: 'apt-get autoremove' failed or encountered issues." + fi + + # Remove kubeconfig and other user-specific Kubernetes files + if [ -d "$HOME/.kube" ]; then + echo "Removing user's .kube directory..." + if ! rm -rf "$HOME/.kube"; then + echo "Warning: Failed to remove '$HOME/.kube' directory." + fi + else + echo "Info: '$HOME/.kube' directory not found. Skipping removal." + fi + + echo "Existing cluster cleanup attempt completed." +} + +# Function to disable swap +disable_swap() { + echo "Disabling swap..." + swapon --show > /dev/null 2>&1 + if [ $? -eq 0 ]; then + swapoff -a + rm /swapfile + sed -i 's/\/swap.img/#\/swap.img/' /etc/fstab + else + echo "No swap is currently enabled." + fi +} + +# Function to check Ubuntu version +check_ubuntu_version() { + os_version=$(lsb_release -rs) + if [ "$os_version" != "22.04" ] && [ "$os_version" != "24.04" ]; then + echo "Error: Unsupported Ubuntu version. This script supports 22.04 and 24.04 only." + exit 1 + fi +} + +# Function to handle errors +handle_error() { + echo "Error occurred at step $1. Exiting..." + exit 1 +} + +# Function to check if namespace exists +check_namespace_not_exists() { + local namespace="$1" + if kubectl get namespace "$namespace" &> /dev/null; then + echo "Namespace '$namespace' exists. Skipping steps..." + return 0 # Namespace exists + else + echo "Namespace '$namespace' does not exist." + return 1 # Namespace does not exist + fi +} + + +# Check Ubuntu version +echo "Checking Ubuntu version..." +check_ubuntu_version + +# Check internet connection +check_internet + +# Check and install curl if not present +check_and_install_curl + +# Check for existing Kubernetes cluster and prompt for removal +check_existing_cluster + +# Disable swap +disable_swap + +# Script for Installing Docker,Kubernetes and Helm + +wait_for_pods_running () { + NS="$2" + CMD="kubectl get pods --all-namespaces " + if [ "$NS" != "all-namespaces" ]; then + CMD="kubectl get pods -n $2 " + fi + KEYWORD="Running" + if [ "$#" == "3" ]; then + KEYWORD="${3}.*Running" + fi + + CMD2="$CMD | grep \"$KEYWORD\" | wc -l" + NUMPODS=$(eval "$CMD2") + echo "waiting for $NUMPODS/$1 pods running in namespace [$NS] with keyword [$KEYWORD]" + while [ $NUMPODS -lt $1 ]; do + sleep 5 + NUMPODS=$(eval "$CMD2") + echo "> waiting for $NUMPODS/$1 pods running in namespace [$NS] with keyword [$KEYWORD]" + done +} + +# Step x: Edit /etc/sysctl.conf to add fs.inotify.max_user_watches and fs.inotify.max_user_instances. This shoudl be done before containerd installation +echo "===========================================================" +echo " Preping the ENV: Editing /etc/sysctl.conf..." +echo "===========================================================" + +bash -c 'echo "fs.inotify.max_user_watches=524288" >> /etc/sysctl.conf' +bash -c 'echo "fs.inotify.max_user_instances=512" >> /etc/sysctl.conf' +bash -c 'echo "fs.inotify.max_queued_events=16384" >> /etc/sysctl.conf' +bash -c 'echo "vm.max_map_count=262144" >> /etc/sysctl.conf' + +# Apply sysctl params without reboot +sysctl --system + +# Installing containerd +modprobe overlay +modprobe br_netfilter + +cat <