1

I have created a mongod replica cluster of 2 nodes , mongo1 and mongo2. to load balance I have added following rule in my haproxy

listen mongo_replica_cluster
        bind *:27017
        mode tcp
        balance roundrobin
        server mongo1 10.2.0.12:27017 check inter 10s fall 3 rise 99999999
        server mongo2 10.2.0.11:27017 check backup

but if the mongo1 goes down mongo2 becomes the primary but even if mongo1 comes up mongo2 remains primary but ha redirect all traffic to mongo1 which is slave now. I want to redirect all my traffic to only the primary node.

user128113
  • 11
  • 1
  • 2

2 Answers2

1

You don't use ha-proxy.

Every node what connects to this replica set should have mongodb://mongo1,mongo2/?replicaSet="yourRS" -connection string, what tells which nodes are on that replicaset.. String don't need always list all nodes, most of them is enough, because Application can "ask" where current primary is located.

JJussi
  • 5,873
  • 1
  • 18
  • 19
1

As stated in the previous answer, you don't use HAProxy. But if you need to, you can use that.

frontend mongo-primary
  bind *:27017
  option tcplog
  mode tcp
  default_backend mongo-primary

backend mongo-primary mode tcp option tcp-check

Credit: https://blog.danman.eu/mongodb-haproxy/

Wire protocol

tcp-check send-binary 3a000000 # Message Length (58) tcp-check send-binary EEEEEEEE # Request ID (random value) tcp-check send-binary 00000000 # Response To (nothing) tcp-check send-binary d4070000 # OpCode (Query) tcp-check send-binary 00000000 # Query Flags tcp-check send-binary 61646d696e2e # fullCollectionName (admin.$cmd) tcp-check send-binary 24636d6400 # continued tcp-check send-binary 00000000 # NumToSkip tcp-check send-binary FFFFFFFF # NumToReturn

Start of Document

tcp-check send-binary 13000000 # Document Length (19) tcp-check send-binary 10 # Type (Int32) tcp-check send-binary 69736d617374657200 # ismaster: tcp-check send-binary 01000000 # Value : 1 tcp-check send-binary 00 # Term tcp-check expect binary 69736d61737465720001 #ismaster True

server mongo1 10.2.0.12:27017 check server mongo1 10.2.0.11:27017 check

rely
  • 11
  • 2