<p>I have an application that has 3 pods and each pod needs a fixed variable name stored in each pod. So if everything is running fine, the three pods would have var1, var2, and var3 stored on the corresponding pods.</p>

<p>If the first pod gets replaced which has var1, how can I determine that the other 2 pods have var2 and var3, and thus know that the new pod should be assigned var1?</p>

<p>Can this be done with Stateful Sets?</p>

William Ross
<p>I see two ways of doing that:</p>

<ol>
<li>Using <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="nofollow noreferrer">StatefulSets</a>:</li>
</ol>

<blockquote>
  <p>For a StatefulSet with N replicas, each Pod in the StatefulSet will be
  assigned an integer ordinal, from 0 up through N-1, that is unique
  over the Set.</p>
</blockquote>

<ol start="2">
<li>Creating the <a href="https://kubernetes.io/docs/tasks/configure-pod-container/" rel="nofollow noreferrer">Pods</a> manually. Example:  </li>
</ol>

<blockquote>
<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: memory-demo-3
  namespace: mem-example
spec:
  containers:
  - name: memory-demo-3-ctr
    image: polinux/stress
</code></pre>
</blockquote>

<p>If you need your application to be aware of the Pod where it is running on, there is an interesting page in Kubernetes documentation: <a href="https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/" rel="nofollow noreferrer">"Expose Pod Information to Containers Through Environment Variables"</a>.</p>

<p>Example:</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: mypod-var1
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      env:
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
</code></pre>
