Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Multi-node k3s cluster #66

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions molecules/k3s/inputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ variable "servers" {
default = []
}

variable "agents" {
description = "machines which will run the workloads"
type = list(object({
host = string
user = string
private_key = string
}))
default = []
}

variable "k3s" {
type = object({
download_url = optional(string),
Expand Down
14 changes: 9 additions & 5 deletions molecules/k3s/inputs.tfvars
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
servers = [ {
host = "127.0.0.1"
private_key = "~/.ssh/id_rsa"
user = "root"
} ]
servers = [
{
host = "192.168.0.100"
private_key = "~/.ssh/id_rsa"
user = "root"
}
]

agents = []
6 changes: 3 additions & 3 deletions molecules/k3s/server.tf → molecules/k3s/k3s-agents.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
resource "ssh_resource" "install_k3s" {
resource "ssh_resource" "install_k3s_agent" {
for_each = local.servers
host = each.value.host
user = each.value.user
commands = [
"curl -sfL ${local.k3s.download_url} | INSTALL_K3S_VERSION='${local.k3s.version}' sh -s - server --docker --write-kubeconfig-mode 644 --disable=traefik"
"curl -sfL ${local.k3s.download_url} | INSTALL_K3S_VERSION='${local.k3s.version}' sh -s - agent"
# TODO: For HA k3s cluster setup
# "curl -sfL ${local.k3s.download_url} | INSTALL_K3S_VERSION='${local.k3s.version}' sh -s - server --cluster-init --docker --write-kubeconfig-mode 644 --disable=traefik"
]
Expand All @@ -13,7 +13,7 @@ resource "ssh_resource" "install_k3s" {

# Note: Removed waiting for k3s server to be ready

resource "ssh_resource" "uninstall_k3s" {
resource "ssh_resource" "uninstall_k3s_agent" {
for_each = { for server in var.servers : server.host => server }
host = each.value.host
when = "destroy"
Expand Down
29 changes: 29 additions & 0 deletions molecules/k3s/k3s-servers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
resource "random_password" "agent_token" {
length = 16
special = true
override_special = "!#$%&*()-_=+[]{}<>:?"
}

resource "ssh_resource" "install_k3s_server" {
for_each = local.servers
host = each.value.host
user = each.value.user
commands = [
"curl -sfL ${local.k3s.download_url} | INSTALL_K3S_VERSION='${local.k3s.version}' K3S_AGENT_TOKEN=${random_password.agent_token.result} sh -s - server --docker --write-kubeconfig-mode 644 --disable=traefik"
# TODO: For HA k3s cluster setup
# "curl -sfL ${local.k3s.download_url} | INSTALL_K3S_VERSION='${local.k3s.version}' sh -s - server --cluster-init --docker --write-kubeconfig-mode 644 --disable=traefik"
]
private_key = file(each.value.private_key)
timeout = "10m"
}

# Note: Removed waiting for k3s server to be ready

resource "ssh_resource" "uninstall_k3s_server" {
for_each = { for server in var.servers : server.host => server }
host = each.value.host
when = "destroy"
user = each.value.user
commands = ["bash -c 'k3s-killall.sh; k3s-uninstall.sh;'"]
private_key = file(each.value.private_key)
}