1. Using kubernetes operators.coreos.com with mongodbcommunity.mongodb.com

    TypeScript

    To deploy a MongoDB Community Operator on a Kubernetes cluster using Pulumi, you will need to perform a series of steps:

    1. Set up a Kubernetes cluster where you can deploy the operator.
    2. Install the Custom Resource Definitions (CRDs) provided by the MongoDB Community Operator.
    3. Deploy the MongoDB Community Operator itself.
    4. Create a MongoDB Custom Resource (CR), which the operator will use to provision and manage MongoDB instances.

    Let's start by installing the MongoDB Community Operator.

    Firstly, you will need to make sure that you have Pulumi installed and configured for use with your Kubernetes cluster. If you are using a managed Kubernetes service like Amazon EKS, Microsoft AKS, or Google GKE, Pulumi can work with your cloud provider's configuration.

    Now, let's begin with the code required to deploy the MongoDB Community Operator. This involves creating the necessary Kubernetes resources such as the CRDs, the operator deployment, roles and role bindings, service accounts, and finally creating an instance of a MongoDB database using a Custom Resource.

    import * as k8s from "@pulumi/kubernetes"; // Define the necessary resources for deploying the MongoDB Community Operator // Namespace where the operator will run const namespace = new k8s.core.v1.Namespace("mongodb-operator-ns", { metadata: { name: "mongodb-operator" } }); // Apply the MongoDB Community Operator CRDs const mongodbCrdUrl = "https://raw.githubusercontent.com/mongodb/mongodb-kubernetes-operator/master/config/crd/bases/mongodbcommunity.mongodb.com_mongodbcommunity.yaml"; const mongodbCrd = new k8s.yaml.ConfigFile("mongodb-crd", { file: mongodbCrdUrl, }, { dependsOn: namespace }); // Deploy the MongoDB Community Operator const operatorUrl = "https://raw.githubusercontent.com/mongodb/mongodb-kubernetes-operator/master/config/samples/mongodb.com_v1_mongodbcommunity_cr.yaml"; const mongodbOperator = new k8s.yaml.ConfigFile("mongodb-operator", { file: operatorUrl, transformations: [(resource: any) => { // Adjust resources under the "mongodb-operator" namespace if (resource.metadata.namespace !== undefined) { resource.metadata.namespace = namespace.metadata.name; } }], }, { dependsOn: [namespace, mongodbCrd] }); // Create a MongoDB Custom Resource to deploy a MongoDB instance const mongodbInstance = new k8s.apiextensions.CustomResource("mongodb-instance", { apiVersion: "mongodbcommunity.mongodb.com/v1", kind: "MongoDBCommunity", metadata: { namespace: namespace.metadata.name, name: "my-mongodb", }, spec: { members: 3, type: "ReplicaSet", version: "4.2.6", security: { authentication: { modes: ["SCRAM"], }, }, users: [{ name: "my-user", db: "admin", passwordSecretRef: { name: "my-user-password", }, roles: [{ name: "readWrite", db: "my-database", }], scramCredentialsSecretName: "my-scram", }], // Define storage, stateful set settings, etc. } }, { dependsOn: [mongodbOperator] }); // Export the MongoDB instance address when it is ready export const mongodbAddress = mongodbInstance.status.host;

    In this program, we do the following:

    • Import the Kubernetes package from Pulumi.
    • Create a Kubernetes Namespace resource where our MongoDB operator will be deployed.
    • Load the CRDs required for the MongoDB Community operator from the provided URL.
    • Load the MongoDB operator's configuration from another URL.
    • Apply a transformation to all loaded Kubernetes resources to ensure they are created within our namespace.
    • Deploy a MongoDB instance using the MongoDBCommunity custom resource, which is defined by the CRDs we installed.

    Make sure you replace the URLs with the latest ones from the MongoDB Community GitHub repository, and adjust the MongoDB CR specifications as per your requirements.

    After deploying this Pulumi program with pulumi up, the resources get created on your Kubernetes cluster. The MongoDB Community Operator will then create a MongoDB ReplicaSet as specified in the mongodb-instance CustomResource.

    Please note that the passwords and secrets must be managed securely, which is not covered in this example for brevity. Also, remember that the URLs to the raw YAML files are subject to change; you should reference the official MongoDB Kubernetes Operator repository for the latest configurations.

    Once your MongoDB cluster is up and running, you can connect to it using the credentials and connection information that you have specified in your Custom Resource and by accessing the mongodbAddress output.