<p>Is there a way in Kubernetes to upgrade a given type of pod first when we have a deployment or stateful set with two or more replicas ( where one pod is master and others are not)?</p>
<p>My requirement to be specific is to ensure <strong>when calling upgrade on deployment/statefull set is to upgrade master as the last pod under a given number of replicas.</strong>.</p>

DT.
<p>The only thing that's built into Kubernetes is the <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#pod-identity" rel="nofollow noreferrer">automatic sequential naming of StatefulSet pods</a>.</p>
<p>If you have a StatefulSet, one of its pods is guaranteed to be named <code>statefulsetname-0</code>.  That pod can declare itself the &quot;master&quot; for whatever purposes this is required.  A pod can easily determine (by looking at its <strong>hostname</strong>(1)) whether it is the &quot;master&quot;, and if it isn't, it can also easily determine what pod is.  <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#rolling-updates" rel="nofollow noreferrer">Updates happen by default in numerically reverse order</a>, so <code>statefulsetname-0</code> will be upgraded last, which matches your requirement.</p>
<p>StatefulSets have other properties, which you may or may not want.  It's impossible for another pod to take over as the master if the first one fails; startup and shutdown happens in a fairly rigid order; if any part of your infrastructure is unstable then you may not be able to reliably scale the StatefulSet.</p>
<p>If you don't want a StatefulSet, you can implement your own leader election in a couple of ways (use a service like ZooKeeper or etcd that can help you maintain this state across the cluster; bring in a library for a leader-election algorithm like Raft).  Kubernetes doesn't provide this on its own.  The cluster will also be unaware of the &quot;must upgrade the leader last&quot; requirement, but if the current leader is terminated, another pod can take over the leadership.</p>
