您可以使用 Kubernetes 操作符部署 MongoDB Search 和向量搜索资源,以便在 Kubernetes 集群上与 MongoDB Enterprise v8.2.0 或更高版本一起运行。本过程演示了如何在您的 Kubernetes 集群中部署和配置 mongot 进程,以使其与新的或现有的副本集一起运行。该部署使用 TLS 证书,以确保 MongoDB 节点与 mongot 搜索过程之间的安全通信。
先决条件
要部署MongoDB Search 和 Vector Search,您必须具备以下条件:
一个运行的Kubernetes集群,且本地可用
kubeconfig。Kubernetes命令行工具
kubectl,配置为与集群通信。Helm( Kubernetes的包管理器),用于安装Kubernetes Operator。
Bash v5.1 或更高版本,用于运行本教程中的命令。
MongoDB Ops Manager 或 MongoDB Cloud Manager 项目和 API 凭证。
步骤
必填。设置环境变量。
设置环境变量以供本过程中的后续步骤使用。复制以下命令,更新环境的值,然后运行这些命令以加载变量:
1 # set it to the context name of the k8s cluster 2 export K8S_CTX="<local cluster context>" 3 4 # the following namespace will be created if not exists 5 export MDB_NS="mongodb" 6 7 # name of the MongoDB Custom Resource. 8 export MDB_RESOURCE_NAME="mdb-rs" 9 10 export MDB_MEMBERS=3 11 # OM/CM's project name to be used to manage mongodb replica set 12 export OPS_MANAGER_PROJECT_NAME="<arbitrary project name>" 13 14 # URL to Cloud Manager or Ops Manager instance 15 export OPS_MANAGER_API_URL="https://cloud-qa.mongodb.com" 16 17 # The API key can be an Org Owner - the operator can create the project automatically then. 18 # The API key can also be created in a particular project that was created manually with the Project Owner scope. 19 export OPS_MANAGER_API_USER="<SET API USER>" 20 export OPS_MANAGER_API_KEY="<SET API KEY>" 21 export OPS_MANAGER_ORG_ID="<SET ORG ID>" 22 23 # minimum required MongoDB version for running MongoDB Search is 8.2.0 24 export MDB_VERSION="8.2.0-ent" 25 26 # root admin user for convenience, not used here at all in this guide 27 export MDB_ADMIN_USER_PASSWORD="admin-user-password-CHANGE-ME" 28 # regular user performing restore and search queries on sample mflix database 29 export MDB_USER_PASSWORD="mdb-user-password-CHANGE-ME" 30 # user for MongoDB Search to connect to the replica set to synchronise data from 31 export MDB_SEARCH_SYNC_USER_PASSWORD="search-sync-user-password-CHANGE-ME" 32 33 export OPERATOR_HELM_CHART="mongodb/mongodb-kubernetes" 34 # comma-separated key=value pairs for additional parameters passed to the helm-chart installing the operator 35 export OPERATOR_ADDITIONAL_HELM_VALUES="" 36 37 export MDB_TLS_CERT_SECRET_PREFIX="certs" 38 export MDB_TLS_CA_CONFIGMAP="${MDB_RESOURCE_NAME}-ca-configmap" 39 40 export CERT_MANAGER_NAMESPACE="cert-manager" 41 export MDB_TLS_SELF_SIGNED_ISSUER="selfsigned-bootstrap-issuer" 42 export MDB_TLS_CA_CERT_NAME="my-selfsigned-ca" 43 export MDB_TLS_CA_SECRET_NAME="root-secret" 44 export MDB_TLS_CA_ISSUER="my-ca-issuer" 45 export MDB_TLS_SERVER_CERT_SECRET_NAME="${MDB_TLS_CERT_SECRET_PREFIX}-${MDB_RESOURCE_NAME}-cert" 46 export MDB_SEARCH_TLS_SECRET_NAME="${MDB_RESOURCE_NAME}-search-tls" 47 48 export MDB_CONNECTION_STRING="mongodb://mdb-user:${MDB_USER_PASSWORD}@${MDB_RESOURCE_NAME}-svc.${MDB_NS}.svc.cluster.local:27017/?replicaSet=${MDB_RESOURCE_NAME}&tls=true&tlsCAFile=/tls/ca.crt"
有条件。添加MongoDB Helm存储库。
Helm 可自动部署和管理Kubernetes上的MongoDB实例。如果您已经添加了包含用于安装Kubernetes Operator 操作符 的Helm 图表的 Helm存储库,请跳过此步骤。否则,请添加 Helm存储库。
要添加、复制、粘贴并运行以下命令:
1 helm repo add mongodb https://mongodb.github.io/helm-charts 2 helm repo update mongodb 3 helm search repo mongodb/mongodb-kubernetes
有条件。为Kubernetes Operator 安装MongoDB控制器。
Kubernetes 操作符监视 MongoDB、MongoDBOpsManager 和 MongoDBSearch 自定义资源,并管理 MongoDB 部署的生命周期。如果您已经安装了MongoDB Controllers for Kubernetes 操作符,请跳过此步骤。否则,请从您在上一个步骤中添加的 Helm 存储库安装 MongoDB Controllers for Kubernetes 操作符。
要在 mongodb命名空间中安装MongoDB Controllers for Kubernetes Operator,请复制、粘贴并运行以下命令:
1 helm 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}"
有条件。安装 cert-manager。
管理TLS证书需要 cert-manager。如果您已在集群中安装 cert-manager,请跳过此步骤。否则,请使用Helm 安装 cert-manager。
要在 cert-manager命名空间中安装 cert-manager,请在终端中运行以下命令:
1 helm upgrade --install \ 2 cert-manager \ 3 oci://quay.io/jetstack/charts/cert-manager \ 4 --kube-context "${K8S_CTX}" \ 5 --namespace "${CERT_MANAGER_NAMESPACE}" \ 6 --create-namespace \ 7 --set crds.enabled=true 8 9 for deployment in cert-manager cert-manager-cainjector cert-manager-webhook; do 10 kubectl --context "${K8S_CTX}" \ 11 -n "${CERT_MANAGER_NAMESPACE}" \ 12 wait --for=condition=Available "deployment/${deployment}" --timeout=300s 13 done 14 15 echo "cert-manager is ready in namespace ${CERT_MANAGER_NAMESPACE}."
必需。准备证书颁发者和 CA 基础架构。
创建证书授权机构基础设施,用于针对 MongoDB 和 MongoDBSearch 资源颁发 TLS 证书。这些命令会执行以下操作:
创建自签名
ClusterIssuer。生成 CA 证书。
发布所有命名空间都可以使用的集群范围的 CA 颁发者。
通过
ConfigMap公开 CA 捆绑包,以便 MongoDB 资源可以使用它。
1 Bootstrap a self-signed ClusterIssuer to mint the CA secret consumed by application workloads. 2 kubectl apply --context "${K8S_CTX}" -f - <<EOF_MANIFEST 3 apiVersion: cert-manager.io/v1 4 kind: ClusterIssuer 5 metadata: 6 name: ${MDB_TLS_SELF_SIGNED_ISSUER} 7 spec: 8 selfSigned: {} 9 EOF_MANIFEST 10 11 kubectl --context "${K8S_CTX}" wait --for=condition=Ready clusterissuer "${MDB_TLS_SELF_SIGNED_ISSUER}" 12 13 kubectl apply --context "${K8S_CTX}" -f - <<EOF_MANIFEST 14 apiVersion: cert-manager.io/v1 15 kind: Certificate 16 metadata: 17 name: ${MDB_TLS_CA_CERT_NAME} 18 namespace: ${CERT_MANAGER_NAMESPACE} 19 spec: 20 isCA: true 21 commonName: ${MDB_TLS_CA_CERT_NAME} 22 secretName: ${MDB_TLS_CA_SECRET_NAME} 23 privateKey: 24 algorithm: ECDSA 25 size: 256 26 issuerRef: 27 name: ${MDB_TLS_SELF_SIGNED_ISSUER} 28 kind: ClusterIssuer 29 EOF_MANIFEST 30 31 kubectl --context "${K8S_CTX}" wait --for=condition=Ready -n "${CERT_MANAGER_NAMESPACE}" certificate "${MDB_TLS_CA_CERT_NAME}" 32 33 kubectl apply --context "${K8S_CTX}" -f - <<EOF_MANIFEST 34 apiVersion: cert-manager.io/v1 35 kind: ClusterIssuer 36 metadata: 37 name: ${MDB_TLS_CA_ISSUER} 38 spec: 39 ca: 40 secretName: ${MDB_TLS_CA_SECRET_NAME} 41 EOF_MANIFEST 42 43 kubectl --context "${K8S_CTX}" wait --for=condition=Ready clusterissuer "${MDB_TLS_CA_ISSUER}" 44 45 TMP_CA_CERT="$(mktemp)" 46 trap 'rm -f "${TMP_CA_CERT}"' EXIT 47 48 kubectl --context "${K8S_CTX}" get secret "${MDB_TLS_CA_SECRET_NAME}" -n "${CERT_MANAGER_NAMESPACE}" -o jsonpath="{.data['ca\\.crt']}" | base64 --decode > "${TMP_CA_CERT}" 49 50 kubectl --context "${K8S_CTX}" create configmap "${MDB_TLS_CA_CONFIGMAP}" -n "${MDB_NS}" \ 51 --from-file=ca-pem="${TMP_CA_CERT}" --from-file=mms-ca.crt="${TMP_CA_CERT}" \ 52 --from-file=ca.crt="${TMP_CA_CERT}" \ 53 --dry-run=client -o yaml | kubectl --context "${K8S_CTX}" apply -f -
必需。颁发 TLS 证书。
为 MongoDB 服务器 (${MDB_RESOURCE_NAME}-server-tls) 和 MongoDBSearch 服务 (${MDB_RESOURCE_NAME}-search-tls) 颁发 TLS 证书。MongoDB 服务器证书包含了用于 pod 和服务通信的所有必要 DNS 名称。这两种证书都支持服务器和客户端身份验证。
1 server_certificate="${MDB_RESOURCE_NAME}-server-tls" 2 search_certificate="${MDB_RESOURCE_NAME}-search-tls" 3 4 mongo_dns_names=() 5 for ((member = 0; member < MDB_MEMBERS; member++)); do 6 mongo_dns_names+=("${MDB_RESOURCE_NAME}-${member}") 7 mongo_dns_names+=("${MDB_RESOURCE_NAME}-${member}.${MDB_RESOURCE_NAME}-svc.${MDB_NS}.svc.cluster.local") 8 done 9 mongo_dns_names+=( 10 "${MDB_RESOURCE_NAME}-svc.${MDB_NS}.svc.cluster.local" 11 "*.${MDB_RESOURCE_NAME}-svc.${MDB_NS}.svc.cluster.local" 12 ) 13 14 search_dns_names=( 15 "${MDB_RESOURCE_NAME}-search-svc.${MDB_NS}.svc.cluster.local" 16 ) 17 18 render_dns_list() { 19 local dns_list=("$@") 20 for dns in "${dns_list[@]}"; do 21 printf " - \"%s\"\n" "${dns}" 22 done 23 } 24 25 kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF_MANIFEST 26 apiVersion: cert-manager.io/v1 27 kind: Certificate 28 metadata: 29 name: ${server_certificate} 30 namespace: ${MDB_NS} 31 spec: 32 secretName: ${MDB_TLS_SERVER_CERT_SECRET_NAME} 33 issuerRef: 34 name: ${MDB_TLS_CA_ISSUER} 35 kind: ClusterIssuer 36 duration: 240h0m0s 37 renewBefore: 120h0m0s 38 usages: 39 - digital signature 40 - key encipherment 41 - server auth 42 - client auth 43 dnsNames: 44 (render_dns_list "${mongo_dns_names[@]}") 45 --- 46 apiVersion: cert-manager.io/v1 47 kind: Certificate 48 metadata: 49 name: ${search_certificate} 50 namespace: ${MDB_NS} 51 spec: 52 secretName: ${MDB_SEARCH_TLS_SECRET_NAME} 53 issuerRef: 54 name: ${MDB_TLS_CA_ISSUER} 55 kind: ClusterIssuer 56 duration: 240h0m0s 57 renewBefore: 120h0m0s 58 usages: 59 - digital signature 60 - key encipherment 61 - server auth 62 - client auth 63 dnsNames: 64 (render_dns_list "${search_dns_names[@]}") 65 EOF_MANIFEST 66 67 kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait --for=condition=Ready certificate "${server_certificate}" --timeout=300s 68 kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait --for=condition=Ready certificate "${search_certificate}" --timeout=300s
有条件。创建和部署MongoDB Enterprise资源。
如果您已部署MongoDB Enterprise,请跳至下一步。否则,部署MongoDB Enterprise资源。
要部署MongoDB Enterprise,请完成以下步骤:
如果您正在使用 MongoDB Ops Manager ,请为 MongoDB Ops Manager 项目创建 ConfigMap 和密钥。
To store the configuration and credentials for integration with MongoDB Ops Manager, copy, paste, and run the following commands:
1 kubectl --context "${K8S_CTX}" -n "${MDB_NS}" create configmap om-project \ 2 --from-literal=projectName="${OPS_MANAGER_PROJECT_NAME}" --from-literal=baseUrl="${OPS_MANAGER_API_URL}" \ 3 --from-literal=orgId="${OPS_MANAGER_ORG_ID:-}" 4 5 kubectl --context "${K8S_CTX}" -n "${MDB_NS}" create secret generic om-credentials \ 6 --from-literal=publicKey="${OPS_MANAGER_API_USER}" \ 7 --from-literal=privateKey="${OPS_MANAGER_API_KEY}" 创建名为
mdb-rs的MongoDB自定义资源。该资源定义了
mongod和mongodb-agent容器的 CPU 和内存资源,并指示Kubernetes Operator 配置具有 3 成员的MongoDB副本集:要部署MongoDB Enterprise ,请在命名空间中复制、粘贴并运行以下命令:
1 kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF 2 apiVersion: mongodb.com/v1 3 kind: MongoDB 4 metadata: 5 name: ${MDB_RESOURCE_NAME} 6 spec: 7 members: ${MDB_MEMBERS} 8 version: ${MDB_VERSION} 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 certsSecretPrefix: ${MDB_TLS_CERT_SECRET_PREFIX} 21 tls: 22 enabled: true 23 ca: ${MDB_TLS_CA_CONFIGMAP} 24 agent: 25 logLevel: INFO 26 podSpec: 27 podTemplate: 28 spec: 29 containers: 30 - name: mongodb-enterprise-database 31 resources: 32 limits: 33 cpu: "2" 34 memory: 2Gi 35 requests: 36 cpu: "1" 37 memory: 1Gi 38 EOF 等待
MongoDB资源部署完成。当您应用
MongoDB自定义资源时, Kubernetes 操作符开始部署MongoDB节点 (Pod)。此步骤会暂停执行,直到mdbc-rs资源的状态阶段为Running,这表示MongoDB Community副本集可操作。1 echo "Waiting for MongoDB resource to reach Running phase..." 2 kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait --for=jsonpath='{.status.phase}'=Running "mdb/${MDB_RESOURCE_NAME}" --timeout=400s 3 echo; echo "MongoDB resource" 4 kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get "mdb/${MDB_RESOURCE_NAME}" 5 echo; echo "Pods running in cluster ${K8S_CTX}" 6 kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get pods
必需。创建并加载MongoDB用户密钥。
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 2 kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \ 3 create secret generic mdb-admin-user-password \ 4 --from-literal=password="${MDB_ADMIN_USER_PASSWORD}" 5 6 kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF 7 apiVersion: mongodb.com/v1 8 kind: MongoDBUser 9 metadata: 10 name: mdb-admin 11 spec: 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 22 EOF 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. 27 kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \ 28 create secret generic "${MDB_RESOURCE_NAME}-search-sync-source-password" \ 29 --from-literal=password="${MDB_SEARCH_SYNC_USER_PASSWORD}" 30 kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF 31 apiVersion: mongodb.com/v1 32 kind: MongoDBUser 33 metadata: 34 name: search-sync-source-user 35 spec: 36 username: search-sync-source 37 db: admin 38 mongodbResourceRef: 39 name: ${MDB_RESOURCE_NAME} 40 passwordSecretKeyRef: 41 name: ${MDB_RESOURCE_NAME}-search-sync-source-password 42 key: password 43 roles: 44 - name: searchCoordinator 45 db: admin 46 EOF 47 48 user performing search queries 49 kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \ 50 create secret generic mdb-user-password \ 51 --from-literal=password="${MDB_USER_PASSWORD}" 52 kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF 53 apiVersion: mongodb.com/v1 54 kind: MongoDBUser 55 metadata: 56 name: mdb-user 57 spec: 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 68 EOF
必需。为MongoDB Search 和 Vector Search 创建并部署资源。
您可以部署一个搜索节点实例,而无需任何负载均衡。要部署,请完成以下步骤:
创建名为
mdbc-rs的 MongoDBSearch 自定义资源。此资源指定搜索节点的 CPU 和内存资源要求。要学习;了解有关此自定义资源中设置的更多信息,请参阅MongoDB搜索和向量搜索设置。
1 kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF 2 apiVersion: mongodb.com/v1 3 kind: MongoDBSearch 4 metadata: 5 name: ${MDB_RESOURCE_NAME} 6 spec: 7 no need to specify source.mongodbResourceRef if MongoDBSearch CR has the same name as MongoDB CR 8 the operator infer it automatically 9 security: 10 tls: 11 certificateKeySecretRef: 12 name: ${MDB_SEARCH_TLS_SECRET_NAME} 13 resourceRequirements: 14 limits: 15 cpu: "3" 16 memory: 5Gi 17 requests: 18 cpu: "2" 19 memory: 3Gi 20 EOF 等待 MongoDBSearch资源部署完成。
当您应用MongoDBSearch 自定义资源时, Kubernetes 操作符开始部署搜索节点 (pod)。此步骤会暂停执行,直到
mdbc-rs资源的状态阶段为Running,这表示MongoDB Community副本集可操作。1 echo "Waiting for MongoDBSearch resource to reach Running phase..." 2 kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait --for=jsonpath='{.status.phase}'=Running "mdbs/${MDB_RESOURCE_NAME}" --timeout=300s
可选。查看命名空间中所有运行的Pod。
查看MongoDB副本集成员、 Kubernetes Operator 的MongoDB控制器以及搜索节点的命名空间Pod 中运行的所有 Pod。
1 echo; echo "MongoDB resource" 2 kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get "mdb/${MDB_RESOURCE_NAME}" 3 echo; echo "MongoDBSearch resource" 4 kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get "mdbs/${MDB_RESOURCE_NAME}" 5 echo; echo "Pods running in cluster ${K8S_CTX}" 6 kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get pods
后续步骤
现在,您已成功部署MongoDB Search 和 Vector Search 以便与MongoDB Enterprise Edition 一起使用,接下来您可以在MongoDB 集群中添加数据、创建MongoDB Search 和 Vector Search 索引,并对数据运行查询。要学习;了解更多信息,请参阅MongoDB搜索和向量搜索设置。