<p>I have a java web stateful service which connects to mongodb and currently it is being deployed as a jar directly on the VM. This service has a property in properties file i.e. <code>primary</code>.</p>
<p>If we set <code>primary</code> to true then it is primary instance of the application and on this instance mongodb aggregation will happen.</p>
<p>If we set <code>primary</code> to false then it is secondary instance of the application and on this instance no mongodb aggregation will happen but it will serve all read and write queries.</p>
<p>Now we are migrating towards k8s deployment.</p>
<p>Challenges are :</p>
<p><strong>How do we set the value of primary during the k8s pod deployment having multiple replicas ?</strong></p>
<p><strong>How do we handle the failure of primary instance of application pod ?</strong></p>
<p>I am open for any architectural changes suggestions in order to facilitate better deployment structure.</p>
<p>Thanks in advance.</p>

<p>For <strong>primary</strong> and <strong>secondary</strong> you can pass the <strong>environment</strong> variables to POD and based on that POD will start with the respective configuration.</p>
<pre><code>import java.util.Map;

public class EnvMap {
    public static void main (String[] args) {
        Map&lt;String, String&gt; env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format(&quot;%s=%s%n&quot;,
                              envName,
                              env.get(envName));
        }
    }
}
</code></pre>
<p>You can create the TWO different deployment into K8s one for the Primary and second for secondary.</p>
<blockquote>
<p>How do we set the value of primary during the k8s pod deployment
having multiple replicas ?</p>
</blockquote>
<p>You can set the environment variable which will be the same across all replicas of Primary and different environment variables for secondary.</p>
<blockquote>
<p>How do we handle the failure of primary instance of application pod ?</p>
</blockquote>
<p>You can use the <strong>readiness</strong> and <strong>liveness</strong> probe to deployment so if any failure occurs K8s will auto restart the POD.</p>
<p><a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/</a></p>
<p>If you can create the two deployment in K8s</p>
<pre><code>apiVersion: apps/v1
kind: deployment
metadata:
  name: primary
  labels:
    purpose: primary
spec:
  containers:
  - name: primary
    image: gcr.io/google-samples/node-hello:1.0
    env:
    - name: primary
      value: &quot;True&quot;
</code></pre>
<p>Second deployment environment variables will goes like</p>
<pre><code>apiVersion: apps/v1
kind: deployment
metadata:
  name: secondary
  labels:
    purpose: secondary
spec:
  containers:
  - name: secondary
    image: gcr.io/google-samples/node-hello:1.0
    env:
    - name: secondary
      value: &quot;False&quot;
</code></pre>
<p>If you are using the stateful set you can also configure same in it if not using the deployment.</p>
