zhong (钟鹏群) 1 miesiąc temu
rodzic
commit
2c05c535fe
1 zmienionych plików z 28 dodań i 88 usunięć
  1. 28 88
      terraform/tfs/workers.tf

+ 28 - 88
terraform/tfs/workers.tf

@@ -1,33 +1,12 @@
-resource "null_resource" "k3s_cleanup_worker1" {
-  # SSH 登录到 worker 节点
-  connection {
-    type        = "ssh"
-    host        = var.worker_ips[0]
-    user        = "root"
-    password    = var.worker_password
-  }
-
-  provisioner "remote-exec" {
-    inline = [
-      "# Stop k3s-agent service if running",
-      "systemctl stop k3s-agent 2>/dev/null || true",
-      "# Kill any remaining k3s processes",
-      "pkill -f k3s 2>/dev/null || true",
-      "# Remove k3s data directory",
-      "rm -rf /var/lib/rancher/k3s",
-      "# Remove k3s-agent service file",
-      "rm -f /etc/systemd/system/k3s-agent.service",
-      "echo 'Cleanup completed on worker node 1'"
-    ]
-  }
-}
+# 循环清理所有 worker 节点
+resource "null_resource" "k3s_cleanup_worker" {
+  count = length(var.worker_ips)
 
-resource "null_resource" "k3s_cleanup_worker2" {
   connection {
-    type        = "ssh"
-    host        = var.worker_ips[1]
-    user        = "root"
-    password    = var.worker_password
+    type     = "ssh"
+    host     = var.worker_ips[count.index]
+    user     = "root"
+    password = var.worker_password
   }
 
   provisioner "remote-exec" {
@@ -40,19 +19,22 @@ resource "null_resource" "k3s_cleanup_worker2" {
       "rm -rf /var/lib/rancher/k3s",
       "# Remove k3s-agent service file",
       "rm -f /etc/systemd/system/k3s-agent.service",
-      "echo 'Cleanup completed on worker node 2'"
+      "systemctl daemon-reload 2>/dev/null || true",
+      "echo 'Cleanup completed on worker node ${count.index + 1}'"
     ]
   }
 }
 
-resource "null_resource" "k3s_install_worker1" {
-  depends_on = [null_resource.copy_token_to_workers, null_resource.k3s_cleanup_worker1]
+# 循环安装所有 worker 节点(修复了多行字符串问题)
+resource "null_resource" "k3s_install_worker" {
+  count      = length(var.worker_ips)
+  depends_on = [null_resource.copy_token_to_workers, null_resource.k3s_cleanup_worker]
 
   connection {
-    type        = "ssh"
-    host        = var.worker_ips[0]
-    user        = "root"
-    password    = var.worker_password
+    type     = "ssh"
+    host     = var.worker_ips[count.index]
+    user     = "root"
+    password = var.worker_password
   }
 
   provisioner "remote-exec" {
@@ -65,64 +47,18 @@ resource "null_resource" "k3s_install_worker1" {
       "fi",
       "TOKEN=$(cat /root/node-token)",
       "echo -e '\\033[32m--Install k3s-agent systemd service--\\033[0m'",
-      # 注册 systemd 服务
-      "echo '[Unit]
+      # 关键修复:使用 heredoc 写入多行 systemd 配置
+      <<EOT
+cat > /etc/systemd/system/k3s-agent.service <<'EOF'
+[Unit]
 Description=Lightweight Kubernetes
 Documentation=https://k3s.io
 After=network-online.target
 Wants=network-online.target
-[Service]
-Type=notify
-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
-KillMode=process
-Delegate=yes
-LimitNOFILE=1048576
-LimitNPROC=infinity
-LimitCORE=infinity
-TasksMax=infinity
-TimeoutStartSec=0
-Restart=always
-RestartSec=5s
-[Install]
-WantedBy=multi-user.target' > /etc/systemd/system/k3s-agent.service",
-      "systemctl daemon-reload",
-      "systemctl enable --now k3s-agent",
-      "echo -e '\\033[32m--k3s agent started successfully--\\033[0m'",
-      "sleep 3",
-    ]
-  }
-}
 
-
-resource "null_resource" "k3s_install_worker2" {
-  depends_on = [null_resource.copy_token_to_workers, null_resource.k3s_cleanup_worker2]
-
-  connection {
-    type        = "ssh"
-    host        = var.worker_ips[1]
-    user        = "root"
-    password    = var.worker_password
-  }
-
-  provisioner "remote-exec" {
-    inline = [
-      "if [ -f /usr/local/bin/k3s ]; then",
-      "  echo 'k3s binary already exists, skipping download'",
-      "else",
-      "  wget -O /usr/local/bin/k3s ${var.k3s_download_url}",
-      "  chmod +x /usr/local/bin/k3s",
-      "fi",
-      "TOKEN=$(cat /root/node-token)",
-      "echo -e '\\033[32m--Install k3s-agent systemd service--\\033[0m'",
-      # 注册 systemd 服务
-      "echo '[Unit]
-Description=Lightweight Kubernetes
-Documentation=https://k3s.io
-After=network-online.target
-Wants=network-online.target
 [Service]
 Type=notify
-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
+ExecStart=/usr/local/bin/k3s agent --server https://${var.master_ip}:6443 --token $TOKEN --node-name worker-node-${replace(var.worker_ips[count.index], ".", "-")} --node-external-ip=${var.worker_ips[count.index]} --data-dir /var/lib/rancher/k3s
 KillMode=process
 Delegate=yes
 LimitNOFILE=1048576
@@ -132,12 +68,16 @@ TasksMax=infinity
 TimeoutStartSec=0
 Restart=always
 RestartSec=5s
+
 [Install]
-WantedBy=multi-user.target' > /etc/systemd/system/k3s-agent.service",
+WantedBy=multi-user.target
+EOF
+EOT
+      ,
       "systemctl daemon-reload",
       "systemctl enable --now k3s-agent",
       "echo -e '\\033[32m--k3s agent started successfully--\\033[0m'",
       "sleep 3",
     ]
   }
-}
+}