<p>I have one <strong>kubernetes cluster</strong> on <strong>gcp</strong>, running my <strong>express</strong> and <strong>node.js</strong> application, operating <strong><code>CRUD</code></strong> operations with <strong><code>MongoDB</code></strong>.</p>
<p>I created one secret, containing <code>username</code> and <code>password</code>,
connecting with <code>mongoDB</code> specifiedcified secret as <code>environment</code> in my <code>kubernetes</code> <strong><code>yml</code></strong> file.
Now My question is &quot;<strong>How to access that username and password
in node js application for connecting mongoDB</strong>&quot;.</p>
<p>I tried <code>process.env.SECRET_USERNAME</code> and  <code>process.env.SECRET_PASSWORD</code>
in <code>Node.JS</code> application, it is throwing <strong><code>undefined</code></strong>.</p>
<p>Any idea ll'be appreciated .</p>
<p><strong>Secret.yaml</strong></p>
<pre><code> apiVersion: v1

  data:

     password: pppppppppppp==

     username: uuuuuuuuuuuu==

kind: Secret

metadata:

 creationTimestamp: 2018-07-11T11:43:25Z

 name: test-mongodb-secret

 namespace: default

 resourceVersion: &quot;00999&quot;

 selfLink: /api-path-to/secrets/test-mongodb-secret

 uid: 0900909-9090saiaa00-9dasd0aisa-as0a0s-

 type: Opaque
</code></pre>
<p><strong>kubernetes.yaml</strong></p>
<pre><code> apiVersion: extensions/v1beta1
 
kind: Deployment

metadata:

annotations:deployment.kubernetes.io/

revision: &quot;4&quot;

creationTimestamp: 2018-07-11T11:09:45Z

generation: 5

labels:
    name: test
 name: test
 namespace: default
 resourceVersion: &quot;90909&quot;
 selfLink: /api-path-to/default/deployments/test
 uid: htff50d-8gfhfa-11egfg-9gf1-42010gffgh0002a
spec:
  replicas: 1
   selector:
   matchLabels:
   name: test
 strategy:
    rollingUpdate:
     maxSurge: 1
     maxUnavailable: 1
    type: RollingUpdate
  template:
     metadata:
      creationTimestamp: null
  labels:
    name: test
  spec:
    containers:
  - env:
    - name: SECRET_USERNAME
      valueFrom:
        secretKeyRef:
          key: username
          name: test-mongodb-secret
    - name: SECRET_PASSWORD
      valueFrom:
        secretKeyRef:
          key: password
          name: test-mongodb-secret
    image: gcr-image/env-test_node:latest
    imagePullPolicy: Always
    name: env-test-node
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
  dnsPolicy: ClusterFirst
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  terminationGracePeriodSeconds: 30
 
  status:
   availableReplicas: 1
   conditions:
  - lastTransitionTime: 2018-07-11T11:10:18Z
     lastUpdateTime: 2018-07-11T11:10:18Z
    message: Deployment has minimum availability.
     reason: MinimumReplicasAvailable
    status: &quot;True&quot;
     type: Available
     observedGeneration: 5
      readyReplicas: 1
      replicas: 1
      updatedReplicas: 1
</code></pre>

Shree Prakash
<p>Your<code>kubernetes.yaml</code> file specifies which environment variable to store your secret so it is accessible by apps in that namespace.</p>

<p>Using <a href="https://kubernetes.io/docs/concepts/configuration/secret/" rel="noreferrer">kubectl secrets cli interface</a> you can upload your secret.</p>

<pre><code>kubectl create secret generic -n node-app test-mongodb-secret --from-literal=username=a-username --from-literal=password=a-secret-password
</code></pre>

<p>(the namespace arg <code>-n node-app</code> is optional, else it will uplaod to the default namespace)</p>

<p>After running this command, you can check your kube dashboard to see that the secret has been save</p>

<p>Then from you node app, access the environment variable <code>process.env.SECRET_PASSWORD</code></p>

<p>Perhaps in your case the secretes are created in the wrong namespace hence why <code>undefined</code> in yourapplication.</p>

<p><strong>EDIT 1</strong></p>

<p>Your indentation for <code>container.env</code> seems to be wrong</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: secret-env-pod
spec:
  containers:
  - name: mycontainer
    image: redis
    env:
      - name: SECRET_USERNAME
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: username
      - name: SECRET_PASSWORD
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: password
  restartPolicy: Never
</code></pre>
