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
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.
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.
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 <---