Skip to content

Commit f92caf5

Browse files
committed
use klipper lb ok
1 parent 8adaa6f commit f92caf5

File tree

6 files changed

+40
-10
lines changed

6 files changed

+40
-10
lines changed

init.tf

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ resource "null_resource" "kustomization" {
8888
"https://raw.githubusercontent.com/rancher/system-upgrade-controller/master/manifests/system-upgrade-controller.yaml",
8989
],
9090
var.disable_hetzner_csi ? [] : ["https://raw.githubusercontent.com/hetznercloud/csi-driver/${local.csi_version}/deploy/kubernetes/hcloud-csi.yml"],
91-
local.is_single_node_cluster ? [] : var.traefik_enabled ? ["traefik_config.yaml"] : [],
91+
local.using_klipper_lb ? [] : var.traefik_enabled ? ["traefik_config.yaml"] : [],
9292
var.cni_plugin == "calico" ? ["https://projectcalico.docs.tigera.io/manifests/calico.yaml"] : [],
9393
var.enable_longhorn ? ["longhorn.yaml"] : [],
9494
var.enable_cert_manager || var.enable_rancher ? ["cert-manager.yaml"] : [],
@@ -109,7 +109,7 @@ resource "null_resource" "kustomization" {
109109

110110
# Upload traefik config
111111
provisioner "file" {
112-
content = local.is_single_node_cluster || var.traefik_enabled == false ? "" : templatefile(
112+
content = local.using_klipper_lb || var.traefik_enabled == false ? "" : templatefile(
113113
"${path.module}/templates/traefik_config.yaml.tpl",
114114
{
115115
name = "${var.cluster_name}-traefik"
@@ -196,6 +196,7 @@ resource "null_resource" "kustomization" {
196196
provisioner "remote-exec" {
197197
inline = concat([
198198
"set -ex",
199+
199200
# This ugly hack is here, because terraform serializes the
200201
# embedded yaml files with "- |2", when there is more than
201202
# one yamldocument in the embedded file. Kustomize does not understand
@@ -205,12 +206,27 @@ resource "null_resource" "kustomization" {
205206
# due to indendation this should not changes the embedded
206207
# manifests themselves
207208
"sed -i 's/^- |[0-9]\\+$/- |/g' /var/post_install/kustomization.yaml",
209+
210+
# Wait for k3s to become ready (we check one more time) because in some edge cases,
211+
# the cluster had become unvailable for a few seconds, at this very instant.
212+
<<-EOT
213+
timeout 120 bash <<EOF
214+
until [[ "\$(kubectl get --raw='/readyz' 2> /dev/null)" == "ok" ]]; do
215+
echo "Waiting for the cluster to become ready..."
216+
sleep 2
217+
done
218+
EOF
219+
EOT
220+
,
221+
222+
# Ready, set, go for the kustomization
208223
"kubectl apply -k /var/post_install",
209224
"echo 'Waiting for the system-upgrade-controller deployment to become available...'",
210225
"kubectl -n system-upgrade wait --for=condition=available --timeout=120s deployment/system-upgrade-controller",
211226
"kubectl -n system-upgrade apply -f /var/post_install/plans.yaml"
212227
],
213-
local.is_single_node_cluster || var.traefik_enabled == false ? [] : [<<-EOT
228+
229+
local.using_klipper_lb || var.traefik_enabled == false ? [] : [<<-EOT
214230
timeout 120 bash <<EOF
215231
until [ -n "\$(kubectl get -n kube-system service/traefik --output=jsonpath='{.status.loadBalancer.ingress[0].ip}' 2> /dev/null)" ]; do
216232
echo "Waiting for load-balancer to get an IP..."

locals.tf

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,18 @@ locals {
6868
# if we are in a single cluster config, we use the default klipper lb instead of Hetzner LB
6969
control_plane_count = sum([for v in var.control_plane_nodepools : v.count])
7070
agent_count = sum([for v in var.agent_nodepools : v.count])
71-
is_single_node_cluster = local.control_plane_count + local.agent_count == 1
71+
is_single_node_cluster = (local.control_plane_count + local.agent_count) == 1
72+
73+
using_klipper_lb = var.use_klipper_lb || local.is_single_node_cluster
7274

7375
# disable k3s extras
74-
disable_extras = concat(["local-storage"], local.is_single_node_cluster ? [] : ["servicelb"], var.traefik_enabled ? [] : ["traefik"], var.metrics_server_enabled ? [] : ["metrics-server"])
76+
disable_extras = concat(["local-storage"], local.using_klipper_lb ? [] : ["servicelb"], var.traefik_enabled ? [] : ["traefik"], var.metrics_server_enabled ? [] : ["metrics-server"])
7577

7678
# Default k3s node labels
7779
default_agent_labels = concat([], var.automatically_upgrade_k3s ? ["k3s_upgrade=true"] : [])
7880
default_control_plane_labels = concat([], var.automatically_upgrade_k3s ? ["k3s_upgrade=true"] : [])
7981

80-
allow_scheduling_on_control_plane = local.is_single_node_cluster ? true : var.allow_scheduling_on_control_plane
82+
allow_scheduling_on_control_plane = local.using_klipper_lb ? true : var.allow_scheduling_on_control_plane
8183

8284
# Default k3s node taints
8385
default_control_plane_taints = concat([], local.allow_scheduling_on_control_plane ? [] : ["node-role.kubernetes.io/master:NoSchedule"])
@@ -201,7 +203,7 @@ locals {
201203
"0.0.0.0/0"
202204
]
203205
}
204-
], !local.is_single_node_cluster ? [] : [
206+
], !local.using_klipper_lb ? [] : [
205207
# Allow incoming web traffic for single node clusters, because we are using k3s servicelb there,
206208
# not an external load-balancer.
207209
{

main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ resource "hcloud_placement_group" "agent" {
6060
}
6161

6262
data "hcloud_load_balancer" "traefik" {
63-
count = local.is_single_node_cluster ? 0 : var.traefik_enabled == false ? 0 : 1
63+
count = local.using_klipper_lb ? 0 : var.traefik_enabled == false ? 0 : 1
6464
name = "${var.cluster_name}-traefik"
6565

6666
depends_on = [null_resource.kustomization]

output.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ output "agents_public_ipv4" {
1919

2020
output "load_balancer_public_ipv4" {
2121
description = "The public IPv4 address of the Hetzner load balancer"
22-
value = local.is_single_node_cluster ? [
22+
value = local.using_klipper_lb ? [
2323
for obj in module.control_planes : obj.ipv4_address
2424
][0] : var.traefik_enabled == false ? null : data.hcloud_load_balancer.traefik[0].ipv4
2525
}

terraform.tfvars.example

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,16 @@ load_balancer_location = "fsn1"
170170
# Example: traefik_additional_options = ["--log.level=DEBUG", "--tracing=true"]
171171
# traefik_additional_options = []
172172

173+
# Use the klipper LB, instead of the default Hetzner one, that has an advantage of dropping the cost of the setup,
174+
# but you would need to point your DNS to every schedulable IPs in your cluster (usually agents). The default is "false".
175+
# Automatically "true" in the case of single node cluster.
176+
# use_klipper_lb = "true"
177+
173178
# If you want to configure a different CNI for k3s, use this flag
174179
# possible values: flannel (Default), calico
175180
# Cilium or other would be easy to add, you can mirror how Calico was added. PRs are welcome!
176-
# CAVEATS: Calico is not supported for single node setups, because of the following issue https://github.com/k3s-io/klipper-lb/issues/6.
181+
# CAVEATS: Calico is not supported when not using the Hetzner LB (like when use_klipper_lb is set to true or when using a single node cluster),
182+
# because of the following issue https://github.com/k3s-io/klipper-lb/issues/6.
177183
# cni_plugin = "calico"
178184

179185
# If you want to disable the k3s default network policy controller, use this flag!

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,9 @@ variable "rancher_registration_manifest_url" {
210210
description = "The url of a rancher registration manifest to apply. (see https://rancher.com/docs/rancher/v2.6/en/cluster-provisioning/registered-clusters/)"
211211
default = ""
212212
}
213+
214+
variable "use_klipper_lb" {
215+
type = bool
216+
default = false
217+
description = "Use klipper load balancer"
218+
}

0 commit comments

Comments
 (0)