对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

为Kubernetes Operator 部署设置MongoDB控制器范围

在安装 Kubernetes Operator 之前,您可以设置 Kubernetes Operator 部署的范围。 范围取决于您选择在其中部署 Ops Manager 和MongoDB资源的命名空间。

您可以设置以下范围之一:

You can set the scope for the Kubernetes Operator to use the same namespace as resources. In this case, the Kubernetes Operator watches Ops Manager and MongoDB resources in that same namespace.

安装 Kubernetes 操作符时,它会使用默认命名空间。

You can set the scope for the Kubernetes Operator to use one or more namespaces that differ from the namespace used by the Kubernetes Operator resources. In this case, the Kubernetes Operator watches Ops Manager and MongoDB resources in a subset of namespaces that you specify.

要安装具有此范围的 Kubernetes 操作符 instances,请将helm操作符.watchNamespace结合使用参数。

在单个 Kubernetes 操作符实例监视不同集群资源类型的部署中,监视命名空间子集非常有用。例如,您可以将 Kubernetes 操作符 配置为监视一个命名空间子集中的MongoDB资源,并监视另一命名空间子集中的MongoDBMultiCluster资源。为了避免在资源协调期间出现竞争条件,对于您希望 Kubernetes 操作符监视的每种自定义资源类型,请确保将范围设置为命名空间的不同子集。

按照helm的相关安装说明进行操作,但在Operator.watchNamespace中指定一个或多个命名空间 Kubernetes Operator 要监视的参数:

# Watch one namespace
helm install mongodb-kubernetes-operator mongodb/mongodb-kubernetes \
--set operator.watchNamespace='namespace-to-watch' <...>
# Watch both namespace-a and namespace-b
helm install mongodb-kubernetes-operator mongodb/mongodb-kubernetes \
--set operator.watchNamespace="namespace-a\,namespace-b"
# Operator with name `mongodb-kubernetes-operator-qa-envs` will
# watch ns-dev, ns-qa and ns-uat namespaces
helm install mongodb-kubernetes-operator-qa-envs mongodb/mongodb-kubernetes \
--set operator.watchNamespace="ns-dev\,ns-qa\,ns-uat"
# Operator with name `mongodb-kubernetes-operator-staging` will
# watch ns-staging and ns-pre-prod
helm install mongodb-kubernetes-operator-staging mongodb/mongodb-kubernetes --set operator.watchNamespace="ns-staging\,ns-pre-prod"

安装 Kubernetes Operator 以监控一个或多个命名空间(部署 Kubernetes Operator 的命名空间以外)中的资源时:

  1. 创建以下资源:

  2. ClusterRole ClusterRoleBinding 包含在安装期间应用的默认配置文件中。

  3. 创建本地Kubernetes ServiceAccounts:

    为每个命名空间创建以下部分或全部本地Kubernetes ServiceAccounts:

    • 如果要在命名空间中部署 MongoDB 实例,请使用mongodb-kubernetes-database-pods

    • 如果要在命名空间中部署 Ops Manager,请使用mongodb-kubernetes-appdbmongodb-kubernetes-ops-manager

以下示例说明了 ClusterRoleClusterRoleBinding 如何在集群中协同工作。

假设您在mongodb命名空间中创建一个 ServiceAccount,然后在此命名空间中安装 Kubernetes 操作符。Kubernetes 操作符 使用此 ServiceAccount。

要将 Kubernetes 操作符范围设置为监视命名空间ns1ns2 ,请执行以下操作:

  1. 使用这些特权,创建一个集群范围的、无命名空间的ClusterRole

  2. Create a ClusterRoleBinding in three namespaces: mongodb, ns1 and ns2. This ClusterRoleBinding will bind the ClusterRole to the ServiceAccount in the mongodb namespace. The clusterRoleBinding will allow the Kubernetes Operator deployed in the mongodb namespace to access the resources described in the clusterRole of the target namespace, that is, in mongodb, ns1 and ns2.

另请参阅Operator.watchNamespace。

You can set the scope for the Kubernetes Operator to the Kubernetes cluster. In this case, the Kubernetes Operator watches Ops Manager and MongoDB resources in all namespaces in the Kubernetes cluster.

重要

对于每个 Kubernetes 集群,您只能部署一个具有集群范围范围的 Kubernetes Operator 实例。

要为 Kubernetes Operator 设置集群范围的作用域,请按照您的首选安装方法的说明进行操作。

  1. Set the spec.template.spec.containers.name.env.name:WATCH_NAMESPACE in mongodb-kubernetes.yaml to "*". You must include the double quotation marks (") around the asterisk (*) in the YAML file.

    WATCH_NAMESPACE: "*"
  2. In mongodb-kubernetes.yaml, change:

    kind: Role
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
    name: mongodb-kubernetes-operator

    至:

    kind: ClusterRole
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
    name: mongodb-kubernetes-operator
  3. 将以下代码添加到刚刚修改的ClusterRole中:

    - apiGroups:
    - ""
    resources:
    - namespaces
    verbs:
    - list
    - watch
  4. In mongodb-kubernetes.yaml, change:

    kind: RoleBinding
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
    name: mongodb-kubernetes-operator
    namespace: mongodb
    roleRef:
    apiGroup: rbac.authorization.k8s.io
    kind: Role
    name: mongodb-kubernetes-operator
    subjects:
    - kind: ServiceAccount
    name: mongodb-kubernetes-operator
    namespace: mongodb

    至:

    kind: ClusterRoleBinding
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
    name: mongodb-kubernetes-operator
    namespace: mongodb
    roleRef:
    apiGroup: rbac.authorization.k8s.io
    kind: ClusterRole
    name: mongodb-kubernetes-operator
    subjects:
    - kind: ServiceAccount
    name: mongodb-kubernetes-operator
    namespace: mongodb
  5. mongodb-kubernetes.yaml文件中,将<namespace>值更改为您希望Kubernetes Operator部署资源并应用YAML文件的命名空间。

    1---
    2kind: ServiceAccount
    3apiVersion: v1
    4metadata:
    5 name: mongodb-kubernetes-appdb
    6 namespace: <namespace>
    7---
    8kind: ServiceAccount
    9apiVersion: v1
    10metadata:
    11 name: mongodb-kubernetes-database-pods
    12 namespace: <namespace>
    13---
    14kind: ServiceAccount
    15apiVersion: v1
    16metadata:
    17 name: mongodb-kubernetes-ops-manager
    18 namespace: <namespace>
    19---
    20kind: Role
    21apiVersion: rbac.authorization.k8s.io/v1
    22metadata:
    23 name: mongodb-kubernetes-appdb
    24 namespace: <namespace>
    25rules:
    26 - apiGroups:
    27 - ""
    28 resources:
    29 - secrets
    30 verbs:
    31 - get
    32 - apiGroups:
    33 - ""
    34 resources:
    35 - pods
    36 verbs:
    37 - patch
    38---
    39kind: RoleBinding
    40apiVersion: rbac.authorization.k8s.io/v1
    41metadata:
    42 name: mongodb-kubernetes-appdb
    43 namespace: <namespace>
    44roleRef:
    45 apiGroup: rbac.authorization.k8s.io
    46 kind: Role
    47 name: mongodb-kubernetes-appdb
    48subjects:
    49 - kind: ServiceAccount
    50 name: mongodb-kubernetes-appdb
    51 namespace: <namespace>
    52...
  6. 创建本地Kubernetes服务帐户

    为每个命名空间创建以下部分或全部本地Kubernetes 服务帐户

    • 如果要在命名空间中部署 MongoDB 实例,请使用mongodb-kubernetes-database-pods

    • 如果要在命名空间中部署 Ops Manager,请使用mongodb-kubernetes-appdbmongodb-kubernetes-ops-manager

    复制并粘贴适用的示例,并将<namespace>值替换为标识命名空间的标签。

    ---
    kind: ServiceAccount
    apiVersion: v1
    metadata:
    name: mongodb-kubernetes-database-pods
    namespace: <namespace>
    ---
    kind: ServiceAccount
    apiVersion: v1
    metadata:
    name: mongodb-kubernetes-appdb
    namespace: <namespace>
    ---
    kind: ServiceAccount
    apiVersion: v1
    metadata:
    name: mongodb-kubernetes-ops-manager
    namespace: <namespace>

在部署Kubernetes Operator 之前,请配置以下项目:

  1. 配置Kubernetes Operator 以监视所有命名空间:

    helm install mongodb-kubernetes-operator mongodb/mongodb-kubernetes \
    --set operator.watchNamespace="*"
  2. 创建本地Kubernetes服务帐户

    为每个命名空间创建以下部分或全部本地Kubernetes 服务帐户

    • 如果要在命名空间中部署 MongoDB 实例,请使用mongodb-kubernetes-database-pods

    • 如果要在命名空间中部署 Ops Manager,请使用mongodb-kubernetes-appdbmongodb-kubernetes-ops-manager

    复制并粘贴适用的示例,并将<namespace>值替换为标识命名空间的标签。

    helm template mongodb/mongodb-kubernetes \
    --set operator.namespace=<metadata.namespace> \
    --show-only templates/database-roles.yaml | kubectl apply -f -

在部署Kubernetes Operator 之前,请配置以下项目:

  1. Set the spec.template.spec.containers.name.env.name:WATCH_NAMESPACE in mongodb-kubernetes-openshift.yaml to "*". You must include the double quotation marks (") around the asterisk (*) in the YAML file.

    WATCH_NAMESPACE: "*"
  2. 为这些帐户创建相应的角色。在 mongodb-kubernetes-openshift.yaml 中,更改:

    kind: Role
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
    name: enterprise-operator

    至:

    kind: ClusterRole
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
    name: enterprise-operator
  3. 将以下代码添加到刚刚修改的ClusterRole中:

    - apiGroups:
    - ""
    resources:
    - namespaces
    verbs:
    - list
    - watch
  4. mongodb-kubernetes-openshift.yaml 中,更改:

    kind: RoleBinding
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
    name: enterprise-operator
    namespace: mongodb
    roleRef:
    apiGroup: rbac.authorization.k8s.io
    kind: Role
    name: enterprise-operator
    subjects:
    - kind: ServiceAccount
    name: enterprise-operator
    namespace: mongodb

    至:

    kind: ClusterRoleBinding
    apiVersion: rbac.authorization.k8s.io/v1
    metadata:
    name: enterprise-operator
    namespace: mongodb
    roleRef:
    apiGroup: rbac.authorization.k8s.io
    kind: ClusterRole
    name: enterprise-operator
    subjects:
    - kind: ServiceAccount
    name: enterprise-operator
    namespace: mongodb
  5. Create the secret only in the namespace where you will deploy the Kubernetes Operator. If you deploy MongoDB resources in multiple namespaces or with a cluster-wide scope, the Kubernetes Operator synchronizes the secret across all watched namespaces. To learn more, see the registry.imagePullSecrets setting in the Helm installation settings.

    mongodb-kubernetes.yaml文件中,将 <namespace> 替换为要安装Kubernetes Operator 的命名空间。使用 oc 或 OpenShift Container Platform 用户界面应用生成的 YAML 文件。

    1---
    2kind: ServiceAccount
    3apiVersion: v1
    4metadata:
    5 name: mongodb-kubernetes-appdb
    6 namespace: <namespace>
    7---
    8kind: ServiceAccount
    9apiVersion: v1
    10metadata:
    11 name: mongodb-kubernetes-database-pods
    12 namespace: <namespace>
    13---
    14kind: ServiceAccount
    15apiVersion: v1
    16metadata:
    17 name: mongodb-kubernetes-ops-manager
    18 namespace: <namespace>
    19---
    20kind: Role
    21apiVersion: rbac.authorization.k8s.io/v1
    22metadata:
    23 name: mongodb-kubernetes-appdb
    24 namespace: <namespace>
    25rules:
    26 - apiGroups:
    27 - ""
    28 resources:
    29 - secrets
    30 verbs:
    31 - get
    32 - apiGroups:
    33 - ""
    34 resources:
    35 - pods
    36 verbs:
    37 - patch
    38---
    39kind: RoleBinding
    40apiVersion: rbac.authorization.k8s.io/v1
    41metadata:
    42 name: mongodb-kubernetes-appdb
    43 namespace: <namespace>
    44roleRef:
    45 apiGroup: rbac.authorization.k8s.io
    46 kind: Role
    47 name: mongodb-kubernetes-appdb
    48subjects:
    49 - kind: ServiceAccount
    50 name: mongodb-kubernetes-appdb
    51 namespace: <namespace>
    52...
  6. 创建本地Kubernetes服务帐户

    为每个命名空间创建以下部分或全部本地Kubernetes 服务帐户

    • 如果要在命名空间中部署 MongoDB 实例,请使用mongodb-kubernetes-database-pods

    • 如果要在命名空间中部署 Ops Manager,请使用mongodb-kubernetes-appdbmongodb-kubernetes-ops-manager

    复制并粘贴适用的示例,并将<namespace>值替换为标识命名空间的标签。

在部署Kubernetes Operator 之前,请配置以下项目:

  1. 配置Kubernetes Operator 以监视所有命名空间:

    helm install mongodb-kubernetes-operator mongodb/mongodb-kubernetes \
    --set operator.watchNamespace="*" \
  2. Create the secret only in the namespace where you will deploy the Kubernetes Operator. If you deploy MongoDB resources in multiple namespaces or with a cluster-wide scope, the Kubernetes Operator synchronizes the secret across all watched namespaces. To learn more, see the registry.imagePullSecrets setting in the Helm installation settings.

    mongodb-kubernetes.yaml文件中,将 <namespace> 替换为要安装Kubernetes Operator 的命名空间。使用 oc 或 OpenShift Container Platform 用户界面应用生成的 YAML 文件。

    1---
    2kind: ServiceAccount
    3apiVersion: v1
    4metadata:
    5 name: mongodb-kubernetes-appdb
    6 namespace: <namespace>
    7---
    8kind: ServiceAccount
    9apiVersion: v1
    10metadata:
    11 name: mongodb-kubernetes-database-pods
    12 namespace: <namespace>
    13---
    14kind: ServiceAccount
    15apiVersion: v1
    16metadata:
    17 name: mongodb-kubernetes-ops-manager
    18 namespace: <namespace>
    19---
    20kind: Role
    21apiVersion: rbac.authorization.k8s.io/v1
    22metadata:
    23 name: mongodb-kubernetes-appdb
    24 namespace: <namespace>
    25rules:
    26 - apiGroups:
    27 - ""
    28 resources:
    29 - secrets
    30 verbs:
    31 - get
    32 - apiGroups:
    33 - ""
    34 resources:
    35 - pods
    36 verbs:
    37 - patch
    38---
    39kind: RoleBinding
    40apiVersion: rbac.authorization.k8s.io/v1
    41metadata:
    42 name: mongodb-kubernetes-appdb
    43 namespace: <namespace>
    44roleRef:
    45 apiGroup: rbac.authorization.k8s.io
    46 kind: Role
    47 name: mongodb-kubernetes-appdb
    48subjects:
    49 - kind: ServiceAccount
    50 name: mongodb-kubernetes-appdb
    51 namespace: <namespace>
    52...
  3. 创建本地Kubernetes服务帐户

    为每个命名空间创建以下部分或全部本地Kubernetes 服务帐户

    • 如果要在命名空间中部署 MongoDB 实例,请使用mongodb-kubernetes-database-pods

    • 如果要在命名空间中部署 Ops Manager,请使用mongodb-kubernetes-appdbmongodb-kubernetes-ops-manager

    复制并粘贴适用的示例,并将<namespace>值替换为标识命名空间的标签。

    helm template mongodb/mongodb-kubernetes \
    --set operator.namespace=<metadata.namespace> \
    --show-only templates/database-roles.yaml | oc apply -f -

设置Kubernetes Operator 的MongoDB控制器的作用域后,您可以: