环境:

192.168.36.7:80
192.168.36.17:80
192.168.36.18:80
三台web服务器已经搭好
]# yum install -y httpd
]# echo `hostname` > /var/www/html/index.html
]# systemctl start httpd

配置HAProxy,两台主机一样的配置

]# vim /etc/haproxy/haproxy.cfg
frontend web *:80
    default_backend websrvs
backend websrvs
    balance roundrobin
    server srv1 192.168.36.7:80 check
    server srv2 192.168.36.17:80 check
 配置keepalived实现高可用,一台为MASTER一台为BACKUP.
]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
     root@localhost
   }
   notification_email_from keepalived@localhoat
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id node1
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   vrrp_mcast_group4 224.0.111.111
   vrrp_iptables
}
vrrp_script chk_haproxy {
        script "killall -0 haproxy"  #监控haproxy进程
        interval 1
        weight -5
        fall 2
        rise 1
}

vrrp_script chk_down {
    script "/bin/bash -c '[[ -f /etc/keepalived/down ]]' && exit 1 || exit 0"  #在keepalived中要特别地指明作为bash的参数的运行
    interval 1
    weight -10
}

vrrp_instance VI_1 {
    state MASTER            #一台为BACKUP
    interface eth0
    virtual_router_id 51
    priority 100            #当为BACKUP时,priority适当减少,建议95。
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass fd57721a
    }
    virtual_ipaddress {
        192.168.36.2/24 dev eth0
    }
    track_script {  #调用监控脚本
            chk_haproxy
            chk_down
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}

测试:创建down文件后使得降优先级,从而使得VIP漂移到node2,进入维护模式
]# touch /etc/keepalived/down