首页 > 解决方案 > 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 并填充其配置文件。我该如何解决这个问题,它首先是一个实际问题吗?

在此先感谢大家,让我知道如何改进我的问题!

标签: kubernetesterraformload-balancingkubeadm

解决方案



推荐阅读