global_defs {

   router_id TENGINE_HA_MASTER

}


#vrrp_script check_run {

#    script "/root/bin/nginx_check.sh"

#    interval 5

#}


vrrp_instance V_1 {

    state BACKUP


    interface eth0

    virtual_router_id 55

    priority 90

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass caikR2015

    }


#   track_script {

#       check_run

#   }    

   

    virtual_ipaddress {

        172.31.100.55

    }

#   notify_master "script" 

#   变为master后执行脚本

#   notify_backup "script"

#   变为backup后执行脚本

}

#########################################################

global_defs {

   router_id TENGINE_HA_BACKUP

}


vrrp_instance V_1 {

    state BACKUP

    nopreempt

    ####################

    interface eth0

    virtual_router_id 55

    priority 150

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass caikR2015

    }

    virtual_ipaddress {

        172.31.100.55

    }

}




#######################################################################################

1,在主备机器上安装keepalived,
步骤如下:下载keepalived-1.1.15.tar.gz,然后解压安装
#tar zxvf keepalived-1.1.15.tar.gz
#cd keepalived-1.1.15
#./configure
#make
#make install
2,配置keepalived
配置中的state MASTER决定了节点为主节点
priority决定了优先级,比如在有多个备用节点的时候,主节点故障后优先级值大的接管。
主节点的配置如下:

global_defs {

    router_id NodeA

}

vrrp_instance VI_1 {

    state MASTER    #设置为主服务器

    interface eth0  #监测网络接口

    virtual_router_id 51  #主、备必须一样

    priority 100   #(主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高)

    advert_int 1   #VRRP Multicast广播周期秒数

    authentication {

auth_type PASS  #VRRP认证方式,主备必须一致

auth_pass 1111   #(密码)

}

virtual_ipaddress {

192.168.8.100/24  #VRRP HA虚拟地址

}

备用节点的配置如下:


global_defs {

    router_id NodeB

}

vrrp_instance VI_1 {

    state BACKUP    #设置为主服务器

    interface eth0  #监测网络接口

    virtual_router_id 51  #主、备必须一样

priority 90   #(主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高)

    advert_int 1   #VRRP Multicast广播周期秒数

    authentication {

auth_type PASS  #VRRP认证方式,主备必须一致

auth_pass 1111   #(密码)

}

virtual_ipaddress {

192.168.8.100/24  #VRRP HA虚拟地址

}


3,启动keepalived:
keepalived -D -f /usr/local/etc/keepalived/keepalived.conf
查看log消息:
tail -f /var/log/messages
启动主节点A后的日志为:会广播ARP消息:

[root@srv4 ~]# tail -f /var/log/messages

Sep 20 01:45:29 srv4 Keepalived_vrrp: Configuration is using : 34546 Bytes

Sep 20 01:45:29 srv4 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(8,9)]

Sep 20 01:45:30 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE

Sep 20 01:45:31 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE

Sep 20 01:45:31 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.

Sep 20 01:45:31 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.8.100

Sep 20 01:45:31 srv4 Keepalived_vrrp: Netlink reflector reports IP 192.168.8.100 added

Sep 20 01:45:31 srv4 Keepalived_healthcheckers: Netlink reflector reports IP 192.168.8.100 added

Sep 20 01:45:31 srv4 avahi-daemon[4029]: Registering new address record for 192.168.8.100 on eth0.

Sep 20 01:45:36 srv4 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.8.100

通过ip a 命令可以看到192.168.8.100/24绑定到了eth0上

[root@srv4 bin]# ip a

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

    inet6 ::1/128 scope host 

       valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000

    link/ether 00:0c:29:50:2d:9d brd ff:ff:ff:ff:ff:ff

    inet 192.168.8.4/24 brd 192.168.8.255 scope global eth0

    inet 192.168.8.100/24 scope global secondary eth0

    inet6 fe80::20c:29ff:fe50:2d9d/64 scope link 

       valid_lft forever preferred_lft forever

启动备用节点B后的日志为:

Sep 20 01:47:31 hadoopsrv Keepalived_vrrp: Configuration is using : 34262 Bytes

Sep 20 01:47:31 hadoopsrv Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE

Sep 20 01:47:31 hadoopsrv Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]

Sep 20 01:47:31 hadoopsrv Keepalived: Starting VRRP child process, pid=20567