How to Run Highly Available MongoDB on Kubernetes

7 Min Read

Welcome to this comprehensive guide on running high availability MongoDB on Kubernetes! In this article, we will explore the process of setting up and configuring a MongoDB cluster that can automatically recover from failures and scale according to your application’s requirements. We will provide detailed code examples and step-by-step instructions to help you get started. Let’s dive in!
Before we begin, it’s crucial to understand Kubernetes and MongoDB fundamentals. If you’re new to either technology, we recommend visiting these links to familiarize yourself with their core concepts and benefits.

1. Installing and Configuring MongoDB on Kubernetes

To start, we need to deploy a MongoDB cluster in our Kubernetes environment. We’ll utilize StatefulSets for this purpose, as they ensure stable network identities and persistent storage for each MongoDB instance.
Create a YAML file named mongodb-statefulset.yaml with the following content:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongodb
spec:
  serviceName: "mongodb"
  replicas: 3
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      terminationGracePeriod:
          PeriodSeconds: 10
containers:
- name: mongodb
image: mongo:4.2
command:
- mongod
- "--replSet"
- rs0
- "--bind_ip_all"
ports:
- containerPort: 27017
volumeMounts:
- name: mongodb-data
mountPath: /data/db
volumes:
- name: mongodb-data
persistentVolumeClaim:
claimName: mongodb-data
volumeClaimTemplates:

metadata:
name: mongodb-data
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi

Next, apply the YAML file using the following command:

kubectl apply -f mongodb-statefulset.yaml

2. Initializing the MongoDB Replica Set

Once the MongoDB StatefulSet is deployed, we need to initialize the replica set. A replica set is a group of MongoDB instances that maintain the same data set, providing redundancy and high availability. To initialize the replica set, first, determine the name of the MongoDB pod:
kubectl get pods -l app=mongodb

Then, execute the following command to initiate a MongoDB shell:

kubectl exec -it mongodb-0 -- mongo

Inside the MongoDB shell, run these commands to initialize the replica set:

rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "mongodb-0.mongodb:27017" },
{ _id: 1, host: "mongodb-1.mongodb:27017" },
{ _id: 2, host: "mongodb-2.mongodb:27017" }
]
})

After the replica set is initialized, you should see the following output:

{
"ok" : 1,
"operationTime" : Timestamp(1234567890, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1234567890, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

Your MongoDB replica set is now up and running on Kubernetes!

3. Exposing MongoDB to Your Application

With the MongoDB replica set initialized, we need to expose it to our application. We’ll create a Kubernetes Service to achieve this. The service will load balance connections between the MongoDB instances and provide a stable network identity for external access.
Create a YAML file named mongodb-service.yaml with the following content:

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

Apply the YAML file using the command:

kubectl apply -f mongodb-service.yaml

Your application can now connect to MongoDB using the mongodb://mongodb:27017 URI.

4. Monitoring and Scaling Your MongoDB Cluster

Monitoring your MongoDB cluster is essential for maintaining high availability and performance. Tools like MongoDB Compass and Datadog can help you analyze and optimize your MongoDB deployment. Visit this tutorial to learn more about monitoring MongoDB on Kubernetes.
Scaling your MongoDB cluster is as simple as updating the replicas field in the mongodb-statefulset.yaml file and applying the changes with kubectl apply. Kubernetes will automatically create new MongoDB instances and join them to the replica set. However, ensure to adjust the replica set configuration accordingly. Learn more about scaling MongoDB on Kubernetes in this article.

5. Automatically Deploying New MongoDB Instances in the Cloud or On-Prem Data Center

To automatically deploy a new MongoDB instance in the cloud or on-prem data center, consider using Kubernetes Operators or Infrastructure as Code (IaC) tools like Terraform. These tools enable the creation and management of infrastructure resources, including MongoDB instances, through code templates. This approach simplifies the deployment process, increases consistency across environments, and enables easy updates and maintenance.

6. Failing Over a MongoDB Pod to Another Availability Zone or Rack if the Instance Goes Down

In case a MongoDB instance goes down, Kubernetes automatically reschedules the affected pod to another available node within the same availability zone or rack. To ensure failover to another zone or rack, configure your Kubernetes cluster with multiple availability zones or racks and use appropriate labels and selectors in your MongoDB deployment.

7. Resizing the MongoDB Volume When Running Out of Space

To resize your MongoDB volume, update the storage request value in the mongodb-statefulset.yaml file, and then apply the changes using kubectl apply. Note that resizing might require your storage class to support dynamic volume expansion.

8. Snapshotting and Backing Up MongoDB for Disaster Recovery

Implement regular snapshots and backups of your MongoDB data for disaster recovery purposes. Tools like Velero or custom scripts can help automate the process of taking snapshots, backing up data, and restoring it when needed.

9. Testing Upgrades

Before upgrading your MongoDB deployment, test the upgrade in a staging environment that mirrors your production setup. This will allow you to identify and fix any issues or compatibility problems before applying the upgrade to the production environment. Make sure to create a backup before performing the upgrade and have a rollback plan in place.

10. Running MongoDB Deployment in Any Environment

Yes, you can take your MongoDB deployment and run it in any environment if needed, including AWS, GCE, Azure, VMWare, OpenStack, or bare metal. Kubernetes provides a consistent platform across various environments, making it easier to migrate your MongoDB deployment to different infrastructure without significant changes.
That’s it! You’ve successfully set up a highly available MongoDB cluster on Kubernetes. Follow the tips and best practices shared in this post to ensure your MongoDB deployment remains scalable, resilient, and performant. Happy coding!

Share this Article
Leave a comment