1. Answers
  2. Deploying MongoDB Clusters with StatefulSets

How do I deploy MongoDB clusters with StatefulSets?

In this guide, we will deploy a MongoDB cluster on Kubernetes using StatefulSets. StatefulSets are designed to manage stateful applications that require persistent storage and stable network identities. MongoDB, being a distributed database, benefits from these features. We will create a StatefulSet that deploys a MongoDB replica set with persistent storage.

Below is the Pulumi program in TypeScript to achieve this:

import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";

// Define the namespace
const ns = new k8s.core.v1.Namespace("mongo-ns", {
    metadata: { name: "mongo-ns" },
});

// Define the persistent volume claim template
const pvc = {
    metadata: { name: "mongo-pvc" },
    spec: {
        accessModes: ["ReadWriteOnce"],
        resources: {
            requests: {
                storage: "1Gi",
            },
        },
    },
};

// Define the StatefulSet for MongoDB
const mongoStatefulSet = new k8s.apps.v1.StatefulSet("mongo", {
    metadata: {
        namespace: ns.metadata.name,
        name: "mongo",
    },
    spec: {
        serviceName: "mongo",
        replicas: 3,
        selector: {
            matchLabels: {
                app: "mongo",
            },
        },
        template: {
            metadata: {
                labels: {
                    app: "mongo",
                },
            },
            spec: {
                containers: [
                    {
                        name: "mongo",
                        image: "mongo:4.4.6",
                        ports: [
                            {
                                containerPort: 27017,
                                name: "mongo",
                            },
                        ],
                        volumeMounts: [
                            {
                                name: "mongo-pvc",
                                mountPath: "/data/db",
                            },
                        ],
                    },
                ],
            },
        },
        volumeClaimTemplates: [pvc],
    },
});

// Define a headless service for MongoDB
const mongoService = new k8s.core.v1.Service("mongo", {
    metadata: {
        namespace: ns.metadata.name,
        name: "mongo",
    },
    spec: {
        clusterIP: "None",
        selector: {
            app: "mongo",
        },
        ports: [
            {
                port: 27017,
                targetPort: "mongo",
            },
        ],
    },
});

// Export the namespace and StatefulSet name
export const namespaceName = ns.metadata.name;
export const statefulSetName = mongoStatefulSet.metadata.name;

Key Points

  • Namespace: We create a separate namespace for MongoDB to keep resources organized.
  • Persistent Volume Claim (PVC): The PVC ensures that each MongoDB pod gets its own persistent storage.
  • StatefulSet: The StatefulSet manages the deployment and scaling of MongoDB pods, ensuring each pod has a unique identity and stable storage.
  • Headless Service: The headless service allows MongoDB pods to communicate with each other directly.

Summary

In this guide, we deployed a MongoDB cluster on Kubernetes using StatefulSets. The StatefulSet ensures that each MongoDB pod has a stable network identity and persistent storage, which is crucial for running a reliable MongoDB replica set. We also created a headless service to facilitate communication between MongoDB pods.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up