mongodb
by groundhog2k
A Helm chart to deploy MongoDB
About this chart
MongoDB
Changelog
see RELEASENOTES.md
A Helm chart for MongoDB on Kubernetes
TL;DR
helm repo add groundhog2k https://groundhog2k.github.io/helm-charts/
helm install my-release groundhog2k/mongodb
Introduction
This chart uses the original MongoDB image from Docker Hub to deploy a stateful MongoDB instance in a Kubernetes cluster.
It fully supports deployment of the multi-architecture docker image.
Prerequisites
- Kubernetes 1.12+
- Helm 3.x
- PV provisioner support in the underlying infrastructure
Installing the Chart
To install the chart with the release name my-release:
helm install my-release groundhog2k/mongodb
Uninstalling the Chart
To uninstall/delete the my-release deployment:
helm uninstall my-release
Common parameters
| Key | Type | Default | Description |
|---|---|---|---|
| fullnameOverride | string | "" | Fully override the deployment name |
| nameOverride | string | "" | Partially override the deployment name |
Deployment parameters
| Key | Type | Default | Description |
|---|---|---|---|
| image.pullPolicy | string | "IfNotPresent" | Image pull policy |
| image.registry | string | "docker.io" | Image registry |
| image.repository | string | "mongo" | Image name |
| image.tag | string | "" | Image tag |
| imagePullSecrets | list | [] | Image pull secrets |
| extraInitContainers | list | [] | Extra init containers |
| extaContainers | list | [] | Extra containers for usage as sidecars |
| startupProbe | object | see values.yaml | Startup probe configuration |
| livenessProbe | object | see values.yaml | Liveness probe configuration |
| readinessProbe | object | see values.yaml | Readiness probe configuration |
| customStartupProbe | object | {} | Custom startup probe (overwrites default startup probe configuration) |
| customLivenessProbe | object | {} | Custom liveness probe (overwrites default liveness probe configuration) |
| customReadinessProbe | object | {} | Custom readiness probe (overwrites default readiness probe configuration) |
| initResources | object | {} | Resource limits and requests for the default init container |
| resources | object | {} | Resource limits and requests |
| priorityClassName | string | "" | Deployment priority class name |
| nodeSelector | object | {} | Deployment node selector |
| customLabels | object | {} | Additional labels for Deployment or StatefulSet |
| customAnnotations | object | {} | Additional annotations for Deployment or StatefulSet |
| podAnnotations | object | {} | Additional pod annotations |
| podLabels | object | {} | Additional pod labels |
| podSecurityContext | object | see values.yaml | Pod security context |
| securityContext | object | see values.yaml | Container security context |
| env | list | [] | Additional container environmment variables |
| args | list | [] | Additional container command arguments |
| serviceAccount.annotations | object | {} | Additional service account annotations |
| serviceAccount.create | bool | false | Enable service account creation |
| serviceAccount.name | string | "" | Name of the service account |
| affinity | object | {} | Pod affinity |
| tolerations | list | [] | Pod tolerations |
| topologySpreadConstraints | object | {} | Topology spread constraints for pods |
| podManagementPolicy | string | OrderedReady | Pod management policy |
| updateStrategyType | string | RollingUpdate | Update strategy |
| revisionHistoryLimit | int | nil | Maximum number of revisions maintained in revision history |
Service paramters
| Key | Type | Default | Description |
|---|---|---|---|
| service.type | string | "ClusterIP" | Service type |
| service.port | int | 27017 | MongoDB service port |
| service.nodePort | int | nil | The node port (only relevant for type LoadBalancer or NodePort) |
| service.clusterIP | string | nil | The cluster ip address (only relevant for type LoadBalancer or NodePort) |
| service.loadBalancerIP | string | nil | The load balancer ip address (only relevant for type LoadBalancer) |
| service.loadBalancerSourceRanges | list | [] | The list of IP CIDR ranges that are allowed to access the load balancer (only relevent for type LoadBalancer) |
| service.annotations | object | {} | Additional service annotations |
| service.labels | object | {} | Additional service labels |
Metrics
Deploys a mongodb_exporter sidecar that exposes Prometheus metrics, together with an optional metrics Service and a Prometheus Operator ServiceMonitor. When metrics.exporter.uri is empty the connection string is built automatically from the chart's root credentials (or without authentication when none are set).
The exporter runs on every data-bearing member: the primary/secondary StatefulSet and, when replicaSet.hiddenSecondaries.instances > 0, the hidden-secondary StatefulSet as well. Each set gets its own metrics Service (<release>-mongodb-metrics and <release>-mongodb-metrics-hidden) and ServiceMonitor, so per-member state (member role, oplog window, replication lag) is scraped from the node it actually runs on. Arbiters are intentionally not scraped — they hold no data, and their member state is already reported by the primary's replSetGetStatus. The auto-built URI pins directConnection=true so each sidecar reports its own node's metrics instead of being routed to the primary by replica-set discovery; set metrics.exporter.uri to override (for example, to monitor only the primary or to point at an external MongoDB). The hidden metrics Service inherits metrics.service.type but omits any fixed nodePort / loadBalancerIP, which can belong to only one Service.
Every --collector.* flag of mongodb_exporter is opt-in — started with no arguments the exporter exposes only mongodb_up, which makes dashboards and alerts report missing metrics even though the scrape target is healthy. metrics.exporter.args therefore defaults to --collector.diagnosticdata (serverStatus, i.e. connections, opcounters, memory, WiredTiger cache) and --collector.replicasetstatus (replSetGetStatus, i.e. member state and replication lag) — the set standard MongoDB dashboards consume. --collect-all is deliberately not the default: it additionally enables $collStats and $indexStats, whose series count grows with the number of user collections. Add --collector.dbstats, --collector.collstats or --collector.indexstats (or switch to --collect-all) when that detail is worth the extra cardinality.
| Key | Type | Default | Description |
|---|---|---|---|
| metrics.enabled | bool | false | Enable metrics export via a mongodb_exporter sidecar |
| metrics.exporter.image.registry | string | "docker.io" | Exporter image registry |
| metrics.exporter.image.repository | string | "percona/mongodb_exporter" | Exporter image repository |
| metrics.exporter.image.pullPolicy | string | "IfNotPresent" | Exporter image pull policy |
| metrics.exporter.image.tag | string | "0.51.0" | Exporter image tag |
| metrics.exporter.uri | string | "" | MongoDB connection URI for the exporter (auto-built from root credentials when empty) |
| metrics.exporter.securityContext | object | (non-root) | Security context for the exporter container |
| metrics.exporter.resources | object | {} | Resource limits and requests for the exporter container |
| metrics.exporter.customStartupProbe | object | {} | Custom startup probe (overwrites the default startup probe) |
| metrics.exporter.startupProbe | object | (enabled) | Default startup probe |
| metrics.exporter.customLivenessProbe | object | {} | Custom liveness probe (overwrites the default liveness probe) |
| metrics.exporter.livenessProbe | object | (enabled) | Default liveness probe |
| metrics.exporter.customReadinessProbe | object | {} | Custom readiness probe (overwrites the default readiness probe) |
| metrics.exporter.readinessProbe | object | (enabled) | Default readiness probe |
| metrics.exporter.args | list | ["--collector.diagnosticdata","--collector.replicasetstatus"] | Arguments for the exporter entrypoint process |
| metrics.exporter.env | list | [] | Additional environment variables (exporter only) |
| metrics.exporter.extraExporterEnvSecrets | list | [] | Existing secrets mounted into the exporter as environment variables |
| metrics.service.enabled | bool | true | Enable the metrics service |
| metrics.service.type | string | "ClusterIP" | Metrics service type |
| metrics.service.servicePort | int | 9216 | Metrics service port |
| metrics.service.containerPort | int | 9216 | Exporter container port |
| metrics.service.nodePort | int | nil | The node port (only relevant for type LoadBalancer or NodePort) |
| metrics.service.clusterIP | string | nil | The cluster ip address (only relevant for type LoadBalancer or NodePort) |
| metrics.service.loadBalancerIP | string | nil | The load balancer ip address (only relevant for type LoadBalancer) |
| metrics.service.loadBalancerSourceRanges | list | [] | The list of IP CIDR ranges that are allowed to access the load balancer (only relevant for type LoadBalancer) |
| metrics.service.annotations | object | {} | Additional metrics service annotations |
| metrics.service.labels | object | {} | Additional metrics service labels |
| metrics.serviceMonitor.enabled | bool | true | Enable the Prometheus Operator ServiceMonitor |
| metrics.serviceMonitor.additionalLabels | object | {} | Additional labels for the ServiceMonitor object |
| metrics.serviceMonitor.annotations | object | {} | Annotations for the ServiceMonitor object |
| metrics.serviceMonitor.interval | string | nil | The scrape interval for prometheus |
| metrics.serviceMonitor.scrapeTimeout | string | nil | The scrape timeout value |
| metrics.serviceMonitor.extraEndpointParameters | object | {} | Extra parameters rendered to the ServiceMonitor endpoint |
| metrics.serviceMonitor.extraParameters | object | {} | Extra parameters rendered to the ServiceMonitor |
| metrics.serviceMonitor.path | string | "/metrics" | Path to metrics |
| metrics.serviceMonitor.scheme | string | "http" | Scheme to use for metrics endpoint |
Network policies
Allows to define optional network policies for ingress and egress The policyTypes will be automatically set
| Key | Type | Default | Description |
|---|---|---|---|
| networkPolicy.ingress | object | {} | Ingress network policies |
| networkPolicy.egress | object | {} | Egress network policies |
Storage parameters
| Key | Type | Default | Description |
|---|---|---|---|
| storage.accessModes[0] | string | "ReadWriteOnce" | Storage access mode |
| storage.persistentVolumeClaimName | string | nil | PVC name when existing storage volume should be used |
| storage.persistentVolumeClaimRetentionPolicy | object | nil | Persistent volume claim retention policy (only applies when useDeployment: false) |
| storage.volumeName | string | "mongodb-volume" | Internal volume name and prefix of a created PVC |
| storage.requestedSize | string | nil | Size for new PVC, when no existing PVC is used |
| storage.className | string | nil | Storage class name |
| storage.keepPvc | bool | false | Keep a created Persistent volume claim when uninstalling the helm chart (only for useDeploymentWhenNonHA) |
| storage.annotations | object | {} | Additional storage annotations |
| storage.labels | object | {} | Additional storage labels |
MongoDB parameters
| Key | Type | Default | Description |
|---|---|---|---|
| extraInit.retries | int | 10 | Number of retries to detect whether mongod is fully up and running in background |
| extraInit.delay | int | 3 | Seconds to wait between retries |
| extraInit.initDelay | int | 5 | Seconds to wait after mongod is running to give it time for internal initialization |
| shutdown.delay | int | 10 | Delay until termination request is forwarded to mongod process to give ReplicaSet time for electing a new primary instance |
| settings.rootUsername | string | admin | The root username |
| settings.rootPassword | string | {} | The root users password |
| userDatabase | object | {} | Optional MongoDB user database |
| userDatabase.name | string | nil | Name of the user database |
| userDatabase.user | string | nil | User name with full access to user database |
| userDatabase.password | string | nil | Password of created user |
| customConfig | string | nil | Custom MongoDB configuration block that will be mounted as file in /etc/mongo/custom.conf |
| extraEnvSecrets | list | [] | A list of existing secrets that will be mounted into the container as environment variables |
| extraSecretConfigs | string | nil | An existing secret with files that will be added to the mongodb configuration in addition to /etc/mongo/custom.conf |
| customScripts | object | nil | Optional custom scripts that can be defined inline and will be mounted as files in /docker-entrypoint-initdb.d |
| extraScripts | string | nil | An existing configMap with files that will be mounted into the container as script files (*.js, *.sh) in /docker-entrypoint-initdb.d |
| extraSecrets | list | [] | A list of additional existing secrets that will be mounted into the container |
| extraSecrets[].name | string | nil | Name of the existing K8s secr |
Deploy mongodb with OpenTenant
Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has mongodb on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.
- Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
- Place the chartmongodb is pinned at v0.8.5 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
- ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below opentenant validate opentenant.yaml terraform init && terraform apply