keepalived+haproxy 安装配置

Posted vilenx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了keepalived+haproxy 安装配置相关的知识,希望对你有一定的参考价值。

1.安装配置keepalived

技术图片
修改配置文件/etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
      router_id Node_A (另一台Node_B)
}

#vrrp_script check_haproxy {
#    script "/etc/keepalived/check_haproxy.sh"  (如果启用脚本则打开注释)
#    interval 2
#    weight 2
#}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 100 (另一台改为50)
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 密码(自定义)
    }

#    track_script {
#        check_haproxy
#    }

    virtual_ipaddress {
        VIP/掩码 dev eth0 label eth0:0
    }
}
技术图片

check_haproxy.sh脚本内容

技术图片
#!/bin/bash

/etc/init.d/haproxy status &> /dev/null || /etc/init.d/haproxy restart &> /dev/null

if [ $? -ne 0 ]; then
  /etc/init.d/keepalived stop &> /dev/null
fi
技术图片

 

2.安装haproxy,修改配置/etc/haproxy/haproxy.cfg

删除frontend,backend块的内容,然后添加如下内容

技术图片
listen server (自定义名称)
    bind *:PORT(端口)
    mode tcp
    timeout connect         30s
    timeout client          30s
    timeout server          180s
    timeout check           180s
    no option dontlog-normal
    balance leastconn
    option socket-stats
    option tcplog
    option tcp-check
    tcp-check connect
    server rs1(自定义名称) IP:PORT weight 1 check inter 6000 rise 2 fall 3
    server rs2(自定义名称) IP:PORT weight 1 check inter 6000 rise 2 fall 3
技术图片

3.如果haproxy要配置监听在具体VIP上面

技术图片
listen server VIP:PORT

如果遇到本机没有VIP,不能启动haproxy
修改/etc/sysctl.conf文件
net.ipv4.ip_nonlocal_bind = 1

sysctl -p
技术图片

 4.开启haproxy日志

技术图片
修改/etc/rsyslog.conf

打开下面两行的注释
$ModLoad imudp
$UDPServerRun 514

添加
local2.*       /var/log/haproxy.log

重启/etc/init.d/rsyslog restart
技术图片

 

以上是关于keepalived+haproxy 安装配置的主要内容,如果未能解决你的问题,请参考以下文章

haproxy+keepalived安装配置

HaProxy+Keepalived+Mycat高可用群集配置

HaProxy+Keepalived+Mycat高可用群集配置

安装配置Keepalived

keepalived+haproxy安装配置

HAproxy+keepalived+nginx实验部署