A Helm chart for deploying MongoDB 8.0 on Kubernetes with StatefulSet and persistent storage.
To install the chart with the release name mongodb:
helm install mongodb ./mongodb
To install in a specific namespace:
helm install mongodb ./mongodb --namespace database --create-namespace
To uninstall the mongodb release:
helm uninstall mongodb
This command removes all the Kubernetes components associated with the chart and deletes the release.
Note: PersistentVolumeClaims are not automatically deleted. To delete them:
kubectl delete pvc -l app.kubernetes.io/instance=mongodb
The following table lists the configurable parameters of the MongoDB chart and their default values.
| Parameter | Description | Default |
|---|---|---|
image.registry | MongoDB image registry | docker.io |
image.repository | MongoDB image repository | mongo |
image.tag | MongoDB image tag | 8.0 |
image.pullPolicy | Image pull policy | IfNotPresent |
| Parameter | Description | Default |
|---|---|---|
auth.rootUsername | MongoDB root username | dbadmin |
auth.rootPassword | MongoDB root password | HxPlsytQ3Rt |
auth.existingSecret | Name of existing secret to use for credentials | nil |
| Parameter | Description | Default |
|---|---|---|
replicaCount | Number of MongoDB replicas | 1 |
| Parameter | Description | Default |
|---|---|---|
service.type | Kubernetes service type | LoadBalancer |
service.port | MongoDB service port | 27017 |
service.clusterIP | Static cluster IP or "None" for headless service | nil |
service.sessionAffinity | Session affinity | None |
| Parameter | Description | Default |
|---|---|---|
resources.requests.memory | Memory request | 2Gi |
resources.requests.cpu | CPU request | 1000m |
resources.limits.memory | Memory limit | 16Gi |
resources.limits.cpu | CPU limit | 6000m |
| Parameter | Description | Default |
|---|---|---|
persistence.enabled | Enable persistent storage | true |
persistence.storageClass | Storage class for PVC | standard-rwo |
persistence.accessMode | PVC access mode | ReadWriteOnce |
persistence.size | PVC storage size | 100Gi |
persistence.annotations | PVC annotations | {} |
| Parameter | Description | Default |
|---|---|---|
livenessProbe.enabled | Enable liveness probe | true |
livenessProbe.initialDelaySeconds | Initial delay | 30 |
livenessProbe.periodSeconds | Period | 10 |
livenessProbe.timeoutSeconds | Timeout | 5 |
livenessProbe.failureThreshold | Failure threshold | 3 |
readinessProbe.enabled | Enable readiness probe | true |
readinessProbe.initialDelaySeconds | Initial delay | 10 |
readinessProbe.periodSeconds | Period | 5 |
readinessProbe.timeoutSeconds | Timeout | 3 |
readinessProbe.failureThreshold | Failure threshold | 3 |
| Parameter | Description | Default |
|---|---|---|
podLabels | Additional pod labels | {component: database} |
podAnnotations | Pod annotations | {} |
nodeSelector | Node selector | {} |
tolerations | Tolerations | [] |
affinity | Affinity rules | {} |
securityContext | Pod security context | {} |
serviceAccount.create | Create service account | false |
serviceAccount.name | Service account name | "" |
serviceAccount.annotations | Service account annotations | {} |
extraEnv | Additional environment variables | [] |
extraVolumes | Additional volumes | [] |
extraVolumeMounts | Additional volume mounts | [] |
Create a custom-values.yaml file:
auth:
rootUsername: "admin"
rootPassword: "SecurePassword123!"
persistence:
size: 200Gi
storageClass: "premium-rwo"
resources:
requests:
memory: "4Gi"
cpu: "2000m"
limits:
memory: "32Gi"
cpu: "8000m"
service:
type: ClusterIP
Install the chart:
helm install mongodb ./mongodb -f custom-values.yaml
First, create a secret:
kubectl create secret generic mongodb-credentials \
--from-literal=MONGO_INITDB_ROOT_USERNAME=admin \
--from-literal=MONGO_INITDB_ROOT_PASSWORD=SecurePassword123!
Then install the chart:
helm install mongodb ./mongodb \
--set auth.existingSecret=mongodb-credentials
helm install mongodb ./mongodb \
--set replicaCount=3 \
--set service.clusterIP=None
Note: After scaling to multiple replicas, you need to configure the MongoDB replica set manually.
helm upgrade mongodb ./mongodb -f custom-values.yaml
helm install mongodb ./mongodb --dry-run --debug
Connection string format:
mongodb://<username>:<password>@<release-name>-mongodb-0.<release-name>-mongodb.<namespace>.svc.cluster.local:27017
Example:
mongodb://dbadmin:[email protected]:27017
Get the LoadBalancer IP:
export SERVICE_IP=$(kubectl get svc mongodb --template "{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}")
mongosh "mongodb://dbadmin:HxPlsytQ3Rt@$SERVICE_IP:27017"
kubectl port-forward svc/mongodb 27017:27017
mongosh "mongodb://dbadmin:HxPlsytQ3Rt@localhost:27017"
kubectl exec mongodb-0 -- mongodump \
-u dbadmin -p HxPlsytQ3Rt \
--out /tmp/backup
kubectl cp mongodb-0:/tmp/backup ./mongodb-backup
kubectl cp ./mongodb-backup mongodb-0:/tmp/backup
kubectl exec mongodb-0 -- mongorestore \
-u dbadmin -p HxPlsytQ3Rt \
/tmp/backup
To check the status of your MongoDB deployment:
# Check StatefulSet
kubectl get statefulset mongodb
# Check pods
kubectl get pods -l app.kubernetes.io/name=mongodb
# Check service
kubectl get svc mongodb
# Check PVCs
kubectl get pvc -l app.kubernetes.io/name=mongodb
# View logs
kubectl logs -l app.kubernetes.io/name=mongodb -f
# Check MongoDB status
kubectl exec mongodb-0 -- mongosh -u dbadmin -p HxPlsytQ3Rt --eval "db.adminCommand('ping')"
Use external secret management: Don't store credentials in values.yaml. Use auth.existingSecret or external secret managers (e.g., Sealed Secrets, External Secrets Operator).
Set up regular backups: Implement automated backup solutions using tools like Velero or MongoDB Ops Manager.
Configure MongoDB replica sets: For high availability, deploy with 3+ replicas and configure a replica set.
Enable TLS/SSL: Configure MongoDB to use TLS for encrypted connections.
Resource planning: Adjust CPU and memory based on your workload requirements.
Storage planning: Use high-performance storage classes (e.g., SSD-backed volumes).
Set up monitoring: Deploy monitoring solutions like Prometheus with MongoDB Exporter and Grafana.
Network policies: Implement network policies to restrict access to MongoDB.
Regular updates: Keep MongoDB and the chart up to date with security patches.
Test disaster recovery: Regularly test backup and restore procedures.
kubectl describe pod mongodb-0
kubectl logs mongodb-0
kubectl get pvc
kubectl describe pvc mongodb-data-mongodb-0
kubectl get secret mongodb -o yaml
kubectl exec mongodb-0 -- env | grep MONGO
kubectl run -it --rm debug --image=mongo:8.0 --restart=Never -- \
mongosh "mongodb://dbadmin:[email protected]:27017"
Copyright © 2024 SPL Team
For issues, questions, or contributions, please contact the SPL Team or open an issue in the repository.
Content type
Helm
Digest
sha256:93ff956ac…
Size
7.7 kB
Last updated
about 1 month ago
helm pull oci://registry-1.docker.io/rilusmahmud/mongodb --version 1.2.3