feat(deploy): add scripts for k3d installation and SSH tunnel setup

This commit is contained in:
Janis Eccarius
2026-05-30 14:13:58 +02:00
parent dcdcaff3b0
commit cb28dc88b1
4 changed files with 9215 additions and 6 deletions
+9 -6
View File
@@ -17,13 +17,16 @@ generate_flux_manifests() {
echo "----------------------------------------"
GOTK="$REPO_ROOT/htwg-1/flux-system/gotk-components.yaml"
if [[ -f "$GOTK" ]]; then
echo "✅ gotk-components.yaml already exists, skipping generation."
if [[ -s "$GOTK" ]]; then
echo "✅ gotk-components.yaml already exists, skipping download."
else
echo "🚀 Generating FluxCD controller manifests..."
flux install --export > "$GOTK"
echo "✅ Generated $GOTK"
echo "⚠️ Commit this file to the repository before continuing."
echo "🚀 Downloading FluxCD controller manifests..."
FLUX_VERSION=$(curl -Ls https://api.github.com/repos/fluxcd/flux2/releases/latest \
| grep '"tag_name"' | cut -d'"' -f4)
curl -Lo "$GOTK" \
"https://github.com/fluxcd/flux2/releases/download/${FLUX_VERSION}/install.yaml"
echo "✅ Downloaded FluxCD ${FLUX_VERSION}$GOTK"
echo "⚠️ Commit this file to the repository."
read -rp "Press Enter after committing gotk-components.yaml..."
fi
}
+92
View File
@@ -0,0 +1,92 @@
#!/usr/bin/bash
set -euo pipefail
CLUSTER_NAME="htwg-1"
BIN_DIR="${HOME}/.local/bin"
mkdir -p "$BIN_DIR"
# Ensure BIN_DIR is on PATH for this session
export PATH="${BIN_DIR}:${PATH}"
# ----
install_k3d() {
if command -v k3d &>/dev/null; then
echo "✅ k3d already installed: $(k3d version | head -1)"
return
fi
echo "🚀 Installing k3d to ${BIN_DIR}..."
curl -s https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | K3D_INSTALL_DIR="${BIN_DIR}" USE_SUDO=false bash
echo "✅ k3d installed: $(k3d version | head -1)"
}
# ----
install_kubectl() {
if command -v kubectl &>/dev/null; then
echo "✅ kubectl already installed: $(kubectl version --client 2>/dev/null | head -1)"
return
fi
echo "🚀 Installing kubectl to ${BIN_DIR}..."
KUBECTL_VERSION=$(curl -Ls https://dl.k8s.io/release/stable.txt)
curl -Lo "${BIN_DIR}/kubectl" "https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl"
chmod +x "${BIN_DIR}/kubectl"
echo "✅ kubectl installed: ${KUBECTL_VERSION}"
}
# ----
install_flux_cli() {
if command -v flux &>/dev/null; then
echo "✅ flux CLI already installed: $(flux version --client 2>/dev/null | head -1)"
return
fi
echo "🚀 Installing flux CLI to ${BIN_DIR}..."
FLUX_VERSION=$(curl -Ls https://api.github.com/repos/fluxcd/flux2/releases/latest | grep '"tag_name"' | cut -d'"' -f4 | tr -d v)
curl -Lo /tmp/flux.tar.gz "https://github.com/fluxcd/flux2/releases/download/v${FLUX_VERSION}/flux_${FLUX_VERSION}_linux_amd64.tar.gz"
tar -xzf /tmp/flux.tar.gz -C "${BIN_DIR}" flux
rm /tmp/flux.tar.gz
echo "✅ flux CLI installed: v${FLUX_VERSION}"
}
# ----
create_cluster() {
if k3d cluster list | grep -q "^${CLUSTER_NAME}"; then
echo "✅ Cluster '${CLUSTER_NAME}' already exists."
return
fi
echo "🚀 Creating k3d cluster '${CLUSTER_NAME}'..."
k3d cluster create "${CLUSTER_NAME}" \
--servers 1 \
--api-port 6443 \
--port "80:80@loadbalancer" \
--port "443:443@loadbalancer" \
--k3s-arg "--disable=traefik@server:0" \
--k3s-arg "--disable=servicelb@server:0"
echo "✅ Cluster '${CLUSTER_NAME}' created!"
}
# ----
install_k3d
install_kubectl
install_flux_cli
create_cluster
echo ""
echo "----------------------------------------"
echo " 🎉 k3d cluster '${CLUSTER_NAME}' ready!"
echo " 🎉 kubeconfig merged — context: k3d-${CLUSTER_NAME}"
echo "----------------------------------------"
echo ""
echo "⚠️ Add ${BIN_DIR} to your PATH permanently if not already set:"
echo " echo 'export PATH=\"\${HOME}/.local/bin:\${PATH}\"' >> ~/.bashrc"
echo ""
echo "Next step: run deploy-to-cluster-htwg-1.sh"
+63
View File
@@ -0,0 +1,63 @@
#!/usr/bin/bash
set -euo pipefail
REMOTE_HOST="chess@141.37.74.142"
REMOTE_API_PORT="6443"
LOCAL_PORT="6443"
KUBECONFIG_FILE="${HOME}/.kube/htwg-1.yaml"
# ----
fetch_kubeconfig() {
echo "📥 Fetching kubeconfig from remote..."
mkdir -p "$(dirname "$KUBECONFIG_FILE")"
ssh "$REMOTE_HOST" "~/.local/bin/k3d kubeconfig get htwg-1" \
| sed "s|server: https://0.0.0.0:${REMOTE_API_PORT}|server: https://127.0.0.1:${LOCAL_PORT}|" \
> "$KUBECONFIG_FILE"
chmod 600 "$KUBECONFIG_FILE"
echo "✅ Kubeconfig saved to ${KUBECONFIG_FILE}"
}
# ----
cleanup() {
echo ""
echo "🔌 Closing tunnel..."
kill "$SSH_PID" 2>/dev/null || true
exit 0
}
# ----
# Re-fetch kubeconfig if flag passed or file missing
if [[ "${1:-}" == "--refresh" ]] || [[ ! -f "$KUBECONFIG_FILE" ]]; then
fetch_kubeconfig
fi
export KUBECONFIG="$KUBECONFIG_FILE"
echo "🔗 Starting SSH tunnel: localhost:${LOCAL_PORT}${REMOTE_HOST}:${REMOTE_API_PORT}"
ssh -N -L "${LOCAL_PORT}:localhost:${REMOTE_API_PORT}" "$REMOTE_HOST" &
SSH_PID=$!
trap cleanup INT TERM
# Wait for tunnel to be ready
sleep 1
if ! kill -0 "$SSH_PID" 2>/dev/null; then
echo "❌ SSH tunnel failed to start."
exit 1
fi
echo "✅ Tunnel active (PID ${SSH_PID})"
echo ""
echo "Use in another terminal:"
echo " export KUBECONFIG=${KUBECONFIG_FILE}"
echo " kubectl get nodes"
echo ""
echo "Or source this file:"
echo " source <(echo 'export KUBECONFIG=${KUBECONFIG_FILE}')"
echo ""
echo "Press Ctrl+C to close tunnel."
wait "$SSH_PID"