workers.tf 4.2 KB

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