Change dBpath for mongo db in kubernetes

2/26/2020

I have some problems regarding the deploy of mongodb (as stateful set) on k8s using a persistent volume on a nfs. Basically I'd like to change the dbPath in where the mongo will write its data.

This is my Stateful set yaml definition (part of it):

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongodb-standalone
spec:
  serviceName: mongo-database
  replicas: 1
  selector:
    matchLabels:
      app: mongo-database
  template:
    metadata:
      labels:
        app: mongo-database
        selector: mongodb-standalone
    spec:
      containers:
      - name: mongodb-standalone
        image: mongo:4.2.0
        env:
          - name: MONGO_INITDB_ROOT_USERNAME_FILE
            value: /etc/k8-secrets/admin/MONGO_ROOT_USERNAME
          - name: MONGO_INITDB_ROOT_PASSWORD_FILE
            value: /etc/k8-secrets/admin/MONGO_ROOT_PASSWORD
        volumeMounts:
        ...
        - name: mongodb-conf
          mountPath: /config
          readOnly: true
        - name: mongodb-data
          mountPath: /data/db3
      volumes:
      ...
      - name: mongodb-conf
        configMap:
          name: mongodb-standalone
          items:
          - key: mongo.conf
            path: mongo.conf
      - name: mongodb-data
        persistentVolumeClaim:
          claimName: mongodb-standalone

And this is the ConfigMap yaml for the defining of the configuration file in which is saved the new dbPath:

apiVersion: v1
kind: ConfigMap
metadata:
  name: mongodb-standalone
data:
  mongo.conf: |
    storage:
      dbPath: /data/db3

The problem is that, when I do the deploy in this way, as I can see from the logs, the mongo is started always on the predefined path:

2020-02-26T16:22:45.726+0000 I  CONTROL  [main] ***** SERVER RESTARTED *****
2020-02-26T16:22:45.732+0000 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2020-02-26T16:22:45.740+0000 I  CONTROL  [initandlisten] MongoDB starting : pid=28 port=27017 dbpath=/data/db 64-bit host=mongodb-standalone-0

But when I look inside my container, what I see is this:

root@mongodb-standalone-0:/data# ls
configdb  db  db3

I can find both the folders, but only in the db one are present collections and indexs data (the real db content). Why? How can I set the dbPath properly?

I have seen that there is this entrypoint that my image uses (mongo 4.2) but I don't understand it really well

Edit:

This is the content of the db3 folder (my custom folder):

root@mongodb-standalone-0:/data/db3# ls
WiredTiger  WiredTiger.lock  WiredTiger.turtle  WiredTiger.wt  dev_canp_  diagnostic.data  journal  mongod.lock

So mongo is writing something in there. This is the content of the db folder (default one):

root@mongodb-standalone-0:/data/db# ls   
WiredTiger         WiredTiger.wt     collection-0-6274164459280941380.wt  collection-8-6274164459280941380.wt  index-10-6274164459280941380.wt  index-6-6274164459280941380.wt  mongod.lock
WiredTiger.lock    WiredTigerLAS.wt  collection-2-6274164459280941380.wt  diagnostic.data                      index-3-6274164459280941380.wt   index-9-6274164459280941380.wt  sizeStorer.wt
WiredTiger.turtle  _mdb_catalog.wt   collection-4-6274164459280941380.wt  index-1-6274164459280941380.wt       index-5-6274164459280941380.wt   journal                         storage.bson

Also, when I search for the default config file, I obtain this:

root@mongodb-standalone-0:/etc# ls -lah mongod*          
-rw-r--r-- 1 root root 626 Dec 19  2013 mongod.conf.orig

with the *.orig suffix. What does it mean?

-- Nikaido
deployment
kubernetes
kubernetes-statefulset
mongodb
yaml

0 Answers