使用 Terraform 配置 Kubernetes Ingress 的健康检查

Configuring the Health Check of a Kubernetes Ingress with Terraform

我们正在使用入口 (kubernetes_ingress.db_admin_ingress) 公开 Google Kubernetes Engine (GKE) 中部署 (kubernetes_deployment.db_admin) 的服务 (kubernetes_service.db_admin)地形。

当 Terraform 创建入口时,会自动创建一个带有默认健康检查的 7 级负载均衡器:

我们的部署 (kubernetes_deployment.db_admin) 未使用 200 响应路径 /,因此健康检查失败。

如何更改健康检查配置中的路径?

resource "google_compute_managed_ssl_certificate" "db_admin_ssl_certificate" {
  provider = google-beta

  name = "db-admin-ssl-certificate"

  managed {
    domains = ["db.${var.domain}."]
  }
}

resource "kubernetes_deployment" "db_admin" {
  metadata {
    name = "db-admin"
    labels = {
      App = "db-admin"
    }
  }

  spec {
    replicas = 1
    selector {
      match_labels = {
        App = "db-admin"
      }
    }
    template {
      metadata {
        labels = {
          App = "db-admin"
        }
      }
      spec {
        container {
          image = "dpage/pgadmin4:2022-01-10-1"
          name  = "db-admin"
          env {
            name = "PGADMIN_DEFAULT_EMAIL"
            value = "test@test.com"
          }
          env {
            name = "PGADMIN_DEFAULT_PASSWORD"
            value = "test"
          }      

          port {
            container_port = 80
          }

          resources {}
        }
      }
    }
  }
}

resource "kubernetes_service" "db_admin" {
  metadata {
    name = "db-admin"
  }
  spec {
    selector = {
      App = kubernetes_deployment.db_admin.spec.0.template.0.metadata[0].labels.App
    }
    port {
      protocol    = "TCP"
      port        = 80
      target_port = 80
    }

    type = "NodePort"
  }
}

resource "kubernetes_ingress" "db_admin_ingress" {
  wait_for_load_balancer = true
  
  metadata {
    name = "db-admin-ingress"
    annotations = {
      "ingress.gcp.kubernetes.io/pre-shared-cert"   = google_compute_managed_ssl_certificate.db_admin_ssl_certificate.name
    }
  }

  spec {

    rule {
      http {
        path {
          backend {
            service_name = "db-admin"
            service_port = 80
          }

          path = "/*"
        }


      }
    }

  }
}

根据 Google Kubernetes Engine (GKE) 官方文档 here,您可以通过以下任一方式自定义 ingress/Level 7 负载均衡器健康检查:

  • podcontainerreadinessProbe 您的 ingress 正在为

    提供流量

    警告:此方法带有警告here

  • 一个backendconfig资源

高度建议创建一个backendconfig资源。

不幸的是,kubernetes Terraform 提供者 似乎支持基于 this GitHub 问题的 backendconfig 资源.这意味着您可以:

  • 使用 kubernetes-alpha 提供商(找到 here) to transcribe a YAML backendconfig manifest to HCL with the manifest argument for the only kubernetes-alpha resource: kubernetes-manifest (more on that here
  • 使用非官方提供者(例如 banzaicloud/k8s 发现 here
  • backendconfig 清单(作为 JSON 或 YAML)检查到 SCM

示例 backendconfig YAML 清单:

apiVersion: cloud.google.com/v1
kind: BackendConfig
metadata:
  name: db-admin
  namespace: default
spec:
  healthCheck:
    checkIntervalSec: 30
    timeoutSec: 5
    healthyThreshold: 1
    unhealthyThreshold: 2
    type: HTTP
    requestPath: /v1/some/path
    port: 80

注意:需要service才能将backendconfigingress/7级负载均衡器相关联:

apiVersion: v1
kind: Service
metadata:
  name: db-admin-ingress-backend-config
  labels:
    app: db-admin
  annotations:
    cloud.google.com/backend-config: '{"ports": {"80":"db-admin"}}'
    cloud.google.com/neg: '{"ingress": true}'
spec:
  type: NodePort
  selector:
    app: db-admin
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80

您可以详细了解 backendconfig 资源以及它需要的 service here