Deploying MongoDB and MongoDB Express on Kubernetes with Minikube

In this article, we'll walk through the process of deploying a MongoDB database and MongoDB Express (a web-based MongoDB admin interface) on a local Kubernetes cluster using Minikube. We'll also create Kubernetes Secrets, ConfigMaps, and Services to securely manage and access our deployments.

Prerequisites

Before we begin, ensure you have the following installed:

Step 1: Start Minikube

First, start your Minikube cluster:

minikube start --driver docker

Verify that your cluster is up and running:

kubectl get nodes

Step 2: Create MongoDB

We'll start by creating the deployment for MongoDB and the Service on the same file. Below is the mongodb.yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongodb
  labels:
    app: mongodb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      containers:
      - name: mongodb
        image: mongo
        ports:
        - containerPort: 27017
        env:
        - name: MONGO_INITDB_ROOT_USERNAME
          valueFrom:
            secretKeyRef:
              name: mongodb-secrets
              key: mongo-root-username
        - name: MONGO_INITDB_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mongodb-secrets
              key: mongo-root-password

---
apiVersion: v1
kind: Service
metadata:
  name: mongodb-service
spec:
  selector:
    app: mongodb
  ports:
  - protocol: TCP
    port: 27017
    targetPort: 27017

For the dayabase we need to have username and password , and to refer them on the mongodb deployment file. The environment variables can be found on DockerHub, on the official mondo image specifications.

Step 3: Create MongoDB Secrets

Because the mongodb file depends on the credetials, before we apply the deployment, we'll create a Kubernetes Secret to store the MongoDB username and password. Here's the mongodb-secrets.yaml file:

apiVersion: v1
kind: Secret
metadata:
  name: mongodb-secrets
type: Opaque
data:
  mongo-root-username: <base64-encoded-username>
  mongo-root-password: <base64-encoded-password>

Replace <base64-encoded-username> and <base64-encoded-password> with your actual base64-encoded credentials. You can encode them using:

echo -n 'your-username' | base64
echo -n 'your-password' | base64

Apply the secret:

kubectl apply -f mongodb-secrets.yaml

Now that we have created the credentials, we can deploy the mongodb pod.

kubectl apply -f mongodb.yaml

Step 4: Create MongoDB Express

Now, let's create MongoDB Express, a web-based MongoDB admin interface. Below is the mongodb-express.yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongodb-express
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongodb-express
  template:
    metadata:
      labels:
        app: mongodb-express
    spec:
      containers:
      - name: mongodb-express
        image: mongo-express
        ports:
        - containerPort: 8081
        env:
        - name: ME_CONFIG_MONGODB_ADMINUSERNAME
          valueFrom:
            secretKeyRef:
              name: mongodb-secrets
              key: mongo-root-username
        - name: ME_CONFIG_MONGODB_ADMINPASSWORD
          valueFrom:
            secretKeyRef:
              name: mongodb-secrets
              key: mongo-root-password
        - name: ME_CONFIG_MONGODB_SERVER
          valueFrom:
            configMapKeyRef:
              name: mongodb-configmap
              key: database_url
---
apiVersion: v1
kind: Service
metadata:
  name: mongodb-express-service
spec:
  selector:
    app: mongodb-express
  type: LoadBalancer
  ports:
  - protocol: TCP
    port: 8081
    targetPort: 8081
    nodePort: 30100

Step 5: Create ConfigMap for MongoDB URL

Finally, we'll create a ConfigMap to store the MongoDB connection URL. Here's the mongodb-configmap.yaml file:

apiVersion: v1
kind: ConfigMap
metadata:
  name: mongodb-configmap
data:
  database_url: mongodb-service

Because again we have dependecies, we need first to apply the ConfigMap:

kubectl apply -f mongodb-configmap.yaml

And now we can apply the deployment:

kubectl apply -f mongodb-express.yaml

Step 6: Access MongoDB Express

To access MongoDB Express, in minikube we write the following command:

minikube service mongodb-express-service

This will asign the external service an IP address and in my case, because I am using a minikube, it will create a Tunnel for the MongoDB Express Service.

It will open your browser and you will need to insert your credentials to access MongoDB Express.

Step 7: How to Delete Resources and Minikube Cluster

Before deleting Minikube, it's good practice to clean up any deployed resources:

kubectl delete all --all

Shut down Minikube before deletion:

minikube stop

Then delete the Minikube cluster:

minikube delete

Conclusion

In this article, we successfully deployed MongoDB and MongoDB Express on a local Kubernetes cluster using Minikube. We also created Kubernetes Secrets and ConfigMaps to manage sensitive information and configuration data. This setup is ideal for local development and testing.

Official Documentation

Mongo-Express - Docker Official Image

Mongo - Docker Official Image

Minikube Start Official Site

1
Subscribe to my newsletter

Read articles from Georgiana Gorgan directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Georgiana Gorgan
Georgiana Gorgan

Originally from Romania 🏑, I have decided to come to Germany in 2022. After a period of travelling, making friends and learning the language, I made the decision to stay here. Currently I am following my passion for IT and I am continuously specialising in Cloud Engineering and Artificial Intelligence.πŸ‘©πŸ»β€πŸ’» I completed a year-long training course here in Germany, where I learned about AWS, Doker, DevOps, Terraform, 🐧Linux, Python and other cloud services and tools. During this time, I also worked as a tutor for my colleagues, explaining cloud concepts and building cloud solutions together.