【问题标题】:Terraform: retrieve the nginx ingress controller Load Balancer IPTerraform:检索 nginx 入口控制器负载均衡器 IP
【发布时间】:2022-01-03 04:06:17
【问题描述】:

我正在尝试在 Azure AKS 中获取 nginx 入口控制器负载均衡器 ip。我想我会通过以下方式使用 kubernetes 提供程序:

data "kubernetes_service" "nginx_service" {
  metadata {
    name      = "${local.ingress_name}-ingress-nginx-controller"
    namespace = local.ingress_ns
  }

  depends_on = [helm_release.ingress]
}

但是,我没有看到 IP 地址,这是我返回的:

nginx_service = [
      + {
          + cluster_ip                  = "10.0.165.249"
          + external_ips                = []
          + external_name               = ""
          + external_traffic_policy     = "Local"
          + health_check_node_port      = 31089
          + load_balancer_ip            = ""
          + load_balancer_source_ranges = []
          + port                        = [
              + {
                  + name        = "http"
                  + node_port   = 30784
                  + port        = 80
                  + protocol    = "TCP"
                  + target_port = "http"
                },
              + {
                  + name        = "https"
                  + node_port   = 32337
                  + port        = 443
                  + protocol    = "TCP"
                  + target_port = "https"
                },
            ]
          + publish_not_ready_addresses = false
          + selector                    = {
              + "app.kubernetes.io/component" = "controller"
              + "app.kubernetes.io/instance"  = "nginx-ingress-internal"
              + "app.kubernetes.io/name"      = "ingress-nginx"
            }
          + session_affinity            = "None"
          + type                        = "LoadBalancer"
        },
   ]

但是,当我通过kubectl 拉下服务时,我可以通过以下方式获取 IP 地址:

 kubectl get svc nginx-ingress-internal-ingress-nginx-controller -n nginx-ingress -o json | jq -r '.status.loadBalancer.ingress[].ip'
10.141.100.158

这是 AKS 的 kubernetes 提供程序的限制吗?如果是这样,其他人使用的解决方法是什么?我的最终目标是使用 IP 来配置应用程序网关后端。

我想我可以使用local-exec,但这似乎很老套。但是,这可能是我目前唯一的选择。

谢谢,

杰瑞

【问题讨论】:

    标签: kubernetes terraform azure-aks


    【解决方案1】:

    虽然我强烈建议不要在 Kubernetes 中使用 Terraform 创建资源,但您可以这样做:

    使用 Terraform 创建公共 IP -> 使用 Terraform 在 Kubernetes 中创建 ingress-nginx,并使用来自 Terraform 资源的数据传递 annotationsloadBalancerIP。最终清单应如下所示:

    apiVersion: v1
    kind: Service
    metadata:
      annotations:
        service.beta.kubernetes.io/azure-load-balancer-resource-group: myResourceGroup
      name: ingress-nginx-controller
    spec:
      loadBalancerIP: <YOUR_STATIC_IP>
      type: LoadBalancer
    

    Terraform 可能如下所示:

    resource "kubernetes_service" "ingress_nginx" {
      metadata {
        name = "tingress-nginx-controller"
        
        annotations {
          "service.beta.kubernetes.io/azure-load-balancer-resource-group" = "${azurerm_resource_group.YOUR_RG.name}"
        }
    
      spec {
        selector = {
          app = <PLACEHOLDER>
        }
        port {
          port        = <PLACEHOLDER>
          target_port = <PLACEHOLDER>
        }
    
        type = "LoadBalancer"
        load_balancer_ip = "${azurerm_public_ip.YOUR_IP.ip_address}"
      }
    }
    

    【讨论】:

      【解决方案2】:

      不幸的是,这是针对内部入口而不是面向公众的,并且 IP 是动态分配的。我们目前不想使用静态 ips

      这是我想出的:

      module "load_balancer_ip" {
        count = local.create_ingress ? 1 : 0
      
        source  = "github.com/matti/terraform-shell-resource?ref=v1.5.0"
        command = "./scripts/get_load_balancer_ip.sh"
      
        environment = {
          KUBECONFIG = base64encode(module.aks.kube_admin_config_raw)
        }
      
        depends_on = [local_file.load_balancer_ip_script]
      }
      
      resource "local_file" "load_balancer_ip_script" {
        count = local.create_ingress ? 1 : 0
      
        filename = "./scripts/get_load_balancer_ip.sh"
        content  = <<-EOT
          #!/bin/bash
          echo $KUBECONFIG | base64 --decode > kubeconfig
          kubectl get svc -n ${local.ingress_ns} ${local.ingress_name}-ingress-nginx-controller --kubeconfig kubeconfig -o=jsonpath='{.status.loadBalancer.ingress[0].ip}'
          rm -f kubeconfig 2>&1 >/dev/null
        EOT
      }
      
      output nginx_ip {
        description = "IP address of the internal nginx controller"
        value = local.create_ingress ? module.load_balancer_ip[0].content : null
      }
      

      【讨论】:

      • 使用此配置,当您的 kubeconfig 更改时您将遇到问题....bcs terrafrom 将刷新 kubeconfig,然后您的 local_file 资源无法应用....我建议使用静态 ips或将此配置分离到自己的 terraform 状态文件
      猜你喜欢
      • 2019-08-05
      • 2019-06-28
      • 2021-05-23
      • 2019-01-01
      • 2020-07-29
      • 2021-05-21
      • 1970-01-01
      • 2017-03-01
      • 2018-12-28
      相关资源
      最近更新 更多