terraform { required_providers { ssh = { source = "loafoe/ssh" version = "2.6.0" } kubernetes = { source = "hashicorp/kubernetes" version = "2.27.0" } helm = { source = "hashicorp/helm" version = "2.12.1" } kubectl = { source = "gavinbunney/kubectl" version = "1.14.0" } } } locals { ssh_host = "192.168.1.2" ssh_user = "junv" ssh_port = 22422 ssh_private_key = file("~/.ssh/id_ed25519") kubeconfig_cmd = "scp -P ${local.ssh_port} ${local.ssh_user}@${local.ssh_host}:/etc/rancher/k3s/k3s.yaml ~/.kube/config && sed -i '' 's/127.0.0.1/${local.ssh_host}/g' ~/.kube/config" token_cmd = "ssh -p ${local.ssh_port} ${local.ssh_user}@${local.ssh_host} 'sudo cat /var/lib/rancher/k3s/server/node-token'" } provider "kubernetes" { config_path = "~/.kube/config" } provider "helm" { kubernetes { config_path = "~/.kube/config" } } provider "kubectl" { config_path = "~/.kube/config" } resource "ssh_resource" "k3s_install" { host = local.ssh_host user = local.ssh_user port = local.ssh_port private_key = local.ssh_private_key timeout = "15m" when = "create" commands = [ "echo 'Installing k3s...'", "curl -sfL https://get.k3s.io > install.sh", "chmod +x install.sh", "INSTALL_K3S_EXEC='--tls-san ${local.ssh_host} --kube-apiserver-arg service-node-port-range=80-32767 --disable traefik' sudo -E ./install.sh", "echo 'Waiting for k3s to start (60s)...'", "sleep 60", "sudo chmod 644 /etc/rancher/k3s/k3s.yaml", "echo 'Checking node status...'", "sudo kubectl --kubeconfig=/etc/rancher/k3s/k3s.yaml get nodes -o wide", "echo 'K3s installation completed!'" ] } output "k3s_token" { value = local.token_cmd sensitive = false } output "kubeconfig_command" { value = local.kubeconfig_cmd sensitive = false } output "next_steps" { value = <