| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- 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-external-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-external-ip=${var.worker_ips[1]} --data-dir /var/lib/rancher/k3s",
- "echo -e '\\033[32m--Done k3s agent 2--\\033[0m'",
- "sleep 5",
- ]
- }
- }
|