Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

install in readme + upgrade minor fix #4

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 46 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <bool> Enable or disable noninteractive mode
--ssh-user <username> Username to use for SSH connection
--kubernetes-version <version> Kubernetes version to install (create/upgrade only)
--control-plane-ip <ip> Control plane node IP address
--worker-ips <ip1,ip2,...> Worker node IP addresses (create only)
--enable-control-plane-workloads <bool> Enable control plane scheduling (create only)
--skip-reqs <bool> Skip minimum requirements validation
--nuke <bool> Perform deep cleanup (cleanup only)
```

### Cluster Creation

Expand Down Expand Up @@ -79,7 +110,8 @@ The upgrade operation will:
./kubelift.sh cleanup \
--noninteractive <true|false> \
--ssh-user <username> \
--control-plane-ip <ip>
--control-plane-ip <ip> \
--nuke <true|false> # Optional
```

The cleanup operation will:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
39 changes: 27 additions & 12 deletions kubelift.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <bool> Enable or disable noninteractive mode
--ssh-user <username> Username to use for SSH connection
--kubernetes-version <version> Kubernetes version to install (create/upgrade only)
--control-plane-ip <ip> Control plane node IP address
--worker-ips <ip1,ip2,...> Worker node IP addresses (create only)
--enable-control-plane-workloads <bool> Enable control plane scheduling (create only)
--skip-reqs <bool> Skip minimum requirements validation
--nuke <bool> Perform deep cleanup (cleanup only)
-h, --help Display this help message
--noninteractive <bool> Enable or disable noninteractive mode
--ssh-user <username> Username to use for SSH connection
--kubernetes-version <version> Kubernetes version to install (create/upgrade only)
--control-plane-ip <ip> Control plane node IP address
--worker-ips <ip1,ip2,...> Worker node IP addresses (create only)
--enable-control-plane-workloads <bool> Enable control plane scheduling (create only)
--skip-reqs <bool> Skip minimum requirements validation
--nuke <bool> Perform deep cleanup (cleanup only)
EOF
exit 0
}
Expand Down Expand Up @@ -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"

Expand All @@ -391,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
Expand Down