69

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

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

5
  • 43
    kubectl -n <namespace> exec <pod_name> -- env ? Commented Dec 5, 2019 at 15:20
  • 6
    I suggest only by parsing of kubectl describe pod <podname>. Commented Dec 5, 2019 at 15:21
  • @EgorStambakio yeah also just found this. thanks! Commented Dec 5, 2019 at 15:22
  • Trying to tag @AlexeyUsharovski : thank you! If you would like to post it as an answer yourself, please let me know and I will delete mine. Commented May 11, 2022 at 20:31
  • Other ways see: This other post Commented Apr 3, 2023 at 16:03

8 Answers 8

98

kubectl exec -it <pod_name> -- env

Sign up to request clarification or add additional context in comments.

2 Comments

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).
This only works if env is installed in the container.
23

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.

2 Comments

This doesn't work for environment variables sourced from a ConfigMap or Secret via envFrom.
It also assumes you're using a container that has normal POSIX commands and isn't using distroless or something.
23

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

3 Comments

if the env pass from configmap you won't see the env value. But then you have to check the configmap.
@SanyLiew, thank you for this clarification! Was struggling a lot, cuz was thinking that set env command isn't working (in my case ENVs were from the config map).
You can also add --list --resolve flags to the commands to resolve the secrets as Gerhard Powell suggested in his answer.
6

Execute in bash:

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

You will get all environment variables that consists env keyword.

2 Comments

Do you know why this command only returns the firs occurrence but not all the matched strings?
This only works if printenv is installed in the container.
4

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`
    

Comments

0

I normally use:

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

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.