Files
kube/main.tf
2024-02-29 11:05:08 +01:00

91 lines
2.5 KiB
HCL

terraform {
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "0.7.6"
}
}
}
provider "libvirt" {
uri = "qemu:///system"
}
variable "vms" {
description = "Map of VMs to create"
type = map(object({
name = string
memory = string
vcpu = number
}))
default = {
"vm1" = {
name = "kube-master"
memory = "2048"
vcpu = 2
},
"vm2" = {
name = "kube-worker-01"
memory = "2048"
vcpu = 2
}
}
}
resource "libvirt_volume" "base-kube-qcow2" {
name = "base-kube"
source = "/home/alex/ISOs/ubuntu-22.04-server-cloudimg-amd64.img"
format = "qcow2"
}
resource "libvirt_volume" "kube-qcow2" {
for_each = var.vms
name = each.value.name
base_volume_id = libvirt_volume.base-kube-qcow2.id
size = 25 * 1024 * 1024 * 1024
format = "qcow2"
}
resource "libvirt_domain" "kube-vm" {
for_each = var.vms
name = each.value.name
memory = each.value.memory
vcpu = each.value.vcpu
network_interface {
network_name = "default"
}
disk {
volume_id = libvirt_volume.kube-qcow2[each.key].id
}
cloudinit = libvirt_cloudinit_disk.commoninit.id
}
resource "libvirt_cloudinit_disk" "commoninit" {
name = "commoninit.iso"
user_data = <<-EOF
#cloud-config
password: passw0rd
chpasswd: { expire: False }
ssh_pwauth: True
runcmd:
- swapoff -a
- sed -i '/ swap / s/^/#/' /etc/fstab
- apt-get update && apt-get install -y apt-transport-https curl
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
- add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
- apt-get update && apt-get install -y docker-ce docker-ce-cli containerd.io
- curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
- echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | tee -a /etc/apt/sources.list.d/kubernetes.list
- apt-get update
- apt-get install -y kubelet kubeadm
EOF
network_config = <<-EOF
version: 2
ethernets:
ens3:
dhcp4: true
EOF
}