A simple Kubernetes Operator for managing single-tenant MongoDB replica sets.
It allows applications to consume MongoDB through a declarative Custom Resource rather than manually managing StatefulSets, Secrets, and replica set administration.
-
Install Helm chart (controller and CRD)
helm install mongodb-operator oci://ghcr.io/mrhachi/charts/mongodb-operator --namespace mongodb-system
-
Deploy user secrets
kubectl -n sample-app create secret generic singletenantmongodb-sample-admin-pass \ --from-literal=password=Ar34l5ecureP@ssw0rd? kubectl -n sample-app create secret generic singletenantmongodb-sample-app-user-pass \ --from-literal=password=An0th3r1. kubectl -n sample-app create secret generic singletenantmongodb-sample-operation-user-pass \ --from-literal=password=4N0THER0N3. -
Install MongoDB custom resource
kubectl -n sample-app apply -f singletenantmongodb.yml
singletenantmongodb.yml
apiVersion: db.mrhachi.dev/v1alphav1 kind: SingleTenantMongoDB metadata: labels: app.kubernetes.io/name: controller app.kubernetes.io/managed-by: kustomize name: singletenantmongodb-sample spec: databaseName: sample replicas: 2 admin: username: admin secretRef: name: singletenantmongodb-sample-admin-pass users: - username: app secretRef: name: singletenantmongodb-sample-app-user-pass roles: - role: readWrite database: sample - username: operation secretRef: name: singletenantmongodb-sample-operation-user-pass roles: - role: read database: sample storage: size: "2Gi" resources: requests: cpu: "500m" memory: "256Mi" limits: cpu: "1" memory: "512Mi"
Represents a single MongoDB replica set managed by the operator.
Creating a SingleTenantMongoDB causes the operator to create and reconcile the following:
- MongoDB StatefulSets
- MongoDB Services
- Connection information ConfigMap
- Replica set topology
- Application user roles and passwords
- MongoDB replica set keyfile secrets
The default MongoDB database used.
Number of MongoDB replica set members.
Defines the MongoDB administrator account.
MongoDB administrator username
Kubernetes Secret containing the administrator password
The referenced Secret must contain the secret under the password key.
Defines MongoDB application users managed by the operator.
MongoDB administrator username
Kubernetes Secret containing the administrator password
The referenced Secret must contain the secret under the password key.
[].role: MongoDB user role [].database: Database against which the role applies
Defines persistent storage allocated to each MongoDB replica.
Example:
storage:
size: "10Gi"The value is applied to the StatefulSet volume claims.
Defines Kubernetes resource requests and limits for MongoDB containers.
A SingleTenantMongoDB resource creates:
- StatefulSet
- Headless Service
- PersistentVolumeClaims
- Connection ConfigMap
- MongoDB authentication Secrets
Resources are owned by the Custom Resource and are garbage collected when the resource is deleted.