<p>I'm trying to setup a postgresql cluster in primary-standby mode using <a href="https://repmgr.org/" rel="nofollow noreferrer">repmgr</a>, and I'm thinking about how to correctly redirect the traffic to the "primary" pod.</p>

<p>My intuition is to use one label to "highlight" the primary pod and rely on the label selector of the service object to "bind" the cluster IP to it. But this lead to the question: How to "move" the label from the old primary pod to a new one after the failover?</p>

<p>Is there a way like let me register a custom script in my pod definition yaml to periodically check the role of pod and change the pod label depending on results?</p>

<p>Do you guys know if this method is possible? Or maybe there is already a sharp solution that can deal with my situation?</p>

Michael Huang
<ul>
<li><strong><code>StatefulSets</code> is the answer!</strong></li>
</ul>

<p>I'm quoting from offical <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="nofollow noreferrer">Kubernetes 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>And even more</p>

<blockquote>
  <p>StatefulSets are valuable for applications that require one or more of
  the following.</p>

<pre><code>1. Stable, unique network identifiers.
2. Stable, persistent storage.
3. Ordered, graceful deployment and scaling.
4. Ordered, automated rolling updates.
</code></pre>
</blockquote>

<p>I believe, the point number 1 is more important for you(According to your question). StatefullSets maintain order of spawing the pods too. Back in a while I worked on Redis Cluster deployment on K8s, I don't remember correctly or repo, but they used some scripts in the container to determine the Redis Master. Since StatefullSet maintain the order of spawning, it is easy to make first spawned pod is master. And again, please refer the doc till the end and any blogs on this concept. </p>

<p>And even even more</p>

<blockquote>
  <p>Stable Network ID</p>
  
  <p>Each Pod in a StatefulSet derives its hostname from the name of the
  StatefulSet and the ordinal of the Pod. The pattern for the
  constructed hostname is $(statefulset name)-$(ordinal)</p>
</blockquote>

<p>Because of stable n/w ID, the DNS name for pod never changes</p>

<p>I would recommend to deploy your postgress cluster as StatefullSet. Please refer below links to get some idea</p>

<ul>
<li><a href="https://kubernetes.io/blog/2017/02/postgresql-clusters-kubernetes-statefulsets/" rel="nofollow noreferrer">https://kubernetes.io/blog/2017/02/postgresql-clusters-kubernetes-statefulsets/</a></li>
<li><a href="https://portworx.com/ha-postgresql-kubernetes/" rel="nofollow noreferrer">https://portworx.com/ha-postgresql-kubernetes/</a></li>
<li>Or deploy as helm charts - <a href="https://github.com/helm/charts/tree/master/stable/postgresql" rel="nofollow noreferrer">https://github.com/helm/charts/tree/master/stable/postgresql</a></li>
</ul>
