<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>

<ol>
<li><p>I don't see the <code>mongo-claim0</code> PersistentVolumeClaim matching your <code>mongo-pv</code>  ❓. In any case, add these ➕ to your PVC to change the PV reclaim policy to <code>Retain</code> from <code>Delete</code>:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  labels:
    app: mongo-claim0
  name: mongo-claim0
spec:
  accessModes:
  - ReadWriteOnce
  storageClassName: manual 👈
  resources:
    requests:
      storage: 1Gi
</code></pre>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: manual
provisioner: k8s.io/minikube-hostpath
reclaimPolicy: Retain 👈
volumeBindingMode: Immediate
</code></pre>
<p>Note: Local volumes are not supported officially.</p>
</li>
<li><p>These:</p>
<p><code>kubectl port-forward service/mongo 27017:27017</code>.</p>
<p><code>minikube service mongo --url</code> also works but will give you a random local port.</p>
</li>
<li><p>Since you have one replica you can use:</p>
<pre><code>mongodb://username:password@mongo:27017/dbname_?
</code></pre>
<p>or</p>
<pre><code>mongodb://username:password@mongo.&lt;K8s-namespace&gt;.svc.cluster.local:27017/dbname_?
</code></pre>
</li>
</ol>
<p>✌️</p>
