Deploy MongoDB on Kubernetes: NoSQL Database

Deploy MongoDB on Kubernetes to manage persistent document database workloads with built-in authentication. Running MongoDB as a StatefulSet ensures stable network identifiers and dedicated persistent storage across pod restarts. This setup provides a complete single-replica database instance configured with internal routing and operational health checks.
| Component | Type | Port | Role |
|---|---|---|---|
| default | Namespace | N/A | Groups resources within the default namespace scope |
| mongodb-secret | Secret | N/A | Stores root username and password credentials |
| mongodb-headless | Service | 27017 | Provides network identity for StatefulSet pods |
| mongodb | Service | 27017 | Exposes MongoDB internally via ClusterIP |
| mongodb | StatefulSet | 27017 | Manages single-replica MongoDB pod lifecycle |
| mongodb-storage | PersistentVolumeClaim | N/A | Provisions 20Gi persistent storage for database files |
The StatefulSet executes a single mongo container that pulls root initialization credentials directly from the Secret. Data persists across restarts via a 20Gi PersistentVolumeClaim mounted at /data/db. The headless Service maintains direct network identity for the pod, while the standard ClusterIP Service exposes database port 27017 to cluster-internal clients. Exec probes run mongosh ping commands inside the container to monitor startup, liveness, and readiness states.
Active Kubernetes cluster with support for dynamic volume provisioning.
StorageClass configured to satisfy the ReadWriteOnce 20Gi volume claim.
KubeKanvas CLI installed and running on your computer (Optional, if you want to use one-click deployment)
Verify target namespace configuration or keep the default namespace scope.
Update the default MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD secret entries.
Adjust the 20Gi storage request in the volumeClaimTemplates section if required.
Deploy the template to your cluster via the Play button in the top right bar. If you prefer to deploy manually, download the YAML and apply it with kubectl.
Wait for all pods to reach Running status. You can monitor progress in the Release Monitor screen.
Check database readiness by running kubectl exec -n default statefulset/mongodb -- mongosh --eval "db.adminCommand('ping')".
Authenticate against the admin database by running kubectl exec -it -n default statefulset/mongodb -- mongosh -u admin -p ChangeMe123! --authenticationDatabase admin.
Single-Node Application Backend: Serve microservices running inside the cluster through internal ClusterIP service routing.
Stateful Document Storage: Store persistent JSON documents using dedicated volume storage and automated container lifecycle management.
This template configures a secure MongoDB StatefulSet on Kubernetes with persistent storage, internal service endpoints, and exec-based health probing.