haproxy代理mongodb

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 keepalived
vrrp_script chk_haproxy {
    script "killall -0 haproxy"
    interval 2
    weight 2
}
# Define the script used to check if mongod is running
vrrp_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 primary
vrrp_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 interface
vrrp_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

时间: 2024-08-09 02:20:20

haproxy代理mongodb的相关文章

haproxy代理https配置方法

记得在之前的一篇文章中介绍了nginx反向代理https的方法,今天这里介绍下haproxy代理https的方法: haproxy代理https有两种方式:1)haproxy服务器本身提供ssl证书,后面的web服务器走正常的http 2)haproxy服务器本身只提供代理,后面的web服务器走https(配置ssl证书) 第一种方式:haproxy服务器本身提供ssl证书 注意:需要编译haproxy的时候支持ssl编译参数: #make TARGET=linux26 USE_OPENSSL=

haproxy代理设置及配置文件详解

haproxy代理配置: 结果图: haproxy代理配置2方式: 结果配置: Haproxy的配置文件由两部分组成:全局设定和对代理的设定,共分为五段:global. Default.frontened.backend.listen 配置文件格式: HAPROXY的配置处理3类来主要参数来源: ---最先处理的命令行参数 ---"global"配置段,用于设定全局的参数配置 ----proxy相关配置段,如default,listen,frontend和backend 全局配置: *

Haproxy代理配置---传输层

Haproxy简介: 1.HAProxy 是一款提供高可用性.负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,支持虚拟主机,它是免费.快速并且可靠的一种解决方案. HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理.HAProxy运行在时下的硬件上,完全可以支持数以万计的 并发连接.并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上. 2.HAProxy 实现了一种事件驱动.单一进程模

基于TCP的Haproxy代理

HaProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.应用到Haproxy主要是因为他免费,并且基于TCP和HTTP的应用代理. 先介绍一下Haproxy的应用场景,一个是TCP代理,这边也是我项目中应用的,另一个是HTTP代理,我就简单介绍一下.基于Tcp协议的Haproxy代理应用场景:在VPC网络环境下,虚机A的数据库不能被外界网络访问到,只能被在同一内网环境下的虚机B访问,这时候可以通过在虚机B上搭建Haproxy环境

解决haproxy代理nginx获取客户端真实ip问题

问题背景: 我们公司之前由于DDOS攻击导致网络出口受影响,经过了解,流量进入亚马逊是不收钱的,而且不限流量,为了加强防范网络攻击对业务造成影响,在亚马逊部署一套haproxy代理,把部分业务迁移试用. 为了使用nginx能获取到客户端的ip地址,在haproxy中配置option httpclose和option forwardfor,nginx配置set_real_ip_from x.x.x.x;和real_ip_header X-Forwarded-For;(其中x.x.x.x是hapro

haproxy代理https配置方法【转】

记得在之前的一篇文章中介绍了nginx反向代理https的方法,今天这里介绍下haproxy代理https的方法: haproxy代理https有两种方式:1)haproxy服务器本身提供ssl证书,后面的web服务器走正常的http 2)haproxy服务器本身只提供代理,后面的web服务器走https(配置ssl证书) 第一种方式:haproxy服务器本身提供ssl证书 注意:需要编译haproxy的时候支持ssl编译参数: #make TARGET=linux26 USE_OPENSSL=

haproxy代理https

环境准备 ip 服务 10.0.0.101 haproxy代理 10.0.0.102 httpd 原文地址:http://blog.51cto.com/11007527/2347273

如何利用HAProxy 代理 MySQL Master-Slave Replication

背景 我有一个MySQL Master-Slave Replication, 想要通过HAProxy代理, 做读写分离. 写流量给到Master 节点, 读流量给到Slave 节点. 首先我应该由一个HAProxy的配置, 我想应该这样写(伪配置): listen mysql bind *:3316 mode tcp ... server 192.168.1.111 192.168.1.111:3306 check server 192.168.1.112 192.168.1.112:3306

haproxy代理web

haproxy+keepalive:192.168.1.1.192.168.1.2 web服务                 :192.168.1.3.192.168.1.4 安装haproxy: #yum install pcre-devel zlib-devel -y #tar -zxvf haproxy-1.4.24.tar.gz -C /usr/src/ #cd /usr/src/haproxy-1.4.24/ #make TARGET=linux26 PREFIX=/usr/loca