diff options
author | makefunstuff <[email protected]> | 2024-07-18 21:09:37 +0200 |
---|---|---|
committer | makefunstuff <[email protected]> | 2024-07-18 21:09:37 +0200 |
commit | 07822ea3f2239cf9cd3335ad2d7fb0aab7a3e1dc (patch) | |
tree | e9f4e9c69d3653b19139da565e5426b2c70806f8 | |
parent | a52d9c49b7f4166b18a49b4907a2b0afba3aac05 (diff) | |
download | k3s-lab-07822ea3f2239cf9cd3335ad2d7fb0aab7a3e1dc.tar.gz |
infra updates
-rw-r--r-- | terraform/cloud-init.cfg | 9 | ||||
-rwxr-xr-x | terraform/configure-k3s.sh | 22 | ||||
-rw-r--r-- | terraform/main.tf | 60 |
3 files changed, 91 insertions, 0 deletions
diff --git a/terraform/cloud-init.cfg b/terraform/cloud-init.cfg new file mode 100644 index 0000000..75fa7c7 --- /dev/null +++ b/terraform/cloud-init.cfg @@ -0,0 +1,9 @@ + +#cloud-config +hostname: k3s-node +ssh_authorized_keys: + - ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAr9Y6...your-public-key... user@hostname +runcmd: + - echo "#!/bin/bash" > /usr/local/bin/k3s-install.sh + - echo "curl -sfL https://get.k3s.io | sh -" >> /usr/local/bin/k3s-install.sh + - chmod +x /usr/local/bin/k3s-install.sh diff --git a/terraform/configure-k3s.sh b/terraform/configure-k3s.sh new file mode 100755 index 0000000..76076d7 --- /dev/null +++ b/terraform/configure-k3s.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# Get the IP addresses from Terraform output +MASTER_IP=$(terraform output -json k8s_node_ips | jq -r '.[0]') +WORKER_IPS=$(terraform output -json k8s_node_ips | jq -r '.[1,2]') + +# Set up the master node +ssh ubuntu@$MASTER_IP <<EOF +sudo /usr/local/bin/k3s-install.sh +EOF + +# Get the K3s token +K3S_TOKEN=$(ssh ubuntu@$MASTER_IP "sudo cat /var/lib/rancher/k3s/server/node-token") + +# Join worker nodes to the cluster +for WORKER_IP in $WORKER_IPS; do + ssh ubuntu@$WORKER_IP <<EOF +sudo K3S_URL=https://$MASTER_IP:6443 K3S_TOKEN=$K3S_TOKEN /usr/local/bin/k3s-install.sh +EOF +done + +echo "K3s cluster setup completed. Master node IP: $MASTER_IP" diff --git a/terraform/main.tf b/terraform/main.tf new file mode 100644 index 0000000..187bd47 --- /dev/null +++ b/terraform/main.tf @@ -0,0 +1,60 @@ +provider "libvirt" { + uri = "qemu:///system" +} + +resource "libvirt_volume" "ubuntu_image" { + name = "ubuntu-18.04-minimal-cloudimg-amd64" + pool = "default" + source = "https://cloud-images.ubuntu.com/minimal/releases/bionic/release/ubuntu-18.04-minimal-cloudimg-amd64.img" + format = "qcow2" +} + +resource "libvirt_network" "k8s_network" { + name = "k8s_network" + mode = "nat" + addresses = ["192.168.122.0/24"] +} + +resource "libvirt_domain" "k8s_node" { + count = 3 + name = "k8s-node-${count.index + 1}" + memory = "2048" + vcpu = 2 + + cloudinit = libvirt_cloudinit_disk.common.id + + network_interface { + network_name = libvirt_network.k8s_network.name + hostname = "k8s-node-${count.index + 1}" + } + + disk { + volume_id = libvirt_volume.ubuntu_image.id + } + + console { + type = "pty" + target_type = "serial" + target_port = "0" + } + + graphics { + type = "spice" + listen_type = "none" + } +} + +resource "libvirt_cloudinit_disk" "common" { + name = "common-init.iso" + user_data = data.template_file.user_data.rendered +} + +data "template_file" "user_data" { + template = file("${path.module}/cloud_init.cfg") +} + +output "k8s_node_ips" { + value = { + for i in libvirt_domain.k8s_node : i.name => i.network_interface[0].addresses[0] + } +} |