---
title: "How To Perform a MongoDB Kubernetes Installation"
description: "Learn how to conquer the challenge of deploying a MongoDB Kubernetes installation in this step-by-step tutorial!"
canonical: "https://adamtheautomator.com/mongodb-kubernetes/"
---

# How To Perform a MongoDB Kubernetes Installation

> Learn how to conquer the challenge of deploying a MongoDB Kubernetes installation in this step-by-step tutorial!

Source: https://adamtheautomator.com/mongodb-kubernetes/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How To Perform a MongoDB Kubernetes Installation](https://adamtheautomator.com/wp-content/uploads/2022/04/How-To-Perform-a-MongoDB-Kubernetes-Installation.jpg)

# How To Perform a MongoDB Kubernetes Installation

[![](https://secure.gravatar.com/avatar/572a248f516b6d0cd566cb44fdbd9336f30ef95aa0f6d78f118c1f47b1b6d6b7?s=192&d=mm&r=g)Arvid Larson](https://adamtheautomator.com/author/arvid-larson/)4 May 20229 min. read

Categories: [DevOps](/category/devops/)

Tags:[Kubernetes](/tag/kubernetes/)[MongoDB](/tag/mongodb/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Creating Namespace for MongoDB](#creating-namespace-for-mongodb)
*   [Downloading the MongoDB Kubernetes Operator](#downloading-the-mongodb-kubernetes-operator)
*   [Deploying the MongoDB Operator](#deploying-the-mongodb-operator)
*   [Deploying MongoDB ReplicaSet to Kubernetes](#deploying-mongodb-replicaset-to-kubernetes)
*   [Creating a New User and Database for the MongoDB Deployment](#creating-a-new-user-and-database-for-the-mongodb-deployment)
*   [Connecting to the MongoDB Deployment](#connecting-to-the-mongodb-deployment)
*   [Conclusion](#conclusion)

Application deployments in Kubernetes can be challenging, especially for [stateful](https://cloud.google.com/kubernetes-engine/docs/how-to/stateful-apps) applications such as MongoDB to Kubernetes cluster. How do you ease up the deployment? Let the [MongoDB Kubernetes Operator](https://www.mongodb.com/docs/kubernetes-operator/master/) automatically deploy and manage stateful applications inside your Kubernetes cluster.

In this tutorial, you’ll learn how to deploy MongoDB to Kubernetes using the MongoDB Kubernetes Operator.

Read on and start automating your Kubernetes deployments!

## Prerequisites

This tutorial will be a hands-on demonstration. To follow along, be sure you have the following requirements:

*   A Kubernetes cluster – This demo uses the Kubernetes cluster v1.23.

Related:[How to Create a Kubernetes Cluster With the AWS EKS CLI](https://adamtheautomator.com/aws-eks-cli/)

*   [Kubectl](https://kubernetes.io/docs/reference/kubectl/kubectl/) installed on the local machine.
    
*   [MongoDB Compass](https://www.mongodb.com/products/compass) installed on the local machine.
    
*   [Git](https://git-scm.com/downloads) installed on your machine.
    
*   [jq](https://stedolan.github.io/jq/) JSON Parser installed on the local machine.
    

## Creating Namespace for MongoDB

When deploying applications to Kubernetes, creating a specific namespace for your projects is always recommended.

Namespaces make managing Kubernetes resources easier for administrators and prevent name collisions. At the same time, creating namespaces prevents incorrect resource usage in the Kubernetes environment.

Open your terminal, and run the [`kubectl create`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#create) command below to create a new namespace (`ns`) called `mongodb` on your Kubernetes cluster.

```bash
kubectl create ns mongodb
```

Now run the below [`kubectl get`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#get) command to verify the list of namespaces on your Kubernetes cluster.

```bash
kubectl get ns
```

You’ll see a new namespace called **mongodb** available on your cluster, as shown below.

![Creating and Verifying the mongodb Namespace](https://adamtheautomator.com/wp-content/uploads/2022/04/image-475.png)

Creating and Verifying the mongodb Namespace

Related:[How to Deploy and Manage a Docker MongoDB Container](https://adamtheautomator.com/docker-mongodb/)

## Downloading the MongoDB Kubernetes Operator

After creating a namespace, it’s time to download the MongoDB Kubernetes Operator. The MongoDB Kubernetes Operator is a set of [Kubernetes operators](https://cloud.redhat.com/learn/topics/operators). These Kubernetes operators offer automatic deployment, configuration, and management of MongoDB Community on the Kubernetes environment.

Run the `git` command below to `clone` the MongoDB Kubernetes Operator project to your local machine.

```bash
git clone <https://github.com/mongodb/mongodb-kubernetes-operator.git>
```

Now, run each command below to change your current working directory to `mongodb-kubernetes-operator` and list (`ls`) all available files and directories.

```bash
# Change the working directory to mongodb-kubernetes-operator
cd mongodb-kubernetes-operator/

# List files and directories
ls
```

You’ll see a subdirectory named config inside the _~/mongodb-kubernetes-operator_ project directory, as shown below. The _config_ subdirectory contains examples of YAML files for deploying MongoDB to Kubernetes.

![Downloading mongodb-kubernetes-operator Source Code](https://adamtheautomator.com/wp-content/uploads/2022/04/image-476.png)

Downloading mongodb-kubernetes-operator Source Code

## Deploying the MongoDB Operator

Now that your custom namespace and MongoDB Kubernetes Operator are set up, you’re ready to deploy the MongoDB Operator to your Kubernetes cluster. How? You’ll use the configuration inside the _config_ directory.

The MongoDB Operator handles the lifecycle of your MongoDB deployment on the Kubernetes cluster. The operator will automatically create, manage, and scale the state of your MongoDB deployment.

To deploy the MongoDB operator, you must create a Kubernetes [Custom Resources Definition (CRD)](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/#customresourcedefinitions) and the controller. The Kubernetes CRD is extensible to Kubernetes API, allowing you to create custom resources in Kubernetes.

1\. Run the [`kubectl apply`](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#apply) command below to create a new Kubernetes CRD for MongoDB deployment.

> _The_ [_Kubernetes operator_](https://www.youtube.com/watch?v=ha3LjlD6g7g) _replaced all human interaction to deploy an application on Kubernetes. The operator is a pod controller that automatically deploys and manages your stateful application on Kubernetes._

```bash
kubectl apply -f config/crd/bases/mongodbcommunity.mongodb.com_mongodbcommunity.yaml
```

![Creating New Kubernetes CRD MongoDB Kubernetes Deployment](https://adamtheautomator.com/wp-content/uploads/2022/04/image-477.png)

Creating New Kubernetes CRD MongoDB Kubernetes Deployment

2\. Next, run the following `kubectl get` command to verify the list of available `crd` in your Kubernetes.

```bash
kubectl get crd/mongodbcommunity.mongodbcommunity.mongodb.com
```

Below, you can see the CRD with the name mongodbcommunity.mongodbcommunity.mongodb.org.

![Checking List of CRDs](https://adamtheautomator.com/wp-content/uploads/2022/04/image-478.png)

Checking List of CRDs

3\. Run the below command to create a new custom [Role-Based Access Control (RBAC)](https://kubernetes.io/docs/reference/access-authn-authz/rbac/) for the MongoDB Operator, and specify RBAC implementation to the namespace **mongodb**.

This command creates a new role, rolebinding, and serviceaccount for the MongoDB operator, which are custom permissions on your Kubernetes cluster. These custom permissions are required for automatically deploying and managing MongoDB.

```bash
kubectl apply -k config/rbac/ -n mongodb
```

![Deploying RBAC for MongoDB Deployment](https://adamtheautomator.com/wp-content/uploads/2022/04/image-479.png)

Deploying RBAC for MongoDB Deployment

4\. Now, run each `kubectl` command below to verify the role, rolebinding, and serviceaccount for the MongoDB operator.

```bash
# Checking list role
kubectl get role mongodb-kubernetes-operator -n mongodb

# Checking list rolebinding
kubectl get rolebinding mongodb-kubernetes-operator -n mongodb

# Checking serviceaccount
kubectl get serviceaccount mongodb-kubernetes-operator -n mongodb
```

The output below shows the role, rolebinding, and serviceaccount mongodb-kubernetes-operator is created on the mongodb namespace.

![Checking role, rolebinding, and serviceaccount on Kubernetes](https://adamtheautomator.com/wp-content/uploads/2022/04/image-480.png)

Checking role, rolebinding, and serviceaccount on Kubernetes

5\. Once verification is complete, run the `kubectl` command below to deploy the MongoDB Operator to the `mongodb` namespace.

This command creates a new pod (mongodb-kubernetes-operator) with the base Docker image (quay.io/mongodb/mongodb-kubernetes-operator). This pod will act as the controller for automatically deploying MongoDB ReplicaSets on the Kubernetes cluster.

```bash
kubectl create -f config/manager/manager.yaml -n mongodb
```

Below, you can see that the MongoDB operator deployment is created, but the pod is still initializing.

![Deploying MongoDB Operator to the mongodb Namespace](https://adamtheautomator.com/wp-content/uploads/2022/04/image-481.png)

Deploying MongoDB Operator to the mongodb Namespace

6\. Finally, run the following commands to verify the deployment and pods on the `mongodb` namespace.

```bash
# Checking deployment on mongodb namespace
kubectl get deployment.apps -n mongodb

# Checking pods on mongodb namespace
kubectl get pods -n mongodb
```

As you can see below, the MongoDB Operator pod is running.

![Verifying MongoDB Operator Deployment and Pod](https://adamtheautomator.com/wp-content/uploads/2022/04/image-482.png)

Verifying MongoDB Operator Deployment and Pod

## Deploying MongoDB ReplicaSet to Kubernetes

After your MongoDB deployment, you’ll deploy ReplicaSet to your Kubernetes to provide high availability and redundancy for your MongoDB deployment. You wouldn’t want downtime on your deployment.

> _MongoDB ReplicaSets deployment using MongoDB Kubernetes Operator is secure by default using the [SCRAM authentication](https://github.com/mongodb/mongodb-kubernetes-operator/blob/master/docs/users.md) for users. At the same time, you can use [TLS secure](https://github.com/mongodb/mongodb-kubernetes-operator/blob/master/docs/secure.md) connection for users and applications connections and expose [Prometheus metrics](https://github.com/mongodb/mongodb-kubernetes-operator/blob/master/docs/prometheus/README.md) for monitoring resources._

_**Related: Getting Started with Grafana & Prometheus Kubernetes Cluster Monitoring**_

Inside the _config/samples/_ directory, you’ll see multiple samples of YAML files for the ReplicaSet deployment. Each file can be used on different deployment scenarios, but the default ReplicaSet deployment file is _mongodb.com\_v1\_mongodbcommunity\_cr.yaml_.

1\. Edit the YAML file for your deployment using your preferred editor. This this demo uses the _/mongodb.com\_v1\_hostpath.yaml_ file for local deployment. This YAML file is located in the _config/samples/arbitrary\_statefulset\_configuration/_ directory.

> _If you’re deploying the MongoDB Kubernetes Operator on the cloud services such as GKE and AKS, use the YAML file mongodb.com\_v1\_mongodbcommunity\_cr.yaml. But if you’re deploying locally inside Kind or Minikube, you can use the custom YAML file (arbitrary\_statefulset\_configuration/mongodb.com\_v1\_hostpath.yaml), which automatically creates custom PV and PVC for MongoDB ReplicaSet pods._

Below, change the number (members) of ReplicaSets you like to deploy depending on your environment, but the default is 3. For this tutorial, the number of ReplicaSets is set to 2.

```yaml
spec:
  members: 2 # Number of replica sets to create
  security:
    authentication:
      modes:
      - SCRAM
  statefulSet:
```

Replace <your-password-here> with your strong password. You’ll use this password to log in to the MongoDB database, and by default, you use the SCRAM authentication.

Save the changes and close the editor once you’re satisfied with the password.

```yaml
---
apiVersion: v1
kind: Secret
metadata:
  name: my-user-password
type: Opaque
stringData:
  password: <your-password-here> # Set password for MongoDB admin
```

Related:[How to Take Control of Your MongoDB Security](https://adamtheautomator.com/mongodb-security/)

2\. Next, run the `kubectl` command below to deploy (`apply`) the MongoDB ReplicaSet using the `mongodb.com_v1_hostpath.yaml` to the **mongodb** namespace.

This command creates the following:

*   A new custom resource (MongoDBCommunity) with the name mdb0 under the `mongodbcommunity.mongodb.com/v1` CRD
    
*   Kubernetes secrets for storing MongoDB user passwords. At the same time, some PVs and PVCs with the type as `hostPath` for the MongoDB Replica Set.
    

The deployment will take some time, depending on how big are the ReplicaSets to create and the Kubernetes cluster itself.

```bash
kubectl apply -f config/samples/arbitrary_statefulset_configuration/mongodb.com_v1_hostpath.yaml -n mongodb
```

![Deploying MongoDB Replica Sets](https://adamtheautomator.com/wp-content/uploads/2022/04/image-483.png)

Deploying MongoDB Replica Sets

3\. After deployment, run each command below to verify the custom resources and pods on the `mongodb` namespace.

```bash
# Checking the mongodbcommunity crd
kubectl get mongodbcommunity -n mongodb

# Checking pods on the mongodb namespace
kubectl get pods -n mongodb
```

The output below shows the following:

*   The custom resource MongoDBCommunity with the name **mdb0** is running with MongoDB **v4.4.0**.
    
*   The two MongoDB ReplicaSet pods are running with the name **mdb0-0** and **mdb0-1**. Each pod contains two different containers, the **mongod,** and the **mongodb-agent** container.
    

![Verifying MongoDB ReplicaSet Deployment and Pods](https://adamtheautomator.com/wp-content/uploads/2022/04/image-484.png)

Verifying MongoDB ReplicaSet Deployment and Pods

4\. Lastly, run another `kubectl` command below to verify the `pv` and `pvc` on the `mongodb` namespace.

```bash
kubectl get pv,pvc -n mongodb
```

The output below shows you some pv and pvc used by the MongoDB ReplicaSet pods.

![Checking PV and PVC on the mongodb Namespace](https://adamtheautomator.com/wp-content/uploads/2022/04/image-485.png)

Checking PV and PVC on the mongodb Namespace

## Creating a New User and Database for the MongoDB Deployment

You’ve completed the deployment of the MongoDB operator and ReplicaSets to your Kubernetes cluster at this point. The next step is to create a new MongoDB database and user for your application.

You’ll create a new Kubernetes secret for the new user, edit the custom resource of ReplicaSet deployment, and update the ReplicaSet to your cluster.

1\. Create a new YAML file using your preferred editor and populate the following Kubernetes secret. You can name the YAML file as you like, but the file is named _new-user.yaml_ in this tutorial.

This YAML file creates a new Kubernetes secret with the name as myappdata and the password as myapppassword.

```yaml
---
apiVersion: v1
kind: Secret
metadata:
  name: myappdata # name of the secret meta.data
type: Opaque
stringData:
  password: myapppassword # password for the new user
```

2\. Next, run the `kubectl` command below to execute the `new-user.yaml` configuration.

This command creates and applies (apply) a new Kubernetes secret to your cluster and applies this secret to the mongodb namespace.

```bash
kubectl apply -f new-user.yaml -n mongodb
```

![Creating Secret to Kubernetes Cluster](https://adamtheautomator.com/wp-content/uploads/2022/04/image-486.png)

Creating Secret to Kubernetes Cluster

3\. Edit the YAML file of your ReplicaSet CRD configuration (_mongodb.com\_v1\_hostpath.yaml_) in the _config/samples/arbitrary\_statefulset\_configuration_ directory

Add the following configuration to the _mongodb.com\_v1\_hostpath.yaml_ file, which creates the database and user for MongoDB.

Be sure to replace scram-secret-myapp with your own scram credentials, save the changes to the file and close the editor.

```yaml
    - name: appuser # New user appuser
      db: admin # Allows authentication to database admin
      passwordSecretRef:
        name: myappdata # The Kubernetes secret
      roles:
        - name: dbAdmin # Setup role dbAdmin
          db: appdb1 # to the appdb1
      scramCredentialsSecretName: scram-secret-myapp
```

4\. Now, run the below command to `apply` the new changes you made in the `mongodb.com_v1_hostpath.yaml` file in step three.

```bash
kubectl apply -f config/samples/arbitrary_statefulset_configuration/mongodb.com_v1_hostpath.yaml -n mongodb
```

![Creating New MongoDB User and Database on Kubernetes](https://adamtheautomator.com/wp-content/uploads/2022/04/image-487.png)

Creating New MongoDB User and Database on Kubernetes

5\. Lastly, run each command below to generate the detailed MongoDB connections for your application. These commands generate the detailed MongoDB connection for your applications in JSON output and parse the output using the `jq` command line.

Replace mdb0-admin-mongoadmin with your environment details as follows:

*   `mdb0` – The meta-data name of the MongoDB resources.
    
*   `admin` – The database to authenticate.
    
*   `mongoadmin` – The user you’ll be using to authenticate
    

```bash
# Reteive admin authentication details and connections
kubectl get secrets mdb0-admin-mongoadmin -n mongodb -o json | jq -r '.data | with_entries(.value |= @base64d)'

# Retrive appuser authentication details and connections
kubectl get secrets mdb0-admin-appuser -n mongodb -o json | jq -r '.data | with_entries(.value |= @base64d)'
```

The output below shows you can connect to MongoDB using the standard and standardSrv connections. You’ll also see the username and password of your MongoDB deployment.

![Retrieving Details of MongoDB User and Password, and Connections](https://adamtheautomator.com/wp-content/uploads/2022/04/image-488.png)

Retrieving Details of MongoDB User and Password, and Connections

## Connecting to the MongoDB Deployment

You’ve completed deploying MongoDB on the Kubernetes cluster using the MongoDB Kubernetes operator. But how do you know the deployment works? You’ll verify your local machine’s database and user connection to MongoDB inside the Kubernetes cluster.

To verify the connection to your MongoDB deployment, you’ll use port-forwarding. Port-forwarding in Kubernetes allows you to create a forwarding port from any Kubernetes service to your local port.

1\. Run the `kubectl` command below to `get` the list of services (`svc`) on the `mongodb` namespace (`-n`).

```bash
kubectl get svc -n mongodb
```

Below, you’ll see the service named mdb0-svc, which exposes port 27017 (default port of MongoDB).

![Listing Services on the mongodb Namespace](https://adamtheautomator.com/wp-content/uploads/2022/04/image-489.png)

Listing Services on the mongodb Namespace

2\. Next, run the below command to create port-forwarding to the Kubernetes cluster.

This command creates a new port forwarding on the Kubernetes service (mdb0-svc) and forwards the local port 27017 to the Kubernetes service port 27017.

```bash
kubectl port-forward service/mdb0-svc -n mongodb 27017:27017
```

![Setting up Port-forward](https://adamtheautomator.com/wp-content/uploads/2022/04/image-490.png)

Setting up Port-forward

3\. Finally, open your MongoDB Compass application on your local machine, add a new connection with the following format, and click **Connect** to connect to MongoDB.

Change the username and password field with your MongoDB user and password.

```powershell
# Connect as mongoadmin
mongodb://mongoadmin:secretpassword@localhost:27017/admin?ssl=false

# Connect as appuser
mongodb://appuser:myapppassword@localhost:27017/admin?ssl=false
```

![Connecting as mongoadmin user to MongoDB](https://adamtheautomator.com/wp-content/uploads/2022/04/image-491.png)

Connecting as mongoadmin user to MongoDB

![Connecting as appuser to MongoDB](https://adamtheautomator.com/wp-content/uploads/2022/04/image-492.png)

Connecting as appuser to MongoDB

When the connection is successful, you’ll get the following window that confirms the MongoDB deployment to Kubernetes is working correctly.

For the mongoadmin user, you’ll see default databases (local, config, and admin).

![Verifying Connection to the MongoDB mongoadmin ](https://adamtheautomator.com/wp-content/uploads/2022/04/image-493.png)

Verifying Connection to the MongoDB mongoadmin

For the appuser, you’ll only see one privileges database named appdb1.

![Verifying Connection to the MongoDB as appuser](https://adamtheautomator.com/wp-content/uploads/2022/04/image-494.png)

Verifying Connection to the MongoDB as appuser

## Conclusion

Throughout this tutorial, you’ve learned how to deploy the MongoDB to the Kubernetes cluster using the MongoDB Kubernetes Operator. You’ve also touched on the basic usage of the Kubernetes operator. And by now, you already have sound knowledge about automatically deploying and managing applications to Kubernetes.

Why not set up a [TLS connection](https://github.com/mongodb/mongodb-kubernetes-operator/blob/master/docs/secure.md) on your MongoDB deployment to build on this newfound knowledge? TLS connection secures connections between replica sets and secures client or app connections to MongoDB.

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fmongodb-kubernetes%2F&text=How%20To%20Perform%20a%20MongoDB%20Kubernetes%20Installation)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fmongodb-kubernetes%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fmongodb-kubernetes%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2026/07/featured_image-3.png)

### [Build Your First Internal Developer Platform](/build-first-internal-developer-platform/)

Build your first Internal Developer Platform with Backstage, a software catalog, software templates, CI/CD handoffs, and Kubernetes deployment manifests.

![](https://adamtheautomator.com/wp-content/uploads/2026/06/featured_image-9.png)

### [DevOps to Platform Engineer: 2026 Transition Roadmap](/devops-platform-engineer-2026-transition-roadmap/)

Learn how to move from DevOps to platform engineering in 2026 with a practical roadmap covering transferable skills, internal developer platforms, Backstage, Crossplane, and portfolio projects.

![](https://adamtheautomator.com/wp-content/uploads/2024/02/kubernetes-blue-green.jpg)

### [Learning the Kubernetes Blue Green Deployment Strategy](/kubernetes-blue-green/)

Dive into Kubernetes blue-green deployments for smooth updates. Enhance your release process with this smart Kubernetes strategy!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
