Installing MongoDB in Kubernetes
2 min readOct 9, 2019
The most straight forward way installing MongoDB in Kubernetes is to use helm. But make sure the nfs provisioner is already deployed in kubernetes. You can refer to Kubernetes: Dynamic NFS-Client Provisioner to deploy nfs provisioner.
helm install --name my-mongodb stable/mongodb --set persistence.storageClass=nfs-client --set persistence.accessMode=ReadWriteMany --set persistence.size=250Gi
persistence.storageClass=nfs-client: refers to your nfs provisioner storage class name
persistence.accessMode and persistence.size are self explanatory.
You will get the following:NAME: my-mongodb
LAST DEPLOYED: Wed Oct 9 12:00:05 2019
NAMESPACE: thingsboard
STATUS: DEPLOYEDRESOURCES:
==> v1/PersistentVolumeClaim
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
my-mongodb Pending nfs-client 0s==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
my-mongodb-f5b999859-s4r8m 0/1 Pending 0 0s==> v1/Secret
NAME TYPE DATA AGE
my-mongodb Opaque 1 0s==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-mongodb ClusterIP 10.103.134.182 <none> 27017/TCP 0s==> v1beta1/Deployment
NAME READY UP-TO-DATE AVAILABLE AGE
my-mongodb 0/1 1 0 0sNOTES:** Please be patient while the chart is being deployed **MongoDB can be accessed via port 27017 on the following DNS name from within your cluster:my-mongodb.thingsboard.svc.cluster.localTo get the root password run:export MONGODB_ROOT_PASSWORD=$(kubectl get secret --namespace thingsboard my-mongodb -o jsonpath="{.data.mongodb-root-password}" | base64 --decode)To connect to your database run the following command:kubectl run --namespace thingsboard my-mongodb-client --rm --tty -i --restart='Never' --image bitnami/mongodb --command -- mongo admin --host my-mongodb --authenticationDatabase admin -u root -p $MONGODB_ROOT_PASSWORDTo connect to your database from outside the cluster execute the following commands:kubectl port-forward --namespace thingsboard svc/my-mongodb 27017:27017 &
mongo --host 127.0.0.1 --authenticationDatabase admin -p $MONGODB_ROOT_PASSWORD