title: How to use kubernetes secrets in nodejs application?
body: <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>