haproxy代理mongodb

來源:互聯網
上載者:User

標籤:mongodb haproxy

mongodb叢集採用的replication set模式,至少3個節點以上。有個問題,當mongodb主節點down的時候,如何讓ceilometer自動連接重新選舉出來的主節點?


這裡有兩種解法,至於採用哪種,看需求。

1、採用vip來定位mongodb主節點

     使用keepalived來起vip,這裡有兩個vip,vrrp_instance VI_1是專門給mongodb用的vip,vrrp_instance VI_2是OpenStack HA用的vip

     vrrp script chk_mongo_primary指令碼專門來檢測該節點是不是mongo master節點,如果是的話權重+2,而且vrrp_instance VI_1的vip是可以搶佔的;

     當mongodb主節點發生切換,vrrp_instance VI_1的vip也會跟著切換。

keepalived.conf設定檔

! Configuration File for keepalivedvrrp_script chk_haproxy {    script "killall -0 haproxy"    interval 2    weight 2}# Define the script used to check if mongod is runningvrrp_script chk_mongod {    script "killall -0 mongod"    interval 2 # every two seconds    weight 2}# Define the script to see if the local node is the primaryvrrp_script chk_mongo_primary {    script "mongo --eval ‘(!!db.runCommand("ismaster")["ismaster"])?quit(0):quit(1)‘"    interval 2 # every two seconds    weight 2}# Configuation for the virtual interfacevrrp_instance VI_1 {    interface br-ex    state node MASTER        # SLAVE on the other nodes    priority 101             # 100 on other nodes    virtual_router_id 55    authentication {        auth_type PASS        auth_pass secret     # Set this to some secret phrase    }    # The virtual ip address shared between the two nodes    virtual_ipaddress {        172.16.140.251/24    }    # Use the script above to check if we should fail over    track_script {        chk_mongod        chk_mongo_primary    }}vrrp_instance VI_2 {    interface br-ex    virtual_router_id 51    state BACKUP    priority 200# if use it,the openstack api do not response normally#    use_vmac virtualmac#    advert_int 1    dont_track_primary    nopreempt    authentication {    auth_type PASS    auth_pass password    }    virtual_ipaddress {       172.16.140.250/24    }    track_script {      chk_haproxy    }    notify /usr/local/bin/keepalivednotify.sh}


2、使用haproxy的tcp-check來檢測

haproyx mongodb配置

listen mongodb_cluster    bind openstack_vip:27017    option tcpka    option tcplog    option tcp-check    # MongoDB 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 controller1 controller1:27017 check inter 2000 rise 2 fall 3    server controller2 controller2:27017 check inter 2000 rise 2 fall 3

這裡有個問題,在haproxy監控頁面上看到的非master節點都是down的,因為tcp-check的緣故。

上面send-binary為什麼那麼寫?(用tcpdum抓包出來,拿到wireshark分析)

詳情參考這裡:https://blog.danman.eu/mongodb-haproxy/

                         http://serverfault.com/questions/625492/how-to-construct-a-mongodb-health-check-in-haproxy



本文出自 “the-way-to-cloud” 部落格,請務必保留此出處http://iceyao.blog.51cto.com/9426658/1836237

haproxy代理mongodb

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.