<p>I have a Kubernetes ReplicaSet with 5 replicas. I want to have a pod ID which does increase for each replica, however it should not increase when a pod is recreating because it crashed.</p>
<p>Example:</p>
<pre><code> * ReplicaSet starts with 4 replicas    *
 * Replica 1 starts with the ID 1       *
 * Replica 2 starts with the ID 2       *
 * Replica 3 starts with the ID 3       *
 * Replica 4 starts with the ID 4       *
 * Replica 3 dies                       *
 * Replica 5(3) starts with ID 3        *

</code></pre>

Chrizz
<p>You're looking for a Statefulset</p>
<p><a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/</a></p>
<p>Naming starts with ID 0, but that's somewhat the behavior you are looking for.</p>
<hr />
<p>Answering your comment. When my SatefulSet uses an image I would build myself, I may use something like this, resolving my container index, before I would start my application:</p>
<pre><code>if test -z &quot;$HOSTNAME&quot;; then
    HOSTNAME=`hostname 2&gt;/dev/null`
fi
if echo &quot;$HOSTNAME&quot; | grep -E '.*-[0-9][0-9]*' &gt;/dev/null ; then
    POD_IDX=`echo &quot;$HOSTNAME&quot; | sed 's|.*-\([0-9]*\)$|\1|'`
fi
#echo My index is $POD_IDX
</code></pre>
<p>In other cases, I may deploy an image from a public registry. Then, I would figure out a way to pass this application with a configuration contextualized by some initContainer, for example with Prometheus - which is not using a Pod index, though it could have re-used the block from above, ...:</p>
<pre><code>  initContainers:
  - args:
    - -c
    - |
         set -x; \
         sed &quot;/external_labels:.*/ascrape_by: $REPLICA&quot; /etc/prometheus.yml.tpl | \
             sed &quot;s|^scrape_by:|    scrape_by:|&quot; &gt;/etc/prometheus/prometheus.yml;
    command: [ /bin/sh ]
    env:
    - name: REPLICA
      valueFrom:
        fieldRef:
          fieldPath: metadata.name
    image: docker.io/prom/prometheus:xx
</code></pre>
