<p>I am trying to set the two <code>env</code> variables of mongo namely - <code>MONGO_INITDB_ROOT_USERNAME</code> and <code>MONGO_INITDB_ROOT_PASSWORD</code> using <code>kubernetes</code> <strong>ConfigMap</strong>  and <strong>Secret</strong> as follows:</p>
<p>When I don't use the config map and password, i.e. I hardcode the username and password, it works, but when I try to replace it with configmap and secret, it says</p>
<blockquote>
<p>'Authentication failed.'</p>
</blockquote>
<p>my username and password is the same, which is <code>admin</code></p>
<p>Here's the <code>yaml</code> definition for these obects, can someone help me what is wrong?</p>
<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: mongodb-username
data:
  username: admin
---
apiVersion: v1
kind: Secret
metadata:
  name: mongodb-password
data:
  password: YWRtaW4K  
type: Opaque
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongodbtest
spec:
  # serviceName: mongodbtest
  replicas: 1
  selector:
    matchLabels:
      app: mongodbtest
  template:
    metadata:
      labels:
        app: mongodbtest
        selector: mongodbtest
    spec:
      containers:
      - name: mongodbtest
        image: mongo:3       
        # env:
        #   - name: MONGO_INITDB_ROOT_USERNAME
        #     value: admin
        #   - name: MONGO_INITDB_ROOT_PASSWORD
        #     value: admin
        env:
          - name: MONGO_INITDB_ROOT_USERNAME
            valueFrom:
              configMapKeyRef:
                name: mongodb-username
                key: username
          - name: MONGO_INITDB_ROOT_PASSWORD
            valueFrom:
              secretKeyRef:
                name: mongodb-password
                key: password
</code></pre>

Jananath Banuka
<p>Finally I was able to find the solution after hours, it is not something I did from kubernetes side, it is when I did <code>base64</code> encode.</p>
<p>The correct way to encode is with following command:</p>
<pre><code>echo -n 'admin' | base64
</code></pre>
<p>and this was the issue with me.</p>
