1. Answers
  2. Using Kubernetes Extensions with MongoDB Community

How do I use Kubernetes extensions with mongodbcommunity.mongodb.com?

In this guide, we will set up MongoDB Community on a Kubernetes cluster using Pulumi. We will create a Kubernetes namespace, deploy a MongoDB StatefulSet, and expose it with a Service. We will also use Kubernetes extensions to enhance our deployment.

Step-by-step Explanation:

  1. Create a Kubernetes Namespace: This will isolate our MongoDB resources.
  2. Deploy MongoDB StatefulSet: This ensures that MongoDB pods are managed and persistent.
  3. Expose MongoDB with a Service: This allows other applications to communicate with MongoDB.
  4. Use Kubernetes Extensions: We will use Pulumi’s Kubernetes provider to manage these resources.

Below is the Pulumi program in TypeScript:

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

// Create a namespace for MongoDB
const mongoNamespace = new k8s.core.v1.Namespace("mongo-namespace", {
    metadata: {
        name: "mongo-namespace",
    },
});

// Define the MongoDB StatefulSet
const mongoStatefulSet = new k8s.apps.v1.StatefulSet("mongo-statefulset", {
    metadata: {
        namespace: mongoNamespace.metadata.name,
        name: "mongo-statefulset",
    },
    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-persistent-storage",
                        mountPath: "/data/db",
                    }],
                }],
            },
        },
        volumeClaimTemplates: [{
            metadata: {
                name: "mongo-persistent-storage",
            },
            spec: {
                accessModes: ["ReadWriteOnce"],
                resources: {
                    requests: {
                        storage: "1Gi",
                    },
                },
            },
        }],
    },
});

// Expose MongoDB with a Service
const mongoService = new k8s.core.v1.Service("mongo-service", {
    metadata: {
        namespace: mongoNamespace.metadata.name,
        name: "mongo-service",
    },
    spec: {
        ports: [{
            port: 27017,
            targetPort: "mongo",
        }],
        selector: {
            app: "mongo",
        },
    },
});

// Export the MongoDB service URL
export const mongoServiceUrl = pulumi.interpolate`mongodb://${mongoService.metadata.name}.${mongoNamespace.metadata.name}.svc.cluster.local:27017`;

Key Points:

  • This program creates a dedicated namespace for MongoDB.
  • A StatefulSet is used to manage MongoDB pods, ensuring data persistence.
  • A Service is created to expose MongoDB, making it accessible within the Kubernetes cluster.
  • We use Pulumi’s Kubernetes provider to manage all these resources programmatically.

Summary:

We have successfully set up MongoDB Community on a Kubernetes cluster using Pulumi. We created a namespace, deployed a MongoDB StatefulSet, and exposed it with a Service. This setup ensures that MongoDB is managed and accessible within the cluster.

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