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'" ] } } resource "null_resource" "k3s_cleanup_worker2" { connection { type = "ssh" host = var.worker_ips[1] 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 2'" ] } } resource "null_resource" "k3s_install_worker1" { depends_on = [null_resource.copy_token_to_workers, null_resource.k3s_cleanup_worker1] connection { type = "ssh" host = var.worker_ips[0] 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--Start k3s agent 1--\\033[0m'", "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", "echo -e '\\033[32m--Done k3s agent 1--\\033[0m'", "sleep 5", ] } } 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--Start k3s agent 2--\\033[0m'", "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", "echo -e '\\033[32m--Done k3s agent 2--\\033[0m'", "sleep 5", ] } }