How to setup username and password authentication for Mongodb docker container to be deployed on Kubernetes Cluster

9/23/2019

The username and password authentication need to be setup to mongo docker container.A YAML file is being used in order to setup the configuration of mongo container and we are using both Environment and command Tags.Even though we set username and password in environment tag, it is getting overridden since we are using command tag as well.

Once the mongo container is created, using any GUI tool if we add user and role to admin database,we are able to access mongo with credentials and as well as without any authentication. Also we tried to see if username and password can be accommodated in mongod.conf but even this approach din't work.

Sample YAML file for mongo Deployment

 spec:
    replicas: 1
    strategy: {}
    template:
      metadata:
        creationTimestamp: null
        labels:
          io.kompose.service: mongo-shard
      spec:
        containers:
        - env:
          - name: MONGO_INITDB_ROOT_USERNAME
            value: root
          - name: MONGO_INITDB_ROOT_PASSWORD
            value: password
          command:
          - "mongod"
          - "--port"
          - "27017"
          - "--bind_ip"
          - "0.0.0.0"
          - "--wiredTigerCacheSizeGB"
          - "2"
          - "--shardsvr"
          - "--replSet"
          - "Shard1RepSet"
...

To summarize, username and password authentication must be enabled on the deployed mongo docker container.Can someone please help me with this?

-- Prajwal M
docker
kubernetes
mongodb
sharding

0 Answers