准备三台Linux服务器,两台做Web服务器,并部署Keepalived高可用软件,一台作为客户端主机

    (关闭SElinux、防火墙)

一、配置网络环境

    [root@web53 ~]# yum -y install keepalived

    [root@web54 ~]# yum -y install keepalived

    lm_sensors-libs net-snmp-libs                                                  依赖包

    [root@web53 ~]# rpm -qc keepalived

    /etc/keepalived/keepalived.conf

    /etc/sysconfig/keepalived

    [root@web53 ~]# cp /etc/keepalived/keepalived.conf /root/                最好备份一份

1.1编写网络文件

     [root@web53 ~]#echo '192.168.4.53' > /var/www/html/test.html  

     [root@web54 ~]#echo '192.168.4.54' > /var/www/html/test.html


    2、修改配置文件

     [root@web53 ~]#vim /etc/keepalived/keepalived.conf

     global_defs {

      4    notification_email {                                 工作邮箱

     15 vrrp_instance webha {

     16     state MASTER                         主服务器

     17     interface eth0                                                  网卡

     18     virtual_router_id 51

     19     priority 150                                                       优先级

     20     advert_int 1

     21     authentication {

     22         auth_type PASS

     23         auth_pass 123456                                      主从密码一致

     24     }

     25     virtual_ipaddress {                                    vip地址

     26         192.168.4.251

     27     }

     28 }

    

 [root@web54 ~]#vim /etc/keepalived/keepalived.conf

     15 vrrp_instance webha {

     16     state BACKUP                             从服务器

     17     interface eth0

     18     virtual_router_id 51

     19     priority 100                                        

     20     advert_int 1

     21     authentication {

     22         auth_type PASS

     23         auth_pass 123456

     24     }

     25     virtual_ipaddress {                                        vip地址

     26         192.168.4.251

   

  3、开启服务并验证(53 54)

    [root@web53 ~]# ip addr show                                                          查看本机的虚拟ip

    [root@web53 ~]# service keepalived start

    [root@web53 ~]# ip addr show | grep 192.168.4.*                   优先级高的先获取虚拟ip

        inet 192.168.4.53/24 brd 192.168.4.255 scope global eth0

        inet 192.168.4.251/32 scope global eth0

    [root@stu4 ~]# elinks --dump http://192.168.4.251/test.html          显示53的网页内容

      ‘192.168.4.53’

    

    [root@web53 ~]# service keepalived stop                           关闭53  虚拟ip会被54抢走

    [root@web54 ~]# ip addr show | grep 192.168.4.*

        inet 192.168.4.54/24 brd 192.168.4.255 scope global eth0

        inet 192.168.4.251/32 scope global eth0

    [root@stu4 ~]# elinks --dump http://192.168.4.251/test.html           在访问就会显示54

       ‘192.168.4.54’