<p>I have a java application that runs inside a Kubernetes pod.</p>

<p>The application performs several tasks (<code>taskA</code>, <code>taskB</code>, etc.).  The application supports running multiple instances in different pods.  All the pods are doing the same tasks.  </p>

<p>However, there is a task that should only be done by only one of the pods (e.g. <code>taskA</code> should only run in one of the pods).  And if the pod that is performing the specific task dies, one of the other nodes should start doing that task (passive node, with regards to <code>taskA</code>, takes over).  </p>

<p>Is there some support for this feature in k8s, or do I need use some other service (e.g. zookeeper)?</p>

Ahmed A
<p>Kubernetes doesn't know what's running inside your pods or what's inside your requests.  There's (almost) no direct support for what you're suggesting.</p>

<p>The more general topic you might look up is <em>leader election</em>.  In your example, the cluster leader is the only one who can perform the <code>taskA</code>.  <a href="http://zookeeper.apache.org" rel="nofollow noreferrer">ZooKeeper</a> is popularly used for this; there's a newer algorithm called <a href="https://raft.github.io" rel="nofollow noreferrer">Raft</a> that can work too.  You'd have to write support for this into your application.</p>

<p>The closest thing in Kubernetes is a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="nofollow noreferrer">StatefulSet</a>.  The helpful feature of a StatefulSet is that <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#pod-identity" rel="nofollow noreferrer">its replicas are numbered starting from 0</a>, so a pod can look at its own hostname and figure out which one it is.  If a lower-number pod fails then Kubernetes will restart it with the same identity.  You could set the rule that only pod 0 could perform <code>taskA</code>, which in practice might meet your requirements.</p>
