From 13474f24997a883c637b3c493b8ef6037f41de1f Mon Sep 17 00:00:00 2001 From: Cezary Stanislawski Date: Fri, 25 Oct 2024 18:50:37 +0200 Subject: [PATCH 1/4] fix spacing & desc --- kubelift.sh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/kubelift.sh b/kubelift.sh index 3a47d48..00ead27 100755 --- a/kubelift.sh +++ b/kubelift.sh @@ -18,18 +18,18 @@ Usage: $0 [operation] [options...] Operations: create Create a new Kubernetes cluster upgrade Upgrade an existing Kubernetes cluster - cleanup Remove Kubernetes cluster while preserving CNI + cleanup Remove Kubernetes cluster Options: - -h, --help Display this help message - --noninteractive Enable or disable noninteractive mode - --ssh-user Username to use for SSH connection - --kubernetes-version Kubernetes version to install (create/upgrade only) - --control-plane-ip Control plane node IP address - --worker-ips Worker node IP addresses (create only) - --enable-control-plane-workloads Enable control plane scheduling (create only) - --skip-reqs Skip minimum requirements validation - --nuke Perform deep cleanup (cleanup only) + -h, --help Display this help message + --noninteractive Enable or disable noninteractive mode + --ssh-user Username to use for SSH connection + --kubernetes-version Kubernetes version to install (create/upgrade only) + --control-plane-ip Control plane node IP address + --worker-ips Worker node IP addresses (create only) + --enable-control-plane-workloads Enable control plane scheduling (create only) + --skip-reqs Skip minimum requirements validation + --nuke Perform deep cleanup (cleanup only) EOF exit 0 } From 4a954ea3fba95ec2f06c120a46a4634fe5631855 Mon Sep 17 00:00:00 2001 From: Cezary Stanislawski Date: Fri, 25 Oct 2024 19:02:36 +0200 Subject: [PATCH 2/4] upgrade readme --- README.md | 59 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 98a1ae5..0ffd446 100644 --- a/README.md +++ b/README.md @@ -6,28 +6,59 @@ Kubernetes cluster operations over SSH The goal of this project is to provide a simple tool to provision a Kubernetes cluster on VMs over SSH. By going with a script-based approach, you can easily customize the installation process to fit your needs, and fail fast if something goes wrong. -kubelift aims to be a bridge between the manual operations with `kubeadm` and the automated installation with `kubespray` requiring a lot of setup. - -## Overview - -kubelift provides scripts for automating the management of Kubernetes clusters over SSH. The scripts handle cluster creation, upgrades, and cleanup tasks, and are designed to work with both on-premise and cloud-based virtual machines. +`kubelift` aims to be a bridge between the manual operations with `kubeadm` and the automated installation with `kubespray` requiring a lot of setup. ## Requirements -- A set of VMs with SSH access +- A set of VM(s) with SSH access - Sudo privileges without password prompt (NOPASSWD in sudoers) for the SSH user - Internet connectivity on all nodes ## Features -- **Noninteractive mode**: Supports non-interactive execution for automated deployments -- **SSH-based Operations**: All operations are performed over SSH for remote management -- **Input Validation**: Thorough input validation ensures all provided parameters are correct -- **Modular Design**: Scripts are organized into functions for better maintainability +- **Noninteractive mode** for automated operations +- **SSH-based Operations** for secure remote management +- **Seamless Upgrades** for managing cluster lifecycle +- **Input Validation** ensures all provided parameters are correct +- **Modular Design** for better maintainability + +## Installation + +### Quick Setup + +```bash +curl -LO https://raw.githubusercontent.com/cstanislawski/kubelift/main/kubelift.sh +chmod +x kubelift.sh +``` + +### System-wide Installation + +```bash +sudo curl -L https://raw.githubusercontent.com/cstanislawski/kubelift/main/kubelift.sh -o /usr/local/bin/kubelift +sudo chmod +x /usr/local/bin/kubelift +``` ## Usage -Before running any operations, copy `.env.example` to `.env` and fill in the required values. +```bash +kubelift --help +Usage: /usr/local/bin/kubelift [operation] [options...] +Operations: + create Create a new Kubernetes cluster + upgrade Upgrade an existing Kubernetes cluster + cleanup Remove Kubernetes cluster + +Options: + -h, --help Display this help message + --noninteractive Enable or disable noninteractive mode + --ssh-user Username to use for SSH connection + --kubernetes-version Kubernetes version to install (create/upgrade only) + --control-plane-ip Control plane node IP address + --worker-ips Worker node IP addresses (create only) + --enable-control-plane-workloads Enable control plane scheduling (create only) + --skip-reqs Skip minimum requirements validation + --nuke Perform deep cleanup (cleanup only) +``` ### Cluster Creation @@ -79,7 +110,8 @@ The upgrade operation will: ./kubelift.sh cleanup \ --noninteractive \ --ssh-user \ - --control-plane-ip + --control-plane-ip \ + --nuke # Optional ``` The cleanup operation will: @@ -124,7 +156,7 @@ The scripts use the following environment variables: Some of the alternatives you could consider are: -- [kubeadm](https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/) - a tool built to provide best-practice "fast paths" for creating Kubernetes clusters, which kubelift is based on +- [kubeadm](https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/) - a tool built to provide best-practice "fast paths" for creating Kubernetes clusters, which `kubelift` is based on - [kubespray](https://github.com/kubernetes-sigs/kubespray) - a set of Ansible playbooks to provision a Kubernetes cluster - [kubean](https://github.com/kubean-io/kubean) - an operator for cluster lifecycle management based on kubespray - [kops](https://github.com/kubernetes/kops) - CLI to create, destroy, upgrade and maintain production-grade Kubernetes clusters hosted on AWS/GCP with more providers in Beta/Alpha @@ -151,6 +183,7 @@ Some of the alternatives you could consider are: - Add support for more CNI plugins: Calico, Cilium - Cluster configuration templating - Add support for HA control plane +- Add downgrades support - Assume presence of the flag equals true (e.g. --noninteractive) if the flag is present - Add k3s support - Add support for more Linux distributions From 4c4c67fb12333c66ad925f9cf3e07f23a4bb8fc1 Mon Sep 17 00:00:00 2001 From: Cezary Stanislawski Date: Fri, 25 Oct 2024 19:06:38 +0200 Subject: [PATCH 3/4] fix version comparison --- kubelift.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/kubelift.sh b/kubelift.sh index 00ead27..1551693 100755 --- a/kubelift.sh +++ b/kubelift.sh @@ -366,10 +366,13 @@ function join_worker_nodes() { function verify_version_compatibility() { local nodes_versions nodes_versions=$(ssh -o StrictHostKeyChecking=no "$SSH_USER@$CONTROL_PLANE_IP" \ - kubectl get nodes -o=jsonpath='{range .items[*]}{.status.nodeInfo.kubeletVersion}{"\n"}{end}' | sort -u) + "kubectl get nodes -o jsonpath='{.items[*].status.nodeInfo.kubeletVersion}'") || \ + error "Failed to get cluster version info" + + [[ -z "$nodes_versions" ]] && error "No nodes found in the cluster" local current_version - current_version=$(echo "$nodes_versions" | head -1) + current_version=$(echo "$nodes_versions" | tr ' ' '\n' | sort -u | head -1) [[ $current_version != "v$KUBERNETES_VERSION" ]] || error "Cluster already at version $KUBERNETES_VERSION" From 6cf90bfe1dec8d7de5bd06c4f8e335189bed101c Mon Sep 17 00:00:00 2001 From: Cezary Stanislawski Date: Fri, 25 Oct 2024 19:25:27 +0200 Subject: [PATCH 4/4] add repo add on upgrade --- kubelift.sh | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/kubelift.sh b/kubelift.sh index 1551693..317030e 100755 --- a/kubelift.sh +++ b/kubelift.sh @@ -394,6 +394,18 @@ function upgrade_node_components() { ssh -o StrictHostKeyChecking=no "$SSH_USER@$node_ip" bash << EOF set -euo pipefail +KUBERNETES_VERSION="$KUBERNETES_VERSION" +KUBERNETES_VERSION_REPOSITORY="v\${KUBERNETES_VERSION%.*}" + +install -m 0755 -d /etc/apt/keyrings +curl -fsSL "https://pkgs.k8s.io/core:/stable:/\$KUBERNETES_VERSION_REPOSITORY/deb/Release.key" | \ + gpg --dearmor --yes -o "/etc/apt/keyrings/kubernetes-apt-keyring-\$KUBERNETES_VERSION_REPOSITORY.gpg" + +echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring-\$KUBERNETES_VERSION_REPOSITORY.gpg] https://pkgs.k8s.io/core:/stable:/\$KUBERNETES_VERSION_REPOSITORY/deb/ /" | \ + tee /etc/apt/sources.list.d/kubernetes.list + +apt-get update + apt-mark unhold kubeadm && apt-get install -y kubeadm=$KUBERNETES_VERSION-* && apt-mark hold kubeadm if $is_control_plane; then