title: How to deploy MongoDB with persistent volume in Kubernetes?
body: <p>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.</p>
  <p>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.</p>
  <ol>
  <li>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?</li>
  <li>If the username/password is <em>admin/admin</em>, how can I connect to mongo from MongoDB Compass from my mac where minikube is running?</li>
  <li>If the username/password is <em>admin/admin</em>, how can I connect to mongo from another node.js application running on the same cluster?</li>
  </ol>
  <p>Here is my configuration</p>
  <p><strong>volume.yaml</strong></p>
  <pre class="lang-yaml prettyprint-override"><code>kind: PersistentVolume
  apiVersion: v1
  metadata:
   name: mongo-pv
   labels:
    type: local
  spec:
   storageClassName: manual
   capacity:
    storage: 1Gi
   accessModes:
    - ReadWriteOnce
   hostPath:
    path: &quot;/mnt/mongo_data&quot;
  </code></pre>
  <p><strong>mongodb.yaml</strong></p>
  <pre class="lang-yaml prettyprint-override"><code>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: &quot;&quot;
          name: mongo
          ports:
          - containerPort: 27017
          resources: {}
          volumeMounts:
          - mountPath: /data/db
            name: mongo-claim0
        restartPolicy: Always
        serviceAccountName: &quot;&quot;  
        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
  </code></pre>