For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Install and Use MongoDB Search and Vector Search With External MongoDB Enterprise Edition

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.2.0 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. This tutorial demonstrates how to configure a secure deployment with TLS enabled. You must provide the TLS certificates:

  • The server certificate and key for the MongoDBSearch service.

  • The public CA certificate for the external MongoDB database.

To deploy MongoDB Search and Vector Search, you must have the following:

  • A running Kubernetes cluster with kubeconfig available 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.2 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.

  • External DB TLS

    Your external MongoDB Enterprise deployment must be configured to use and require TLS connections.

  • External DB CA file

    You must have the public Certificate Authority (CA) certificate file (for example, external-ca.crt) that was used to sign your external MongoDB deployment's server certificates. You will need this file locally.

  • Search Service certificate files

    You must have a valid TLS server certificate and private key (for example, search-service.crt, search-service.key) for the MongoDBSearch service. This certificate must be valid for the hostname you will use to access the service (for example, mdbs-search.example.com).

Before you install MongoDB Search and Vector Search using the Kubernetes Operator, you must do the following:

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.

1
  1. Log in to MongoDB Cloud Manager or Ops Manager.

  2. Click the Deployment tab.

  3. Select the cluster that you want to modify.

2
  1. Click the Modify button to open the deployment configuration editor.

  2. Click Advanced Configuration Options under the Process Configuration section.

  3. Click the Add Option button and select setParameter Startup Option from the dropdown.

  4. Add the following parameters in the fields, one by one, by clicking Add after adding the name and value:

    Parameter
    Value

    mongotHost

    Your search hostname and port. For example: search-node1.example.com:27017.

    searchIndexManagementHostAndPort

    Your search hostname and port. For example: search-node1.example.com:27017.

    skipAuthenticationToSearchIndexManagementServer

    false

    searchTLSMode

    Your configured TLS mode. For example, preferTLS, if the mongot process is configured to accept TLS connections.

    useGrpcForSearch

    true

3
  1. Click the Review & Deploy button.

  2. Review and confirm to apply the changes.

    Cloud Manager or Ops Manager performs a rolling restart of your replica set to apply the new configuration.

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.

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. Log in to Cloud Manager or Ops Manager UI.

  2. Go to the Security tab and click on Users.

2
3
Username

Enter search-sync-source.

Password

Set a strong, secure password.

Authentication Database

Choose the admin database.

4

In the Assign Roles section, select the built-in searchCoordinator role from the dropdown for the admin database.

5

Prepare your environment for running the sample code in this tutorial in a terminal.

1

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 run the commands in your terminal:

1export K8S_CTX="<your kubernetes context here>"
2
3export MDB_NS="mongodb"
4
5export MDB_VERSION="8.2.0"
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_TLS_CA_SECRET_NAME="ca"
12export MDB_SEARCH_TLS_SECRET_NAME="mdbs-search-tls"
13
14export MDB_SEARCH_SERVICE_NAME="mdbs-search"
15export MDB_SEARCH_HOSTNAME="mdbs-search.example.com"
16
17# External MongoDB replica set members - REPLACE THESE VALUES with your actual external MongoDB hosts
18# In production, replace with your actual external MongoDB replica set members
19export MDB_EXTERNAL_HOST_0="mdbc-rs-0.mdbc-rs-svc.${MDB_NS}.svc.cluster.local:27017"
20export MDB_EXTERNAL_HOST_1="mdbc-rs-1.mdbc-rs-svc.${MDB_NS}.svc.cluster.local:27017"
21export MDB_EXTERNAL_HOST_2="mdbc-rs-2.mdbc-rs-svc.${MDB_NS}.svc.cluster.local:27017"
22
23# REPLACE with your actual external MongoDB replica set name
24export MDB_EXTERNAL_REPLICA_SET_NAME="mdbc-rs"
25
26export OPERATOR_HELM_CHART="mongodb/mongodb-kubernetes"
27export OPERATOR_ADDITIONAL_HELM_VALUES=""
28
29export MDB_CONNECTION_STRING="mongodb://mdb-user:${MDB_USER_PASSWORD}@${MDB_EXTERNAL_HOST_0}/?replicaSet=${MDB_EXTERNAL_REPLICA_SET_NAME}&tls=true&tlsCAFile=/tls/ca.crt"

Note that these environment variables are only available in the current terminal session and will need to be set again in any new terminal sessions.

2

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:

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

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:

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}"

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.

1

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 the mongot process 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# Create admin user secret
2kubectl create secret generic mdb-admin-user-password \
3 --from-literal=password="${MDB_ADMIN_USER_PASSWORD}" \
4 --dry-run=client -o yaml | kubectl apply --context "${K8S_CTX}" --namespace "${MDB_NS}" -f -
5
6# Create search sync source user secret
7kubectl create secret generic "${MDB_RESOURCE_NAME}-search-sync-source-password" \
8 --from-literal=password="${MDB_SEARCH_SYNC_USER_PASSWORD}" \
9 --dry-run=client -o yaml | kubectl apply --context "${K8S_CTX}" --namespace "${MDB_NS}" -f -
10
11# Create regular user secret
12kubectl create secret generic mdb-user-password \
13 --from-literal=password="${MDB_USER_PASSWORD}" \
14 --dry-run=client -o yaml | kubectl apply --context "${K8S_CTX}" --namespace "${MDB_NS}" -f -
15
16echo "User secrets created."
2

You can deploy one instance of the search node without any load balancing. To deploy, complete the following steps:

  1. Create a MongoDBSearch custom resource named mdbs.

    This resource contains the following:

    spec.source.external.hostAndPorts

    List of external MongoDB replica set members.

    spec.source.username

    Search synchronization user username.

    spec.source.passwordSecretRef

    Search synchronization user password.

    spec.source.external.tls.ca.name

    Configures MongoDBSearch pods to trust the external database. It points to the Kubernetes secret that contains the public CA certificate for your external MongoDB.

    spec.security.tls.certificateKeySecretRef.name

    Secures the MongoDBSearch service. It points to the Kubernetes secret containing the TLS server certificate and private key that the MongoDBSearch pods will present to incoming clients.

    spec.resourceRequirements

    CPU 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.

    1kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF
    2apiVersion: mongodb.com/v1
    3kind: MongoDBSearch
    4metadata:
    5 name: ${MDB_SEARCH_RESOURCE_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 tls:
    14 ca:
    15 name: ${MDB_TLS_CA_SECRET_NAME}
    16 username: search-sync-source
    17 passwordSecretRef:
    18 name: ${MDB_RESOURCE_NAME}-search-sync-source-password
    19 key: password
    20 security:
    21 tls:
    22 certificateKeySecretRef:
    23 name: ${MDB_SEARCH_TLS_SECRET_NAME}
    24 resourceRequirements:
    25 limits:
    26 cpu: "3"
    27 memory: 5Gi
    28 requests:
    29 cpu: "2"
    30 memory: 3Gi
    31EOF
  2. Wait for the MongoDBSearch resource deployment to complete.

    When you apply the MongoDBSearch custom resource, the Kubernetes operator begins deploying the search nodes (pods). This step pauses the execution until the mdbs resource's status phase is Running, which indicates that the MongoDB Search StatefulSet is operational.

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

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 27028 with an external IP address or hostname that can be accessed from outside the Kubernetes cluster.

1kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<YAML
2apiVersion: v1
3kind: Service
4metadata:
5 name: ${MDB_SEARCH_SERVICE_NAME}
6spec:
7 type: LoadBalancer
8 selector:
9 app: ${MDB_SEARCH_RESOURCE_NAME:-mdbs}-search-svc
10 ports:
11 - name: mongot
12 port: 27028
13 targetPort: 27028
14YAML
15
16echo "Waiting for external IP to be assigned to service ${MDB_SEARCH_SERVICE_NAME}..."
17TIMEOUT=120 # 2 minutes timeout
18ELAPSED=0
19while [ ${ELAPSED} -lt ${TIMEOUT} ]; do
20 EXTERNAL_IP=$(kubectl get service "${MDB_SEARCH_SERVICE_NAME}" --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
4

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.

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

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.