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

按照使用 Service 連線應用中概述的步驟將你的應用與 Service 連線後,你就擁有了一個持續執行的、已複製的、在網路上公開的應用。本教程將幫助你瞭解 Pod 的終止流程,並探索實現優雅連線排空的方法。

Pod 及其端點的終止過程

經常需要終止 Pod,無論是為了升級還是縮容。為了提高應用程式的可用性,實現適當的活躍連線排空可能很重要。

本教程透過使用簡單的 Nginx Web 伺服器來演示 Pod 終止與相應端點狀態和移除之間的關係。

端點終止的示例流程

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

假設你有一個包含單個 nginx 副本的 Deployment(僅為演示目的)和一個 Service。

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 和 Service。

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

一旦 Pod 和 Service 執行起來,你就可以獲取任何關聯的 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:docs: 將錯別字分解為新的 PR (6a73d0e087)