From 5abc0de38ad215619f4054593d60c6afa5bceca6 Mon Sep 17 00:00:00 2001 From: baschno Date: Mon, 22 Dec 2025 11:21:20 +0100 Subject: [PATCH 1/3] add just and mise tool support --- .gitignore | 1 + 01_Basic_Setup/README.md | 23 ++++++- 01_Basic_Setup/justfile | 134 +++++++++++++++++++++++++++++++++++++++ env/env.local.gomplate | 4 ++ env/justfile | 69 ++++++++++++++++++++ justfile | 10 +++ mise.toml | 7 ++ 7 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 01_Basic_Setup/justfile create mode 100644 env/env.local.gomplate create mode 100644 env/justfile create mode 100644 justfile create mode 100644 mise.toml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..11ee758 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env.local diff --git a/01_Basic_Setup/README.md b/01_Basic_Setup/README.md index 7f36d1f..8d30a6e 100644 --- a/01_Basic_Setup/README.md +++ b/01_Basic_Setup/README.md @@ -39,4 +39,25 @@ Rancher Installation ``` kubectl taint nodes master node-role.kubernetes.io/master=:NoSchedule -``` \ No newline at end of file +``` + +# Just Setup // K3sup + +export SERVER_IP=192.168.178.45 +export AGENT_IP=192.168.178.75 +export USER=basti + + +k3sup install \ + --cluster \ + --ip 192.168.178.45 \ + --user $USER \ + --merge \ + --local-path $HOME/.kube/config \ + --context my-k3s + +k3sup join \ + --ip $AGENT_IP \ + --server-ip $SERVER_IP \ + --user $USER + diff --git a/01_Basic_Setup/justfile b/01_Basic_Setup/justfile new file mode 100644 index 0000000..4f154bd --- /dev/null +++ b/01_Basic_Setup/justfile @@ -0,0 +1,134 @@ +set fallback := true + +export LOCAL_K8S_HOST := env("LOCAL_K8S_HOST", "") +export EXTERNAL_K8S_HOST := env("EXTERNAL_K8S_HOST", "") +export KEYCLOAK_HOST := env("KEYCLOAK_HOST", "") +export KEYCLOAK_REALM := env("KEYCLOAK_REALM", "buunstack") +export K8S_OIDC_CLIENT_ID := env('K8S_OIDC_CLIENT_ID', "k8s") +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") + +[private] +default: + @just --list --unsorted --list-submodules + +install: + #!/bin/bash + set -euo pipefail + just env::check + username=$(gum input --prompt="SSH username: " --value="${USER}" --width=100) + kubeconfig="" + context="" + if gum confirm "Update KUBECONFIG?"; then + kubeconfig=$( + gum input --prompt="KUBECONFIG file: " --value="${HOME}/.kube/config" --width=100 + ) + context=$( + gum input --prompt="Context name: " --value="${LOCAL_K8S_HOST}" --width=100 + ) + fi + + args=( + "install" + "--host" "${LOCAL_K8S_HOST}" + "--ip" "${SERVER_IP}" + "--tls-san" "${EXTERNAL_K8S_HOST}" + "--user" "${username}" + ) + if [ -n "${context}" ]; then + args+=("--context" "${context}") + fi + + if [ -n "${kubeconfig}" ]; then + mkdir -p "$(dirname "${kubeconfig}")" + args+=("--local-path" "${kubeconfig}" "--merge") + fi + echo "Running: k3sup ${args[*]}" + k3sup "${args[@]}" + + if [ -n "${context}" ]; then + kubectl config use-context "${context}" + fi + + if [ "${K3S_ENABLE_REGISTRY}" = "true" ]; then + echo "Setting up local Docker registry..." + + # Deploy Docker registry to cluster + kubectl apply -f ./registry/registry.yaml + + # Set Pod Security Standard for registry namespace + kubectl label namespace registry pod-security.kubernetes.io/enforce=restricted --overwrite + + # Wait for registry deployment + echo "Waiting for registry to be ready..." + kubectl wait --for=condition=available --timeout=60s deployment/registry -n registry + + # Configure registries.yaml for k3s + just configure-registry + + echo "✓ Local Docker registry deployed and configured" + echo "" + echo "Registry accessible at:" + echo " localhost:30500" + echo "" + echo "Usage:" + echo " export DOCKER_HOST=ssh://${LOCAL_K8S_HOST}" + echo " docker build -t localhost:30500/myapp:latest ." + echo " docker push localhost:30500/myapp:latest" + echo " kubectl run myapp --image=localhost:30500/myapp:latest" + fi + + echo "k3s cluster installed on ${LOCAL_K8S_HOST}." + + +uninstall: + #!/bin/bash + set -euo pipefail + if gum confirm "Uninstall k3s from ${LOCAL_K8S_HOST}?"; then + + echo "Removing content of Agent node..." + ssh "${AGENT_IP}" "/usr/local/bin/k3s-agent-uninstall.sh" + + echo "Removing content of Server node..." + ssh "${SERVER_IP}" "/usr/local/bin/k3s-uninstall.sh" + echo "Cleaning up kubeconfig entries..." + cluster_name=$(kubectl config view -o json | jq -r ".contexts[] | select(.name == \"${LOCAL_K8S_HOST}\") | .context.cluster // empty") + user_name=$(kubectl config view -o json | jq -r ".contexts[] | select(.name == \"${LOCAL_K8S_HOST}\") | .context.user // empty") + if kubectl config get-contexts "${LOCAL_K8S_HOST}" &>/dev/null; then + kubectl config delete-context "${LOCAL_K8S_HOST}" + echo "Deleted context: ${LOCAL_K8S_HOST}" + fi + if [ -n "${cluster_name}" ] && kubectl config get-clusters | grep -q "^${cluster_name}$"; then + kubectl config delete-cluster "${cluster_name}" + echo "Deleted cluster: ${cluster_name}" + fi + if [ -n "${user_name}" ] && kubectl config get-users | grep -q "^${user_name}$"; then + kubectl config delete-user "${user_name}" + echo "Deleted user: ${user_name}" + fi + echo "k3s cluster uninstalled from ${LOCAL_K8S_HOST}." + else + echo "Uninstallation cancelled." >&2 + exit 1 + fi + +add-agent: + #!/bin/bash + set -euo pipefail + just env::check + + username=$(gum input --prompt="SSH username: " --value="${USER}" --width=100) + new_agent_ip=$(gum input --prompt="Agent IP to join cluster: " --value="${AGENT_IP}" --width=100) + + args=( + "join" + "--ip" "${new_agent_ip}" + "--server-ip" "${SERVER_IP}" + "--user" "${username}" + ) + + + echo "Running: k3sup ${args[*]}" + diff --git a/env/env.local.gomplate b/env/env.local.gomplate new file mode 100644 index 0000000..75d6f33 --- /dev/null +++ b/env/env.local.gomplate @@ -0,0 +1,4 @@ +# shellcheck disable=all +LOCAL_K8S_HOST={{ .Env.LOCAL_K8S_HOST }} +SERVER_IP={{ .Env.SERVER_IP }} +AGENT_IP={{ .Env.AGENT_IP }} diff --git a/env/justfile b/env/justfile new file mode 100644 index 0000000..2e047c1 --- /dev/null +++ b/env/justfile @@ -0,0 +1,69 @@ +set fallback := true + +export ENV_FILE := ".env.local" +export LOCAL_K8S_HOST := env("LOCAL_K8S_HOST", "") +export SERVER_IP := env("SERVER_IP", "") +export AGENT_IP := env("AGENT_IP", "") + +check: + #!/bin/bash + set -euo pipefail + if [ -z "${LOCAL_K8S_HOST}" ]; then + echo "LOCAL_K8S_HOST is not set. Please execute 'just env::setup'" >&2 + exit 1 + fi + if [ -z "${SERVER_IP}" ]; then + echo "SERVER_IP is not set. Please execute 'just env::setup'" >&2 + exit 1 + fi + if [ -z "${AGENT_IP}" ]; then + echo "AGENT_IP is not set. Please execute 'just env::setup'" >&2 + exit 1 + fi + +setup: + #!/bin/bash + set -euo pipefail + if [ -f ../.env.local ]; then + echo ".env.local already exists." >&2 + if gum confirm "Do you want to overwrite it?"; then + LOCAL_K8S_HOST="" + SERVER_IP="" + AGENT_IP="" + elif [[ $? -eq 130 ]]; then + echo "Setup cancelled by user." >&2 + exit 1 + else + echo "Aborting setup." >&2 + exit 1 + fi + fi + while [ -z "${LOCAL_K8S_HOST}" ]; do + if ! LOCAL_K8S_HOST=$( + gum input --prompt="Internal k8s hostname (for SSH): " \ + --width=100 --placeholder="k8s-host" + ); then + echo "Setup cancelled." >&2 + exit 1 + fi + done + while [ -z "${SERVER_IP}" ]; do + if ! SERVER_IP=$( + gum input --prompt="IP of Server/Master Node: " \ + --width=100 --placeholder="Master Node IP" + ); then + echo "Setup cancelled." >&2 + exit 1 + fi + done + while [ -z "${AGENT_IP}" ]; do + if ! AGENT_IP=$( + gum input --prompt="IP of Agent Node: " \ + --width=100 --placeholder="Agent Node IP" + ); then + echo "Setup cancelled." >&2 + exit 1 + fi + done + rm -f ../.env.local + gomplate -f env.local.gomplate -o ../.env.local diff --git a/justfile b/justfile new file mode 100644 index 0000000..1d44fd2 --- /dev/null +++ b/justfile @@ -0,0 +1,10 @@ +set dotenv-filename := ".env.local" + +export PATH := "./node_modules/.bin:" + env_var('PATH') + +[private] +default: + @just --list --unsorted --list-submodules + +mod env +mod BasicSetup '01_Basic_Setup' \ No newline at end of file diff --git a/mise.toml b/mise.toml new file mode 100644 index 0000000..8e6e4e1 --- /dev/null +++ b/mise.toml @@ -0,0 +1,7 @@ +[tools] +jq = '1.8.1' +k3sup = '0.13.11' +helm = '3.19.0' +gum = '0.16.2' +gomplate = '4.3.3' +just = "1.42.4" \ No newline at end of file From 7a54346331a3f618cab75204b99860b51a2ee5b0 Mon Sep 17 00:00:00 2001 From: baschno Date: Mon, 22 Dec 2025 20:15:48 +0100 Subject: [PATCH 2/3] add local container registry --- .../registry/registries.gomplate.yaml | 4 + 01_Basic_Setup/registry/registry.yaml | 109 ++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 01_Basic_Setup/registry/registries.gomplate.yaml create mode 100644 01_Basic_Setup/registry/registry.yaml diff --git a/01_Basic_Setup/registry/registries.gomplate.yaml b/01_Basic_Setup/registry/registries.gomplate.yaml new file mode 100644 index 0000000..3d70c5b --- /dev/null +++ b/01_Basic_Setup/registry/registries.gomplate.yaml @@ -0,0 +1,4 @@ +configs: + "localhost:30500": + tls: + insecure_skip_verify: true \ No newline at end of file diff --git a/01_Basic_Setup/registry/registry.yaml b/01_Basic_Setup/registry/registry.yaml new file mode 100644 index 0000000..a0e4bd5 --- /dev/null +++ b/01_Basic_Setup/registry/registry.yaml @@ -0,0 +1,109 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: registry +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: registry + namespace: registry + labels: + app: registry +spec: + replicas: 1 + selector: + matchLabels: + app: registry + template: + metadata: + labels: + app: registry + spec: + securityContext: + runAsNonRoot: true + runAsUser: 65534 + fsGroup: 65534 + seccompProfile: + type: RuntimeDefault + containers: + - name: registry + image: registry:2 + ports: + - containerPort: 5000 + name: http + resources: + requests: + cpu: 25m + memory: 128Mi + limits: + cpu: 2000m + memory: 20Gi + env: + - name: REGISTRY_STORAGE_DELETE_ENABLED + value: "true" + - name: REGISTRY_HTTP_ADDR + value: "0.0.0.0:5000" + securityContext: + allowPrivilegeEscalation: false + readOnlyRootFilesystem: true + runAsNonRoot: true + runAsUser: 65534 + capabilities: + drop: + - ALL + volumeMounts: + - name: registry-data + mountPath: /var/lib/registry + - name: tmp + mountPath: /tmp + livenessProbe: + httpGet: + path: /v2/ + port: 5000 + initialDelaySeconds: 30 + periodSeconds: 10 + readinessProbe: + httpGet: + path: /v2/ + port: 5000 + initialDelaySeconds: 5 + periodSeconds: 5 + volumes: + - name: registry-data + emptyDir: {} + - name: tmp + emptyDir: {} +--- +apiVersion: v1 +kind: Service +metadata: + name: registry + namespace: registry + labels: + app: registry +spec: + selector: + app: registry + ports: + - port: 5000 + targetPort: 5000 + name: http + type: ClusterIP +--- +apiVersion: v1 +kind: Service +metadata: + name: registry-nodeport + namespace: registry + labels: + app: registry +spec: + selector: + app: registry + ports: + - port: 5000 + targetPort: 5000 + nodePort: 30500 + name: http + type: NodePort \ No newline at end of file From 4f5a18c84c8d2ac42ad145c891b3ef44dc26edf1 Mon Sep 17 00:00:00 2001 From: baschno Date: Mon, 22 Dec 2025 20:41:06 +0100 Subject: [PATCH 3/3] install incl agent ready --- 01_Basic_Setup/justfile | 43 +++++++++++++++++++++-------------------- env/env.local.gomplate | 3 ++- env/justfile | 30 ++++++++++++++++++++-------- 3 files changed, 46 insertions(+), 30 deletions(-) diff --git a/01_Basic_Setup/justfile b/01_Basic_Setup/justfile index 4f154bd..cd5536f 100644 --- a/01_Basic_Setup/justfile +++ b/01_Basic_Setup/justfile @@ -1,11 +1,12 @@ set fallback := true -export LOCAL_K8S_HOST := env("LOCAL_K8S_HOST", "") +export K8S_CONTEXT := env("K8S_CONTEXT", "") +export K8S_MASTER_NODE_NAME := env("K8S_MASTER_NODE_NAME", "") export EXTERNAL_K8S_HOST := env("EXTERNAL_K8S_HOST", "") export KEYCLOAK_HOST := env("KEYCLOAK_HOST", "") export KEYCLOAK_REALM := env("KEYCLOAK_REALM", "buunstack") export K8S_OIDC_CLIENT_ID := env('K8S_OIDC_CLIENT_ID', "k8s") -export K3S_ENABLE_REGISTRY := env("K3S_ENABLE_REGISTRY", "true") +export K3S_ENABLE_REGISTRY := env("K3S_ENABLE_REGISTRY", "false") 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") @@ -26,20 +27,16 @@ install: gum input --prompt="KUBECONFIG file: " --value="${HOME}/.kube/config" --width=100 ) context=$( - gum input --prompt="Context name: " --value="${LOCAL_K8S_HOST}" --width=100 + gum input --prompt="Context name: " --value="${K8S_CONTEXT}" --width=100 ) fi args=( "install" - "--host" "${LOCAL_K8S_HOST}" - "--ip" "${SERVER_IP}" - "--tls-san" "${EXTERNAL_K8S_HOST}" + "--context" "${K8S_CONTEXT}" + "--host" "${K8S_MASTER_NODE_NAME}" "--user" "${username}" ) - if [ -n "${context}" ]; then - args+=("--context" "${context}") - fi if [ -n "${kubeconfig}" ]; then mkdir -p "$(dirname "${kubeconfig}")" @@ -74,31 +71,33 @@ install: echo " localhost:30500" echo "" echo "Usage:" - echo " export DOCKER_HOST=ssh://${LOCAL_K8S_HOST}" + echo " export DOCKER_HOST=ssh://${K8S_MASTER_NODE_NAME}" echo " docker build -t localhost:30500/myapp:latest ." echo " docker push localhost:30500/myapp:latest" echo " kubectl run myapp --image=localhost:30500/myapp:latest" fi - echo "k3s cluster installed on ${LOCAL_K8S_HOST}." + echo "k3s cluster installed on ${K8S_MASTER_NODE_NAME}." uninstall: #!/bin/bash set -euo pipefail - if gum confirm "Uninstall k3s from ${LOCAL_K8S_HOST}?"; then + if gum confirm "Uninstall k3s from ${K8S_MASTER_NODE_NAME}?"; then - echo "Removing content of Agent node..." - ssh "${AGENT_IP}" "/usr/local/bin/k3s-agent-uninstall.sh" + if gum confirm "Also remove Agent node at ${AGENT_IP}?"; then + echo "Removing Agent node at ${AGENT_IP}..." + ssh "${AGENT_IP}" "/usr/local/bin/k3s-agent-uninstall.sh" + fi echo "Removing content of Server node..." - ssh "${SERVER_IP}" "/usr/local/bin/k3s-uninstall.sh" + ssh "${K8S_MASTER_NODE_NAME}" "/usr/local/bin/k3s-uninstall.sh" echo "Cleaning up kubeconfig entries..." - cluster_name=$(kubectl config view -o json | jq -r ".contexts[] | select(.name == \"${LOCAL_K8S_HOST}\") | .context.cluster // empty") - user_name=$(kubectl config view -o json | jq -r ".contexts[] | select(.name == \"${LOCAL_K8S_HOST}\") | .context.user // empty") - if kubectl config get-contexts "${LOCAL_K8S_HOST}" &>/dev/null; then - kubectl config delete-context "${LOCAL_K8S_HOST}" - echo "Deleted context: ${LOCAL_K8S_HOST}" + cluster_name=$(kubectl config view -o json | jq -r ".contexts[] | select(.name == \"${K8S_CONTEXT}\") | .context.cluster // empty") + user_name=$(kubectl config view -o json | jq -r ".contexts[] | select(.name == \"${K8S_CONTEXT}\") | .context.user // empty") + if kubectl config get-contexts "${K8S_CONTEXT}" &>/dev/null; then + kubectl config delete-context "${K8S_CONTEXT}" + echo "Deleted context: ${K8S_CONTEXT}" fi if [ -n "${cluster_name}" ] && kubectl config get-clusters | grep -q "^${cluster_name}$"; then kubectl config delete-cluster "${cluster_name}" @@ -108,7 +107,7 @@ uninstall: kubectl config delete-user "${user_name}" echo "Deleted user: ${user_name}" fi - echo "k3s cluster uninstalled from ${LOCAL_K8S_HOST}." + echo "k3s cluster uninstalled from ${K8S_CONTEXT}." else echo "Uninstallation cancelled." >&2 exit 1 @@ -131,4 +130,6 @@ add-agent: echo "Running: k3sup ${args[*]}" + k3sup "${args[@]}" + echo "Agent node at ${new_agent_ip} added to cluster." diff --git a/env/env.local.gomplate b/env/env.local.gomplate index 75d6f33..484931b 100644 --- a/env/env.local.gomplate +++ b/env/env.local.gomplate @@ -1,4 +1,5 @@ # shellcheck disable=all -LOCAL_K8S_HOST={{ .Env.LOCAL_K8S_HOST }} +K8S_CONTEXT={{ .Env.K8S_CONTEXT }} +K8S_MASTER_NODE_NAME={{ .Env.K8S_MASTER_NODE_NAME }} SERVER_IP={{ .Env.SERVER_IP }} AGENT_IP={{ .Env.AGENT_IP }} diff --git a/env/justfile b/env/justfile index 2e047c1..7365d75 100644 --- a/env/justfile +++ b/env/justfile @@ -1,15 +1,20 @@ set fallback := true export ENV_FILE := ".env.local" -export LOCAL_K8S_HOST := env("LOCAL_K8S_HOST", "") +export K8S_CONTEXT := env("K8S_CONTEXT", "") +export K8S_MASTER_NODE_NAME := env("K8S_MASTER_NODE_NAME", "") export SERVER_IP := env("SERVER_IP", "") export AGENT_IP := env("AGENT_IP", "") check: #!/bin/bash set -euo pipefail - if [ -z "${LOCAL_K8S_HOST}" ]; then - echo "LOCAL_K8S_HOST is not set. Please execute 'just env::setup'" >&2 + if [ -z "${K8S_CONTEXT}" ]; then + echo "K8S_CONTEXT is not set. Please execute 'just env::setup'" >&2 + exit 1 + fi + if [ -z "${K8S_MASTER_NODE_NAME}" ]; then + echo "K8S_MASTER_NODE_NAME is not set. Please execute 'just env::setup'" >&2 exit 1 fi if [ -z "${SERVER_IP}" ]; then @@ -27,7 +32,7 @@ setup: if [ -f ../.env.local ]; then echo ".env.local already exists." >&2 if gum confirm "Do you want to overwrite it?"; then - LOCAL_K8S_HOST="" + K8S_CONTEXT="" SERVER_IP="" AGENT_IP="" elif [[ $? -eq 130 ]]; then @@ -38,10 +43,19 @@ setup: exit 1 fi fi - while [ -z "${LOCAL_K8S_HOST}" ]; do - if ! LOCAL_K8S_HOST=$( - gum input --prompt="Internal k8s hostname (for SSH): " \ - --width=100 --placeholder="k8s-host" + while [ -z "${K8S_CONTEXT}" ]; do + if ! K8S_CONTEXT=$( + gum input --prompt="Context name: " \ + --width=100 --placeholder="context" + ); then + echo "Setup cancelled." >&2 + exit 1 + fi + done + while [ -z "${K8S_MASTER_NODE_NAME}" ]; do + if ! K8S_MASTER_NODE_NAME=$( + gum input --prompt="Master Node Hostname: " \ + --width=100 --placeholder="Master Node Name" ); then echo "Setup cancelled." >&2 exit 1