keepalived+haproxy 安装配置

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
复制代码

 

posted @ 2020-02-27 18:39  vilenx  阅读(439)  评论(0编辑  收藏  举报