Different environment variables with ssh or kubectl exec

Viewed 755

We have a service in our cluster that we call via ssh (test environment etc.). In this container we have different environment variables when we connect with ssh or we connect with kubectl.

Can someone explain me what else is set here with the kubectl exec command?

As an example a small excerpt from both environments.

kubectl exec: (printenv | grep KU)

KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT_443_TCP=tcp://10.4.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_ADDR=10.4.0.1
KUBERNETES_SERVICE_HOST=10.4.0.1
KUBERNETES_PORT=tcp://10.4.0.1:443
KUBERNETES_PORT_443_TCP_PORT=443

ssh into the same container: (printenv | grep KU)

dev-xxxxx:~$ printenv | grep KU
dev-xxxxx:~$
1 Answers

The kubectl exec command allows you to remotely run arbitrary commands inside an existing container of a pod. kubectl exec isn’t much different from using SSH to execute commands on a remote system. SSH and kubectl should both work well with 99% of CLI applications. The only difference I could find when it comes to environment variables is that:

  • kubectl will always set the environment variables provided to the container at startup

  • SSH relies mostly on the system login shell configuration (but can also accept user’s environment via PermitUserEnvironment or SendEnv/AcceptEnv)

Answering your question:

Can someone explain me what else is set here with the kubectl exec command?

They should result with the same output (assuming that you have typed both commands correctly and execute them on the same container).

Below you will find some useful resources regarding the kubectl exec command:

EDIT:

If you wish to learn some more regarding the differences between kubectl exec and SSH I recommend this article. It covers the topics of:

  • Authn/z

  • Shell UX

  • Non-shell features, and

  • Performance

Related