Skip to content

Commit

Permalink
Added options to specify custom storage on all nodes (#3)
Browse files Browse the repository at this point in the history
* Added options to specify custom storage on all nodes

* Terraform fmt

* updated README
  • Loading branch information
fvumbaca authored Jan 5, 2022
1 parent 7135deb commit f6fc871
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 220 deletions.
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ A module for spinning up an expandable and flexible K3s server for your HomeLab.
```terraform
module "k3s" {
source = "fvumbaca/k3s/proxmox"
version = "0.0.0"
version = ">= 0.0.0, < 1.0.0" # Get latest 0.X release
authorized_keys_file = "authorized_keys"
Expand Down Expand Up @@ -82,12 +82,6 @@ output "kubeconfig" {
}
```

You may need to refresh your state:

```sh
terraform refresh
```

Finally output the config file:

```sh
Expand Down
112 changes: 57 additions & 55 deletions master_nodes.tf
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
resource "macaddress" "k3s-masters" {
count = var.master_nodes_count
count = var.master_nodes_count
}

locals {
master_node_settings = defaults(var.master_node_settings, {
cores = 2
sockets = 1
memory = 4096
disk_size = "20G"
user = "k3s"
cores = 2
sockets = 1
memory = 4096
storage_type = "scsi"
storage_id = "local-lvm"
disk_size = "20G"
user = "k3s"
})

master_node_ips = [for i in range(var.master_nodes_count): cidrhost(var.control_plane_subnet, i+1)]
master_node_ips = [for i in range(var.master_nodes_count) : cidrhost(var.control_plane_subnet, i + 1)]
}

resource "random_password" "k3s-server-token" {
length = 32
special = false
length = 32
special = false
override_special = "_%@"
}

Expand All @@ -25,23 +27,23 @@ resource "proxmox_vm_qemu" "k3s-master" {
proxmox_vm_qemu.k3s-support,
]

count = var.master_nodes_count
count = var.master_nodes_count
target_node = var.proxmox_node
name = "${var.cluster_name}-master-${count.index}"
name = "${var.cluster_name}-master-${count.index}"

clone = var.node_template

pool = var.proxmox_resource_pool

# cores = 2
cores = local.master_node_settings.cores
cores = local.master_node_settings.cores
sockets = local.master_node_settings.sockets
memory = local.master_node_settings.memory
memory = local.master_node_settings.memory

disk {
type = "scsi"
storage = "local-lvm"
size = local.master_node_settings.disk_size
type = local.master_node_settings.storage_type
storage = local.master_node_settings.storage_id
size = local.master_node_settings.disk_size
}

network {
Expand All @@ -56,51 +58,51 @@ resource "proxmox_vm_qemu" "k3s-master" {
}


os_type = "cloud-init"
os_type = "cloud-init"

ciuser = local.master_node_settings.user

ipconfig0 = "ip=${local.master_node_ips[count.index]}/${local.lan_subnet_cidr_bitnum},gw=${var.network_gateway}"

sshkeys = file(var.authorized_keys_file)

connection {
type = "ssh"
user = local.master_node_settings.user
host = local.master_node_ips[count.index]
}

provisioner "remote-exec" {
inline = [
templatefile("${path.module}/scripts/install-k3s-server.sh.tftpl", {
mode = "server"
tokens = [random_password.k3s-server-token.result]
alt_names = concat([local.support_node_ip], var.api_hostnames)
server_hosts = []
node_taints = ["CriticalAddonsOnly=true:NoExecute"]
disable = var.k3s_disable_components
datastores = [{
host = "${local.support_node_ip}:3306"
name = "k3s"
user = "k3s"
password = random_password.k3s-master-db-password.result
}]
})
]
}
}
sshkeys = file(var.authorized_keys_file)

data "external" "kubeconfig" {
depends_on = [
proxmox_vm_qemu.k3s-support,
proxmox_vm_qemu.k3s-master
]
connection {
type = "ssh"
user = local.master_node_settings.user
host = local.master_node_ips[count.index]
}

program = [
"/usr/bin/ssh",
"-o UserKnownHostsFile=/dev/null",
"-o StrictHostKeyChecking=no",
"${local.master_node_settings.user}@${local.master_node_ips[0]}",
"echo '{\"kubeconfig\":\"'$(sudo cat /etc/rancher/k3s/k3s.yaml | base64)'\"}'"
provisioner "remote-exec" {
inline = [
templatefile("${path.module}/scripts/install-k3s-server.sh.tftpl", {
mode = "server"
tokens = [random_password.k3s-server-token.result]
alt_names = concat([local.support_node_ip], var.api_hostnames)
server_hosts = []
node_taints = ["CriticalAddonsOnly=true:NoExecute"]
disable = var.k3s_disable_components
datastores = [{
host = "${local.support_node_ip}:3306"
name = "k3s"
user = "k3s"
password = random_password.k3s-master-db-password.result
}]
})
]
}
}

data "external" "kubeconfig" {
depends_on = [
proxmox_vm_qemu.k3s-support,
proxmox_vm_qemu.k3s-master
]

program = [
"/usr/bin/ssh",
"-o UserKnownHostsFile=/dev/null",
"-o StrictHostKeyChecking=no",
"${local.master_node_settings.user}@${local.master_node_ips[0]}",
"echo '{\"kubeconfig\":\"'$(sudo cat /etc/rancher/k3s/k3s.yaml | base64)'\"}'"
]
}
8 changes: 4 additions & 4 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

output "k3s_db_password" {
value = random_password.k3s-master-db-password.result
value = random_password.k3s-master-db-password.result
sensitive = true
}

Expand All @@ -17,7 +17,7 @@ output "k3s_db_host" {
}

output "root_db_password" {
value = random_password.support-db-password.result
value = random_password.support-db-password.result
sensitive = true
}

Expand All @@ -34,7 +34,7 @@ output "master_node_ips" {
}

output "k3s_server_token" {
value = random_password.k3s-server-token.result
value = random_password.k3s-server-token.result
sensitive = true
}

Expand All @@ -43,7 +43,7 @@ output "k3s_master_node_ips" {
}

output "k3s_kubeconfig" {
value = replace(base64decode(replace(data.external.kubeconfig.result.kubeconfig, " ", "")), "server: https://127.0.0.1:6443", "server: https://${local.support_node_ip}:6443")
value = replace(base64decode(replace(data.external.kubeconfig.result.kubeconfig, " ", "")), "server: https://127.0.0.1:6443", "server: https://${local.support_node_ip}:6443")
sensitive = true
}

Loading

0 comments on commit f6fc871

Please sign in to comment.