mongodb

by groundhog2k

Helm chartv0.8.5App 8.3.17

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

KeyTypeDefaultDescription
fullnameOverridestring""Fully override the deployment name
nameOverridestring""Partially override the deployment name

Deployment parameters

KeyTypeDefaultDescription
image.pullPolicystring"IfNotPresent"Image pull policy
image.registrystring"docker.io"Image registry
image.repositorystring"mongo"Image name
image.tagstring""Image tag
imagePullSecretslist[]Image pull secrets
extraInitContainerslist[]Extra init containers
extaContainerslist[]Extra containers for usage as sidecars
startupProbeobjectsee values.yamlStartup probe configuration
livenessProbeobjectsee values.yamlLiveness probe configuration
readinessProbeobjectsee values.yamlReadiness probe configuration
customStartupProbeobject{}Custom startup probe (overwrites default startup probe configuration)
customLivenessProbeobject{}Custom liveness probe (overwrites default liveness probe configuration)
customReadinessProbeobject{}Custom readiness probe (overwrites default readiness probe configuration)
initResourcesobject{}Resource limits and requests for the default init container
resourcesobject{}Resource limits and requests
priorityClassNamestring""Deployment priority class name
nodeSelectorobject{}Deployment node selector
customLabelsobject{}Additional labels for Deployment or StatefulSet
customAnnotationsobject{}Additional annotations for Deployment or StatefulSet
podAnnotationsobject{}Additional pod annotations
podLabelsobject{}Additional pod labels
podSecurityContextobjectsee values.yamlPod security context
securityContextobjectsee values.yamlContainer security context
envlist[]Additional container environmment variables
argslist[]Additional container command arguments
serviceAccount.annotationsobject{}Additional service account annotations
serviceAccount.createboolfalseEnable service account creation
serviceAccount.namestring""Name of the service account
affinityobject{}Pod affinity
tolerationslist[]Pod tolerations
topologySpreadConstraintsobject{}Topology spread constraints for pods
podManagementPolicystringOrderedReadyPod management policy
updateStrategyTypestringRollingUpdateUpdate strategy
revisionHistoryLimitintnilMaximum number of revisions maintained in revision history

Service paramters

KeyTypeDefaultDescription
service.typestring"ClusterIP"Service type
service.portint27017MongoDB service port
service.nodePortintnilThe node port (only relevant for type LoadBalancer or NodePort)
service.clusterIPstringnilThe cluster ip address (only relevant for type LoadBalancer or NodePort)
service.loadBalancerIPstringnilThe load balancer ip address (only relevant for type LoadBalancer)
service.loadBalancerSourceRangeslist[]The list of IP CIDR ranges that are allowed to access the load balancer (only relevent for type LoadBalancer)
service.annotationsobject{}Additional service annotations
service.labelsobject{}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.

KeyTypeDefaultDescription
metrics.enabledboolfalseEnable metrics export via a mongodb_exporter sidecar
metrics.exporter.image.registrystring"docker.io"Exporter image registry
metrics.exporter.image.repositorystring"percona/mongodb_exporter"Exporter image repository
metrics.exporter.image.pullPolicystring"IfNotPresent"Exporter image pull policy
metrics.exporter.image.tagstring"0.51.0"Exporter image tag
metrics.exporter.uristring""MongoDB connection URI for the exporter (auto-built from root credentials when empty)
metrics.exporter.securityContextobject(non-root)Security context for the exporter container
metrics.exporter.resourcesobject{}Resource limits and requests for the exporter container
metrics.exporter.customStartupProbeobject{}Custom startup probe (overwrites the default startup probe)
metrics.exporter.startupProbeobject(enabled)Default startup probe
metrics.exporter.customLivenessProbeobject{}Custom liveness probe (overwrites the default liveness probe)
metrics.exporter.livenessProbeobject(enabled)Default liveness probe
metrics.exporter.customReadinessProbeobject{}Custom readiness probe (overwrites the default readiness probe)
metrics.exporter.readinessProbeobject(enabled)Default readiness probe
metrics.exporter.argslist["--collector.diagnosticdata","--collector.replicasetstatus"]Arguments for the exporter entrypoint process
metrics.exporter.envlist[]Additional environment variables (exporter only)
metrics.exporter.extraExporterEnvSecretslist[]Existing secrets mounted into the exporter as environment variables
metrics.service.enabledbooltrueEnable the metrics service
metrics.service.typestring"ClusterIP"Metrics service type
metrics.service.servicePortint9216Metrics service port
metrics.service.containerPortint9216Exporter container port
metrics.service.nodePortintnilThe node port (only relevant for type LoadBalancer or NodePort)
metrics.service.clusterIPstringnilThe cluster ip address (only relevant for type LoadBalancer or NodePort)
metrics.service.loadBalancerIPstringnilThe load balancer ip address (only relevant for type LoadBalancer)
metrics.service.loadBalancerSourceRangeslist[]The list of IP CIDR ranges that are allowed to access the load balancer (only relevant for type LoadBalancer)
metrics.service.annotationsobject{}Additional metrics service annotations
metrics.service.labelsobject{}Additional metrics service labels
metrics.serviceMonitor.enabledbooltrueEnable the Prometheus Operator ServiceMonitor
metrics.serviceMonitor.additionalLabelsobject{}Additional labels for the ServiceMonitor object
metrics.serviceMonitor.annotationsobject{}Annotations for the ServiceMonitor object
metrics.serviceMonitor.intervalstringnilThe scrape interval for prometheus
metrics.serviceMonitor.scrapeTimeoutstringnilThe scrape timeout value
metrics.serviceMonitor.extraEndpointParametersobject{}Extra parameters rendered to the ServiceMonitor endpoint
metrics.serviceMonitor.extraParametersobject{}Extra parameters rendered to the ServiceMonitor
metrics.serviceMonitor.pathstring"/metrics"Path to metrics
metrics.serviceMonitor.schemestring"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

KeyTypeDefaultDescription
networkPolicy.ingressobject{}Ingress network policies
networkPolicy.egressobject{}Egress network policies

Storage parameters

KeyTypeDefaultDescription
storage.accessModes[0]string"ReadWriteOnce"Storage access mode
storage.persistentVolumeClaimNamestringnilPVC name when existing storage volume should be used
storage.persistentVolumeClaimRetentionPolicyobjectnilPersistent volume claim retention policy (only applies when useDeployment: false)
storage.volumeNamestring"mongodb-volume"Internal volume name and prefix of a created PVC
storage.requestedSizestringnilSize for new PVC, when no existing PVC is used
storage.classNamestringnilStorage class name
storage.keepPvcboolfalseKeep a created Persistent volume claim when uninstalling the helm chart (only for useDeploymentWhenNonHA)
storage.annotationsobject{}Additional storage annotations
storage.labelsobject{}Additional storage labels

MongoDB parameters

KeyTypeDefaultDescription
extraInit.retriesint10Number of retries to detect whether mongod is fully up and running in background
extraInit.delayint3Seconds to wait between retries
extraInit.initDelayint5Seconds to wait after mongod is running to give it time for internal initialization
shutdown.delayint10Delay until termination request is forwarded to mongod process to give ReplicaSet time for electing a new primary instance
settings.rootUsernamestringadminThe root username
settings.rootPasswordstring{}The root users password
userDatabaseobject{}Optional MongoDB user database
userDatabase.namestringnilName of the user database
userDatabase.userstringnilUser name with full access to user database
userDatabase.passwordstringnilPassword of created user
customConfigstringnilCustom MongoDB configuration block that will be mounted as file in /etc/mongo/custom.conf
extraEnvSecretslist[]A list of existing secrets that will be mounted into the container as environment variables
extraSecretConfigsstringnilAn existing secret with files that will be added to the mongodb configuration in addition to /etc/mongo/custom.conf
customScriptsobjectnilOptional custom scripts that can be defined inline and will be mounted as files in /docker-entrypoint-initdb.d
extraScriptsstringnilAn existing configMap with files that will be mounted into the container as script files (*.js, *.sh) in /docker-entrypoint-initdb.d
extraSecretslist[]A list of additional existing secrets that will be mounted into the container
extraSecrets[].namestringnilName 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.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartmongodb is pinned at v0.8.5 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. 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