1.安裝配置keepalivedbash
修改配置文件/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腳本內容frontend
#!/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.cfgsocket
刪除frontend,backend塊的內容,而後添加以下內容tcp
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上面spa
listen server VIP:PORT 若是遇到本機沒有VIP,不能啓動haproxy 修改/etc/sysctl.conf文件 net.ipv4.ip_nonlocal_bind = 1 sysctl -p
4.開啓haproxy日誌rest
修改/etc/rsyslog.conf 打開下面兩行的註釋 $ModLoad imudp $UDPServerRun 514 添加 local2.* /var/log/haproxy.log 重啓/etc/init.d/rsyslog restart