renew-k3s-agent-certs.sh 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/bin/bash
  2. set -e
  3. # Renew k3s agent certificates using `k3s certificate rotate` and restart k3s-agent.
  4. #
  5. # Usage:
  6. # ./renew-k3s-agent-certs.sh <agent_host> [ssh_user] [ssh_port]
  7. #
  8. # Examples:
  9. # ./renew-k3s-agent-certs.sh 10.0.0.10
  10. # ./renew-k3s-agent-certs.sh 10.0.0.10 root 22
  11. # ./renew-k3s-agent-certs.sh "10.0.0.10 10.0.0.11 10.0.0.12"
  12. AGENT_HOSTS="${1:?Usage: $0 <agent_host(s)> [ssh_user] [ssh_port]}"
  13. SSH_USER="${2:-root}"
  14. SSH_PORT="${3:-22}"
  15. SSH_OPTS="-o StrictHostKeyChecking=no -o ConnectTimeout=10 -p ${SSH_PORT}"
  16. renew_agent_certs() {
  17. local host="$1"
  18. echo "=========================================="
  19. echo "Processing k3s-agent on: ${host}"
  20. echo "=========================================="
  21. ssh ${SSH_OPTS} "${SSH_USER}@${host}" bash <<'REMOTE_SCRIPT'
  22. set -e
  23. echo "[1/3] Checking k3s-agent status..."
  24. if ! systemctl is-enabled k3s-agent &>/dev/null; then
  25. echo "ERROR: k3s-agent service not found on this host, skipping."
  26. exit 1
  27. fi
  28. echo "[2/4] Stopping k3s-agent..."
  29. systemctl stop k3s-agent
  30. echo "[3/4] Rotating k3s agent certificates..."
  31. k3s certificate rotate
  32. echo "[4/4] Starting k3s-agent..."
  33. systemctl start k3s-agent
  34. sleep 5
  35. if systemctl is-active k3s-agent &>/dev/null; then
  36. echo "SUCCESS: k3s-agent is running on $(hostname)"
  37. else
  38. echo "ERROR: k3s-agent failed to start. Check: journalctl -u k3s-agent -n 50"
  39. exit 1
  40. fi
  41. REMOTE_SCRIPT
  42. echo ""
  43. }
  44. # Process each host
  45. for host in ${AGENT_HOSTS}; do
  46. renew_agent_certs "${host}"
  47. done
  48. echo "All done."