<p>I'm deploying two statefulset pods of same image running as headless service. I wish to pass seperate env variables to containers running same images inside statefulset pods. May anyone tell me how to achieve this ?</p>

<p>It is not possible to expose different values of same environment variable to different containers of a <code>Statefulsets</code> as the pods in a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="noreferrer"><code>Statefulsets</code></a> are identical replicas of the same application.</p>
<p>From the <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="noreferrer">docs</a>:</p>
<blockquote>
<p>Like a Deployment, a StatefulSet manages Pods that are based on an
identical container spec. Unlike a Deployment, a StatefulSet maintains
a sticky identity for each of their Pods. These pods are created from
the same spec, but are not interchangeable: each has a persistent
identifier that it maintains across any rescheduling.</p>
</blockquote>
<p>However, as noted in the above doc, as each pod of a Statefulset has sticky identity using which you can pass the same set of environment variables to the pods and take appropriate actions using the different environment variables based on the identity of the pods.</p>
<p>You can expose the pod name to the containers via environment variables through <a href="https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/" rel="noreferrer">Downward API</a> and use it inside the scripts to take appropriate action:</p>
<pre><code>env:
  - name: MY_POD_NAME
      valueFrom:
        fieldRef:
          fieldPath: metadata.name
  - name: env0
    value: value0
  - name: env1
    value: value1
</code></pre>
<p>So, if you have two replicas of a <code>Statefulset</code> with name <code>myapp</code> then the pods will have the names as <code>myapp-0</code> and <code>myapp-1</code> and you can then use the environment variable based on the name of the pod (<code>env0</code> if pod name is <code>myapp-0</code> and <code>env1</code> if pod name is <code>myapp-1</code>).</p>
<p>Note: Although the solution works yet it should not be preferred and we should instead have separate statefulsets objects for exposing different environment variables depending on the use case.</p>
