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