Ingress 在 HTTP 而不是 TCP 上创建健康检查

Ingress creating health check on HTTP instead of TCP

我实际上正在尝试 运行 我的 gke 集群中的 3 个容器。我通过网络负载均衡器将它们暴露出来,除此之外,我正在使用入口,这样我就可以从带有 SSL 证书的不同域访问我的服务。

这是完整的清单

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app:web
    spec:
      containers:
      - name: web
        image: us-east4-docker.pkg.dev/web:e856485      # docker image
        ports:
        - containerPort: 3000
        env:
        - name: NODE_ENV
          value: production
---
# DEPLOYMENT MANIFEST #
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cms
spec:
  replicas: 3
  selector:
    matchLabels:
      app: cms
  template:
    metadata:
      labels:
        app: cms
    spec:
      containers:
      - name: cms
        image: us-east4-docker.pkg.dev/cms:4e1fe2f      # docker image
        ports:
        - containerPort: 8055
        env:
        - name  : DB
          value : "postgres"

        - name  : DB_HOST
          value : 10.142.0.3

        - name  : DB_PORT
          value : "5432"
---
# DEPLOYMENT MANIFEST #
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api
spec:
  replicas: 3
  selector:
    matchLabels:
      app: api
  template:
    metadata:
      labels:
        app: api
    spec:
      containers:
      - name: api
        image: us-east4-docker.pkg.dev/api:4e1fe2f      # docker image
        ports:
        - containerPort: 8080
        env:
        - name  : HOST
          value : "0.0.0.0"

        - name  : PORT
          value : "8080"
     
        - name  : NODE_ENV
          value : production
---
# SERVICE MANIFEST #
apiVersion: v1
kind: Service
metadata:
  name: web-lb
  annotations:
    cloud.google.com/neg: '{"ingress": true}'
  labels:
    app: web
spec:
  ports:
  - port: 3000
    protocol: TCP
    targetPort: 3000
  selector:
    app: web
  type: NodePort
---
# SERVICE MANIFEST #
apiVersion: v1
kind: Service
metadata:
  name: cms-lb
  annotations:
    cloud.google.com/neg: '{"ingress": true}'
  labels:
    app: cms
spec:
  ports:
  - port: 8055
    protocol: TCP
    targetPort: 8055
  selector:
    app: cms
  type: NodePort
---
# SERVICE MANIFEST #
apiVersion: v1
kind: Service
metadata:
  name: api-lb
  annotations:
    cloud.google.com/neg: '{"ingress": true}'
  labels:
    app: api
spec:
  ports:
  - port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: api
  type: NodePort
---
apiVersion: v1
data:
  tls.crt: abc
  tls.key: abc
kind: Secret
metadata:
  name: web-cert
type: kubernetes.io/tls
---
apiVersion: v1
data:
  tls.crt: abc
  tls.key: abc
kind: Secret
metadata:
  name: cms-cert
type: kubernetes.io/tls
---
apiVersion: v1
data:
  tls.crt: abc
  tls.key: abc
kind: Secret
metadata:
  name: api-cert
type: kubernetes.io/tls
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress
  annotations:
    # If the class annotation is not specified it defaults to "gce".
    kubernetes.io/ingress.class: "gce"
spec:
  tls:
  - secretName: api-cert
  - secretName: cms-cert
  - secretName: web-cert
  rules:
  - host: web-gke.dev
    http:
      paths:
      - pathType: ImplementationSpecific
        backend:
          service:
            name: web-lb
            port:
              number: 3000
  - host: cms-gke.dev
    http:
      paths:
      - pathType: ImplementationSpecific
        backend:
          service:
            name: cms-lb
            port:
              number: 8055
  - host: api-gke.dev
    http:
      paths:
      - pathType: ImplementationSpecific
        backend:
          service:
            name: api-lb
            port:
              number: 8080

容器可通过负载均衡器(网络)访问,但从入口(L7 lb)运行状况检查失败

我尝试将健康检查从 HTTP:80 手动编辑为 TCP:8080/8055/3000 以获取 3 项服务并且它有效。

但最终,ingress 将其恢复为 HTTP 健康检查,但再次失败。我还尝试使用 NodePort 而不是负载均衡器作为服务类型,但没有成功。 有帮助吗?

我想提的第一件事是您需要重新检查您的实施,因为据我所知,您正在创建一个 Ingress,这将创建一个 LoadBanacer,而这个 Ingress 正在使用三个类型为 LoadBalancer 的服务,其中每个服务也将创建其 LoadBalancer (我假设默认行为,除非您应用了著名的解决方法删除 serviceLoadBalancer在创建后手动创建)。

而且我认为这是不正确的,除非您出于某种原因需要该设计。因此,我的建议是您可能希望将服务类型更改为 NodePort.


关于回答你的问题,你缺少的是:

您需要使用自定义 HealthCheck 配置实施 BackendConfig

1- 创建 Backendconfig:

apiVersion: cloud.google.com/v1
kind: BackendConfig
metadata:
  name: api-lb-backendconfig
spec:
  healthCheck:
    checkIntervalSec: INTERVAL
    timeoutSec: TIMEOUT
    healthyThreshold: HEALTH_THRESHOLD
    unhealthyThreshold: UNHEALTHY_THRESHOLD
    type: PROTOCOL
    requestPath: PATH
    port: PORT

2- 在 service/s

中使用此配置
apiVersion: v1
kind: Service
metadata:
  annotations:
    cloud.google.com/backend-config: '{"ports": {
    "PORT_NAME_1":"api-lb-backendconfig"
    }}'
spec:
  ports:
  - name: PORT_NAME_1
    port: PORT_NUMBER_1
    protocol: TCP
    targetPort: TARGET_PORT

应用此类配置后,您的 IngressLoadBalanacer 将使用 BackendConfig "api-lb-backendconfig" 创建


考虑 this 文档页面作为参考。