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 With MongoDB Community Edition

You can use the Kubernetes Operator and deploy the mongot process resources to run with MongoDB Community Edition v8.2.0 or higher on a Kubernetes cluster. The mongot process supports both MongoDB Search search Vector Search. The following procedure demonstrates how to deploy and configure MongoDB Search and Vector Search to run with a new or existing replica set in your Kubernetes cluster.

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

  • A running Kubernetes cluster.

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

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 load the environment variables:

1# set it to the context name of the k8s cluster
2export K8S_CTX="<local cluster context>"
3
4# the following namespace will be created if not exists
5export MDB_NS="mongodb"
6
7# minimum required MongoDB version for running MongoDB Search is 8.2.0
8export MDB_VERSION="8.2.0"
9
10# root admin user for convenience, not used here at all in this guide
11export MDB_ADMIN_USER_PASSWORD="admin-user-password-CHANGE-ME"
12# regular user performing restore and search queries on sample mflix database
13export MDB_USER_PASSWORD="mdb-user-password-CHANGE-ME"
14# user for MongoDB Search to connect to the replica set to synchronise data from
15export MDB_SEARCH_SYNC_USER_PASSWORD="search-sync-user-password-CHANGE-ME"
16
17export OPERATOR_HELM_CHART="mongodb/mongodb-kubernetes"
18# comma-separated key=value pairs for additional parameters passed to the helm-chart installing the operator
19export OPERATOR_ADDITIONAL_HELM_VALUES=""
20
21export MDB_CONNECTION_STRING="mongodb://mdb-user:${MDB_USER_PASSWORD}@mdbc-rs-0.mdbc-rs-svc.${MDB_NS}.svc.cluster.local:27017/?replicaSet=mdbc-rs"
2

Helm automates the deployment and management of MongoDB instances on Kubernetes. If you already have the Helm repository that contains the Helm chart for installing the Kubernetes Operator operator, or skip this step. Otherwise, add the Helm repository.

To add the Helm repository, copy, paste, and run the following:

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 MongoDBCommunity 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 commands:

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

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 uses passwords from those secrets to automatically create the users in the MongoDB database.

To create the secrets, copy, paste, and run the following command:

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

If you've already deployed the MongoDB Community Edition, skip this step. Otherwise, deploy the MongoDB Community Edition.

To deploy the MongoDB Community Edition, complete the following steps:

  1. Create a MongoDBCommunity custom resource named mdb-rs.

    The resource defines CPU and memory resources for the mongod and mongodb-agent containers, and sets up the following three users:

    mdb-user

    User that can restore database and run search queries. This user uses the mdb-user-password secret to perform these operations.

    search-sync-source

    User that MongoDB Search uses to connect to MongoDB database in order to manage and build indexes. This user uses searchCoordinator role that the Kubernetes operator creates. This uses uses the mdbc-rs-search-sync-source-password secret to connect mongot to mongod.

    admin-user

    Database admin user.

    The Kubernetes Operator uses this resource to configure a MongoDB replica set with 3 members.

    To create the secrets, copy, paste, and run the following commands:

    1kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF
    2apiVersion: mongodbcommunity.mongodb.com/v1
    3kind: MongoDBCommunity
    4metadata:
    5 name: mdbc-rs
    6spec:
    7 version: ${MDB_VERSION}
    8 type: ReplicaSet
    9 members: 3
    10 security:
    11 authentication:
    12 ignoreUnknownUsers: true
    13 modes:
    14 - SCRAM
    15 agent:
    16 logLevel: DEBUG
    17 statefulSet:
    18 spec:
    19 template:
    20 spec:
    21 containers:
    22 - name: mongod
    23 resources:
    24 limits:
    25 cpu: "2"
    26 memory: 2Gi
    27 requests:
    28 cpu: "1"
    29 memory: 1Gi
    30 - name: mongodb-agent
    31 resources:
    32 limits:
    33 cpu: "1"
    34 memory: 2Gi
    35 requests:
    36 cpu: "0.5"
    37 memory: 1Gi
    38 users:
    39 # admin user with root role
    40 - name: mdb-admin
    41 db: admin
    42 # a reference to the secret containing user password
    43 passwordSecretRef:
    44 name: mdb-admin-user-password
    45 scramCredentialsSecretName: mdb-admin-user
    46 roles:
    47 - name: root
    48 db: admin
    49 # user performing search queries
    50 - name: mdb-user
    51 db: admin
    52 # a reference to the secret containing user password
    53 passwordSecretRef:
    54 name: mdb-user-password
    55 scramCredentialsSecretName: mdb-user-scram
    56 roles:
    57 - name: restore
    58 db: sample_mflix
    59 - name: readWrite
    60 db: sample_mflix
    61 # user used by MongoDB Search to connect to MongoDB database to
    62 # synchronize data from.
    63 # For MongoDB <8.2, the operator will be creating the
    64 # searchCoordinator custom role automatically.
    65 # From MongoDB 8.2, searchCoordinator role will be a
    66 # built-in role.
    67 - name: search-sync-source
    68 db: admin
    69 # a reference to the secret that will be used to generate the user's password
    70 passwordSecretRef:
    71 name: mdbc-rs-search-sync-source-password
    72 scramCredentialsSecretName: mdbc-rs-search-sync-source
    73 roles:
    74 - name: searchCoordinator
    75 db: admin
    76EOF
  2. Wait for the MongoDBCommunity resource deployment to complete.

    When you apply the MongoDBCommunity 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.

    1echo "Waiting for MongoDBCommunity resource to reach Running phase..."
    2kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait \
    3 --for=jsonpath='{.status.phase}'=Running mdbc/mdbc-rs --timeout=400s
    4echo; echo "MongoDBCommunity resource"
    5kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get mdbc/mdbc-rs
    6echo; echo "Pods running in cluster ${K8S_CTX}"
    7kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get pods
6

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.

    1kubectl apply --context "${K8S_CTX}" -n "${MDB_NS}" -f - <<EOF
    2apiVersion: mongodb.com/v1
    3kind: MongoDBSearch
    4metadata:
    5 name: mdbc-rs
    6spec:
    7 resourceRequirements:
    8 limits:
    9 cpu: "3"
    10 memory: 5Gi
    11 requests:
    12 cpu: "2"
    13 memory: 3Gi
    14EOF
  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 MongoDBSearch resource's status phase is Running, which indicates that the MongoDB Search is operational.

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

Ensure that the MongoDBCommunity resource deployment with MongoDBSearch was successful.

1echo "Waiting for MongoDBCommunity resource to reach Running phase..."
2kubectl --context "${K8S_CTX}" -n "${MDB_NS}" wait \
3 --for=jsonpath='{.status.phase}'=Running mdbc/mdbc-rs --timeout=400s
8

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 "MongoDBCommunity resource"
2kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get mdbc/mdbc-rs
3echo; echo "MongoDBSearch resource"
4kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get mdbs/mdbc-rs
5echo; echo "Pods running in cluster ${K8S_CTX}"
6kubectl --context "${K8S_CTX}" -n "${MDB_NS}" get pods

Now that you've successfully deployed MongoDB Search and Vector Search to use with MongoDB Community Edition, proceed to add data into your MongoDB cluster, create MongoDB Search and Vector Search indexes, and run queries agaist your data. To learn more, see Use MongoDB Search and Vector Search.