Robert Yi
Robert Yi

Reputation: 2313

Get environment variable from kubernetes pod?

What's the best way to list out the environment variables in a kubernetes pod?

(Similar to this, but for Kube, not Docker.)

Upvotes: 64

Views: 130523

Answers (9)

trckster
trckster

Reputation: 610

Every time I need to do this I come across this answer and every time I can't find what I need so I just leave this for you and future me:

kubectl get secrets -o json | jq '.items[] | {name: .metadata.name,data: .data|map_values(@base64d)}' | grep <what-are-you-lookng-for> -C 5

Upvotes: 0

Gerhard Powell
Gerhard Powell

Reputation: 6185

I have two ways that I get the configurations:

  • List all the pods with their environment variables. It also resolves the secret or configmap references

    kubectl set env pods --all --list --resolve
    
  • List all the pods in a table with its enviroment variables and arguments

    kubectl get pods -n qa -o custom-columns=NAME:.metadata.name,command:.spec.containers[*].command,ENV:.spec.containers[*].env,ARGS:.spec.containers[*].args`
    

Upvotes: 1

Nick G
Nick G

Reputation: 146

Because Kubernetes deployments or statefulsets usually manage pods, and pods inherit the variables from their manifests, I would use the kubectl describe ... instead of kubectl exec -it ... approach since finding the correct dynamic pod name is not always easy or fast.

For example:

kubectl describe deployment/my-app -n my_namespace | grep MY_VAR_NAME

is more robust and works better for me.

Upvotes: 0

R...
R...

Reputation: 2620

kubectl set env can be used for both setting environment variables and reading them .

You can use kubectl set env [resource] --list option to get them.

For example to list all environment variables for all PODs in the DEFAULT namespace:

kubectl set env pods --all --list

or for an specific POD in a given namespace

kubectl set env pod/<pod-NAME> --list -n <NAMESPACE-NAME>

or for a deployment in DEFAULT namespace

kubectl set env deployment/<deployment-NAME> --list

this is better than running command inside the POD as in some cases the OS command may not exist in very slim containers

For more see : https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#set

Upvotes: 17

AndreyKo
AndreyKo

Reputation: 909

kubectl exec <POD_NAME> -- sh -c 'echo $VAR_NAME'

Upvotes: -1

Arslan Ali
Arslan Ali

Reputation: 83

I normally use:

kubectl exec -it <POD_NAME> -- env | grep "<VARIABLE_NAME>"

Upvotes: -1

pyb
pyb

Reputation: 5289

Both answers have the following issues:

  1. They assume you have the permissions to start pod, which is not the case in a locked-down environment
  2. They start a new pod, which is invasive and may give different environment variables than "a[n already running] kubernetes pod"

To inspect a running pod and get its environment variables, one can run:

kubectl describe pod <podname>

This is from Alexey Usharovski's comment.

I am hoping this gives more visibility to your great answer. If you would like to post it as an answer yourself, please let me know and I will delete mine.

Upvotes: 22

Sabuhi Shukurov
Sabuhi Shukurov

Reputation: 1926

Execute in bash:

kubectl exec -it <pod-name> -- printenv | grep -i env

You will get all environment variables that consists env keyword.

Upvotes: 6

Robert Yi
Robert Yi

Reputation: 2313

kubectl exec -it <pod_name> -- env

Upvotes: 94

Related Questions