diff options
author | David Runge <dave@sleepmap.de> | 2020-12-16 21:24:26 +0100 |
---|---|---|
committer | David Runge <dave@sleepmap.de> | 2020-12-16 21:24:26 +0100 |
commit | 0543e3011817211445da32afe1eaa60ded44c673 (patch) | |
tree | 1ff4a268a70f0474a827b28ebbfe1de037138a97 /k8s_cluster/main.tf | |
download | terraform-master.tar.gz terraform-master.tar.bz2 terraform-master.tar.xz terraform-master.zip |
k8s_cluster/*:
Add a terraform-provider-libvirt based examples to semi-automatically
create a k8s cluster for testing.
Diffstat (limited to 'k8s_cluster/main.tf')
-rw-r--r-- | k8s_cluster/main.tf | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/k8s_cluster/main.tf b/k8s_cluster/main.tf new file mode 100644 index 0000000..6e0c0a3 --- /dev/null +++ b/k8s_cluster/main.tf @@ -0,0 +1,183 @@ +terraform { + required_providers { + libvirt = { + source = "dmacvicar/libvirt" + version = ">= 0.6.2" + } + } + backend "pg" { + conn_str = "postgres://terraform_local@localhost/terraform_local?sslmode=disable" + } +} + +provider "libvirt" { + uri = var.libvirt_provider_uri +} + +resource "libvirt_volume" "base_volume" { + name = "vm_base_volume" + source = var.vm_image_source +} + +resource "libvirt_volume" "control_plane_volume" { + name = "control_plane_${count.index}.qcow2" + base_volume_id = libvirt_volume.base_volume.id + count = var.control_plane_count +} + +resource "libvirt_volume" "node_volume" { + name = "worker_${count.index}.qcow2" + base_volume_id = libvirt_volume.base_volume.id + count = var.node_count +} + +resource "libvirt_volume" "load_balancer_volume" { + name = "load_balancer_${count.index}.qcow2" + base_volume_id = libvirt_volume.base_volume.id + count = var.load_balancer_count +} + +# Public ssh key for vm (it is directly injected in cloud-init configuration) # +data "template_file" "public_ssh_key" { + template = file("${var.vm_ssh_private_key}.pub") +} + +# Cloud-init configuration template # +data "template_file" "cloud_init_tpl" { + template = file("templates/cloud_init.cfg.tpl") + + vars = { + ssh_public_key = data.template_file.public_ssh_key.rendered + } +} + +# Cloud-init configuration template for load balancer # +data "template_file" "cloud_init_load_balancer_tpl" { + template = file("templates/cloud_init_load_balancer.cfg.tpl") + + vars = { + ssh_public_key = data.template_file.public_ssh_key.rendered + } +} + +# Creates cloud-init configuration file from template for node # +resource "local_file" "cloud_init_node_file" { + content = data.template_file.cloud_init_tpl.rendered + filename = "config/cloud_init.cfg" +} + +# Creates cloud-init configuration file from template for load balancer # +resource "local_file" "cloud_init_load_balancer_file" { + content = data.template_file.cloud_init_load_balancer_tpl.rendered + filename = "config/cloud_init_load_balancer.cfg" +} + +data "template_file" "network_config" { + template = file("${path.module}/cloud-init/network_config.yaml") +} + +resource "libvirt_cloudinit_disk" "cloud_init_k8s" { + name = "cloud_init_k8s.iso" + user_data = data.template_file.cloud_init_tpl.rendered + network_config = data.template_file.network_config.rendered +} + +resource "libvirt_cloudinit_disk" "cloud_init_load_balancer" { + name = "cloud_init_load_balancer.iso" + user_data = data.template_file.cloud_init_load_balancer_tpl.rendered + network_config = data.template_file.network_config.rendered +} + +resource "libvirt_network" "vm_net" { + name = "vm_net" + mode = "bridge" + bridge = "virbr0" + autostart = true +} + +resource "libvirt_domain" "load_balancer" { + name = "load_balancer${count.index}" + description = "Kubernetes Load Balancer ${count.index}" + cpu = { + mode = "host-passthrough" + } + vcpu = 1 + memory = "1024" + cloudinit = libvirt_cloudinit_disk.cloud_init_load_balancer.id + disk { + volume_id = libvirt_volume.load_balancer_volume[count.index].id + } + qemu_agent = true + autostart = true + running = true + count = var.load_balancer_count + + network_interface { + network_id = libvirt_network.vm_net.id + hostname = "load-balancer${count.index}" + mac = "AA:BB:CC:11:33:0${count.index}" + wait_for_lease = true + } +} + +resource "libvirt_domain" "control_plane" { + name = "controller${count.index}" + description = "Kubernetes control plane ${count.index}" + cpu = { + mode = "host-passthrough" + } + vcpu = 2 + memory = "2048" + cloudinit = libvirt_cloudinit_disk.cloud_init_k8s.id + disk { + volume_id = libvirt_volume.control_plane_volume[count.index].id + } + network_interface { + network_id = libvirt_network.vm_net.id + hostname = "controller${count.index}" + mac = "AA:BB:CC:11:11:0${count.index}" + wait_for_lease = true + } + qemu_agent = true + autostart = true + running = true + count = var.control_plane_count + +} + +resource "libvirt_domain" "node" { + name = "worker${count.index}" + description = "Kubernetes Node ${count.index}" + cpu = { + mode = "host-passthrough" + } + vcpu = 2 + memory = "2048" + cloudinit = libvirt_cloudinit_disk.cloud_init_k8s.id + disk { + volume_id = libvirt_volume.node_volume[count.index].id + } + qemu_agent = true + autostart = true + running = true + count = var.node_count + + network_interface { + network_id = libvirt_network.vm_net.id + hostname = "worker${count.index}" + mac = "AA:BB:CC:11:22:0${count.index}" + wait_for_lease = true + } +} + +output "load_balancer_ip_addresses" { + value = libvirt_domain.load_balancer.*.network_interface.0.addresses +} + +output "control_plane_ip_addresses" { + value = libvirt_domain.control_plane.*.network_interface.0.addresses +} + +output "node_ip_addresses" { + value = libvirt_domain.node.*.network_interface.0.addresses +} |