You can use the Kubernetes Operator to deploy MongoDB Search and Vector Search on a Kubernetes cluster to run with an external MongoDB Enterprise Edition v8.0.10 or higher server. This procedure demonstrates how to deploy and configure the mongot process in your Kubernetes cluster to use a new or existing external replica set deployment.
Prerequisites
To deploy MongoDB Search and Vector Search, you must have the following:
A running Kubernetes cluster with
kubeconfigavailable locally.Kubernetes command-line tool,
kubectl, configured to communicate with your cluster.Helm, the package manager for Kubernetes, to install the Kubernetes Operator.
Bash v5.1 or higher for running the commands in this tutorial.
A MongoDB Enterprise Edition replica set running version 8.0.10 or higher for storing data.
To learn more about deploying MongoDB Enterprise, see Deploy and Configure MongoDB Database Resources.
A running MongoDB Cloud Manager or Ops Manager for managing MongoDB tasks.
Before You Begin
Before you install MongoDB Search and Vector Search using the Kubernetes Operator, you must do the following:
Configure Cloud Manager or Ops Manager Parameters for MongoDB Search and Vector Search
Log in to the Cloud Manager or Ops Manager UI and perform the following steps to configure Cloud Manager or Ops Manager for MongoDB Search and Vector Search.
Modify your Cloud Manager or Ops Manager deployment configuration.
Click the Modify button to open the deployment configuration editor.
Click Advanced Configuration Options under the Process Configuration section.
Click the Add Option button and select setParameter Startup Option from the dropdown.
Add the following parameters in the fields, one by one, by clicking Add after adding the name and value:
ParameterValuemongotHostYour search hostname and port. For example:
search-node1.example.com:27017.searchIndexManagementHostAndPortYour search hostname and port. For example:
search-node1.example.com:27017.skipAuthenticationToSearchIndexManagementServerfalsesearchTLSModeYour configured TLS mode. For example,
preferTLS, if themongotprocess is configured to accept TLS connections.
Configure a Cloud Manager or Ops Manager User for MongoDB Search and Vector Search
You must create a user with the searchCoordinator role. In MongoDB versions 8.2 and later, the searchCoordinator is a built-in role. You must create a user and assign the role to the user. For MongoDB versions earlier than 8.2 and 8.0.10 or later, you must manually create the role and assign the privileges before creating the user. Select the tab that corresponds to the MongoDB version you are running to create a user with the searchCoordinator role.
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:
To create the searchCoordinator role with the privileges and then add a user with the searchCoordinator role, complete the following steps in the Cloud Manager or Ops Manager UI:
Add the database privileges to assign to the role.
In the Privileges section, click Add Privilege.
For Resource, enter
__mdb_internal_searchfor the database.Do not enter anything in the collection field.
Under Actions, add the following actions.
changeStreamcleanupStructuredEncryptionData,compactStructuredEncryptionDatacollStatsconvertToCappedcreateCollection,dropCollection,listCollections,renameCollectionSameDBcreateIndex,dropIndex,listIndexescreateSearchIndexes,dropSearchIndex,listSearchIndexes,updateSearchIndexdbHash,dbStatsfind,insert,remove,updatekillCursorsplanCacheRead
In mongosh, run the following commands:
Create the custom role.
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" } ] });
Setup Your Environment
Prepare your environment for running the sample code in this tutorial in a terminal.
Required. Set the environment variables.
To set the environment variables for use in the subsequent steps in this procedure, copy the following, set the values for the environment variables, and then load the environment variables:
1 export K8S_CTX="<your kubernetes context here>" 2 3 export MDB_NS="mongodb" 4 5 export MDB_VERSION="8.0.10" 6 7 export MDB_ADMIN_USER_PASSWORD="admin-user-password-CHANGE-ME" 8 export MDB_USER_PASSWORD="mdb-user-password-CHANGE-ME" 9 export MDB_SEARCH_SYNC_USER_PASSWORD="search-sync-user-password-CHANGE-ME" 10 11 export 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 15 export MDB_EXTERNAL_HOST_0="mdbc-rs-0.mdbc-rs-svc.${MDB_NS}.svc.cluster.local:27017" 16 export MDB_EXTERNAL_HOST_1="mdbc-rs-1.mdbc-rs-svc.${MDB_NS}.svc.cluster.local:27017" 17 export 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 20 export 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 24 export MDB_EXTERNAL_KEYFILE_CONTENT="your-mongodb-keyfile-content-CHANGE-ME" 25 26 # REPLACE with your actual external MongoDB replica set name 27 export MDB_EXTERNAL_REPLICA_SET_NAME="mdbc-rs" 28 29 export OPERATOR_HELM_CHART="mongodb/mongodb-kubernetes" 30 export OPERATOR_ADDITIONAL_HELM_VALUES="" 31 32 export MDB_CONNECTION_STRING="mongodb://mdb-user:${MDB_USER_PASSWORD}@${MDB_EXTERNAL_HOST_0}/?replicaSet=${MDB_EXTERNAL_REPLICA_SET_NAME}"
Conditional. Add the MongoDB Helm repository.
Helm automates the deployment and management of MongoDB instances on Kubernetes. If you have already added the Helm repository that contains the Helm chart for installing the Kubernetes Operator operator, skip this step. Otherwise, add the Helm repository.
To add, copy, paste, and run the following command:
1 helm repo add mongodb https://mongodb.github.io/helm-charts 2 helm repo update mongodb 3 helm search repo mongodb/mongodb-kubernetes
Conditional. Install the MongoDB Controllers for Kubernetes Operator.
The Kubernetes Operator watches MongoDB, MongoDBOpsManager, and MongoDBSearch custom resources and manages the lifecycle of your MongoDB deployments. If you already installed the MongoDB Controllers for Kubernetes Operator, skip this step. Otherwise, install the MongoDB Controllers for Kubernetes Operator from the Helm repository you added in the previous step.
To install the MongoDB Controllers for Kubernetes Operator in the mongodb namespace, copy, paste, and run the following:
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}"
The preceding command installs Kubernetes Operator in the mongodb namespace, which it creates if it doesn't already exist. After installation, the Kubernetes Operator watches for MongoDBSearch custom resources and manage the lifecycle of your MongoDB Search and Vector Search deployments.
Install MongoDB Search and Vector Search
Required. Create and load the MongoDB user secrets.
The mongot process requires authentication credentials to connect to your external MongoDB deployment for creating search indexes and running search queries. This step creates the following Kubernetes secrets:
mdb-admin-user-password- credentials for the MongoDB administrator.mdb-user-password- credentials for the user authorized to perform search queries.mdbc-rs-search-sync-source-password- credentials for a dedicated search user used internally by themongotprocess to synchronize data and manage indexes.
Kubernetes Operator mounts these secrets into the MongoDB pods.
To create the secrets, copy, paste, and run the following in the namespace where you plan to deploy MongoDB Search and Vector Search:
1 kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \ 2 create secret generic mdb-admin-user-password \ 3 --from-literal=password="${MDB_ADMIN_USER_PASSWORD}" 4 5 kubectl --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 9 kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \ 10 create secret generic mdb-user-password \ 11 --from-literal=password="${MDB_USER_PASSWORD}"
Required. Deploy the keyfile secret to use to connect to the mongod.
To authenticate connections from your external MongoDB, the mongot process uses the same keyfile that your external MongoDB replica set members use for internal authentication between replica set members.
To create a Kubernetes secret containing the keyfile content from your external MongoDB, copy, paste, and run the following command:
1 kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \ 2 create secret generic "${MDB_EXTERNAL_KEYFILE_SECRET_NAME}" \ 3 --from-literal=keyfile="${MDB_EXTERNAL_KEYFILE_CONTENT}"
Required. Create and deploy the resource for MongoDB Search and Vector Search.
You can deploy one instance of the search node without any load balancing. To deploy, complete the following steps:
Create a
MongoDBSearchcustom resource namedmdbs.This resource contains the following:
spec.source.external.hostAndPortsList of external MongoDB replica set members.
spec.source.external.keyfileSecretRefKeyfile secret used by the external replica set members.
spec.source.usernameSearch synchronization user username.
spec.source.passwordSecretRefSearch synchronization user password.
spec.resourceRequirementsCPU and memory resource requirements for the search container.
To learn more about the settings in this custom resource, see MongoDB Search and Vector Search Settings.
1 kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF 2 apiVersion: mongodb.com/v1 3 kind: MongoDBSearch 4 metadata: 5 name: mdbs 6 spec: 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 27 EOF Wait for the
MongoDBSearchresource deployment to complete.When you apply the
MongoDBSearchcustom resource, the Kubernetes operator begins deploying the search nodes (pods). This step pauses the execution until themdbsresource's status phase isRunning, which indicates that the MongoDB Search StatefulSet is operational.1 echo "Waiting for MongoDBSearch resource to reach Running phase..." 2 kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait \ 3 --for=jsonpath='{.status.phase}'=Running mdbs/mdbs --timeout=300s
Required. Configure external access for MongoDB Search and Vector Search.
To enable your external MongoDB instances to connect to the search service, you must configure external access for MongoDB Search and Vector Search. You can create a LoadBalancer Service that exposes the search pods outside the Kubernetes cluster.
This following service exposes the MongoDBSearch service on port 27027 with an external IP address or hostname that can be accessed from outside the Kubernetes cluster.
1 kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<YAML 2 apiVersion: v1 3 kind: Service 4 metadata: 5 name: ${MDB_SEARCH_HOSTNAME} 6 spec: 7 type: LoadBalancer 8 selector: 9 app: mdbs-search-svc 10 ports: 11 - name: mongot 12 port: 27027 13 targetPort: 27027 14 YAML 15 16 echo "Waiting for external IP to be assigned to service ${MDB_SEARCH_HOSTNAME}..." 17 TIMEOUT=120 # 2 minutes timeout 18 ELAPSED=0 19 while [ ${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)) 28 done 29 30 if [ ${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 34 fi
Optional. View all the running pods in your namespace.
View all the running pods in your namespace pods for the MongoDB replica set members, the MongoDB Controllers for Kubernetes Operator, and the Search nodes.
1 echo; echo "MongoDBSearch resource" 2 kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get mdbs/mdbs 3 echo; echo "Search pods running in cluster ${K8S_CTX}" 4 kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get pods -l app=mdbs-search-svc 5 echo; echo "All pods in namespace ${MDB_NS}" 6 kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get pods
Next Steps
Now that you've successfully deployed MongoDB Search and Vector Search to use with external MongoDB Enterprise Edition, you can add data into your MongoDB cluster, create MongoDB Search and Vector Search indexes, and run queries against your data. To learn more, see MongoDB Search and Vector Search Settings.