2

I'm using Secrets as an environmental variable and I was wondering how you would call the secret in the client side of my application? I'm running a Node.js application and want to use the Secrets environmental variable. I would normally call my environment variables by doing process.env.VARIABLE_NAME locally since I have an env file, but I know that it's not the same for a secret as environmental variable when deployed onto Kubernetes.

Could anybody help me with this? Thanks!

fairlyMinty
  • 413
  • 8
  • 22
  • 5
    Environment variables should act the same no matter whether they're set from a literal value, the downward API, a ConfigMap, or a Secret value. [Distribute Credentials Securely Using Secrets](https://kubernetes.io/docs/tasks/inject-data-application/distribute-credentials-secure/#define-container-environment-variables-using-secret-data) in the Kubernetes documentation has some examples. Is there a specific setup you're having trouble with? – David Maze Jun 16 '20 at 00:05
  • I see.. so if I do `process.env.*` to access my environmental variable that is a Secret, it should work the same way then? – fairlyMinty Jun 16 '20 at 20:25

2 Answers2

5

The environment variable created by secret will read as eny other environment variable passed to pod.

So if you create a secret, for example:

kubectl create secret generic user --from-literal=username=xyz

and pass it to the pod:

env:
- name: USERNAME
  valueFrom:
    secretKeyRef:
      name: user
      key: username

It will be passed to the pod as environment variable. You can check it by executing printenv USERNAME in the pod and output will be similar to this:

kubectl exec -it secret-env -- printenv USERNAME
xyz

Multiple secrets can be passed to the pod as environment variables.

kool
  • 3,214
  • 1
  • 10
  • 26
4

After days of figuring out how to use Secrets as an environmental variable, I figured out how to reference it in my nodejs application!

Before I was doing the normal way of calling environmental variables, process.env.VARIABLE_NAME, but that did not work for me when I had Secrets as an environment variable. In order to get the value of the variable, I had to do process.env.ENVIRONMENTAL_VARIABLES_USERNAME in my Javascript file and that worked for me! Where ENVIRONMENTAL_VARIABLES is the name and USERNAME is the key! Refer to my other question for clarification: Environmental variables returning undefined for Kubernetes deployment

Not sure if this will help anyone else but this is how I managed to access my Secrets in my nodejs application!

fairlyMinty
  • 413
  • 8
  • 22