31

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

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

Robert Yi
  • 1,553
  • 1
  • 14
  • 18

6 Answers6

56

kubectl exec -it <pod_name> -- env

Robert Yi
  • 1,553
  • 1
  • 14
  • 18
  • 8
    Note: No need for the `-it` flags. The best answer is actually Egor Stambakio's in the OP comments (it also covers when in a namespace). – Eric Platon Dec 11 '20 at 00:55
10

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.

pyb
  • 4,813
  • 2
  • 27
  • 45
7

Execute in bash:

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

You will get all environment variables that consists env keyword.

Sabuhi Shukurov
  • 1,616
  • 16
  • 17
3

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

pref
  • 1,651
  • 14
  • 24
  • 1
    if the env pass from configmap you won't see the env value. But then you have to check the configmap. – Sany Liew May 27 '23 at 18:04
-1
kubectl exec <POD_NAME> -- sh -c 'echo $VAR_NAME'
AndreyKo
  • 901
  • 7
  • 16
-1

I normally use:

kubectl exec -it <POD_NAME> -- env | grep "<VARIABLE_NAME>"
Arslan Ali
  • 65
  • 5