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

安装和使用MongoDB Search 和 Vector Search With external MongoDB Enterprise Edition

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

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

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

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

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

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

  • 运行8.0.10 或更高版本的MongoDB Enterprise Edition副本集,用于存储数据。

    要学习;了解有关部署MongoDB Enterprise 的更多信息,请参阅部署和配置MongoDB数据库资源。

  • 运行的MongoDB Cloud Manager或MongoDB Ops Manager ,用于管理MongoDB任务。

在使用Kubernetes Operator 安装MongoDB Search 和 Vector Search 之前,您必须执行以下操作:

登录到Cloud Manager或MongoDB Ops Manager用户界面并执行以下步骤,为MongoDB Search 和 Vector Search 配置Cloud Manager或MongoDB Ops Manager 。

1
  1. 登录MongoDB Cloud Manager或MongoDB Ops Manager。

  2. 单击 Deployment 标签页。

  3. 选择要修改的集群。

2
  1. 单击 Modify 按钮打开部署配置编辑器。

  2. Process Configuration(添加特性和功能)部分下,单击 Advanced Configuration Options(权限)。

  3. 单击 Add Option 按钮,然后从下拉列表中选择 setParameter Startup Option

  4. 添加名称和值后,单击 Add,在字段中逐个添加以下参数:

    Parameter

    mongotHost

    您的搜索主机名和端口。示例:search-node1.example.com:27017

    searchIndexManagementHostAndPort

    您的搜索主机名和端口。示例:search-node1.example.com:27017

    skipAuthenticationToSearchIndexManagementServer

    false

    searchTLSMode

    您配置的 TLS模式。示例,如果 mongot进程配置为接受 TLS 连接,则为 preferTLS

3
  1. 单击 Review & Deploy 按钮。

  2. 查看并确认以应用更改。

    Cloud Manager或MongoDB Ops Manager对副本集执行滚动重启以应用新配置。

您必须创建具有 searchCoordinator角色的用户。在MongoDB 8.2 及更高版本中,searchCoordinator 是一个内置角色。您必须创建一个用户并为该用户分配角色。对于 8.2 和 8.0.10 之前或更高版本的MongoDB版本,您必须在创建用户之前手动创建角色并分配权限。选择与您运行的MongoDB版本对应的标签页,创建具有 searchCoordinator角色的用户。

如果您使用MongoDB 8.2 或更高版本,请完成本部分中的步骤。

To create the user and assign the user the built-in searchCoordinator role, complete the following steps by using either the Cloud Manager or Ops Manager UI or the mongosh:

1
  1. 登录Cloud Manager或MongoDB Ops Manager用户界面。

  2. GoSecurity标签页并单击 Users

2
3
Username

输入 search-sync-source

Password

设置安全强度高的密码。

Authentication Database

选择 admin数据库。

4

Assign Roles 部分,从 admin数据库的下拉列表中选择内置searchCoordinator角色。

5

In mongosh, run the following commands:

1
use admin;
2
db.createUser({
user: "search-sync-source",
pwd: "<PASSWORD>", // Replace with your actual password
roles: [
{ role: "searchCoordinator", db: "admin" }
]
});

如果您使用 8.2 之前的MongoDB版本,请完成本部分中的步骤。

To create the user and assign the user the built-in searchCoordinator role, complete the following steps by using either the Cloud Manager or Ops Manager UI or the mongosh:

要创建具有权限的 searchCoordinator 角色,然后添加具有 searchCoordinator 角色的用户,请在 Cloud Manager 或 MongoDB Ops Manager 用户界面中完成以下步骤:

1
  1. 登录Cloud Manager或MongoDB Ops Manager用户界面。

  2. Security标签页下,GoRoles 子标签页。

  3. 单击 Add New Custom Role(连接)。

2
3

Role Inheritance 部分中,从 admin数据库添加以下三个角色:

  • clusterMonitor

  • directShardOperations

  • readAnyDatabase

4
  1. Privileges 部分中,单击 Add Privilege

  2. 对于 Resource,输入数据库的 __mdb_internal_search

    不要在集合字段中输入任何内容。

  3. Actions 下,添加以下操作。

    • changeStream

    • cleanupStructuredEncryptionData, compactStructuredEncryptionData

    • collStats

    • convertToCapped

    • createCollection, dropCollection , listCollections , renameCollectionSameDB

    • createIndex, dropIndex , listIndexes

    • createSearchIndexes, dropSearchIndex , listSearchIndexes , updateSearchIndex

    • dbHash, dbStats

    • find, insert , remove , update

    • killCursors

    • planCacheRead

5
  1. 单击 Add Privilege(连接)。

  2. 对于 Resource,将资源类型切换为 cluster

  3. 对于 Action(Atlas 问题类别),选择 bypassDefaultMaxTimeMS(其他)。

6
7

GoSecurity标签页并单击 Users

8
9
Username

输入 search-sync-source

Password

设置安全强度高的密码。

Authentication Database

选择 admin数据库。

10

Assign Roles 部分,从 admin数据库的下拉列表中选择内置searchCoordinator角色。

In mongosh, run the following commands:

1
use admin;
2
db.createRole({
role: "searchCoordinator",
privileges: [
{
resource: { db: "__mdb_internal_search", collection: "" },
actions: [
"changeStream", "cleanupStructuredEncryptionData", "collStats",
"compactStructuredEncryptionData", "convertToCapped", "createCollection",
"createIndex", "createSearchIndexes", "dbHash", "dbStats",
"dropCollection", "dropIndex", "dropSearchIndex", "find", "insert",
"killCursors", "listCollections", "listIndexes", "listSearchIndexes",
"planCacheRead", "remove", "renameCollectionSameDB", "update", "updateSearchIndex"
]
},
{
resource: { cluster: true },
actions: [ "bypassDefaultMaxTimeMS" ]
}
],
roles: [
{ role: "clusterMonitor", db: "admin" },
{ role: "directShardOperations", db: "admin" },
{ role: "readAnyDatabase", db: "admin" }
]
});
3
db.createUser({
user: "search-sync-source",
pwd: "<PASSWORD>", // Replace with your actual password
roles: [
{ role: "searchCoordinator", db: "admin" }
]
});

准备环境,以便在终端中运行本教程中的示例代码。

1

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

1export K8S_CTX="<your kubernetes context here>"
2
3export MDB_NS="mongodb"
4
5export MDB_VERSION="8.0.10"
6
7export MDB_ADMIN_USER_PASSWORD="admin-user-password-CHANGE-ME"
8export MDB_USER_PASSWORD="mdb-user-password-CHANGE-ME"
9export MDB_SEARCH_SYNC_USER_PASSWORD="search-sync-user-password-CHANGE-ME"
10
11export MDB_SEARCH_HOSTNAME="mdbs-search"
12
13# External MongoDB replica set members - REPLACE THESE VALUES with your actual external MongoDB hosts
14# In production, replace with your actual external MongoDB replica set members
15export MDB_EXTERNAL_HOST_0="mdbc-rs-0.mdbc-rs-svc.${MDB_NS}.svc.cluster.local:27017"
16export MDB_EXTERNAL_HOST_1="mdbc-rs-1.mdbc-rs-svc.${MDB_NS}.svc.cluster.local:27017"
17export MDB_EXTERNAL_HOST_2="mdbc-rs-2.mdbc-rs-svc.${MDB_NS}.svc.cluster.local:27017"
18
19# REPLACE with your external MongoDB keyfile secret name
20export MDB_EXTERNAL_KEYFILE_SECRET_NAME="mdbc-rs-keyfile"
21
22# REPLACE with the actual keyfile content from your external MongoDB replica set
23# For testing, this will be automatically generated by the MongoDB Community resource
24export MDB_EXTERNAL_KEYFILE_CONTENT="your-mongodb-keyfile-content-CHANGE-ME"
25
26# REPLACE with your actual external MongoDB replica set name
27export MDB_EXTERNAL_REPLICA_SET_NAME="mdbc-rs"
28
29export OPERATOR_HELM_CHART="mongodb/mongodb-kubernetes"
30export OPERATOR_ADDITIONAL_HELM_VALUES=""
31
32export MDB_CONNECTION_STRING="mongodb://mdb-user:${MDB_USER_PASSWORD}@${MDB_EXTERNAL_HOST_0}/?replicaSet=${MDB_EXTERNAL_REPLICA_SET_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}"

前面的命令会在 mongodb命名空间中安装Kubernetes Operator;如果该命名空间不存在,则会创建该命名空间。安装后, Kubernetes Operator 会监视 MongoDBSearch 自定义资源并管理MongoDB Search 和 Vector Search 部署的生命周期。

1

mongot进程需要身份验证凭证才能连接到外部MongoDB 部署,从而创建搜索索引和运行搜索查询。此步骤将创建以下Kubernetes密钥:

  • mdb-admin-user-password — MongoDB管理员的凭证。

  • mdb-user-password — 经授权执行搜索查询的用户的凭证。

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

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

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

1kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \
2 create secret generic mdb-admin-user-password \
3 --from-literal=password="${MDB_ADMIN_USER_PASSWORD}"
4
5kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \
6 create secret generic mdbc-rs-search-sync-source-password \
7 --from-literal=password="${MDB_SEARCH_SYNC_USER_PASSWORD}"
8
9kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \
10 create secret generic mdb-user-password \
11 --from-literal=password="${MDB_USER_PASSWORD}"
2

为了对来自外部MongoDB 的连接进行身份验证,mongot进程使用的密钥文件与外部MongoDB副本集用于副本集之间的内部身份验证的密钥文件相同。

要创建包含来自外部MongoDB 的密钥文件内容的Kubernetes密钥,请复制、粘贴并运行以下命令:

1kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \
2 create secret generic "${MDB_EXTERNAL_KEYFILE_SECRET_NAME}" \
3 --from-literal=keyfile="${MDB_EXTERNAL_KEYFILE_CONTENT}"
3

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

  1. 创建名为 mdbsMongoDBSearch 自定义资源。

    此资源包含以下内容:

    spec.source.external.hostAndPorts

    外部MongoDB副本集节点列表。

    spec.source.external.keyfileSecretRef

    外部副本集成员使用的密钥文件密钥。

    spec.source.username

    搜索同步用户用户名。

    spec.source.passwordSecretRef

    搜索同步用户密码。

    spec.resourceRequirements

    搜索容器的 CPU 和内存资源要求。

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

    1kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF
    2apiVersion: mongodb.com/v1
    3kind: MongoDBSearch
    4metadata:
    5 name: mdbs
    6spec:
    7 source:
    8 external:
    9 hostAndPorts:
    10 - ${MDB_EXTERNAL_HOST_0}
    11 - ${MDB_EXTERNAL_HOST_1}
    12 - ${MDB_EXTERNAL_HOST_2}
    13 keyfileSecretRef:
    14 name: ${MDB_EXTERNAL_KEYFILE_SECRET_NAME}
    15 key: keyfile
    16 username: search-sync-source
    17 passwordSecretRef:
    18 name: mdbc-rs-search-sync-source-password
    19 key: password
    20 resourceRequirements:
    21 limits:
    22 cpu: "3"
    23 memory: 5Gi
    24 requests:
    25 cpu: "2"
    26 memory: 3Gi
    27EOF
  2. 等待 MongoDBSearch资源部署完成。

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

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

启用外部MongoDB实例能够连接到搜索服务,您必须为MongoDB Search 和 Vector Search 配置外部访问权限。您可以创建一个 LoadBalancer 服务,在Kubernetes集群外部公开搜索Pod。

以下服务使用可从Kubernetes集群外部访问的外部IP解决或主机名公开端口 27027 上的 MongoDBSearch 服务。

1kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<YAML
2apiVersion: v1
3kind: Service
4metadata:
5 name: ${MDB_SEARCH_HOSTNAME}
6spec:
7 type: LoadBalancer
8 selector:
9 app: mdbs-search-svc
10 ports:
11 - name: mongot
12 port: 27027
13 targetPort: 27027
14YAML
15
16echo "Waiting for external IP to be assigned to service ${MDB_SEARCH_HOSTNAME}..."
17TIMEOUT=120 # 2 minutes timeout
18ELAPSED=0
19while [ ${ELAPSED} -lt ${TIMEOUT} ]; do
20 EXTERNAL_IP=$(kubectl get service "${MDB_SEARCH_HOSTNAME}" --context "${K8S_CTX}" -n "${MDB_NS}" -o jsonpath='{.status.loadBalancer.ingress[0].ip}' 2>/dev/null)
21 if [ -n "${EXTERNAL_IP}" ] && [ "${EXTERNAL_IP}" != "null" ]; then
22 echo "External IP assigned: ${EXTERNAL_IP}"
23 break
24 fi
25 echo "Still waiting for external IP assignment... (${ELAPSED}s/${TIMEOUT}s)"
26 sleep 5
27 ELAPSED=$((ELAPSED + 5))
28done
29
30if [ ${ELAPSED} -ge ${TIMEOUT} ]; then
31 echo "ERROR: Timeout reached (${TIMEOUT}s) while waiting for external IP assignment"
32 echo "LoadBalancer service may take longer to provision or there may be an issue"
33 exit 1
34fi
5

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

1echo; echo "MongoDBSearch resource"
2kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get mdbs/mdbs
3echo; echo "Search pods running in cluster ${K8S_CTX}"
4kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get pods -l app=mdbs-search-svc
5echo; echo "All pods in namespace ${MDB_NS}"
6kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get pods

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