kubernetes - Terraform kubernetes 集群通过 hetzner 云:lb 和服务器之间可能的循环依赖
问题描述
我正在尝试使用 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 https://registry.terraform.io/providers/hetznercloud/hcloud/latest/docs/resources/load_balancer_target创建 hcloud_load_balancer_target 资源
我建议使用一些现有的 Terraform 模块在 Hetzner Cloud 上部署 Kubernetes。
推荐阅读
- firebase - 通过一个应用访问多个 Firebase 数据库
- javascript - 关于在 javascript 中使用数组进行 web 编程的问题
- python-3.x - 查找列表中所有不重叠的区间
- javascript - 合并对象数组中的重复项,连接嵌套数组
- r - 时间序列包`forecast`和`aTSA`之间的冲突-如何在RMarkdown文档中使其协同工作
- mingw - 使用mingw64构建时如何生成调试符号
- mysql - mysql: Kubernetes 中的未知选项'--"'
- git - 交互式 Git rebase 完成后如何运行命令?
- java - 有没有办法在接口上定义类引用?
- java - 未找到 Maven 包,但 jar 中存在类