cloudbud
cloudbud

Reputation: 3278

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

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?

Upvotes: 3

Views: 6772

Answers (2)

user7886237
user7886237

Reputation: 11

 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.

Upvotes: 1

Mark Watney
Mark Watney

Reputation: 5930

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

Upvotes: 1

Related Questions