在 Kubernetes 中對資料庫叢集使用維護模式

本頁面說明如何在 Kubernetes 叢集中,將資料庫設為維護模式。

如果資料庫在 Kubernetes 叢集中執行,您可以停用在容器化應用程式中執行的 liveness 探測和啟動探測,將資料庫設為維護模式,確保資料庫 Pod 處於健康狀態。當您需要進行維護更新或修復 Pod 時,維護模式可協助您將 Pod 置於執行狀態。

啟用維護模式

  1. 確認資料庫 Pod 上有探測:

    1. 有效性探測:

      kubectl get po -n NAMESPACE DATABASE_POD_NAME  -o jsonpath='{.spec.containers[?(@.name=="database")].livenessProbe}'

      更改下列內容:

      • NAMESPACE:用於執行資料庫的命名空間名稱,例如 db。
      • DATABASE_POD_NAME:資料庫 Pod 的名稱,例如 al-4017-dbcluster-sample-0。

      以下是存活度探測的輸出內容範例:

      {"failureThreshold":3,"httpGet":{"path":"/healthz?","port":8090,"scheme":"HTTP"},"periodSeconds":10,"successThreshold":1,"timeoutSeconds":3}
      
    2. 啟動探測:

      kubectl get po -n NAMESPACE DATABASE_POD_NAME -o jsonpath='{.spec.containers[?(@.name=="database")].startupProbe}'

      以下顯示含有啟動探針的輸出內容範例:

      {"exec":{"command":["/usr/lib/postgresql/15/bin/psql","-hlocalhost","-Ualloydbadmin","-c","SELECT 1"]},"failureThreshold":180,"periodSeconds":10,"successThreshold":1,"timeoutSeconds":1}
      
  2. 在 DBCluster 資訊清單的 spec 區段中,將 mode 設為 maintenance:

    apiVersion: alloydbomni.dbadmin.goog/v1
    kind: DBCluster
    metadata:
      name: DB_CLUSTER_NAME
      namespace: NAMESPACE
    spec:
      allowExternalIncomingTraffic: false
      isDeleted: false
      mode: maintenance
      primarySpec:
      ...
    

    將 DB_CLUSTER_NAME 替換為這個資料庫叢集的名稱,例如 my-db-cluster。

  3. 套用資訊清單:

    kubectl apply -f DB_CLUSTER_YAML

    將 DB_CLUSTER_YAML 替換為這個資料庫叢集資訊清單檔案的名稱,例如 alloydb-omni-db-cluster.yaml。

  4. 執行下列指令,確認維護模式已啟用:

    • 有效性探測:

      kubectl get po -n NAMESPACE DATABASE_POD_NAME  -o jsonpath='{.spec.containers[?(@.name=="database")].livenessProbe}'
    • 啟動探測:

      kubectl get po -n NAMESPACE DATABASE_POD_NAME  -o jsonpath='{.spec.containers[?(@.name=="database")].startupProbe}'

    輸出內容的 POD_NAME Pod spec 區段不含 Liveness 和 Startup 欄位。

停用維護模式

如要恢復標準資料庫叢集行為,並執行存活和啟動探查,請使用下列指令從 DBCluster 資訊清單的 spec 區段中移除 maintenance:

kubectl patch dbclusters.alloydbomni.dbadmin.goog DB_CLUSTER_NAME -p '{"spec":{"mode":""}}' --type=merge -n DB_CLUSTER_NAMESPACE --type=merge

輸出內容範例:

  • 使用有效性探測:

    {"failureThreshold":3,"httpGet":{"path":"/healthz?","port":8090,"scheme":"HTTP"},"periodSeconds":10,"successThreshold":1,"timeoutSeconds":3}
    
  • 使用啟動探測:

    {"exec":{"command":["/usr/lib/postgresql/15/bin/psql","-hlocalhost","-Ualloydbadmin","-c","SELECT 1"]},"failureThreshold":180,  "periodSeconds":10,"successThreshold":1,"timeoutSeconds":1}