首页 > 解决方案 > GCP GKE - 具有 2 个暴露端口的同一应用程序的原生 GKE 入口的健康检查

问题描述

我有一个应该公开 2 个端口的应用程序,并且该应用程序没有/返回的默认运行状况检查端点200,所以目前,我只为 1 个端口提供自定义运行状况检查端点。我还没有公开另一个端口,因为我不知道如何为同一个应用程序提供另一个自定义运行状况检查端点。

这就是我的 Terraform 配置的样子。

resource "kubernetes_deployment" "core" {
  metadata {
    name = "core"
    labels = {
      app = "core"
    }
  }

  spec {
    replicas = 1

    selector {
      match_labels = {
        app = "core"
      }
    }

    template {
      metadata {
        labels = {
          app = "core"
        }
      }
      spec {
        container {
          name = "core"
          image = "asia.gcr.io/admin/core:${var.app_version}"

          port {
            container_port = 8069
          }

          readiness_probe {
            http_get {
              path = "/web/database/selector"
              port = "8069"
            }
            initial_delay_seconds = 15
            period_seconds = 30
          }

          image_pull_policy = "IfNotPresent"
        }
      }
    }
  }
}

resource "kubernetes_service" "core_service" {
  metadata {
    name = "core-service"
  }
  spec {
    type = "NodePort"
    selector = {
      app = "core"
    }
    port {
      port = 8080
      protocol = "TCP"
      target_port = "8069"
    }
  }
}

如何告诉 GKE 公开另一个端口 (8072) 并为两个端口使用自定义运行状况检查端点?

标签: kubernetesgoogle-cloud-platformterraformgoogle-kubernetes-engine

解决方案


有一个称为GKE Ingress 的功能FrontendConfigBackendConfig 自定义资源定义 (CRD),可让您进一步自定义负载均衡器,您可以使用Unique BackendConfig per Service 端口BackendConfig来为服务的特定端口或端口指定自定义,或者MultiClusterService使用与端口名称或端口号匹配的密钥。Ingress 控制器在BackendConfig为引用的服务端口创建负载均衡器后端服务时使用特定的

使用 aBackendConfig提供自定义负载均衡器健康检查时,您用于负载均衡器健康检查的端口号可能与 Service 的spec.ports[].port编号不同,以下是服务和自定义健康检查的示例:

服务:

apiVersion: v1
kind: Service
metadata:
annotations:
cloud.google.com/backend-config: '{"ports": {
"service-reference-a":"backendconfig-reference-a",
"service-reference-b":"backendconfig-reference-b"
}}'
spec:
  ports:
  - name: port-name-1
     port: port-number-1
     protocol: TCP
     targetPort: 50000
  - name: port-name-2
     port: port-number-2
     protocol: TCP
     targetPort: 8080

自定义健康检查:

apiVersion: cloud.google.com/v1
kind: BackendConfig
metadata:
name: my-backendconfig
 spec:
    healthCheck:
    checkIntervalSec: interval
    timeoutSec: timeout
    healthyThreshold: health-threshold
    unhealthyThreshold: unhealthy-threshold
    type: protocol
    requestPath: path
    port: port

推荐阅读