Does anyone know why this one doesn't (shows empty line):
kubectl exec kube -- echo $KUBERNETES_SERVICE_HOST
but this one works?
kubectl exec kube -- sh -c 'echo $KUBERNETES_SERVICE_HOST'
The reason this shows an empty line has nothing to do with kubernetes or the container. In your first line the $KUBERNETES_SERVICE_HOST
is interpreted by the shell you're running in. And you must not have KUBERNETES_SERVICE_HOST set within that shell, so it uses an empty string instead.
Based on what you're seeing, I infer that you're trying to find out the value of the KUBERNETES_SERVICE_HOST variable inside your kubernetes container. Your last command will do that for you, and I suspect the following one would do the same.
kubectl exec kube -- echo '$KUBERNETES_SERVICE_HOST'
Running successfully will depend on the image that you are using for kube
. But in general terms echo
is a built-in Bourne shell command.
With this command:
$ kubectl exec kube -- echo $KUBERNETES_SERVICE_HOST
You are not instantiating a Bourne Shell environment and there is not an echo
executable in the container. It turns out what kubectl
does is basically running echo $KUBERNETES_SERVICE_HOST
on your client! You can try running for example:
$ kubectl exec kube -- echo $PWD
You'll see that is the home directory on your client.
Whereas with this command:
$ kubectl exec kube -- sh -c 'echo $KUBERNETES_SERVICE_HOST'
There is a sh
executable in your environment and that's Bourne Shell that understands the built-in echo
command with the given Kubernetes environment in the default container of your Pod.