简体   繁体   中英

Access secrets as environment variables in Kubernetes

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!

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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM