<p>I'd like to set-up a Mongo replica set on Kubernetes. I'd like to have three replicas. This means I'd need to start 3 instances.</p>

<p>Should I start three pods, with Mongo in each one, and use the service the point to the primary? Or should I used a replication controller somehow?</p>

<p><strong>This answer is out of date. I wrote a detailed step-by-step tutorial <a href="http://blog.kubernetes.io/2017/01/running-mongodb-on-kubernetes-with-statefulsets.html" rel="noreferrer">here</a> using more up to date methods. I highly recommend reading it all.</strong></p>

<p>In a nutshell, you run a sidecar app to configure the replica set for you, and either use a service per instance or ping the K8s API for the pod IP addresses.</p>

<p><strong>Example:</strong> This will only work in Google Cloud. You will need to make modifications for other platforms, particularly around the volumes:</p>

<ol>
<li>Follow the example in <a href="https://github.com/leportlabs/mongo-k8s-sidecar.git" rel="noreferrer">https://github.com/leportlabs/mongo-k8s-sidecar.git</a>

<ul>
<li><code>git clone https://github.com/leportlabs/mongo-k8s-sidecar.git</code></li>
<li><code>cd mongo-k8s-sidecar/example/</code></li>
<li><code>make add-replica ENV=GoogleCloudPlatform</code> (do this three times)</li>
</ul></li>
<li>Connect to the replica set via services. 

<ul>
<li><code>mongodb://mongo-1,mongo-2,mongo-3:27017/dbname_?</code></li>
</ul></li>
<li>You can also use the raw pod IP addresses instead of creating a service per pod

<ul>
<li>Use this <a href="https://github.com/thesandlord/kubernetes-pod-ip-finder.git" rel="noreferrer">https://github.com/thesandlord/kubernetes-pod-ip-finder.git</a></li>
</ul></li>
</ol>
