Logo

dev-resources.site

for different kinds of informations.

Deploy MongoDB Replica Set on K8s

Published at
6/3/2024
Categories
kubernetes
mongodb
replicaset
deploy
Author
almatins
Author
8 person written this
almatins
open
Deploy MongoDB Replica Set on K8s

So, I set a new k8s cluster on DigitalOcean recently, and I want to share the way I deploy my MongoDB Replica Set on the k8s cluster. Let’s start it.

Storage Class

I use Longhorn instead of the default do-block-storage provided by DigitalOcean as the default. I set up the longhorn using the helm chart. Since I use Rancher to manage my cluster, I install Longhorn from the App menu and choose Longhorn with the default value. super simple!

Cluster IP Service

To create a statefulset, we will need to provide the serviceName so, let’s create a ClusterIP service for our MongoDB. The mongodb-clusterip.yaml file looks like this.

apiVersion: v1
kind: Service
metadata:
  annotations:
    field.cattle.io/description: MongoDB Cluster IP
  name: mongodb-clusterip
  namespace: default
spec:
  ports:
    - name: mongo
      port: 27017
      protocol: TCP
      targetPort: 27017
  selector:
    app: mongodb
  type: ClusterIP
Enter fullscreen mode Exit fullscreen mode

Then execute the kubectl apply command
kubectl apply -f mongodb-clusterip.yaml

Secret

We will save the mongodb root user and mongodb root password in the Secret, so we need to create a new Secret for our MongoDB.

The mongodb-secret.yaml file looks like this

apiVersion: v1
data:
  password: base6StringVersionOfpassword
  user: base6StringVersionOfuser
kind: Secret
metadata:
  name: mongodb-secret
  namespace: default
Enter fullscreen mode Exit fullscreen mode

Then execute the kubectl apply command
kubectl apply -f mongodb-secret.yaml

StatefulSet

Now the last thing that we need to execute is the statefulset for our MongoDB. The mongodb-statefulset.yaml file looks like this

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongodb
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: mongodb
  serviceName: mongodb-clusterip
  template:
    metadata:
      namespace: default
      labels:
        app: mongodb
    spec:
      containers:
        - args:
            - '--dbpath'
            - /data/db
          command:
            - mongod
            - '--bind_ip_all'
            - '--replSet'
            - rs0
          env:
            - name: MONGO_INITDB_ROOT_USERNAME
              valueFrom:
                secretKeyRef:
                  key: user
                  name: mongodb-secret
            - name: MONGO_INITDB_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  key: password
                  name: mongodb-secret
          image: mongo:6.0.11
          imagePullPolicy: IfNotPresent
          name: mongodb-c
          volumeMounts:
            - mountPath: /data/db
              name: mongodb-pvc
  volumeClaimTemplates:
    - apiVersion: v1
      kind: PersistentVolumeClaim
      metadata:
        name: mongodb-pvc
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 8Gi
        storageClassName: longhorn
Enter fullscreen mode Exit fullscreen mode

Then execute the kubectl apply command
kubectl apply -f mongodb-statefulset.yaml

you can change the replica number with your own. I use 2 here.

After a few minutes, you should see the statefulset created. You can see it in your Rancher or using the kubectl get command.

Let’s make sure that the Mongodb is running using kubectl exec command below.

kubectl --namespace=default exec -ti mongodb-0 -- mongosh
Then you should get the mongosh prompt opened. For the second replica, you can do the same by changing the mongodb-0 with mongodb-1 instead.

Configure Replica Set

To configure MongoDB replica set. First we need to open the mongodb-0 (first pod of the MongoDb) using above command.

Then we use the rs.initiate() command as below

rs.initiate(
 {
  _id: "rs0",
  members: [
   { _id: 0, host: "mongodb-0.mongodb-clusterip.default.svc.cluster.local:27017" },
   { _id: 1, host: "mongodb-1.mongodb-clusterip.default.svc.cluster.local:27017" }
  ]
 }
)
Enter fullscreen mode Exit fullscreen mode

if you notice that the pattern is <pod>.<service>.<namespace>.svc.cluster.local:port

Then try to add some data to the test db using the query
db.test.insertOne({ testFromMaster: "true" })

Now, exit from the master replica and open the mongosh in the secondary replica using the same command

kubectl --namespace=default exec -ti mongodb-1 -- mongosh
You will notice that the prompt is displaying the secondary. To start reading the data changes from the primary replica, we need to run the command

db.getMongo().setReadPref("primaryPreferred")

Then we can test to query the test data that we create before using the query

db.test.find({})

Now, you should see the secondary also has the test data.

That’s it. hopefully, you found this useful. Cheers.

deploy Article's
30 articles in total
Favicon
How to Deploy a Static Website to AWS S3 with Razorops CI/CD
Favicon
Pipeline CD en Jenkins para terraform AWS EKS segunda parte (plugin AWS Credentials)
Favicon
Kamal 2 Quick Start - the missing tutorial
Favicon
Evento de Mobile, Frontend, Backend, Banco de Dados e Deploy Gratuito
Favicon
Deploy a Static Astro Site on Railway
Favicon
Deploy a PHP site to Railway
Favicon
Added advanced debugging features to my machine learning library like pytorch.
Favicon
When Companies Adopt Feature Flags
Favicon
Why should you have a Staging environment?
Favicon
How to Deploy Flutter Apps to the Google Play Store and App Store
Favicon
Deploy MongoDB Replica Set on K8s
Favicon
Deploy Go Application using Docker Compose Replicas and Nginx
Favicon
Despliegue de aplicación de Django con Github Actions para un servidor propio
Favicon
Common and Useful Deployment Patterns
Favicon
From Frustration to Fix: Conquering Vercel Errors Like a Pro
Favicon
Efficient Data Management with Prisma, Fly.io, and LiteFS Configuration
Favicon
Deploying Forem on Render.com PromptZone.com
Favicon
My Docker stack to deploy a Django + Celery web app
Favicon
Firebase Hosting Setup Complete Issue
Favicon
Deploy an Azure Functions app from a monorepo with a GitHub Action for Node.js
Favicon
DevOps, como começar? ...e por que?
Favicon
Kotlin and Azure Functions - Automating the deployment
Favicon
Private Deployment Gantt chart Project management tools
Favicon
Laravel Deployer Free package for laravel and nodejs apps Deployment
Favicon
Don't Couple Your Deployments
Favicon
Six niche tips for shipping Flutter MacOS builds
Favicon
Deploying a Static Site (feat.Vite, gh-pages)
Favicon
Deploy Express App to Render with MySQL
Favicon
Deploying on Netlify via GitHub Actions: A Seamless Guide
Favicon
AWS CodeDeploy Best Practices for Reliable and Efficient Deployments

Featured ones: