<p>In the following pod yaml, I cannot get <code>source</code> command to work. Initially I inserted the command under <code>args</code> between <a href="https://stackoverflow.com/questions/33887194/how-to-set-multiple-commands-in-one-yaml-file-with-kubernetes"><code>echo starting</code> and <code>echo done</code></a> and now I tried <a href="https://stackoverflow.com/questions/44140593/how-to-run-command-after-initialization"><code>{.lifecycle.postStart}</code></a> to no avail.</p>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: mubu62
  labels:
    app: mubu62
spec:
  containers:
  - name: mubu621
    image: dockreg:5000/mubu6:v6
    imagePullPolicy: Always
    ports:
    - containerPort: 5021
    command: [&quot;/bin/sh&quot;,&quot;-c&quot;]
    args: 
    - echo starting;
      echo CONT1=\&quot;mubu621\&quot; &gt;&gt; /etc/environment;
      touch /mubu621;
      sed -i 's/#Port 22/Port 5021/g' /etc/ssh/sshd_config;
      sleep 3650d;
      echo done;
    lifecycle:
      postStart:
        exec:
          command: [&quot;/bin/bash&quot;,&quot;-c&quot;,&quot;source /etc/environment&quot;]
  - name: mubu622
    image: dockreg:5000/mubu6:v6
    imagePullPolicy: Always
    ports:
    - containerPort: 5022
  imagePullSecrets:
  - name: regcred
  nodeName: spring
  restartPolicy: Always

</code></pre>
<p><code>Kubectl apply</code> throws no errors, but <code>echo $CONT1</code> returns nada! <code>mubu6</code> is an ubuntu modified image.</p>
<p>The reason I am doing this, is because when I <code>ssh</code> from another pod in this pod <code>(mubu621)</code>, Kubernetes environment variables set through <code>env</code> are not seen in the <code>ssh</code> session.</p>
<p>Any help would be much appreciated!</p>

<p>After experimenting with the suggestions under <a href="https://unix.stackexchange.com/questions/101168/set-environment-variable-automatically-upon-ssh-login-no-root-access">set-environment-variable-automatically-upon-ssh-login</a>, what worked was to substitute</p>
<pre><code>echo CONT1=\&quot;mubu621\&quot; &gt;&gt; /etc/environment;
</code></pre>
<p>with</p>
<pre><code>echo CONT1=\&quot;mubu621\&quot; &gt;&gt; /root/.bashrc;
</code></pre>
<p>and delete</p>
<pre><code>lifecycle:
      postStart:
        exec:
          command: [&quot;/bin/bash&quot;,&quot;-c&quot;,&quot;source /etc/environment&quot;]
</code></pre>
<p>that didn't work anyway.</p>
<p>Upon SSH-ing from <code>container mubu622</code> to <code>container mubu621</code>, I can now successfully execute <code>echo $CONT1</code> with <code>mubu621</code> output, <strong>without having to <code>source</code> <code>/root/.bashrc</code> first</strong>, which was initially the case with writing the <code>env_variable</code> in <code>/etc/environment</code>.</p>
<p><strong>In summary:</strong> when using a <code>bash shell</code> in <code>kubernetes containers</code>, you can <code>SSH</code> from another container and <code>echo</code> variables written in <code>/root/.bashrc</code> without sourcing (because <code>kubernetes env_variables</code> are not available in a ssh session).
This is very useful e.g in the case of <strong>multi-container pods</strong>, so you know amongst other things in which container you are currently logged in.</p>
