问题描述
我正在尝试使用 terraform 和 hetzner 云提供商引导一个 HA Kubernetes 集群。在我的设置中,控制平面节点前面的负载均衡器需要知道集群中主节点的 IP 地址。这样我就可以将主节点注册为负载均衡器的目标。
同样,在引导主节点时,需要了解负载均衡器的 IP 地址才能填充其配置。
我可以在 masters 配置中使用 dns 名称,然后在 lb ip 和 name 之间创建关联,但我想避免使用 dns 名称。有没有其他方法可以达到这个结果?
对于某些上下文,这是从我的代码中摘录的:
resource "hcloud_load_balancer" "cluster-lb" {
name = "my-load-balancer"
load_balancer_type = "lb11"
location = "nbg1"
dynamic "target" {
for_each = var.master_node_ids # this is an input parameter
content { # that requires the master servers to exist.
type = "server"
server_id = target.value["id"]
}
}
}
locals {
# Here I must crate both a InitConfiguration and a ClusterConfiguration. These config files are used
# by kubeadm to bootstrap the cluster. Among other things,ClusterConfiguration requires the
# controlPlaneEndpoint argument to be specified. This represents the shared endpoint to access the
# cluster. In a HA scenario it is the ip address of the loadbalancer.
kubeadm_init = templatefile(
"kubeadm_init.tmpl",{
controlPlaneEndpoint = ???
}
}
# Later on the kubeadm_init is incorporated in a cloud-init write_files attribute so it is copied to
# the server. I've omitted this section as it is quite verbose and not really useful in answering the
# question. If necessary i can provide it as well.
# Here I create the master nodes :
resource "hcloud_server" "cluster-masters" {
for_each = local.masters
name = "server-${each.key}"
server_type = "cpx11"
image = "ubuntu-20.04"
location = each.value["availability_zone"]
user_data = local.cloud_init_data
network {
network_id = var.network_id
ip = each.value["ip"]
}
}
在我看来,集群负载均衡器和服务器之间存在循环依赖。第一个必须等待主节点的创建,以便将它们添加为目标。另一方面,主节点必须等待负载均衡器才能在创建之前获取其 ip 并填充其配置文件。我怎样才能解决这个问题,它首先是一个实际问题吗?
先谢谢大家,让我知道如何改进我的问题!
解决方法
-
创建没有可选目标列表的 hcloud_load_balancer cluster-lb 资源 https://registry.terraform.io/providers/hetznercloud/hcloud/latest/docs/resources/load_balancer#target
-
使用 hcloud_load_balancer.cluster-lb.ipv4 作为 controlPlaneEndpoint https://registry.terraform.io/providers/hetznercloud/hcloud/latest/docs/resources/load_balancer#ipv4
-
使用 type = label_selector 和 load_balancer_id = hcloud_load_balancer.cluster-lb.id 和 label_selector = hcloud_server.cluster-masters.labels 创建 hcloud_load_balancer_target 资源 https://registry.terraform.io/providers/hetznercloud/hcloud/latest/docs/resources/load_balancer_target
-
我建议使用一些现有的 Terraform 模块在 Hetzner Cloud 上部署 Kubernetes。