In this quick article let us see how to list the Environment variables of a POD and its containers.
Environment variables are common in all ecosystems, Let it be monolith applications or Micro Services.
Those who started their journey with Java might be well aware of this Environment variable JAVA_HOME
and PATH
If you do not set them right, your Java Program would not even run.
These Environment variables hold great value and it has never faded a bit. And for the applications to run and function properly they heavily depend on these environmental variables.
Just like Java needs JAVA_HOME
and PATH
etc. Every Application has its own dependency on environment variables.
These Environment variables often hold configuration data and sometimes it holds confidential secrets
You can use configmap
or secrets
to store your configuration data or confidential information like passwords etc, and refer to them as Environment variables for your application to run and thrive.
Now enough with a Context. Now to the objective. How do you list these Environment variables of a running POD and its containers?
Let us see.
List Environment variables of all the containers inside the POD
You can use this option to list all the Environment variables available in your pod. Let it be a multi-container setup, A POD with more than one container
Or a POD with a Single container.
This command would bring all the environment variables
kubectl get deployments <name-of-deployment> -o jsonpath='{range .spec.template.spec.containers[*].env[*]}{@.name}{"="}{@.value}{"\n"}{end}
as you can see, we are using the JSONpath to filter out the selective fields and also add =
sign while printing the Key and Values.
To understand this JSONPath syntax a little better, you can try to evaluate this with the JSONPath Online evaluator
List Environment variables of a Selective Container from the POD
In the previous example, we have seen how to list the Environment variables of all the containers inside the pod
It was made possible with JSONPath syntax and if you look at the JSONPath query we have used, we have used .spec.containers[*]
as we have used *
within the array, all the elements of containers would be chosen.
Instead of using *
we can apply a filter or perform a search using the following syntax
[?(@.name=="containername")]
this filter Selects the container with the name matching what we are looking for.
Let us suppose that your POD has two containers such as nginx
and a python-flask
application and the name of containers are the same too.
You just need only the Environment variables of a container with a name python-flask
inside the POD.
Here is the kubectl command to list environment variables of a selective container.
kubectl get deployments <name-of-deployment> -o jsonpath='{range .spec.template.spec.containers[?(@.name=="python-flask")].env[*]}{@.name}{"="}{@.value}{"\n"}{end}
Print Only Keys and Not Values.
For security reasons, we might not really want to print the values of the environment variables but just the keys
So how do you print only the keys but not values?
This is how you can do it
List only Environment variable keys For a selective container
$ kubectl get deployments v4 -n production -o jsonpath='{range .spec.template.spec.containers[?(@.name=="python-flask")].env[*]}{.name}{"\n"}{end}'
List only Environment variable keys For all the containers
$ kubectl get deployments v4 -n production -o jsonpath='{range .spec.template.spec.containers[*].env[*]}{.name}{"\n"}{end}
Hope these commands help you.
Cheers
Sarav AK