From bb5add7a108d2fda189cc472e66e529c01e827a5 Mon Sep 17 00:00:00 2001 From: baschno Date: Wed, 1 Apr 2026 21:54:37 +0200 Subject: [PATCH] add k8s stop --- 01_Basic_Setup/justfile | 49 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/01_Basic_Setup/justfile b/01_Basic_Setup/justfile index 36cd887..86ad91e 100644 --- a/01_Basic_Setup/justfile +++ b/01_Basic_Setup/justfile @@ -10,6 +10,7 @@ export K3S_ENABLE_REGISTRY := env("K3S_ENABLE_REGISTRY", "true") export SERVER_IP := env("K3S_SERVER_IP","192.168.178.45") export AGENT_IP := env("K3S_AGENT_IP","192.168.178.75") export USER := env("K3S_USER","basti") +export LONGHORN_NAMESPACE := env("LONGHORN_NAMESPACE","longhorn-system") [private] default: @@ -145,4 +146,50 @@ configure-registry: echo "Restarting k3s to apply registry configuration..." ssh "${K8S_MASTER_NODE_NAME}" "sudo systemctl restart k3s" - echo "✓ Registry configuration applied" \ No newline at end of file + echo "✓ Registry configuration applied" + + +stop: + #!/bin/bash + set -euo pipefail + START_TIME=$(date +%s) + elapsed() { + echo "$(($(date +%s) - START_TIME))s" + } + nodenames=$(kubectl get nodes -o=jsonpath="{.items[*]['metadata.name']}") + # for node in ${nodenames}; do + # kubectl drain "${node}" --ignore-daemonsets --delete-emptydir-data --force --disable-eviction --grace-period=60 --timeout=180s 2>&1 || true + # kubectl cordon "${node}" + # echo "Node ${node} stopped." + # done + # echo "Drain complete. Nodes are cordoned and drained." + + # if helm status longhorn -n ${LONGHORN_NAMESPACE} &>/dev/null; then + # echo "[$(elapsed)] Waiting for Longhorn volumes to be detached..." + # TIMEOUT=90 + # ELAPSED=0 + # while [ $ELAPSED -lt $TIMEOUT ]; do + + # ATTACHED=$(kubectl get volumes.longhorn.io -n ${LONGHORN_NAMESPACE} -o json 2>/dev/null | \ + # jq -r '.items[] | select(.status.state == "attached") | .metadata.name' 2>/dev/null || true) + + # if [ -z "$ATTACHED" ]; then + # echo "[$(elapsed)] ✓ All Longhorn volumes detached successfully" + # break + # fi + + # ATTACHED_COUNT=$(echo "$ATTACHED" | grep -c . || echo 0) + # echo " Still waiting for $ATTACHED_COUNT volume(s) to detach..." + # sleep 2 + # ELAPSED=$((ELAPSED + 2)) + # done + # if [ $ELAPSED -ge $TIMEOUT ]; then + # echo "[$(elapsed)] ⚠ Warning: Timeout waiting for volumes to detach" + # fi + # fi + + for node in ${nodenames}; do + echo "[$(elapsed)] Stopping and disabling k3s service..." + ssh "${node}" "sudo systemctl stop k3s 2>/dev/null || true" + ssh "${node}" "sudo systemctl disable k3s 2>/dev/null || true" + done \ No newline at end of file