# 循环清理所有 worker 节点 resource "null_resource" "k3s_cleanup_worker" { count = length(var.worker_ips) connection { type = "ssh" host = var.worker_ips[count.index] 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", "systemctl daemon-reload 2>/dev/null || true", "echo 'Cleanup completed on worker node ${count.index + 1}'" ] } } # 循环安装所有 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[count.index] 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'", # 关键修复:使用 heredoc 写入多行 systemd 配置 < /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[count.index], ".", "-")} --node-external-ip=${var.worker_ips[count.index]} --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 EOF EOT , "systemctl daemon-reload", "systemctl enable --now k3s-agent", "echo -e '\\033[32m--k3s agent started successfully--\\033[0m'", "sleep 3", ] } }