<p>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.</p>
<p>Can someone explain me what else is set here with the kubectl exec command?</p>
<p>As an example a small excerpt from both environments.</p>
<p><strong>kubectl exec: (printenv | grep KU)</strong></p>
<pre><code>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
</code></pre>
<p><strong>ssh into the same container: (printenv | grep KU)</strong></p>
<pre><code>dev-xxxxx:~$ printenv | grep KU
dev-xxxxx:~$
</code></pre>

<p>The <code>kubectl exec</code> command allows you to remotely run arbitrary commands inside an existing container of a pod. <code>kubectl exec</code> isn’t much different from using <code>SSH</code> to execute commands on a remote system. <code>SSH</code> and <code>kubectl</code> should both work well with 99% of CLI applications. The only difference I could find when it comes to environment variables is that:</p>
<ul>
<li><p><code>kubectl</code> will always set the environment variables provided to the container at startup</p>
</li>
<li><p><code>SSH</code> relies mostly on the system login shell configuration (but can also accept user’s environment via <a href="https://superuser.com/questions/48783/how-can-i-pass-an-environment-variable-through-an-ssh-command">PermitUserEnvironment or SendEnv/AcceptEnv</a>)</p>
</li>
</ul>
<p>Answering your question:</p>
<blockquote>
<p>Can someone explain me what else is set here with the kubectl exec
command?</p>
</blockquote>
<p>They should result with the same output (assuming that you have typed both commands correctly and execute them on the same container).</p>
<p>Below you will find some useful resources regarding the <code>kubectl exec</code> command:</p>
<ul>
<li><p><a href="https://kubernetes.io/docs/tasks/debug-application-cluster/get-shell-running-container/" rel="nofollow noreferrer">Get a Shell to a Running Container</a></p>
</li>
<li><p><a href="https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#exec" rel="nofollow noreferrer">kubectl-commands#exec docs</a></p>
</li>
<li><p><a href="https://erkanerol.github.io/post/how-kubectl-exec-works/" rel="nofollow noreferrer">How does 'kubectl exec' work?</a></p>
</li>
</ul>
<p><strong>EDIT:</strong></p>
<p>If you wish to learn some more regarding the differences between <code>kubectl exec</code> and <code>SSH</code> I recommend <a href="https://goteleport.com/blog/ssh-vs-kubectl/" rel="nofollow noreferrer">this article</a>. It covers the topics of:</p>
<ul>
<li><p>Authn/z</p>
</li>
<li><p>Shell UX</p>
</li>
<li><p>Non-shell features, and</p>
</li>
<li><p>Performance</p>
</li>
</ul>
