在 Kubernetes 上将 Active Directory 与 AlloyDB Omni 集成

选择文档版本:

本文档介绍了如何在基于 Kubernetes 的 AlloyDB Omni 数据库集群上启用 Active Directory 集成,以允许基于 Active Directory 的现有用户访问 AlloyDB Omni 数据库。Active Directory 集成使用 GSSAPI 为通过 Kerberos 机制进行身份验证的用户提供授权,以便他们访问 AlloyDB Omni。

AlloyDB Omni 中的 Active Directory 配置是可选项,默认处于停用状态。只有使用 Active Directory 服务器进行身份验证的环境才能使用此配置机制。

本文档假定您熟悉如何应用 Kubernetes 清单文件以及如何使用 kubectl 命令行工具。如需了解详情,请参阅命令行工具 (kubectl)

准备工作

如需启用 Active Directory 集成,您必须拥有以下各项:

  • 部署在 Kubernetes 环境中的 AlloyDB Omni 集群
  • Active Directory 服务器 keytab

启用 Active Directory 身份验证

如需启用 Active Directory 身份验证,请运行以下 Helm 命令:

helm upgrade alloydbomni-operator PATH_TO_CHART -n alloydb-omni-system --set userDefinedAuthentication.enabled=true

该命令返回以下输出:

Release "alloydbomni-operator" has been upgraded. Happy Helming!
NAME: alloydbomni-operator
LAST DEPLOYED: CURRENT_TIMESTAMP
NAMESPACE: alloydb-omni-system
STATUS: deployed
REVISION: 2
TEST SUITE: None

检查 Active Directory 身份验证状态

如需确定 Active Directory 身份验证的状态,请按照以下步骤操作:

  1. 通过运行以下命令获取舰队控制器的当前部署 args 并确保 enable-user-defined-authentication 设置为 true

    kubectl get deployment -n alloydb-omni-system fleet-controller-manager -o json | jq '.spec.template.spec.containers[0].args'
    
    [
      "--health-probe-bind-address=:8081",
      "--metrics-bind-address=127.0.0.1:8080",
      "--leader-elect",
      "--image-registry=gcr.io",
      ...
      "--enable-user-defined-authentication=true"
    ]
    
  2. 通过运行以下命令获取本地控制器的当前部署参数并确保 enable-user-defined-authentication 设置为 true

    kubectl get deployment -n alloydb-omni-system local-controller-manager -o json | jq '.spec.template.spec.containers[0].args'
    
    [
      "--health-probe-bind-address=:8081",
      "--metrics-bind-address=127.0.0.1:8080",
      "--leader-elect",
      "--deployment-platform=generic-k8s",
      "--enable-backup-from-standby=true",
      "--enable-user-defined-authentication=true"
    ]
    

配置 Active Directory 支持

  1. 使用 pg_hba.conf 文件条目创建并应用 ConfigMap:

    kubectl apply -f - 
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: pg-hba-config
    data:
      pg_hba_entries: |
        # Active Directory-based users
        hostgssenc all all 0.0.0.0/0    gss
        hostgssenc all all ::1/128      gss
        # Database-based users
        hostssl    all all 0.0.0.0/0  scram-sha-256
        hostssl    all all ::/0       scram-sha-256
    EOF
    

    根据您的要求修改这些条目。这些条目会覆盖 pg_hba.conf 中的默认条目。如果您添加了无效的配置,用户将无法登录。

    在上面的示例中,您添加了基于 GSS 的身份验证条目,然后添加了基于密码的身份验证条目。这意味着用户已在使用 GSS API 登录。如果此登录方法失败,则会使用基于密码的身份验证作为后备方案。

    如需了解详情,请参阅 pg_hba.conf 文件

  2. 使用 keytab 创建并应用 Secret:

     kubectl apply -f - 
     apiVersion: v1
     kind: Secret
     metadata:
       name: db-keytab-dbcluster-sample
     type: Opaque
     data:
       krb5.keytab: |
        BASE64_ENCODED_KEYTAB
     EOF
     

  3. 可选:使用 pg_ident.conf 条目创建并应用 ConfigMap。

      kubectl apply -f - EOF
      apiVersion: v1
      kind: ConfigMap
      metadata:
        name: pg-ident-config
      data:
        pg_ident_entries: |
    MAP_NAME /^(.)@YOUR.REALM$/ \1 MAP_NAME /^(.)@your.realm$/ \1 EOF

    如需实现用户名映射,请在 pg_hba.conf 文件的选项字段中指定 map=MAP_NAME

    如需了解详情,请参阅 Ident 映射

  4. 如需启用 Active Directory 集成,请向 DBCluster 规范添加注解。

    kubectl apply -f - DB_CLUSTER_NAME
    type: Opaque
    data:
      DB_CLUSTER_NAME: "ENCODED_PASSWORD"
    ---
    apiVersion: alloydbomni.dbadmin.goog/v1
    kind: DBCluster
    metadata:
      name: DB_CLUSTER_NAME
      annotations:
        dbs.dbadmin.goog.com/pg-hba-config-map: pg-hba-config
        dbs.dbadmin.goog.com/pg-ident-config-map: pg-ident-config
        dbs.dbadmin.goog.com/keytab-ref: db-keytab-dbcluster-sample
    spec:
      databaseVersion: "DB_VERSION"
      primarySpec:
        adminUser:
          passwordRef:
            name: db-pw-DB_CLUSTER_NAME
        resources:
          memory: MEMORY_SIZE
          cpu: CPU_COUNT
          disks:
          - name: DataDisk
            size: DISK_SIZE
    EOF
    

以 Active Directory 用户身份创建数据库角色

  1. 在数据库中创建与 Active Directory 用户匹配的角色。如需为 Active Directory 用户创建角色,请连接到集群并运行以下命令:

    username=# CREATE ROLE "USERNAME@REALM" WITH LOGIN;
    
  2. 使用 Active Directory 用户身份登录数据库。您必须在所连接的客户端中启用 kinit

    kubectl exec -it postgres -n DB_CLUSTER_NAMESPACE -- bash
    root:/# kinit USERNAME
    Password for USERNAME@REALM:
    
    root:/# psql -h HOSTNAME -d DB_NAME -U USERNAME@REALM
    psql (16.6 (Ubuntu 16.6-0ubuntu0.24.04.1), server 16.3)
    GSSAPI-encrypted connection
    Type "help" for help.
    
  3. 运行 SQL 查询。

    username=# select * from ;
    

停用 Active Directory 身份验证

如需停用 Active Directory 身份验证,请运行以下 Helm 命令:

helm upgrade alloydbomni-operator PATH_TO_CHART -n alloydb-omni-system --set userDefinedAuthentication.enabled=false

您可以检查 Active Directory 身份验证状态

该命令返回以下输出:

Release "alloydbomni-operator" has been upgraded. Happy Helming!
NAME: alloydbomni-operator
LAST DEPLOYED: CURRENT_TIMESTAMP
NAMESPACE: alloydb-omni-system
STATUS: deployed
REVISION: 2
TEST SUITE: None