How to use kubernetes secrets in nodejs application? How to use kubernetes secrets in nodejs application? kubernetes kubernetes

How to use kubernetes secrets in nodejs application?


Yourkubernetes.yaml file specifies which environment variable to store your secret so it is accessible by apps in that namespace.

Using kubectl secrets cli interface you can upload your secret.

kubectl create secret generic -n node-app test-mongodb-secret --from-literal=username=a-username --from-literal=password=a-secret-password

(the namespace arg -n node-app is optional, else it will uplaod to the default namespace)

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

Then from you node app, access the environment variable process.env.SECRET_PASSWORD

Perhaps in your case the secretes are created in the wrong namespace hence why undefined in yourapplication.

EDIT 1

Your indentation for container.env seems to be wrong

apiVersion: v1kind: Podmetadata:  name: secret-env-podspec:  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