workers.tf 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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--Install k3s-agent systemd service--\\033[0m'",
  62. # 注册 systemd 服务
  63. "cat > /etc/systemd/system/k3s-agent.service <<EOF",
  64. "[Unit]",
  65. "Description=K3s Agent",
  66. "After=network.target",
  67. "[Service]",
  68. "ExecStart=/usr/local/bin/k3s agent --server https://${var.master_ip}:6443 --token $TOKEN --node-name worker-node-${replace(var.worker_ips[0], \".\", \"-\")} --node-external-ip=${var.worker_ips[0]} --data-dir /var/lib/rancher/k3s",
  69. "Restart=always",
  70. "KillMode=process",
  71. "Delegate=yes",
  72. "LimitNOFILE=65536",
  73. "[Install]",
  74. "WantedBy=multi-user.target",
  75. "EOF",
  76. "systemctl daemon-reload",
  77. "systemctl enable --now k3s-agent",
  78. "echo -e '\\033[32m--k3s agent started successfully--\\033[0m'",
  79. "sleep 3",
  80. ]
  81. }
  82. }
  83. resource "null_resource" "k3s_install_worker2" {
  84. depends_on = [null_resource.copy_token_to_workers, null_resource.k3s_cleanup_worker2]
  85. connection {
  86. type = "ssh"
  87. host = var.worker_ips[1]
  88. user = "root"
  89. password = var.worker_password
  90. }
  91. provisioner "remote-exec" {
  92. inline = [
  93. "if [ -f /usr/local/bin/k3s ]; then",
  94. " echo 'k3s binary already exists, skipping download'",
  95. "else",
  96. " wget -O /usr/local/bin/k3s ${var.k3s_download_url}",
  97. " chmod +x /usr/local/bin/k3s",
  98. "fi",
  99. "TOKEN=$(cat /root/node-token)",
  100. "echo -e '\\033[32m--Install k3s-agent systemd service--\\033[0m'",
  101. # 注册 systemd 服务
  102. "cat > /etc/systemd/system/k3s-agent.service <<EOF",
  103. "[Unit]",
  104. "Description=K3s Agent",
  105. "After=network.target",
  106. "[Service]",
  107. "ExecStart=/usr/local/bin/k3s agent --server https://${var.master_ip}:6443 --token $TOKEN --node-name worker-node-${replace(var.worker_ips[0], \".\", \"-\")} --node-external-ip=${var.worker_ips[1]} --data-dir /var/lib/rancher/k3s",
  108. "Restart=always",
  109. "KillMode=process",
  110. "Delegate=yes",
  111. "LimitNOFILE=65536",
  112. "[Install]",
  113. "WantedBy=multi-user.target",
  114. "EOF",
  115. "systemctl daemon-reload",
  116. "systemctl enable --now k3s-agent",
  117. "echo -e '\\033[32m--k3s agent started successfully--\\033[0m'",
  118. "sleep 3",
  119. ]
  120. }
  121. }