From 28ee1115ae35800735688f3087826c725d0facd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Petazzoni?= Date: Wed, 5 Feb 2025 16:28:48 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9E=95=EF=B8=8F=20Add=20support=20to=20deplo?= =?UTF-8?q?y=20kubeadm=20clusters=20on=20Proxmox?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + .../terraform/providers/proxmox/config.tf | 25 ++++++ .../terraform/providers/proxmox/variables.tf | 11 +++ .../terraform/virtual-machines/common.tf | 1 + .../virtual-machines/proxmox/common.tf | 1 + .../virtual-machines/proxmox/config.tf | 1 + .../virtual-machines/proxmox/main.tf | 77 +++++++++++++++++++ .../virtual-machines/proxmox/provider.tf | 15 ++++ .../virtual-machines/proxmox/requires_tfvars | 0 .../virtual-machines/proxmox/tfvars.example | 14 ++++ .../virtual-machines/proxmox/variables.tf | 1 + 11 files changed, 147 insertions(+) create mode 100644 prepare-labs/terraform/providers/proxmox/config.tf create mode 100644 prepare-labs/terraform/providers/proxmox/variables.tf create mode 120000 prepare-labs/terraform/virtual-machines/proxmox/common.tf create mode 120000 prepare-labs/terraform/virtual-machines/proxmox/config.tf create mode 100644 prepare-labs/terraform/virtual-machines/proxmox/main.tf create mode 100644 prepare-labs/terraform/virtual-machines/proxmox/provider.tf create mode 100644 prepare-labs/terraform/virtual-machines/proxmox/requires_tfvars create mode 100644 prepare-labs/terraform/virtual-machines/proxmox/tfvars.example create mode 120000 prepare-labs/terraform/virtual-machines/proxmox/variables.tf diff --git a/.gitignore b/.gitignore index 1259ddd2..dc423f90 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ prepare-labs/terraform/many-kubernetes/one-kubernetes-config/config.tf prepare-labs/terraform/many-kubernetes/one-kubernetes-module/*.tf prepare-labs/terraform/tags prepare-labs/terraform/virtual-machines/openstack/*.tfvars +prepare-labs/terraform/virtual-machines/proxmox/*.tfvars prepare-labs/www slides/*.yml.html diff --git a/prepare-labs/terraform/providers/proxmox/config.tf b/prepare-labs/terraform/providers/proxmox/config.tf new file mode 100644 index 00000000..105e67d4 --- /dev/null +++ b/prepare-labs/terraform/providers/proxmox/config.tf @@ -0,0 +1,25 @@ +variable "proxmox_endpoint" { + type = string + default = "https://localhost:8006/" +} + +variable "proxmox_username" { + type = string + default = null +} + +variable "proxmox_password" { + type = string + default = null +} + +variable "proxmox_template_node_name" { + type = string + default = null +} + +variable "proxmox_template_vm_id" { + type = number + default = null +} + diff --git a/prepare-labs/terraform/providers/proxmox/variables.tf b/prepare-labs/terraform/providers/proxmox/variables.tf new file mode 100644 index 00000000..9fa443fd --- /dev/null +++ b/prepare-labs/terraform/providers/proxmox/variables.tf @@ -0,0 +1,11 @@ +# Since node size needs to be a string... +# To indicate number of CPUs + RAM, just pass it as a string with a space between them. +# RAM is in megabytes. +variable "node_sizes" { + type = map(any) + default = { + S = "1 2048" + M = "2 4096" + L = "3 8192" + } +} diff --git a/prepare-labs/terraform/virtual-machines/common.tf b/prepare-labs/terraform/virtual-machines/common.tf index 13b10959..71c4cefb 100644 --- a/prepare-labs/terraform/virtual-machines/common.tf +++ b/prepare-labs/terraform/virtual-machines/common.tf @@ -56,6 +56,7 @@ locals { cluster_name = format("%s-%03d", var.tag, cn[0]) node_name = format("%s-%03d-%03d", var.tag, cn[0], cn[1]) node_size = lookup(var.node_sizes, var.node_size, var.node_size) + node_index = cn[0] * var.nodes_per_cluster + cn[1] } } } diff --git a/prepare-labs/terraform/virtual-machines/proxmox/common.tf b/prepare-labs/terraform/virtual-machines/proxmox/common.tf new file mode 120000 index 00000000..a0251abd --- /dev/null +++ b/prepare-labs/terraform/virtual-machines/proxmox/common.tf @@ -0,0 +1 @@ +../common.tf \ No newline at end of file diff --git a/prepare-labs/terraform/virtual-machines/proxmox/config.tf b/prepare-labs/terraform/virtual-machines/proxmox/config.tf new file mode 120000 index 00000000..5fec599e --- /dev/null +++ b/prepare-labs/terraform/virtual-machines/proxmox/config.tf @@ -0,0 +1 @@ +../../providers/proxmox/config.tf \ No newline at end of file diff --git a/prepare-labs/terraform/virtual-machines/proxmox/main.tf b/prepare-labs/terraform/virtual-machines/proxmox/main.tf new file mode 100644 index 00000000..91dd9962 --- /dev/null +++ b/prepare-labs/terraform/virtual-machines/proxmox/main.tf @@ -0,0 +1,77 @@ +data "proxmox_virtual_environment_nodes" "_" {} + +locals { + pve_nodes = data.proxmox_virtual_environment_nodes._.names +} + +resource "proxmox_virtual_environment_vm" "_" { + node_name = local.pve_nodes[each.value.node_index % length(local.pve_nodes)] + for_each = local.nodes + name = each.value.node_name + stop_on_destroy = true + cpu { + cores = split(" ", each.value.node_size)[0] + type = "x86-64-v2-AES" # recommended for modern CPUs + } + memory { + dedicated = split(" ", each.value.node_size)[1] + } + #disk { + # datastore_id = "ceph" + # file_id = proxmox_virtual_environment_file._.id + # interface = "scsi0" + # size = 30 + # discard = "on" + #} + clone { + vm_id = var.proxmox_template_vm_id + node_name = var.proxmox_template_node_name + } + agent { + enabled = true + } + initialization { + datastore_id = "ceph" + user_account { + username = "ubuntu" + keys = [trimspace(tls_private_key.ssh.public_key_openssh)] + } + ip_config { + ipv4 { + address = "dhcp" + #gateway = + } + } + } + network_device { + bridge = "vmbr0" + } + operating_system { + type = "l26" + } +} + +#resource "proxmox_virtual_environment_download_file" "ubuntu_2404_20250115" { +# content_type = "iso" +# datastore_id = "cephfs" +# node_name = "pve-lsd-1" +# url = "https://cloud-images.ubuntu.com/releases/24.04/release-20250115/ubuntu-24.04-server-cloudimg-amd64.img" +# file_name = "ubuntu_2404_20250115.img" +#} +# +#resource "proxmox_virtual_environment_file" "_" { +# datastore_id = "cephfs" +# node_name = "pve-lsd-1" +# source_file { +# path = "/root/noble-server-cloudimg-amd64.img" +# } +#} + +locals { + ip_addresses = { + for key, value in local.nodes : + key => [for addr in flatten(concat(proxmox_virtual_environment_vm._[key].ipv4_addresses, ["ERROR"])) : + addr if addr != "127.0.0.1"][0] + } +} + diff --git a/prepare-labs/terraform/virtual-machines/proxmox/provider.tf b/prepare-labs/terraform/virtual-machines/proxmox/provider.tf new file mode 100644 index 00000000..33790a5d --- /dev/null +++ b/prepare-labs/terraform/virtual-machines/proxmox/provider.tf @@ -0,0 +1,15 @@ +terraform { + required_providers { + proxmox = { + source = "bpg/proxmox" + version = "~> 0.70.1" + } + } +} + +provider "proxmox" { + endpoint = var.proxmox_endpoint + username = var.proxmox_username + password = var.proxmox_password + insecure = true +} diff --git a/prepare-labs/terraform/virtual-machines/proxmox/requires_tfvars b/prepare-labs/terraform/virtual-machines/proxmox/requires_tfvars new file mode 100644 index 00000000..e69de29b diff --git a/prepare-labs/terraform/virtual-machines/proxmox/tfvars.example b/prepare-labs/terraform/virtual-machines/proxmox/tfvars.example new file mode 100644 index 00000000..e86f5ef7 --- /dev/null +++ b/prepare-labs/terraform/virtual-machines/proxmox/tfvars.example @@ -0,0 +1,14 @@ +# If you want to deploy to Proxmox, you need to: +# 1) copy that file to e.g. myproxmoxcluster.tfvars +# 2) make sure you have a VM template with QEMU agent pre-installed +# 3) customize the copy (you need to replace all the CHANGEME values) +# 4) deploy with "labctl create --provider proxmox/myproxmoxcluster ..." + +proxmox_endpoint = "https://localhost:8006/" +proxmox_username = "terraform@pve" +proxmox_password = "CHANGEME" + +proxmox_template_node_name = "CHANGEME" +proxmox_template_vm_id = CHANGEME + + diff --git a/prepare-labs/terraform/virtual-machines/proxmox/variables.tf b/prepare-labs/terraform/virtual-machines/proxmox/variables.tf new file mode 120000 index 00000000..bc2f6a7b --- /dev/null +++ b/prepare-labs/terraform/virtual-machines/proxmox/variables.tf @@ -0,0 +1 @@ +../../providers/proxmox/variables.tf \ No newline at end of file