No environment variable displayed through kubectl

12/18/2020

To display some environment variables in a pod on Kubernetes, I tried it in two ways.

(1) Connecting inside a pod

I connected to shell in a pod and I executed 'echo' command like below..

kubectl exec -it <pod-name> /bin/bash

then...

echo $KUBERNETES_SERVICE_HOST 

I saw correct result as I expected.

(2) Send a command to a pod

kubectl exec <pod-name> -- echo $KUBERNETES_SERVICE_HOST

In this case, there is no output.

you can see the screenshot what I did. enter image description here

What is the problem here? What is difference between two situations?

Thanks you :)

-- Daeyeon Joo
bash
environment-variables
kubernetes
linux
sh

1 Answer

12/18/2020

In the second case, '

#x27; DOllar in the command references to your local host envrionment variables. And there is no such variable KUBERNETES_SERVICE_HOST on local host, the command that goes looks like below

kubectl exec <pod-name> -- echo <blank>

use below instead

kubectl exec c-hub-admin-app-systest-6dc46bb776-tvb99 -- printenv | grep KUBERNETES_SERVICE_HOST

enter image description here

-- RAMNEEK GUPTA
Source: StackOverflow