I am trying to setup MongoDB as a standalone on minikube with Persistent Volume using basic auth. On setting the config, I can see Mongodb service and pods up and running. I can also login to mongo shell with username/password that was set up in secrets. I can also successfully insert a sample document inside the mongo shell.
But when I stop the pod (or delete and apply mongodb.yaml), start again, then I don't see the same DB listed where I first created the sample document, and hence I don't find that sample document as well.
- Can I get feedback on whether I am not setting up the volumes correctly to persist the data in mongo outside the life of pods?
- If the username/password is admin/admin, how can I connect to mongo from MongoDB Compass from my mac where minikube is running?
- If the username/password is admin/admin, how can I connect to mongo from another node.js application running on the same cluster?
Here is my configuration
volume.yaml
kind: PersistentVolume
apiVersion: v1
metadata:
name: mongo-pv
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/mongo_data"
mongodb.yaml
apiVersion: v1
data:
MONGO_INITDB_ROOT_USERNAME: YWRtaW4=
MONGO_INITDB_ROOT_PASSWORD: YWRtaW4=
kind: Secret
metadata:
name: mongodb-secrets
type: Opaque
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
labels:
app: mongo-claim0
name: mongo-claim0
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
app: mongo
name: mongo
spec:
serviceName: mongo
replicas: 1
selector:
matchLabels:
app: mongo
template:
metadata:
labels:
app: mongo
spec:
containers:
- env:
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
secretKeyRef:
name: mongodb-secrets
key: MONGO_INITDB_ROOT_USERNAME
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mongodb-secrets
key: MONGO_INITDB_ROOT_PASSWORD
image: mongo
imagePullPolicy: ""
name: mongo
ports:
- containerPort: 27017
resources: {}
volumeMounts:
- mountPath: /data/db
name: mongo-claim0
restartPolicy: Always
serviceAccountName: ""
volumes:
- name: mongo-claim0
persistentVolumeClaim:
claimName: mongo-claim0
---
apiVersion: v1
kind: Service
metadata:
labels:
app: mongo
name: mongo
spec:
ports:
- port: 27017
targetPort: 27017
clusterIP: None
selector:
app: mongo