Get an environment variable from kubernetes pods and store it in an array

12/23/2019

The use case is to get the environment variable *COUNTRY from all the pods running in a namespace

kubectl get pods podname -n namespace -o 'jsonpath={.spec.containers[0].env[?(@.name~="^COUNTRY")].value}'

This does not seem to work. any lead?

-- cloudbud
jsonpath
kubernetes

2 Answers

5/27/2020
 kubectl get pods -o=jsonpath='{.items[*].spec.containers[*].env[?(@.name=="COUNTRY")].value}'

Hope this helps. I was just able to run it on mine and it worked the best.

-- user7886237
Source: StackOverflow

12/23/2019

You can retrieve this information using the following command:

kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.spec.containers[*].env[*].name}{"\t"}{.spec.containers[*].env[*].value}{"\n"}{end}' | grep COUNTRY | cut -f 2

It will return the variables content as follows:

$ kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.spec.containers[*].env[*].name}{"\t"}{.spec.containers[*].env[*].value}{"\n"}{end}' | grep VAR | cut -f 2

123456
7890123
-- mWatney
Source: StackOverflow