探索 Pod 和其端點的終止行為

一旦您依照 使用服務連接應用程式 中概述的步驟將您的應用程式與服務連接後,您就有一個持續執行、複寫的應用程式,該應用程式在網路上公開。本教學課程可協助您查看 Pod 的終止流程,並探索實作優雅連線耗盡的方法。

Pod 和其端點的終止程序

在許多情況下,您需要終止 Pod - 無論是為了升級還是縮減規模。為了提高應用程式可用性,實作適當的活動連線耗盡可能非常重要。

本教學課程說明 Pod 終止流程,其中包含對應的端點狀態和移除,並使用簡單的 nginx Web 伺服器來示範此概念。

端點終止的範例流程

以下是在 Pod 終止 文件中描述的範例流程。

假設您有一個 Deployment 包含單一 nginx 副本 (例如僅為了示範目的) 和一個服務

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      terminationGracePeriodSeconds: 120 # extra long grace period
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
        lifecycle:
          preStop:
            exec:
              # Real life termination may take any time up to terminationGracePeriodSeconds.
              # In this example - just hang around for at least the duration of terminationGracePeriodSeconds,
              # at 120 seconds container will be forcibly terminated.
              # Note, all this time nginx will keep processing requests.
              command: [
                "/bin/sh", "-c", "sleep 180"
              ]
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

現在使用上述檔案建立 Deployment Pod 和服務

kubectl apply -f pod-with-graceful-termination.yaml
kubectl apply -f explore-graceful-termination-nginx.yaml

一旦 Pod 和服務正在執行,您就可以取得任何相關聯的 EndpointSlice 的名稱

kubectl get endpointslice

輸出類似於此

NAME                  ADDRESSTYPE   PORTS   ENDPOINTS                 AGE
nginx-service-6tjbr   IPv4          80      10.12.1.199,10.12.1.201   22m

您可以查看其狀態,並驗證是否已註冊一個端點

kubectl get endpointslices -o json -l kubernetes.io/service-name=nginx-service

輸出類似於此

{
    "addressType": "IPv4",
    "apiVersion": "discovery.k8s.io/v1",
    "endpoints": [
        {
            "addresses": [
                "10.12.1.201"
            ],
            "conditions": {
                "ready": true,
                "serving": true,
                "terminating": false

現在讓我們終止 Pod 並驗證 Pod 是否在尊重優雅終止期間組態的情況下終止

kubectl delete pod nginx-deployment-7768647bf9-b4b9s

所有 Pod

kubectl get pods

輸出類似於此

NAME                                READY   STATUS        RESTARTS      AGE
nginx-deployment-7768647bf9-b4b9s   1/1     Terminating   0             4m1s
nginx-deployment-7768647bf9-rkxlw   1/1     Running       0             8s

您可以看到已排程新的 Pod。

當為新的 Pod 建立新的端點時,舊的端點仍然存在於終止狀態

kubectl get endpointslice -o json nginx-service-6tjbr

輸出類似於此

{
    "addressType": "IPv4",
    "apiVersion": "discovery.k8s.io/v1",
    "endpoints": [
        {
            "addresses": [
                "10.12.1.201"
            ],
            "conditions": {
                "ready": false,
                "serving": true,
                "terminating": true
            },
            "nodeName": "gke-main-default-pool-dca1511c-d17b",
            "targetRef": {
                "kind": "Pod",
                "name": "nginx-deployment-7768647bf9-b4b9s",
                "namespace": "default",
                "uid": "66fa831c-7eb2-407f-bd2c-f96dfe841478"
            },
            "zone": "us-central1-c"
        },
        {
            "addresses": [
                "10.12.1.202"
            ],
            "conditions": {
                "ready": true,
                "serving": true,
                "terminating": false
            },
            "nodeName": "gke-main-default-pool-dca1511c-d17b",
            "targetRef": {
                "kind": "Pod",
                "name": "nginx-deployment-7768647bf9-rkxlw",
                "namespace": "default",
                "uid": "722b1cbe-dcd7-4ed4-8928-4a4d0e2bbe35"
            },
            "zone": "us-central1-c"

這允許應用程式在終止期間傳達其狀態,並允許用戶端 (例如負載平衡器) 實作連線耗盡功能。這些用戶端可能會偵測到終止端點,並為其實作特殊的邏輯。

在 Kubernetes 中,正在終止的端點的 ready 狀態始終設定為 false。這需要為了向後相容性而發生,因此現有的負載平衡器不會將其用於常規流量。如果需要終止 Pod 上的流量耗盡,則可以將實際就緒狀態檢查為條件 serving

當 Pod 被刪除時,舊的端點也將被刪除。

下一步

上次修改時間為 2024 年 11 月 18 日下午 6:41 PST:docs: factor out typos into new pr (6a73d0e087)