From 20f90c129f9ac87cbc508bc6cc9db4e49f279acc Mon Sep 17 00:00:00 2001 From: Kevin Midboe Date: Mon, 2 Jan 2023 23:50:14 +0100 Subject: [PATCH] Plan for spinning up 7 node kubernetes cluster; kazan! - Defines controller & worker resources - terraform.tfvars defines distinct nodes as a dictonary - Output provides final vmid, name & template used --- main.tf | 120 +++++++++++++++++++++++++++++++++++++++++++++++ outputs.tf | 43 +++++++++++++++++ terraform.tfvars | 82 ++++++++++++++++++++++++++++++++ variables.tf | 9 ++++ 4 files changed, 254 insertions(+) create mode 100644 main.tf create mode 100644 outputs.tf create mode 100644 terraform.tfvars create mode 100644 variables.tf diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..b7a4292 --- /dev/null +++ b/main.tf @@ -0,0 +1,120 @@ +terraform { + required_providers { + proxmox = { + source = "telmate/proxmox" + version = ">= 2.9.10" + } + google = { + source = "hashicorp/google" + version = "4.27.0" + } + } + + backend "gcs" { + bucket = "schleppe-tfstate" + prefix = "kazan" + } + + # backend "local" {} +} + +resource "proxmox_vm_qemu" "k8s-kazan-controllers" { + for_each = var.k8s_controllers + name = each.value.name + target_node = each.value.target_node + vmid = each.value.vmid + desc = "Kazan kubernetes cluster controller node: ${each.value.name}" + + sockets = 1 + cores = each.value.vcpu + memory = each.value.memory + cpu = "host" + + clone = "kazan-master-template" + full_clone = true + + agent = 1 + onboot = true + boot = "cdn" + bootdisk = "scsi0" + os_type = "cloud-init" + hotplug = "network,disk,usb" + + ipconfig0 = "ip=${each.value.ip}${each.value.subnet},gw=${each.value.gw}" + nameserver = local.nameserver + searchdomain = local.searchdomain + sshkeys = <<-EOF + %{for key in local.public_ssh_keys~} + ${key} + %{endfor~} + EOF + + disk { + type = "scsi" + storage = "local-lvm" + size = each.value.disk_size + backup = 0 + } + + network { + model = "virtio" + bridge = "vmbr0" + firewall = false + link_down = false + } + + vga { + memory = 0 + type = "serial0" + } +} + +resource "proxmox_vm_qemu" "k8s-kazan-workers" { + for_each = var.k8s_workers + name = each.value.name + target_node = each.value.target_node + vmid = each.value.vmid + desc = "Kazan kubernetes cluster worker node: ${each.value.name}" + + sockets = 1 + cores = each.value.vcpu + memory = each.value.memory + cpu = "host" + + clone = "kazan-master-template" + full_clone = true + + agent = 1 + onboot = true + boot = "cdn" + bootdisk = "scsi0" + os_type = "cloud-init" + hotplug = "network,disk,usb" + + ipconfig0 = "ip=${each.value.ip}${each.value.subnet},gw=${each.value.gw}" + nameserver = local.nameserver + searchdomain = local.searchdomain + sshkeys = <<-EOF + %{for key in local.public_ssh_keys~} + ${key} + %{endfor~} + EOF + + disk { + type = "scsi" + storage = "local-lvm" + size = each.value.disk_size + backup = 0 + } + + network { + model = "virtio" + bridge = "vmbr0" + firewall = false + link_down = false + } + + vga { + type = "serial0" + } +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..0aa1f7d --- /dev/null +++ b/outputs.tf @@ -0,0 +1,43 @@ +# Controller outputs +output "controller_vm_id" { + description = "The VM Id" + value = { + for k, vm in proxmox_vm_qemu.k8s-kazan-controllers : k => vm.id + } +} + +output "controller_vm_name" { + description = "The VM name" + value = { + for k, vm in proxmox_vm_qemu.k8s-kazan-controllers : k => vm.name + } +} + +output "controller_clone" { + description = "Template name that this VM was cloned from" + value = { + for k, vm in proxmox_vm_qemu.k8s-kazan-controllers : k => vm.clone + } +} + +# Worker outputs +output "worker_vm_id" { + description = "The VM Id" + value = { + for k, vm in proxmox_vm_qemu.k8s-kazan-workers : k => vm.id + } +} + +output "worker_vm_name" { + description = "The VM name" + value = { + for k, vm in proxmox_vm_qemu.k8s-kazan-workers : k => vm.name + } +} + +output "worker_clone" { + description = "Template name that this VM was cloned from" + value = { + for k, vm in proxmox_vm_qemu.k8s-kazan-workers : k => vm.clone + } +} diff --git a/terraform.tfvars b/terraform.tfvars new file mode 100644 index 0000000..a067455 --- /dev/null +++ b/terraform.tfvars @@ -0,0 +1,82 @@ +k8s_controllers = { + lb1 = { + target_node = "apollo", + vcpu = "2", + memory = "2048", + disk_size = "10G", + name = "lb1.kazan.schleppe", + vmid = 440 + ip = "10.0.0.140", + subnet = "/24", + gw = "10.0.0.1" + }, + c1 = { + target_node = "apollo", + vcpu = "2", + memory = "2048", + disk_size = "10G", + name = "c1.kazan.schleppe", + vmid = 441 + ip = "10.0.0.141", + subnet = "/24", + gw = "10.0.0.1" + }, + c2 = { + target_node = "apollo", + vcpu = "2", + memory = "2048", + disk_size = "10G", + name = "c2.kazan.schleppe", + vmid = 442 + ip = "10.0.0.142", + subnet = "/24", + gw = "10.0.0.1" + }, + c3 = { + target_node = "apollo", + vcpu = "2", + memory = "2048", + disk_size = "10G", + name = "c3.kazan.schleppe", + vmid = 443 + ip = "10.0.0.143", + subnet = "/24", + gw = "10.0.0.1" + } +} + +k8s_workers = { + w1 = { + target_node = "apollo", + vcpu = "2", + memory = "2048", + disk_size = "10G", + name = "w1.kazan.schleppe", + vmid = 444 + ip = "10.0.0.144", + subnet = "/24", + gw = "10.0.0.1" + }, + w2 = { + target_node = "apollo", + vcpu = "2", + memory = "2048", + disk_size = "10G", + name = "w2.kazan.schleppe", + vmid = 445 + ip = "10.0.0.145", + subnet = "/24", + gw = "10.0.0.1" + }, + w3 = { + target_node = "apollo", + vcpu = "2", + memory = "2048", + disk_size = "10G", + name = "w3.kazan.schleppe", + vmid = 446 + ip = "10.0.0.146", + subnet = "/24", + gw = "10.0.0.1" + } +} \ No newline at end of file diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..93888d4 --- /dev/null +++ b/variables.tf @@ -0,0 +1,9 @@ +variable "k8s_controllers" { + description = "k8s controller node variables as a dictionary" + type = map(any) +} + +variable "k8s_workers" { + description = "k8s worker node variables as a dictionary" + type = map(any) +} \ No newline at end of file