Playground biometrics demo BioID home page

Deployment of a MongoDB database in Kubernetes

If you do not have a MongoDB installation available in your environment, you can alternatively deploy MongoDB as a Pod/Deployment in your Kubernetes Cluster. Note that, if you are running in Azure, you can also use a Azure Cosmos DB for MongoDB account.

In this example we create a MongoDB database, make sure that the data is persisted on a volume and verify the connection to MongoDB using a temporary mongo-client Pod.

Step 1: Create MongoDB Kubernetes Deployment

In a first step, create an external volume to persist the data (we used a persistentVolumeClaim named mongodb-volume-claim in this example here), generate a Kubernetes Deployment, attach the external volume and create a Kubernetes Service.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongodb-deployment
  labels:
    app: mongodb
spec:
  replicas: 2
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      containers:
      - name: mongodb
        image: mongo:latest
        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
        volumeMounts:
          - mountPath: /data/db
            name: mongodb-volume
            readOnly: false
      volumes:
        - name: mongodb-volume
          persistentVolumeClaim:
            claimName: mongodb-volume-claim
---
apiVersion: v1
kind: Service
metadata:
  name: mongodb-service
spec:
  selector:
    app: mongodb
  ports:
    - protocol: TCP
      port: 27017
      targetPort: 27017

Note: The MongoDB connection credentials are stored in Kubernetes secrets (mongodb-secrets) here.

Step 2: Verify the connection to MongoDB

To verify the connection to mongodb we use a temporary mongo-client Pod and connect do MongoDB from the temporary Pod. (mongo-user and mongo-password here are the same credentials you stored in your Kubernetes secrets).

kubectl run mongo-client --image=mongo --rm -it -- bash
mongosh --host mongodb-service --port 27017 -u mongo-user -p mongo-password
Step 3: Create BWS 3 database

Once connected to MongoDB, create an empty BWS 3 database and assign it a user:

use bws

db.createUser({
  user: "bws-user",
  pwd: "some_password",
  roles: [{ role: "dbOwner", db: "bws" }]
}).

Note: The resulting connection string you can use with a BWS 3 on-premises installation would be:
mongodb://bws-user:some_passwpord@mongodb-service:27017/bws?ssl=false