OpenMCF logoOpenMCF

Loading...

Kubernetes MongoDB

Deploys a MongoDB instance on Kubernetes using the Percona Server for MongoDB operator, with automatic password generation, configurable replica sets, optional data persistence via PersistentVolumeClaims, and optional external access through a LoadBalancer Service with external-dns integration.

What Gets Created

When you deploy a KubernetesMongodb resource, OpenMCF provisions:

  • Namespace — created only when createNamespace is true
  • Random Password — a 12-character password with mixed case, numbers, and special characters, generated automatically
  • Password Secret — a Kubernetes Secret storing the generated password for MongoDB authentication
  • PerconaServerMongoDB CRD — deploys MongoDB via the Percona Server for MongoDB operator with a configurable replica set, resource limits, and optional persistent storage
  • LoadBalancer Service — created only when ingress is enabled, exposes MongoDB on port 27017 with an external-dns.alpha.kubernetes.io/hostname annotation for automatic DNS record creation

Prerequisites

  • Kubernetes credentials configured via environment variables or OpenMCF provider config
  • A Kubernetes namespace that already exists, or set createNamespace to true
  • Percona Server for MongoDB operator installed in the cluster (the operator manages the PerconaServerMongoDB custom resource)
  • A StorageClass available in the cluster if enabling persistence (most managed Kubernetes clusters provide a default)
  • external-dns running in the cluster if enabling ingress with a hostname

Quick Start

Create a file mongodb.yaml:

apiVersion: kubernetes.openmcf.org/v1
kind: KubernetesMongodb
metadata:
  name: my-mongodb
  labels:
    openmcf.org/provisioner: pulumi
    pulumi.openmcf.org/organization: my-org
    pulumi.openmcf.org/project: my-project
    pulumi.openmcf.org/stack.name: dev.KubernetesMongodb.my-mongodb
spec:
  namespace: database
  createNamespace: true

Deploy:

openmcf apply -f mongodb.yaml

This creates a single-replica MongoDB instance with persistence enabled, a 1Gi PersistentVolumeClaim, default resource limits (1000m CPU, 1Gi memory), and a randomly generated password stored in a Kubernetes Secret.

Configuration Reference

Required Fields

FieldTypeDescriptionValidation
namespacestringKubernetes namespace for the MongoDB deployment. Can reference a KubernetesNamespace resource via valueFrom.Required

Optional Fields

FieldTypeDefaultDescription
targetCluster.clusterKindenum—Kubernetes cluster kind. Valid values: AwsEksCluster, GcpGkeCluster, AzureAksCluster, DigitalOceanKubernetesCluster, CivoKubernetesCluster.
targetCluster.clusterNamestring—Name of the target Kubernetes cluster in the same environment.
createNamespaceboolfalseWhen true, creates the namespace before deploying resources.
container.replicasint321Number of MongoDB replica set members.
container.resources.limits.cpustring1000mMaximum CPU allocation for each MongoDB pod.
container.resources.limits.memorystring1GiMaximum memory allocation for each MongoDB pod.
container.resources.requests.cpustring50mMinimum guaranteed CPU for each MongoDB pod.
container.resources.requests.memorystring100MiMinimum guaranteed memory for each MongoDB pod.
container.persistenceEnabledbooltrueEnables persistent storage for MongoDB data. When enabled, data is persisted to a PersistentVolumeClaim and survives pod restarts.
container.diskSizestring1GiSize of the PersistentVolumeClaim attached to each MongoDB pod. Required when persistenceEnabled is true. Must be a valid Kubernetes quantity (e.g., 1Gi, 10Gi). Cannot be modified after creation.
ingress.enabledboolfalseCreates a LoadBalancer Service with external-dns annotations exposing MongoDB on port 27017.
ingress.hostnamestring—Hostname for external access (e.g., mongodb.example.com). Configured automatically via external-dns. Required when ingress.enabled is true.
helmValuesmap<string, string>—Additional key-value pairs passed to the Helm chart for further customization. See the Bitnami MongoDB chart documentation for available options.

Examples

Development MongoDB without Persistence

A lightweight MongoDB instance for development with persistence disabled and reduced resources:

apiVersion: kubernetes.openmcf.org/v1
kind: KubernetesMongodb
metadata:
  name: dev-mongodb
  labels:
    openmcf.org/provisioner: pulumi
    pulumi.openmcf.org/organization: my-org
    pulumi.openmcf.org/project: my-project
    pulumi.openmcf.org/stack.name: dev.KubernetesMongodb.dev-mongodb
spec:
  namespace: dev
  createNamespace: true
  container:
    persistenceEnabled: false
    resources:
      limits:
        cpu: "500m"
        memory: "512Mi"
      requests:
        cpu: "100m"
        memory: "128Mi"

Production MongoDB with Increased Storage

A production MongoDB instance with a larger replica set, more disk, and higher resource limits:

apiVersion: kubernetes.openmcf.org/v1
kind: KubernetesMongodb
metadata:
  name: prod-mongodb
  labels:
    openmcf.org/provisioner: pulumi
    pulumi.openmcf.org/organization: my-org
    pulumi.openmcf.org/project: my-project
    pulumi.openmcf.org/stack.name: prod.KubernetesMongodb.prod-mongodb
spec:
  namespace: production
  container:
    replicas: 3
    resources:
      limits:
        cpu: "2000m"
        memory: "4Gi"
      requests:
        cpu: "500m"
        memory: "1Gi"
    persistenceEnabled: true
    diskSize: "50Gi"

MongoDB with External Access

MongoDB exposed outside the cluster via a LoadBalancer with automatic DNS management:

apiVersion: kubernetes.openmcf.org/v1
kind: KubernetesMongodb
metadata:
  name: shared-mongodb
  labels:
    openmcf.org/provisioner: pulumi
    pulumi.openmcf.org/organization: my-org
    pulumi.openmcf.org/project: my-project
    pulumi.openmcf.org/stack.name: prod.KubernetesMongodb.shared-mongodb
spec:
  namespace: shared-services
  container:
    replicas: 3
    resources:
      limits:
        cpu: "2000m"
        memory: "4Gi"
      requests:
        cpu: "500m"
        memory: "1Gi"
    persistenceEnabled: true
    diskSize: "100Gi"
  ingress:
    enabled: true
    hostname: mongodb.example.com

Using Foreign Key References

Reference an OpenMCF-managed namespace instead of hardcoding the name:

apiVersion: kubernetes.openmcf.org/v1
kind: KubernetesMongodb
metadata:
  name: app-mongodb
  labels:
    openmcf.org/provisioner: pulumi
    pulumi.openmcf.org/organization: my-org
    pulumi.openmcf.org/project: my-project
    pulumi.openmcf.org/stack.name: prod.KubernetesMongodb.app-mongodb
spec:
  namespace:
    valueFrom:
      kind: KubernetesNamespace
      name: app-namespace
      field: spec.name
  container:
    persistenceEnabled: true
    diskSize: "20Gi"

Stack Outputs

After deployment, the following outputs are available in status.outputs:

OutputTypeDescription
namespacestringKubernetes namespace where MongoDB is deployed
servicestringKubernetes Service name for the MongoDB instance (format: {name})
port_forward_commandstringkubectl port-forward command for local access
kube_endpointstringCluster-internal FQDN (e.g., my-mongodb.database.svc.cluster.local)
external_hostnamestringPublic hostname for external access, only set when ingress is enabled
internal_hostnamestringInternal hostname for cluster-internal access
usernamestringMongoDB admin username (always databaseAdmin)
password_secret.namestringName of the Kubernetes Secret containing the MongoDB password (format: {name}-password)
password_secret.keystringKey within the password Secret (always MONGODB_DATABASE_ADMIN_PASSWORD)

Related Components

  • KubernetesNamespace — provides the target namespace via valueFrom reference
  • KubernetesDeployment — application deployments that connect to MongoDB as a data store
  • KubernetesExternalDns — manages DNS records for the LoadBalancer ingress hostname

Next article

Kubernetes Namespace

Kubernetes Namespace Deploys a Kubernetes namespace with optional resource quotas, LimitRanges, network policies, service mesh sidecar injection, and Pod Security Standards enforcement. Configuration is abstracted into T-shirt-sized resource profiles and declarative network isolation rules, so each namespace is created with resource limits and network isolation configured from the start. What Gets Created When you deploy a KubernetesNamespace resource, OpenMCF provisions: Namespace — a...
Read next article
Presets
2 ready-to-deploy configurationsView presets →