global daemon nbproc 6 pidfile /var/run/haproxy.pid ulimit-n 65535 defaults mode tcp #mode { tcp|http|health },tcp 表示4层,http表示7层,health仅做为健康检查使用 retries 2 #尝试2次失败则从集群摘除 option redispatch #若是失效则强制转换其余服务器 option abortonclose #链接数过大自动关闭 maxconn 8092 #最大链接数 timeout connect 1d #链接超时时间,重要,hive查询数据能返回结果的保证 timeout client 1d #同上 timeout server 1d #同上 timeout check 2000 #健康检查时间 log 127.0.0.1 local0 #[err warning info debug] listen admin_stats #定义管理界面 bind 0.0.0.0:11000 #管理界面访问IP和端口 mode http #管理界面所使用的协议 maxconn 10 #最大链接数 stats refresh 30s #30秒自动刷新 stats uri /lb #访问url stats realm duobao\ Haproxy #验证窗口提示 stats auth admin:123456 #401验证用户名密码 listen redis-cluster-app-6379 #impala后端定义 bind 0.0.0.0:6379 #ha做为proxy所绑定的IP和端口 mode tcp #以4层方式代理,重要 balance roundrobin #调度算法 'leastconn' 最少链接数分配,或者 'roundrobin',轮询分配 maxconn 8092 #最大链接数 # server bj01-pro-redis-621 10.168.6.21:6380 check inter 180000 rise 1 fall 2 server bj01-pro-redis-620 10.168.6.20:6379 check inter 180000 rise 1 fall 2 # server bj01-pro-redis-619 10.168.6.19:6380 check inter 180000 rise 1 fall 2 listen zoo-app-2181 bind 0.0.0.0:2181 mode tcp balance roundrobin maxconn 8092 server 10.168.30.13 10.168.30.13:2181 check inter 180000 rise 1 fall 2 server 10.168.30.14 10.168.30.14:2181 check inter 180000 rise 1 fall 2 server 10.168.30.15 10.168.30.15:2181 check inter 180000 rise 1 fall 2 listen duobao-interface-8080 bind 0.0.0.0:8080 mode tcp balance roundrobin maxconn 8092 server 10.168.6.13 10.168.6.13:8153 check inter 180000 rise 1 fall 2 server 10.168.6.14 10.168.6.14:8153 check inter 180000 rise 1 fall 2 server 10.168.6.15 10.168.6.15:8153 check inter 180000 rise 1 fall 2 server 10.168.6.16 10.168.6.16:8153 check inter 180000 rise 1 fall 2 server 10.168.6.17 10.168.6.17:8153 check inter 180000 rise 1 fall 2 server 10.168.6.18 10.168.6.18:8153 check inter 180000 rise 1 fall 2 listen ops-es-9200 bind 0.0.0.0:9200 mode tcp balance roundrobin maxconn 8092 server 10.168.2.100 10.168.2.100:9200 check inter 180000 rise 1 fall 2 server 10.168.2.101 10.168.2.101:9200 check inter 180000 rise 1 fall 2 server 10.168.2.102 10.168.2.102:9200 check inter 180000 rise 1 fall 2 listen ops-es-9300 bind 0.0.0.0:9300 mode tcp balance roundrobin maxconn 8092 server 10.168.2.100 10.168.2.100:9300 check inter 180000 rise 1 fall 2 server 10.168.2.101 10.168.2.101:9300 check inter 180000 rise 1 fall 2 server 10.168.2.102 10.168.2.102:9300 check inter 180000 rise 1 fall 2
keepalivedredis
global_defs { notification_email { user@example.com } notification_email_from wangqiankun@lashou-inc.com smtp_server wangqiankun@lashou-inc.com smtp_connect_timeout 30 router_id LVS_DEVEL } #监测haproxy进程状态,每2秒执行一次 vrrp_script chk_haproxy { script "/usr/local/keepalived/chk_haproxy.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER interface em1 virtual_router_id 52 priority 101 #MASTER权重要高于BACKUP advert_int 1 mcast_src_ip 10.168.5.37 #Master服务器IP authentication { auth_type PASS #主从服务器验证方式 auth_pass lashouroot } track_script { chk_haproxy #监测haproxy进程状态 } #VIP virtual_ipaddress { 10.168.5.116 10.168.5.117 10.168.5.118 ####此处的虚ip均可以用,设置多个是为了区分业务 10.168.5.119 } }
chk_haproxy.sh算法
#!/bin/bash # # author: weizhifeng # description: # 定时查看haproxy是否存在,若是不存在则启动haproxy, # 若是启动失败,则中止keepalived # status=`ps aux|grep haproxy | grep -v grep | grep -v bash | wc -l` if [ ${status} = "0" ]; then /etc/init.d/haproxy start status2=`ps aux|grep haproxy | grep -v grep | grep -v bash |wc -l` if [ ${status2} = "0" ]; then /etc/init.d/keepalived stop fi fi