workers.tf 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. resource "null_resource" "k3s_cleanup_worker1" {
  2. # SSH 登录到 worker 节点
  3. connection {
  4. type = "ssh"
  5. host = var.worker_ips[0]
  6. user = "root"
  7. password = var.worker_password
  8. }
  9. provisioner "remote-exec" {
  10. inline = [
  11. "# Stop k3s-agent service if running",
  12. "systemctl stop k3s-agent 2>/dev/null || true",
  13. "# Kill any remaining k3s processes",
  14. "pkill -f k3s 2>/dev/null || true",
  15. "# Remove k3s data directory",
  16. "rm -rf /var/lib/rancher/k3s",
  17. "# Remove k3s-agent service file",
  18. "rm -f /etc/systemd/system/k3s-agent.service",
  19. "echo 'Cleanup completed on worker node 1'"
  20. ]
  21. }
  22. }
  23. resource "null_resource" "k3s_cleanup_worker2" {
  24. connection {
  25. type = "ssh"
  26. host = var.worker_ips[1]
  27. user = "root"
  28. password = var.worker_password
  29. }
  30. provisioner "remote-exec" {
  31. inline = [
  32. "# Stop k3s-agent service if running",
  33. "systemctl stop k3s-agent 2>/dev/null || true",
  34. "# Kill any remaining k3s processes",
  35. "pkill -f k3s 2>/dev/null || true",
  36. "# Remove k3s data directory",
  37. "rm -rf /var/lib/rancher/k3s",
  38. "# Remove k3s-agent service file",
  39. "rm -f /etc/systemd/system/k3s-agent.service",
  40. "echo 'Cleanup completed on worker node 2'"
  41. ]
  42. }
  43. }
  44. resource "null_resource" "k3s_install_worker1" {
  45. depends_on = [null_resource.copy_token_to_workers, null_resource.k3s_cleanup_worker1]
  46. connection {
  47. type = "ssh"
  48. host = var.worker_ips[0]
  49. user = "root"
  50. password = var.worker_password
  51. }
  52. provisioner "remote-exec" {
  53. inline = [
  54. "if [ -f /usr/local/bin/k3s ]; then",
  55. " echo 'k3s binary already exists, skipping download'",
  56. "else",
  57. " wget -O /usr/local/bin/k3s ${var.k3s_download_url}",
  58. " chmod +x /usr/local/bin/k3s",
  59. "fi",
  60. "TOKEN=$(cat /root/node-token)",
  61. "echo -e '\\033[32m--Start k3s agent 1--\\033[0m'",
  62. "k3s agent --server https://${var.master_ip}:6443 --token $TOKEN --node-name worker-node-${replace(var.worker_ips[0], ".", "-")} --node-ip=${var.worker_ips[0]} --data-dir /var/lib/rancher/k3s",
  63. "echo -e '\\033[32m--Done k3s agent 1--\\033[0m'",
  64. "sleep 5",
  65. ]
  66. }
  67. }
  68. resource "null_resource" "k3s_install_worker2" {
  69. depends_on = [null_resource.copy_token_to_workers, null_resource.k3s_cleanup_worker2]
  70. connection {
  71. type = "ssh"
  72. host = var.worker_ips[1]
  73. user = "root"
  74. password = var.worker_password
  75. }
  76. provisioner "remote-exec" {
  77. inline = [
  78. "if [ -f /usr/local/bin/k3s ]; then",
  79. " echo 'k3s binary already exists, skipping download'",
  80. "else",
  81. " wget -O /usr/local/bin/k3s ${var.k3s_download_url}",
  82. " chmod +x /usr/local/bin/k3s",
  83. "fi",
  84. "TOKEN=$(cat /root/node-token)",
  85. "echo -e '\\033[32m--Start k3s agent 2--\\033[0m'",
  86. "k3s agent --server https://${var.master_ip}:6443 --token $TOKEN --node-name worker-node-${replace(var.worker_ips[1], ".", "-")} --node-ip=${var.worker_ips[1]} --data-dir /var/lib/rancher/k3s",
  87. "echo -e '\\033[32m--Done k3s agent 2--\\033[0m'",
  88. "sleep 5",
  89. ]
  90. }
  91. }