Setup k3s with the help of ansible after the infra-provisioning

This change is huge! It allows me to manage the k3s installation on the
hetzner-cloud I setup via terraform.
This commit is contained in:
2025-09-15 04:12:20 +02:00
parent d083b9d446
commit dfcdc9797a
13 changed files with 364 additions and 32 deletions

View File

@@ -15,11 +15,3 @@ runcmd:
- echo "Gateway=${network_gateway}" >> /etc/systemd/network/10-$NIC.network
- sed -e "s/#DNS=/DNS=${dns_servers}/" -i /etc/systemd/resolved.conf
- systemctl restart systemd-resolved
# update system dependency-lists
- apt-get update -y
# install k3s but do not start yet
- curl -sfL https://get.k3s.io | tee install-k3s | INSTALL_K3S_SKIP_ENABLE=true sh -s -
# wait for the server node to be ready by trying to connect to it
- until curl -k https://${server_ip}:6443; do sleep 5; done
# Enable and start k3s-agent
- cat install-k3s | K3S_TOKEN=${k3s_token} sh -s - agent --server https://${server_ip}:6443

View File

@@ -68,10 +68,14 @@ resource "hcloud_firewall" "this" {
}
}
}
resource "hcloud_server" "server" {
depends_on = [hcloud_network_subnet.this]
for_each = local.servers
lifecycle {
ignore_changes = [ user_data ]
}
name = each.key
image = "ubuntu-24.04"
server_type = each.value.type
@@ -100,6 +104,10 @@ resource "hcloud_server" "agent" {
depends_on = [hcloud_server.server]
for_each = local.agents
lifecycle {
ignore_changes = [ user_data ]
}
name = each.key
image = "ubuntu-24.04"
server_type = each.value.type

View File

@@ -0,0 +1,11 @@
output "server_ips_v4" {
value = [for key, value in hcloud_server.server : value.ipv4_address]
}
output "server_ips_v6" {
value = [for key, value in hcloud_server.server : value.ipv6_address]
}
output "agent_ips_v4" {
value = flatten([for key, value in hcloud_server.agent : value.network.*.ip])
}

View File

@@ -5,25 +5,9 @@ users:
- name: cluster
shell: /bin/bash
runcmd:
# update system dependency-lists
- apt-get update -y
# configure NAT
- echo '#!/bin/bash' > /etc/networkd-dispatcher/routable.d/10-eth0-post-up
- echo 'echo 1 > /proc/sys/net/ipv4/ip_forward' >> /etc/networkd-dispatcher/routable.d/10-eth0-post-up
- echo 'iptables -t nat -A POSTROUTING -s ${network_ip_range} -o eth0 -j MASQUERADE' >> /etc/networkd-dispatcher/routable.d/10-eth0-post-up
- chmod +x /etc/networkd-dispatcher/routable.d/10-eth0-post-up
- /etc/networkd-dispatcher/routable.d/10-eth0-post-up
# install k3s but do not start yet
- curl -sfL https://get.k3s.io | tee install-k3s | INSTALL_K3S_SKIP_ENABLE=true sh -s -
%{ if first_ip != "" ~}
- until curl -k https://${first_ip}:6443; do sleep 5; done
%{ endif ~}
# Enable and start k3s-server
%{ if first_ip == "" ~}
- cat install-k3s | K3S_TOKEN=${k3s_token} sh -s - server --cluster-init
%{~ else ~}
- cat install-k3s | INSTALL_K3S_SKIP_DOWNLOAD=true K3S_TOKEN=${k3s_token} sh -s - server --server https://${first_ip}:6443
%{~ endif }
- chown cluster:cluster /etc/rancher/k3s/k3s.yaml
- chown cluster:cluster /var/lib/rancher/k3s/server
- chown cluster:cluster /var/lib/rancher/k3s/server/node-token