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

安装和使用MongoDB Enterprise Edition 搜索

您可以使用Kubernetes Operator部署MongoDB Search 和 Vector Search 资源,以便在Kubernetes集群上与MongoDB Enterprise v8.0.10 或更高版本一起运行。 此过程演示如何部署和配置 mongot进程,使其与Kubernetes集群中的新副本集或现有副本集一起运行。

要部署MongoDB Search 和 Vector Search,您必须具备以下条件:

  • 一个运行的Kubernetes集群,且本地可用 kubeconfig

  • Kubernetes命令行工具 kubectl,配置为与集群通信。

  • Helm( Kubernetes的包管理器),用于安装Kubernetes Operator。

  • Bash v5.1 或更高版本,用于运行本教程中的命令。

  • Ops Manager或MongoDB Cloud Manager项目和API凭证。

1

要设立用于此过程后续步骤的环境变量,请复制以下内容,设立环境变量的值,然后加载环境变量:

1# set it to the context name of the k8s cluster
2export K8S_CTX="<local cluster context>"
3
4# the following namespace will be created if not exists
5export MDB_NS="mongodb"
6
7# name of the MongoDB Custom Resource.
8export MDB_RESOURCE_NAME="mdb-rs"
9
10# OM/CM's project name to be used to manage mongodb replica set
11export OPS_MANAGER_PROJECT_NAME="<arbitrary project name>"
12
13# URL to Cloud Manager or Ops Manager instance
14export OPS_MANAGER_API_URL="https://cloud-qa.mongodb.com"
15
16# The API key can be an Org Owner - the operator can create the project automatically then.
17# The API key can also be created in a particular project that was created manually with the Project Owner scope .
18export OPS_MANAGER_API_USER="abcdefg"
19export OPS_MANAGER_API_KEY="00000-abcd-efgh-1111-12345678"
20export OPS_MANAGER_ORG_ID="62a73abcdefgh12345678"
21
22# minimum required MongoDB version for running MongoDB Search is 8.0.10
23export MDB_VERSION="8.0.10"
24
25# root admin user for convenience, not used here at all in this guide
26export MDB_ADMIN_USER_PASSWORD="admin-user-password-CHANGE-ME"
27# regular user performing restore and search queries on sample mflix database
28export MDB_USER_PASSWORD="mdb-user-password-CHANGE-ME"
29# user for MongoDB Search to connect to the replica set to synchronise data from
30export MDB_SEARCH_SYNC_USER_PASSWORD="search-sync-user-password-CHANGE-ME"
31
32export OPERATOR_HELM_CHART="mongodb/mongodb-kubernetes"
33# comma-separated key=value pairs for additional parameters passed to the helm-chart installing the operator
34export OPERATOR_ADDITIONAL_HELM_VALUES=""
35
36export MDB_CONNECTION_STRING="mongodb://mdb-user:${MDB_USER_PASSWORD}@${MDB_RESOURCE_NAME}-svc.${MDB_NS}.svc.cluster.local:27017/?replicaSet=${MDB_RESOURCE_NAME}"
2

Helm 可自动部署和管理Kubernetes上的MongoDB实例。如果您已经添加了包含用于安装Kubernetes Operator 操作符 的Helm 图表的 Helm存储库,请跳过此步骤。否则,请添加 Helm存储库。

要添加、复制、粘贴并运行以下命令:

1helm repo add mongodb https://mongodb.github.io/helm-charts
2helm repo update mongodb
3helm search repo mongodb/mongodb-kubernetes
3

Kubernetes Operator 监视 MongoDBMongoDBOpsManagerMongoDBSearch 自定义资源,并管理MongoDB部署的生命周期。如果您已经安装了MongoDB Controllers for Kubernetes Operator,请跳过此步骤。否则,请从您在上一步中添加的 Helm存储库安装MongoDB Controllers for Kubernetes Operator。

要在 mongodb命名空间中安装MongoDB Controllers for Kubernetes Operator,请复制、粘贴并运行以下命令:

1helm upgrade --install --debug --kube-context "${K8S_CTX}" \
2 --create-namespace \
3 --namespace="${MDB_NS}" \
4 mongodb-kubernetes \
5 ${OPERATOR_ADDITIONAL_HELM_VALUES:+--set ${OPERATOR_ADDITIONAL_HELM_VALUES}} \
6 "${OPERATOR_HELM_CHART}"
4

如果您已部署MongoDB Enterprise,请跳至下一步。否则,部署MongoDB Enterprise资源。

要部署MongoDB Enterprise,请完成以下步骤:

  1. 创建名为 mdb-rsMongoDB 自定义资源。

    该资源定义了 mongodmongodb-agent 容器的 CPU 和内存资源,并指示Kubernetes Operator 配置具有 3 成员的MongoDB副本集:

    要部署MongoDB Enterprise ,请在命名空间中复制、粘贴并运行以下命令:

    1kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF
    2apiVersion: mongodb.com/v1
    3kind: MongoDB
    4metadata:
    5 name: ${MDB_RESOURCE_NAME}
    6spec:
    7 members: 3
    8 version: ${MDB_VERSION}-ent
    9 type: ReplicaSet
    10 opsManager:
    11 configMapRef:
    12 name: om-project
    13 credentials: om-credentials
    14 security:
    15 authentication:
    16 enabled: true
    17 ignoreUnknownUsers: true
    18 modes:
    19 - SCRAM
    20 agent:
    21 logLevel: DEBUG
    22 statefulSet:
    23 spec:
    24 template:
    25 spec:
    26 containers:
    27 - name: mongodb-enterprise-database
    28 resources:
    29 limits:
    30 cpu: "2"
    31 memory: 2Gi
    32 requests:
    33 cpu: "1"
    34 memory: 1Gi
    35EOF
  2. 等待 MongoDB资源部署完成。

    当您应用MongoDB 自定义资源时, Kubernetes 操作符开始部署MongoDB节点 (Pod)。此步骤会暂停执行,直到 mdbc-rs 资源的状态阶段为 Running,这表示MongoDB Community副本集可操作。

    1echo "Waiting for MongoDB resource to reach Running phase..."
    2kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait --for=jsonpath='{.status.phase}'=Running "mdb/${MDB_RESOURCE_NAME}" --timeout=400s
    3echo; echo "MongoDB resource"
    4kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get "mdb/${MDB_RESOURCE_NAME}"
    5echo; echo "Pods running in cluster ${K8S_CTX}"
    6kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get pods
5

MongoDB需要身份验证才能安全访问权限。在此步骤中,您将创建三个Kubernetes密钥:

  • mdb-admin-user-password: MongoDB管理员的档案。

  • mdb-user-password:授权执行搜索查询的用户的档案。

  • mdbc-rs-search-sync-source-password:专用搜索用户的档案,由 mongot进程在内部用于同步数据和管理索引。

Kubernetes Operator 将这些密钥挂载到MongoDB Pod 中。

要创建密钥,请在部署MongoDB Server并计划部署MongoDB Search 和 Vector Search 的命名空间中复制、粘贴并运行以下命令:

1# admin user with root role
2kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \
3 create secret generic mdb-admin-user-password \
4 --from-literal=password="${MDB_ADMIN_USER_PASSWORD}"
5
6kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF
7apiVersion: mongodb.com/v1
8kind: MongoDBUser
9metadata:
10 name: mdb-admin
11spec:
12 username: mdb-admin
13 db: admin
14 mongodbResourceRef:
15 name: ${MDB_RESOURCE_NAME}
16 passwordSecretKeyRef:
17 name: mdb-admin-user-password
18 key: password
19 roles:
20 - name: root
21 db: admin
22EOF
23
24# user used by MongoDB Search to connect to MongoDB database to synchronize data from
25# For MongoDB <8.2, the operator will be creating the searchCoordinator custom role automatically
26# From MongoDB 8.2, searchCoordinator role will be a built-in role.
27kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \
28 create secret generic mdb-rs-search-sync-source-password \
29 --from-literal=password="${MDB_SEARCH_SYNC_USER_PASSWORD}"
30kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF
31apiVersion: mongodb.com/v1
32kind: MongoDBUser
33metadata:
34 name: search-sync-source-user
35spec:
36 username: search-sync-source
37 db: admin
38 mongodbResourceRef:
39 name: ${MDB_RESOURCE_NAME}
40 passwordSecretKeyRef:
41 name: mdb-rs-search-sync-source-password
42 key: password
43 roles:
44 - name: searchCoordinator
45 db: admin
46EOF
47
48# user performing search queries
49kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \
50 create secret generic mdb-user-password \
51 --from-literal=password="${MDB_USER_PASSWORD}"
52kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF
53apiVersion: mongodb.com/v1
54kind: MongoDBUser
55metadata:
56 name: mdb-user
57spec:
58 username: mdb-user
59 db: admin
60 mongodbResourceRef:
61 name: ${MDB_RESOURCE_NAME}
62 passwordSecretKeyRef:
63 name: mdb-user-password
64 key: password
65 roles:
66 - name: readWrite
67 db: sample_mflix
68EOF
6

您可以部署一个搜索节点实例,而无需任何负载均衡。要部署,请完成以下步骤:

  1. 创建名为 mdbc-rsMongoDBSearch 自定义资源。

    此资源指定搜索节点的 CPU 和内存资源要求。要学习;了解有关此自定义资源中设置的更多信息,请参阅MongoDB搜索和向量搜索设置。

    1kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF
    2apiVersion: mongodb.com/v1
    3kind: MongoDBSearch
    4metadata:
    5 name: ${MDB_RESOURCE_NAME}
    6spec:
    7 # no need to specify source.mongodbResourceRef if MongoDBSearch CR has the same name as MongoDB CR
    8 # the operator infer it automatically
    9 resourceRequirements:
    10 limits:
    11 cpu: "3"
    12 memory: 5Gi
    13 requests:
    14 cpu: "2"
    15 memory: 3Gi
    16EOF
  2. 等待 MongoDBSearch资源部署完成。

    当您应用MongoDBSearch 自定义资源时, Kubernetes 操作符开始部署搜索节点 (pod)。此步骤会暂停执行,直到 mdbc-rs 资源的状态阶段为 Running,这表示MongoDB Community副本集可操作。

    1echo "Waiting for MongoDBSearch resource to reach Running phase..."
    2kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait --for=jsonpath='{.status.phase}'=Running mdbs/mdb-rs --timeout=300s
7

确保使用 MongoDBSearch成功部署MongoDB资源。

1echo "Waiting for MongoDB resource to reach Running phase..."
2kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait --for=jsonpath='{.status.phase}'=Running "mdb/${MDB_RESOURCE_NAME}" --timeout=400s
8

查看MongoDB副本集成员、 Kubernetes Operator 的MongoDB控制器以及搜索节点的命名空间Pod 中运行的所有 Pod。

1echo; echo "MongoDB resource"
2kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get "mdb/${MDB_RESOURCE_NAME}"
3echo; echo "MongoDBSearch resource"
4kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get mdbs/mdb-rs
5echo; echo "Pods running in cluster ${K8S_CTX}"
6kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get pods

现在,您已成功部署MongoDB Search 和 Vector Search 以便与MongoDB Enterprise Edition 一起使用,接下来您可以在MongoDB 集群中添加数据、创建MongoDB Search 和 Vector Search 索引,并对数据运行查询。要学习;了解更多信息,请参阅MongoDB搜索和向量搜索设置。

给本页内容打分