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.

Install and Use Search With MongoDB Enterprise Edition

You can use the Kubernetes Operator to deploy MongoDB Search and Vector Search resources to run with MongoDB enterprise v8.2.0 or higher on a Kubernetes cluster. This procedure demonstrates how to deploy and configure the mongot process to run with a new or existing replica set in your Kubernetes cluster. The deployment uses TLS certificates to ensure secure communication between MongoDB nodes and the mongot search process.

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.

  • MongoDB Ops Manager or MongoDB Cloud Manager project and API credentials.

1

Set the environment variables for use in the subsequent steps in this procedure. Copy the following commands, update the values for your environment, and then run them to load the variables:

# set it to the context name of the k8s clusterexport K8S_CTX="<local cluster context>" # the following namespace will be created if not existsexport MDB_NS="mongodb" # name of the MongoDB Custom Resource.export MDB_RESOURCE_NAME="mdb-rs" export MDB_MEMBERS=3# OM/CM's project name to be used to manage mongodb replica setexport OPS_MANAGER_PROJECT_NAME="<arbitrary project name>" # URL to Cloud Manager or Ops Manager instanceexport OPS_MANAGER_API_URL="https://cloud-qa.mongodb.com" # The API key can be an Org Owner - the operator can create the project automatically then.# The API key can also be created in a particular project that was created manually with the Project Owner scope.export OPS_MANAGER_API_USER="<SET API USER>"export OPS_MANAGER_API_KEY="<SET API KEY>"export OPS_MANAGER_ORG_ID="<SET ORG ID>" # minimum required MongoDB version for running MongoDB Search is 8.2.0export MDB_VERSION="8.2.0-ent" # root admin user for convenience, not used here at all in this guideexport MDB_ADMIN_USER_PASSWORD="admin-user-password-CHANGE-ME"# regular user performing restore and search queries on sample mflix databaseexport MDB_USER_PASSWORD="mdb-user-password-CHANGE-ME"# user for MongoDB Search to connect to the replica set to synchronise data fromexport MDB_SEARCH_SYNC_USER_PASSWORD="search-sync-user-password-CHANGE-ME" export OPERATOR_HELM_CHART="mongodb/mongodb-kubernetes"# comma-separated key=value pairs for additional parameters passed to the helm-chart installing the operatorexport OPERATOR_ADDITIONAL_HELM_VALUES="" export MDB_TLS_CERT_SECRET_PREFIX="certs"export MDB_TLS_CA_CONFIGMAP="${MDB_RESOURCE_NAME}-ca-configmap" export CERT_MANAGER_NAMESPACE="cert-manager"export MDB_TLS_SELF_SIGNED_ISSUER="selfsigned-bootstrap-issuer"export MDB_TLS_CA_CERT_NAME="my-selfsigned-ca"export MDB_TLS_CA_SECRET_NAME="root-secret"export MDB_TLS_CA_ISSUER="my-ca-issuer"export MDB_TLS_SERVER_CERT_SECRET_NAME="${MDB_TLS_CERT_SECRET_PREFIX}-${MDB_RESOURCE_NAME}-cert"export MDB_SEARCH_TLS_SECRET_NAME="${MDB_RESOURCE_NAME}-search-tls" 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"
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:

helm repo add mongodb https://mongodb.github.io/helm-chartshelm repo update mongodbhelm 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:

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

The cert-manager is required for managing TLS certificates. If you already have cert-manager installed in your cluster, skip this step. Otherwise, install cert-manager using Helm.

To install cert-manager in the cert-manager namespace, run the following command in your terminal:

helm upgrade --install \ cert-manager \ oci://quay.io/jetstack/charts/cert-manager \ --kube-context "${K8S_CTX}" \ --namespace "${CERT_MANAGER_NAMESPACE}" \ --create-namespace \ --set crds.enabled=true for deployment in cert-manager cert-manager-cainjector cert-manager-webhook; do kubectl --context "${K8S_CTX}" \ -n "${CERT_MANAGER_NAMESPACE}" \ wait --for=condition=Available "deployment/${deployment}" --timeout=300sdone echo "cert-manager is ready in namespace ${CERT_MANAGER_NAMESPACE}."
5

Create the certificate authority infrastructure that will issue TLS certificates for MongoDB and MongoDBSearch resources. The commands perform the following actions:

  • Create a self-signed ClusterIssuer.

  • Generate a CA certificate.

  • Publish a cluster-wide CA issuer that all namespaces can use.

  • Expose the CA bundle through a ConfigMap so MongoDB resources can use it.

# Bootstrap a self-signed ClusterIssuer to mint the CA secret consumed by application workloads.kubectl apply --context "${K8S_CTX}" -f - <<EOF_MANIFESTapiVersion: cert-manager.io/v1kind: ClusterIssuermetadata: name: ${MDB_TLS_SELF_SIGNED_ISSUER}spec: selfSigned: {}EOF_MANIFEST kubectl --context "${K8S_CTX}" wait --for=condition=Ready clusterissuer "${MDB_TLS_SELF_SIGNED_ISSUER}" kubectl apply --context "${K8S_CTX}" -f - <<EOF_MANIFESTapiVersion: cert-manager.io/v1kind: Certificatemetadata: name: ${MDB_TLS_CA_CERT_NAME} namespace: ${CERT_MANAGER_NAMESPACE}spec: isCA: true commonName: ${MDB_TLS_CA_CERT_NAME} secretName: ${MDB_TLS_CA_SECRET_NAME} privateKey: algorithm: ECDSA size: 256 issuerRef: name: ${MDB_TLS_SELF_SIGNED_ISSUER} kind: ClusterIssuerEOF_MANIFEST kubectl --context "${K8S_CTX}" wait --for=condition=Ready -n "${CERT_MANAGER_NAMESPACE}" certificate "${MDB_TLS_CA_CERT_NAME}" kubectl apply --context "${K8S_CTX}" -f - <<EOF_MANIFESTapiVersion: cert-manager.io/v1kind: ClusterIssuermetadata: name: ${MDB_TLS_CA_ISSUER}spec: ca: secretName: ${MDB_TLS_CA_SECRET_NAME}EOF_MANIFEST kubectl --context "${K8S_CTX}" wait --for=condition=Ready clusterissuer "${MDB_TLS_CA_ISSUER}" TMP_CA_CERT="$(mktemp)"trap 'rm -f "${TMP_CA_CERT}"' EXIT 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}" kubectl --context "${K8S_CTX}" create configmap "${MDB_TLS_CA_CONFIGMAP}" -n "${MDB_NS}" \ --from-file=ca-pem="${TMP_CA_CERT}" --from-file=mms-ca.crt="${TMP_CA_CERT}" \ --from-file=ca.crt="${TMP_CA_CERT}" \ --dry-run=client -o yaml | kubectl --context "${K8S_CTX}" apply -f -
6

Issue TLS certificates for both the MongoDB server (${MDB_RESOURCE_NAME}-server-tls) and the MongoDBSearch service (${MDB_RESOURCE_NAME}-search-tls). The MongoDB server certificate includes all necessary DNS names for the pod and service communication. Both certificates support server and client authentication.

server_certificate="${MDB_RESOURCE_NAME}-server-tls"search_certificate="${MDB_RESOURCE_NAME}-search-tls" mongo_dns_names=()for ((member = 0; member < MDB_MEMBERS; member++)); do mongo_dns_names+=("${MDB_RESOURCE_NAME}-${member}") mongo_dns_names+=("${MDB_RESOURCE_NAME}-${member}.${MDB_RESOURCE_NAME}-svc.${MDB_NS}.svc.cluster.local")donemongo_dns_names+=( "${MDB_RESOURCE_NAME}-svc.${MDB_NS}.svc.cluster.local" "*.${MDB_RESOURCE_NAME}-svc.${MDB_NS}.svc.cluster.local") search_dns_names=( "${MDB_RESOURCE_NAME}-search-svc.${MDB_NS}.svc.cluster.local") render_dns_list() { local dns_list=("$@") for dns in "${dns_list[@]}"; do printf " - \"%s\"\n" "${dns}" done} kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF_MANIFESTapiVersion: cert-manager.io/v1kind: Certificatemetadata: name: ${server_certificate} namespace: ${MDB_NS}spec: secretName: ${MDB_TLS_SERVER_CERT_SECRET_NAME} issuerRef: name: ${MDB_TLS_CA_ISSUER} kind: ClusterIssuer duration: 240h0m0s renewBefore: 120h0m0s usages: - digital signature - key encipherment - server auth - client auth dnsNames:$(render_dns_list "${mongo_dns_names[@]}")---apiVersion: cert-manager.io/v1kind: Certificatemetadata: name: ${search_certificate} namespace: ${MDB_NS}spec: secretName: ${MDB_SEARCH_TLS_SECRET_NAME} issuerRef: name: ${MDB_TLS_CA_ISSUER} kind: ClusterIssuer duration: 240h0m0s renewBefore: 120h0m0s usages: - digital signature - key encipherment - server auth - client auth dnsNames:$(render_dns_list "${search_dns_names[@]}")EOF_MANIFEST kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait --for=condition=Ready certificate "${server_certificate}" --timeout=300skubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait --for=condition=Ready certificate "${search_certificate}" --timeout=300s
7

If you have already deployed MongoDB Enterprise, skip to the next step. Otherwise, deploy the MongoDB Enterprise resource.

To deploy the MongoDB Enterprise, complete the following steps:

  1. Create the ConfigMap and secret for the MongoDB Ops Manager project if you are using MongoDB Ops Manager.

    To store the configuration and credentials for integration with MongoDB Ops Manager, copy, paste, and run the following commands:

    kubectl --context "${K8S_CTX}" -n "${MDB_NS}" create configmap om-project \ --from-literal=projectName="${OPS_MANAGER_PROJECT_NAME}" --from-literal=baseUrl="${OPS_MANAGER_API_URL}" \ --from-literal=orgId="${OPS_MANAGER_ORG_ID:-}" kubectl --context "${K8S_CTX}" -n "${MDB_NS}" create secret generic om-credentials \ --from-literal=publicKey="${OPS_MANAGER_API_USER}" \ --from-literal=privateKey="${OPS_MANAGER_API_KEY}"
  2. Create a MongoDB custom resource named mdb-rs.

    The resource defines CPU and memory resources for the mongod and mongodb-agent containers and instructs the Kubernetes Operator to configure a MongoDB replica set with 3 members:

    To deploy MongoDB Enterprise, copy, paste, and run the following in the namespace:

    kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOFapiVersion: mongodb.com/v1kind: MongoDBmetadata: name: ${MDB_RESOURCE_NAME}spec: members: ${MDB_MEMBERS} version: ${MDB_VERSION} type: ReplicaSet opsManager: configMapRef: name: om-project credentials: om-credentials security: authentication: enabled: true ignoreUnknownUsers: true modes: - SCRAM certsSecretPrefix: ${MDB_TLS_CERT_SECRET_PREFIX} tls: enabled: true ca: ${MDB_TLS_CA_CONFIGMAP} agent: logLevel: INFO podSpec: podTemplate: spec: containers: - name: mongodb-enterprise-database resources: limits: cpu: "2" memory: 2Gi requests: cpu: "1" memory: 1GiEOF
  3. Wait for the MongoDB resource deployment to complete.

    When you apply the MongoDB custom resource, the Kubernetes operator begins deploying the MongoDB nodes (pods). This step pauses the execution until the mdbc-rs resource's status phase is Running, which indicates that the MongoDB Community replica set is operational.

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

MongoDB requires authentication for secure access. In this step, you create three 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 deployed MongoDB Server and plan to deploy MongoDB Search and Vector Search:

# admin user with root rolekubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \ create secret generic mdb-admin-user-password \ --from-literal=password="${MDB_ADMIN_USER_PASSWORD}" kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOFapiVersion: mongodb.com/v1kind: MongoDBUsermetadata: name: mdb-adminspec: username: mdb-admin db: admin mongodbResourceRef: name: ${MDB_RESOURCE_NAME} passwordSecretKeyRef: name: mdb-admin-user-password key: password roles: - name: root db: adminEOF # user used by MongoDB Search to connect to MongoDB database to synchronize data from# For MongoDB <8.2, the operator will be creating the searchCoordinator custom role automatically# From MongoDB 8.2, searchCoordinator role will be a built-in role.kubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \ create secret generic "${MDB_RESOURCE_NAME}-search-sync-source-password" \ --from-literal=password="${MDB_SEARCH_SYNC_USER_PASSWORD}"kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOFapiVersion: mongodb.com/v1kind: MongoDBUsermetadata: name: search-sync-source-userspec: username: search-sync-source db: admin mongodbResourceRef: name: ${MDB_RESOURCE_NAME} passwordSecretKeyRef: name: ${MDB_RESOURCE_NAME}-search-sync-source-password key: password roles: - name: searchCoordinator db: adminEOF # user performing search querieskubectl --context "${K8S_CTX}" --namespace "${MDB_NS}" \ create secret generic mdb-user-password \ --from-literal=password="${MDB_USER_PASSWORD}"kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOFapiVersion: mongodb.com/v1kind: MongoDBUsermetadata: name: mdb-userspec: username: mdb-user db: admin mongodbResourceRef: name: ${MDB_RESOURCE_NAME} passwordSecretKeyRef: name: mdb-user-password key: password roles: - name: readWrite db: sample_mflixEOF
9

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 mdbc-rs.

    This resource specifies the CPU and memory resource requirements for the search nodes. To learn more about the settings in this custom resource, see MongoDB Search and Vector Search Settings.

    kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOFapiVersion: mongodb.com/v1kind: MongoDBSearchmetadata: name: ${MDB_RESOURCE_NAME}spec: # no need to specify source.mongodbResourceRef if MongoDBSearch CR has the same name as MongoDB CR # the operator infer it automatically security: tls: certificateKeySecretRef: name: ${MDB_SEARCH_TLS_SECRET_NAME} resourceRequirements: limits: cpu: "3" memory: 5Gi requests: cpu: "2" memory: 3GiEOF
  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 mdbc-rs resource's status phase is Running, which indicates that the MongoDB Community replica set is operational.

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

Ensure that the MongoDB resource deployment with MongoDBSearch was successful.

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

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.

echo; echo "MongoDB resource"kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get "mdb/${MDB_RESOURCE_NAME}"echo; echo "MongoDBSearch resource"kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get "mdbs/${MDB_RESOURCE_NAME}"echo; echo "Pods running in cluster ${K8S_CTX}"kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get pods

Now that you've successfully deployed MongoDB Search and Vector Search to use with MongoDB Enterprise Edition, you can add data in 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.