Connect to primary replica set mongo on kubernetes

6/25/2019

I have configured a mongodb on kubernetes with two replicas: * mongo-0 * mongo-1 And I have another pod with mongo-express * mongo-express

The problem is, that, I want to connect mongo-express to primary mongo replica, but kubernetes is doing load balancing, so, some times it will connect as primary and others as secondary

How can I solve this?

Thanks

-- Marco Sousa
kubernetes
mongodb

3 Answers

6/25/2019

you should create a service for any of your mongo. it means you should have 3 service for 3 pod. by one service you can not do that.

-- yasin lachini
Source: StackOverflow

6/25/2019

You can create the cluster of MongoDB. But for MongoDB-express replica you can create service and connect it to other replicas may not will work.

-- Harsh Manvar
Source: StackOverflow

6/27/2019

If you would install your mongodb cluster with helm, the mongodb chart is templated in that way, that it allows you to select on Service level definition (with labels selector) which particular replicas it should target - primary or secondary. Just keep in mind it's not leave for user customization with chart values, so it requires 'hard' changes. At least it should give you an idea how to achieve easily your needs.

Here is the example manifest file:

# Source: mongodb/templates/svc-primary-rs.yaml
apiVersion: v1
kind: Service
metadata:
  name: my-mongo-mongodb
  labels:
    app: mongodb
    chart: mongodb-5.3.0
    release: "my-mongo"
    heritage: "Tiller"
spec:
  type: ClusterIP
  ports:
  - name: mongodb
    port: 27017
    targetPort: mongodb
  selector:
    app: mongodb
    release: "my-mongo"
    component: secondary  <---
-- Nepomucen
Source: StackOverflow