在 Kubernetes 中对数据库集群使用维护模式

本页介绍了如何在 Kubernetes 集群中将数据库置于维护模式。

如果您的数据库在 Kubernetes 集群中运行,您可以通过停用在容器化应用中运行的活跃性探测启动探测,将数据库置于维护模式,以确保数据库 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 部分中的 LivenessStartup 字段。

停用维护模式

如需恢复到具有运行状况和启动探测的标准数据库集群行为,请使用以下命令从 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}