Skip to content

Commit b58d080

Browse files
committed
feat(infra): install k3s to additional managers
1 parent 727c4b7 commit b58d080

File tree

4 files changed

+73
-14
lines changed

4 files changed

+73
-14
lines changed

infrastructure/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ No modules.
3434
| [hcloud_placement_group.kubernetes](https://registry.terraform.io/providers/hetznercloud/hcloud/latest/docs/resources/placement_group) | resource |
3535
| [hcloud_server.manager](https://registry.terraform.io/providers/hetznercloud/hcloud/latest/docs/resources/server) | resource |
3636
| [hcloud_ssh_key.server](https://registry.terraform.io/providers/hetznercloud/hcloud/latest/docs/resources/ssh_key) | resource |
37+
| [ssh_resource.additional_managers](https://registry.terraform.io/providers/loafoe/ssh/2.7.0/docs/resources/resource) | resource |
3738
| [ssh_resource.initial_manager](https://registry.terraform.io/providers/loafoe/ssh/2.7.0/docs/resources/resource) | resource |
3839
| [ssh_resource.server_ready](https://registry.terraform.io/providers/loafoe/ssh/2.7.0/docs/resources/resource) | resource |
3940
| [ssh_sensitive_resource.join_token](https://registry.terraform.io/providers/loafoe/ssh/2.7.0/docs/resources/sensitive_resource) | resource |

infrastructure/k3s.tf

+68-14
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,30 @@
1515
locals {
1616
k3s_initial_manager = hcloud_server.manager[0]
1717
k3s_initial_manager_private_ip = tolist(local.k3s_initial_manager.network)[0].ip
18-
k3s_tls_san = var.k3s_manager_count > 1 ? hcloud_load_balancer.k3s_manager[0].ipv4 : local.k3s_initial_manager.ipv4_address
18+
k3s_access_address = var.k3s_manager_count > 1 ? hcloud_load_balancer.k3s_manager[0].ipv4 : local.k3s_initial_manager.ipv4_address
1919
k3s_join_token = chomp(ssh_sensitive_resource.join_token.result)
2020
k3s_kubeconfig = chomp(ssh_sensitive_resource.kubeconfig.result)
21+
k3s_manager_install_command = join(" ", concat(
22+
[
23+
"server",
24+
"--write-kubeconfig-mode=0644",
25+
"--disable servicelb",
26+
"--disable traefik",
27+
"--node-name=$(hostname -f)",
28+
"--node-external-ip=$(hostname -I | awk '{print $1}')", # Public IP
29+
"--node-ip=$(hostname -I | awk '{print $2}')", # Private IP
30+
"--advertise-address=$(hostname -I | awk '{print $2}')", # Private IP
31+
],
32+
# Set TLS SANs - first, add load balancer or managers's public address
33+
var.k3s_manager_count > 1 ? [
34+
"--cluster-init",
35+
"--tls-san=${hcloud_load_balancer.k3s_manager[0].ipv4}"
36+
] : [
37+
"--tls-san=${local.k3s_initial_manager_private_ip}"
38+
],
39+
# Now, add all the servers
40+
[for o in hcloud_server.manager : "--tls-san=${tolist(o.network)[0].ip}"]
41+
))
2142
}
2243

2344
resource "ssh_resource" "server_ready" {
@@ -26,6 +47,7 @@ resource "ssh_resource" "server_ready" {
2647
host = hcloud_server.manager[count.index].ipv4_address
2748
user = local.machine_user
2849
private_key = file(var.ssh_key)
50+
port = var.ssh_port
2951

3052
timeout = "5m"
3153
retry_delay = "5s"
@@ -47,26 +69,19 @@ resource "ssh_resource" "initial_manager" {
4769
host = local.k3s_initial_manager.ipv4_address
4870
user = local.machine_user
4971
private_key = file(var.ssh_key)
72+
port = var.ssh_port
5073

5174
commands = [
5275
format(
5376
"curl -sfL %s | INSTALL_K3S_EXEC=\"%s\" %s sh -",
5477
var.k3s_download_url,
5578
// Install configuration
56-
join(" ", [
57-
"server",
58-
"--write-kubeconfig-mode=0644",
59-
"--disable servicelb",
60-
"--disable traefik",
61-
"--tls-san=${local.k3s_tls_san}",
62-
"--node-name=$(hostname -f)",
63-
"--node-external-ip=$(hostname -I | awk '{print $1}')", # Public IP
64-
"--node-ip=$(hostname -I | awk '{print $2}')", # Private IP
65-
"--advertise-address=$(hostname -I | awk '{print $2}')", # Private IP
66-
]),
79+
local.k3s_manager_install_command,
6780
// Other k3s configuration
6881
""
69-
)
82+
),
83+
# Ensure k3s is running
84+
"sudo systemctl start k3s"
7085
]
7186

7287
timeout = "5m"
@@ -86,6 +101,7 @@ resource "ssh_sensitive_resource" "join_token" {
86101
host = local.k3s_initial_manager.ipv4_address
87102
user = local.machine_user
88103
private_key = file(var.ssh_key)
104+
port = var.ssh_port
89105

90106
commands = [
91107
"sudo cat /var/lib/rancher/k3s/server/token"
@@ -103,9 +119,10 @@ resource "ssh_sensitive_resource" "kubeconfig" {
103119
host = local.k3s_initial_manager.ipv4_address
104120
user = local.machine_user
105121
private_key = file(var.ssh_key)
122+
port = var.ssh_port
106123

107124
commands = [
108-
format("sudo cat /etc/rancher/k3s/k3s.yaml | sed 's/%s/%s/'", "127.0.0.1", local.k3s_tls_san)
125+
format("sudo cat /etc/rancher/k3s/k3s.yaml | sed 's/%s/%s/'", "127.0.0.1", local.k3s_access_address)
109126
]
110127

111128
timeout = "5m"
@@ -115,3 +132,40 @@ resource "ssh_sensitive_resource" "kubeconfig" {
115132
ssh_resource.initial_manager
116133
]
117134
}
135+
136+
resource "ssh_resource" "additional_managers" {
137+
count = var.k3s_manager_count - 1
138+
139+
host = hcloud_server.manager[count.index + 1].ipv4_address
140+
user = local.machine_user
141+
private_key = file(var.ssh_key)
142+
port = var.ssh_port
143+
144+
commands = [
145+
format(
146+
"curl -sfL %s | INSTALL_K3S_EXEC=\"%s\" %s sh -",
147+
var.k3s_download_url,
148+
// Install configuration
149+
local.k3s_manager_install_command,
150+
// Other k3s configuration
151+
join(" ", [
152+
"K3S_URL=https://${local.k3s_initial_manager_private_ip}:6443",
153+
"K3S_TOKEN=${local.k3s_join_token}"
154+
])
155+
),
156+
# Ensure k3s is running
157+
"sudo systemctl start k3s"
158+
]
159+
160+
timeout = "5m"
161+
retry_delay = "5s"
162+
163+
164+
triggers = {
165+
always_run = timestamp()
166+
}
167+
168+
depends_on = [
169+
ssh_resource.initial_manager
170+
]
171+
}

infrastructure/network.tf

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ resource "hcloud_firewall" "name" {
5656
source_ips = [
5757
hcloud_network.network.ip_range
5858
]
59+
protocol = "udp"
5960
},
6061
{
6162
description = "Allow access to Kubernetes API"

infrastructure/server.tf

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ resource "hcloud_server" "manager" {
4646

4747
network {
4848
network_id = hcloud_network.network.id
49+
# Set the alias_ips to avoid this triggering an update each run
50+
# @link https://github.com/hetznercloud/terraform-provider-hcloud/issues/650#issuecomment-1497160625
51+
alias_ips = []
4952
}
5053

5154
public_net {

0 commit comments

Comments
 (0)