<p>I am deploying a mongodb statefulset on kubernetes cluster (GKE).</p>
<p>Every time when I <strong>first</strong> deploy the statefulset I have to run the following command <strong>manually</strong> on mongo statefulset.</p>
<p>My steps:</p>
<ol>
<li><p>deploy the yaml file</p>
</li>
<li><p>enter to mongo pod by &quot;<strong>kubectl exec -it mongo-0 /bin/bash</strong>&quot;</p>
</li>
<li><p>run &quot;<strong>mongo</strong>&quot;</p>
</li>
<li><p>run</p>
<p>rs.initiate( {
_id : &quot;rs0&quot;,
members: [
{ _id: 0, host: &quot;mongo-0.mongo:27017&quot; },
{ _id: 1, host: &quot;mongo-1.mongo:27017&quot; }
]
})</p>
</li>
</ol>
<p>to initiate</p>
<p>Can I put this command in mongo-statefulset.yml file or another ways to handle it <strong>automatically</strong>?</p>
<p>mongo-statefulset.yml:</p>
<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongo
spec:
  selector:
     matchLabels:
       role: mongo
  serviceName: &quot;mongo&quot;
  replicas: 1
  template:
    metadata:
      labels:
        role: mongo
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - name: mongo
          image: mongo:4.0
          command:
            - mongod
            - &quot;--replSet&quot;
            - rs0
            - &quot;--bind_ip&quot;
            - 0.0.0.0
          ports:
            - containerPort: 27017
</code></pre>
<p>Also another critical problem:</p>
<p>Sometime I don't know why all of my pods inside my cluster would <strong>suddenly be restarted</strong>, everything is normal from being restarted and keep running <strong>except</strong> this mongo stateful set. The replica set status of mongo became &quot;<strong>OTHER</strong>&quot; instead of &quot;Primary/Secondary&quot;. Therefore, the mongo service cannot be used until I type the following command to recover it:</p>
<pre><code>rsconf = rs.conf()
rsconf.members = [
      { _id: 0, host: &quot;mongo-0.mongo:27017&quot;, priority: 1 },
      { _id: 1, host: &quot;mongo-1.mongo:27017&quot;, priority: 0.5 }
   ]
rs.reconfig(rsconf, {force: true})
</code></pre>
<p>How to recover it automatically?</p>

potato
<p>Two options here:</p>
<ul>
<li><p>Use Kubernetes <a href="https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/" rel="nofollow noreferrer">Lifecyle Hooks</a>. The <code>PostStart</code> hook would lend itself to your usecase. However, make sure to build in a check that validates whether your mongo has started, before running the configuration call. The hook is also executed when K8s decides to restart a Pod.</p>
</li>
<li><p>Use <a href="https://helm.sh/docs/topics/charts_hooks/" rel="nofollow noreferrer">Helm Chart Hooks</a>. This adds the option to differentiate between post-install and post-upgrade. However, these hooks will only be fired after Helm installation of upgrading of the chart and not when K8s restarts the Pod.</p>
</li>
</ul>
