Skip to content

Instantly share code, notes, and snippets.

@Dagimal
Created July 16, 2025 13:41
Show Gist options
  • Select an option

  • Save Dagimal/941dfb7a69ce615333fbef440a678334 to your computer and use it in GitHub Desktop.

Select an option

Save Dagimal/941dfb7a69ce615333fbef440a678334 to your computer and use it in GitHub Desktop.
Deploy k8s via jumphost
#!/bin/bash
# Script untuk deploy kubernetes units ke multiple servers
# Mengambil daftar unit dari nama file YAML di direktori /root/listunit
# Format file unit: IPADDRESS:KODEUNIT
UNIT_FILE="./deploy/unit"
YAML_DIR="./kubernetes"
# SSH options untuk mencegah hang
SSH_OPTS="-o ConnectTimeout=10 -o BatchMode=yes -o StrictHostKeyChecking=no"
SCP_OPTS="-o ConnectTimeout=10 -o BatchMode=yes -o StrictHostKeyChecking=no"
# Cek apakah file unit ada
if [ ! -f "$UNIT_FILE" ]; then
echo "Error: File '$UNIT_FILE' tidak ditemukan!"
exit 1
fi
# Cek apakah direktori YAML ada
if [ ! -d "$YAML_DIR" ]; then
echo "Error: Direktori '$YAML_DIR' tidak ditemukan!"
exit 1
fi
# Counter untuk tracking
total_lines=0
processed_lines=0
# Baca file unit dan proses setiap baris dengan while loop yang robust
while IFS= read -r line || [[ -n "$line" ]]; do
total_lines=$((total_lines + 1))
# Skip baris kosong atau yang dimulai dengan #
if [[ -z "$line" || "$line" =~ ^[[:space:]]*# ]]; then
echo "Skipping empty or comment line: $line"
continue
fi
# Parse IP dan KODE_UNIT
IP_ADDRESS=$(echo "$line" | cut -d':' -f1 | xargs)
KODE_UNIT=$(echo "$line" | cut -d':' -f2 | xargs)
# Validasi format
if [[ -z "$IP_ADDRESS" || -z "$KODE_UNIT" ]]; then
echo "Warning: Format tidak valid untuk baris '$line', dilewati..."
continue
fi
# Cek apakah file YAML ada di direktori listunit
YAML_FILE="$YAML_DIR/${KODE_UNIT}.yaml"
if [ ! -f "$YAML_FILE" ]; then
echo "Warning: File '$YAML_FILE' tidak ditemukan, dilewati..."
continue
fi
processed_lines=$((processed_lines + 1))
echo "========================================"
echo "Processing $processed_lines/$total_lines: $IP_ADDRESS -> $KODE_UNIT"
echo "========================================"
# Test koneksi dulu
echo "Testing connection to $IP_ADDRESS..."
if timeout 5 ssh $SSH_OPTS "root@${IP_ADDRESS}" "echo 'Connection OK'" < /dev/null; then
echo "✓ Connection test OK"
else
echo "✗ Connection test failed, skipping $IP_ADDRESS"
continue
fi
# SCP file yaml dari /root/listunit ke server tujuan
echo "Copying $YAML_FILE to $IP_ADDRESS:/root/"
if timeout 30 scp $SCP_OPTS "$YAML_FILE" "root@${IP_ADDRESS}:/root/"; then
echo "✓ File berhasil dicopy ke $IP_ADDRESS"
# Jalankan kubectl replace via SSH dengan timeout
echo "Executing kubectl replace pada $IP_ADDRESS..."
if timeout 60 ssh $SSH_OPTS "root@${IP_ADDRESS}" "kubectl replace --force -f /root/${KODE_UNIT}.yaml" < /dev/null; then
echo "✓ kubectl replace berhasil untuk $KODE_UNIT di $IP_ADDRESS"
else
echo "✗ kubectl replace gagal atau timeout untuk $KODE_UNIT di $IP_ADDRESS"
fi
else
echo "✗ Gagal copy file atau timeout ke $IP_ADDRESS, skip kubectl replace"
fi
echo ""
echo "Continuing to next server..."
echo ""
done < "$UNIT_FILE"
echo "========================================"
echo "Deployment selesai!"
echo "Total lines: $total_lines, Processed: $processed_lines"
echo "========================================"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment