GKE : ingressをpodのreadinessProbeと連携させる際の注意点

本記事の要点

GKE上で ingress のヘルスチェックに readinessProbe を利用したい場合、以下の3つのPort番号が合致するよう設定してください。ingress の新規作成時に(1)と(2)が合致しない場合、ReadinessProbe を設定していたとしても無視され、標準の / に対するヘルスチェックルールが生成され、L7LBに適用されます。

  1. NodePort 内に記載の targetPort
  2. deployment内に記載の containerPort
  3. deployment内のReadinessProbeに記載の port

Document上の該当箇所

Configure an HTTP readiness probe. Serve a response with an HTTP 200 status to GET requests on the path specified by the readiness probe. The Service exposed through an Ingress must point to the same container port on which the readiness probe is enabled.

GKE Ingress for HTTP(S) load balancing : Health checks

ingress-gceのコード上で実際の条件判定を実施していると思われる箇所

※ 2021.01.29. 現在 https://github.com/kubernetes/ingress-gce/blob/f1355599d82a164c9f6eb98d2c78b214d64cd329/pkg/controller/translator/translator.go#L392-L393

検証方法

  • 以下の3種のmanifest fileを deployment -> nodePort -> ingress の順にデプロイすると、ヘルスチェックの対象パスがデフォルトの / ではなく、readinessProbe で指定したパスになる。(画像1)
  • ↑の確認後デプロイしたk8sリソースを削除したうえで、deployment 内の containerPort を設定の行を削除、再度、deployment -> nodePort -> ingress の順にデプロイすると、ヘルスチェックの対象パスが / になることが確認できる。

画像1 : readinessProbe内のパスが正しくGCP上のヘルスチェックにコピーされた例 f:id:smatsuzaki:20200318210346p:plain

サンプルマニフェスト

deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        ports:
          - containerPort: 80
            protocol: TCP
        readinessProbe:
          httpGet:
            path: /index.html
            port: 80
          failureThreshold: 3
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 3

nodePort

apiVersion: v1
kind: Service
metadata:
  name: nodeport-test
  annotations:
    # Container-native Load Balacingを有効にすることをingress-gceに通知
    # GCP上に専用のNetwork Endpoint Group(NEG) が生成される
    # doc https://cloud.google.com/kubernetes-engine/docs/how-to/container-native-load-balancing
    # src https://github.com/kubernetes/ingress-gce/blob/fbc1bf99d8e557fb3ece891987f977fa2d139048/pkg/annotations/service.go#L37-L45
    cloud.google.com/neg: '{"ingress": true}'
spec:
  type: NodePort
  ports:
    - name: http-port
      protocol: TCP
      port: 80
      targetPort: 80
  selector:
    app: nginx

ingress

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-test
  annotations:
spec:
  rules:
  - http:
      paths:
      - backend:
          serviceName: nodeport-test
          servicePort: 80

参考:k8sのProbeは 302 を許容するが、GCE ingress200 以外は全てNGと判定する

/* https://sunrise033.com/entry/hatena-blog-how-to-hierarchicalize-categories */