在192.168.1.10和192.168.1.20分别安装:
#yum –y install keepalived
192.168.1.10的配置文件内容:
#vim keepalived.conf
! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "pidof nginx " interval 1 weight -3 fall 2 rise 1 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 } track_script { chk_nginx } notify_master "/etc/keepalived/notify.sh master " notify_backup "/etc/keepalived/notify.sh backup " notify_fault "/etc/keepalived/notify.sh fault " } |
192.168.1.20的配置文件内容:
#vim keepalived.conf
! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "pidof nginx " interval 1 weight -1 fall 2 rise 1 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 } track_script { chk_nginx } notify_master "/etc/keepalived/notify.sh master " notify_backup "/etc/keepalived/notify.sh backup " notify_fault "/etc/keepalived/notify.sh fault " } |
主备服务器上的脚本内容:
#vim /etc/keepalived/notify.sh notify.sh
#!/bin/bash case "$1" in master) /etc/init.d/nginx start exit 0 ;; backup) /etc/init.d/nginx stop exit 0 ;; fault) /etc/init.d/nginx stop exit 0 ;; *) echo ‘Usage: `basename $0` {master|backup|fault}‘ exit 1 ;; esac |
主备服务器上LVS配置内容:
virtual_server 192.168.1.100 80 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 0 protocol TCP real_server 192.168.1.30 80 { weight 1 HTTP_GET { url { path /demo.php status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.1.40 80 { weight 1 HTTP_GET { url { path /demo.php status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } } |
在nginx1和nginx2上分别运行一下命令:
ip addr add 192.168.1.100/32 label lo:0 brd 192.168.1.100 dev lo
echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/eth0/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce