Docs 菜单
Docs 主页
/
MongoDB Enterprise Kubernetes Operator
/ /

使用 SCRAM 身份验证管理数据库用户

在此页面上

  • 注意事项
  • 先决条件
  • 添加数据库用户
  • 删除数据库用户
  • 更改身份验证机制

Kubernetes 操作符 支持在 MongoDB 部署上使用 SCRAM 身份验证来管理数据库用户。

当您指定 SCRAM作为身份验证机制时,所使用的 SCRAM 实施取决于:

  • MongoDB 的版本和

  • 如果数据库是应用程序数据库或其他数据库。

MongoDB 版本
数据库
SCRAM 实施
3.6 或更早版本
除应用程序数据库之外的任何数据库
SCRAM-SHA-1
4.0 或更高版本
除应用程序数据库之外的任何数据库
SCRAM-SHA-256
任何
应用程序数据库
SCRAM-SHA-1

Kubernetes Operator 在其创建的部署中支持 SCRAM、LDAP 和 X. 509身份验证机制。 在Kubernetes Operator 创建的部署中,您不能使用 MongoDB Ops Manager执行以下操作:

  • 为部署配置其他身份验证机制。

  • 托管使用 SCRAM、LDAP 或 X.509 身份验证的用户。

启用 SCRAM 身份验证后,您可以使用 Ops Manager 界面或通过在 CustomResourceDefinition 中配置用户来添加 SCRAM 用户。 基于 MongoDB 用户资源规范。

在管理数据库用户之前,您必须部署独立运行的实例副本集分片集群。

对于多 Kubernetes 集群 MongoDB 部署,您必须部署副本集。 请参阅部署多个集群。

重要

您不能将同一数据库用户分配给多个 MongoDB独立运行的实例副本集分片集群。这包括具有 admin角色的数据库用户。

1

如果您尚未执行,请运行以下命令,执行所创建命名空间的所有 kubectl 命令。

注意

MongoDB Ops Manager如果要在多 Kubernetes 集群 部署中部署MongoDB 资源:

  • context 设置为中心集群的名称,例如:kubectl config set context "$MDB_CENTRAL_CLUSTER_FULL_NAME"

  • --namespace设置为您用于多 Kubernetes 集群 MongoDB 部署的相同范围,例如: kubectl config --namespace "mongodb"

kubectl config set-context $(kubectl config current-context) --namespace=<metadata.namespace>
2

您可以选择使用明文密码:

1---
2apiVersion: v1
3kind: Secret
4metadata:
5 name: <mms-user-1-password>
6 # corresponds to user.spec.passwordSecretKeyRef.name
7type: Opaque
8stringData:
9 password: <my-plain-text-password>
10 # corresponds to user.spec.passwordSecretKeyRef.key
11data:
12 password: <base-64-encoded-password>
13 # corresponds to user.spec.passwordSecretKeyRef.key
14...

或者您可以选择使用 Base64 编码的密码:

1---
2apiVersion: v1
3kind: Secret
4metadata:
5 name: <mms-user-1-password>
6 # corresponds to user.spec.passwordSecretKeyRef.name
7type: Opaque
8stringData:
9 password: <my-plain-text-password>
10 # corresponds to user.spec.passwordSecretKeyRef.key
11data:
12 password: <base-64-encoded-password>
13 # corresponds to user.spec.passwordSecretKeyRef.key
14...

注意

确保复制所需的密码配置。 明文密码使用stringData.password ,Base64 编码密码使用data.password

3
  1. 打开您首选的文本编辑器。

  2. 将此用户密钥粘贴到新的文本文件中。

如果您使用 HashiCorp Vault 作为您的 Secret 存储工具,则可以创建 Vault Secret

要了解有关 Secret 存储的选项,请参阅配置 Secret 存储。

4

使用下表作为指南更改密钥中突出显示的行:

类型
说明
例子
metadata.name
字符串

数据库密码名称。

资源名称必须少于或等于 44 个字符。

mms-scram-user-1-password
stringData.password
字符串

所需用户的明文密码。

注意

使用此选项和值data.password 。 不能同时使用。

<my-plain-text-password>
data.password
字符串

所需用户的 Base64 编码密码。

注意

  • 使用此选项和值stringData.password 。 不能同时使用。

  • 您必须自行将密码编码为 Base64,然后使用此选项粘贴结果值。 几乎每个平台都有适用的工具,还有多种基于 Web 的工具。

<my-base64-encoded-password>
5
1
---
apiVersion: mongodb.com/v1
kind: MongoDBUser
metadata:
name: <mms-scram-user-1>
spec:
passwordSecretKeyRef:
name: <mms-user-1-password>
# Match to metadata.name of the User Secret
key: password
username: "<mms-scram-user-1>"
db: "admin" #
mongodbResourceRef:
name: "<my-replica-set>"
# Match to MongoDB resource using authenticaiton
roles:
- db: "admin"
name: "clusterAdmin"
- db: "admin"
name: "userAdminAnyDatabase"
- db: "admin"
name: "readWrite"
- db: "admin"
name: "userAdminAnyDatabase"
...
2
  1. 打开您首选的文本编辑器。

  2. 将此 MongoDBUser 粘贴到新的 YAML 文件中。

3

使用下表作为指南,更改MongoDB 用户Resource规范中突出显示的行:

类型
说明
例子
metadata.name
字符串

数据库用户资源的名称。

资源名称必须少于或等于 44 个字符。

mms-scram-user-1
spec.username
字符串
数据库用户的名称。
mms-scram-user-1
spec.passwordSecretKeyRef.name
字符串
metadata.name 存储用户密码的密钥值。
my-resource
spec.mongodbResourceRef.name
字符串
与该用户关联的MongoDB 资源的名称。
my-resource
spec.roles.db
字符串
角色可以执行操作的数据库。
admin
spec.roles.name
字符串
要授予数据库用户的角色名称。 角色名称可以是任何 MongoDB内置 角色 Cloud ManagerMongoDB Ops Manager,也可以是 或 中存在的 自定义角色 。
readWriteAnyDatabase
4

您可以授予该用户其他角色。

5
6

调用以下 Kubernetes 命令来创建数据库用户:

kubectl apply -f <database-user-conf>.yaml

当您创建新的MongoDB database 用户时,Kubernetes Operator 会自动创建新的Kubernetes 密钥 。Kubernetes 的 秘密 包含有关新数据库用户的以下信息:

  • username:数据库用户的用户名

  • password:数据库用户的密码

  • connectionString.standard标准连接string ,可以让您以此数据库用户的身份连接到数据库。

  • connectionString.standardSrvDNS 种子列表连接string ,可以让您以此数据库用户的身份连接到数据库。

注意

或者,您可以在MongoDB 用户资源规范中指定可选的spec.connectionStringSecretName字段,以指定 Kubernetes Operator 创建的连接字符串密钥的名称。

您可以使用这些凭证从 Kubernetes 内部连接到 MongoDB database 资源。

7

您可以在 Cloud Manager 或 Ops Manager 中查看新创建的用户:

  1. 从项目的 Deployment 视图中,点击 Security 选项卡。

  2. 单击 MongoDB Users 嵌套标签页。

要删除数据库用户,请将metadata.name从用户 MongoDBUser 传递给以下命令:

kubectl delete mdbu <metadata.name>

要将用户身份验证机制更改为 SCRAM:

  1. 禁用身份验证。

    spec.security.authentication下,将enabled更改为false

    spec:
    security:
    authentication:
    enabled : false
  2. 重新应用用户的资源定义。

  3. 等待 MongoDBResource 进入running状态。

  4. 启用 SCRAM 身份验证。

    spec.security.authentication下,将enabled更改为true并将spec.security.authentication.modes设置为 `` ["SCRAM"]``。

    spec:
    security:
    authentication:
    enabled : true
    modes: ["SCRAM"]
  5. 重新应用 MongoDBUser 资源。

  6. 等待 MongoDBResource 进入running状态。