| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- 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--Install k3s-agent systemd service--\\033[0m'",
- # 注册 systemd 服务
- "cat > /etc/systemd/system/k3s-agent.service <<EOF",
- "[Unit]",
- "Description=K3s Agent",
- "After=network.target",
- "[Service]",
- "ExecStart=/usr/local/bin/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",
- "Restart=always",
- "KillMode=process",
- "Delegate=yes",
- "LimitNOFILE=65536",
- "[Install]",
- "WantedBy=multi-user.target",
- "EOF",
- "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 服务
- "cat > /etc/systemd/system/k3s-agent.service <<EOF",
- "[Unit]",
- "Description=K3s Agent",
- "After=network.target",
- "[Service]",
- "ExecStart=/usr/local/bin/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[1]} --data-dir /var/lib/rancher/k3s",
- "Restart=always",
- "KillMode=process",
- "Delegate=yes",
- "LimitNOFILE=65536",
- "[Install]",
- "WantedBy=multi-user.target",
- "EOF",
- "systemctl daemon-reload",
- "systemctl enable --now k3s-agent",
- "echo -e '\\033[32m--k3s agent started successfully--\\033[0m'",
- "sleep 3",
- ]
- }
- }
|