安装环境
[root@node1 ~]# cat /etc/redhat-release
CentOS Linux release 7.1. (Core)
[root@node1 ~]# cat /proc/version
Linux version 3.10.-.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8. (Red Hat 4.8.-) (GCC) ) # SMP Fri Mar :: UTC
主机IP
10.0.0.20 MASTER
10.0.0.21 BACKUP
安装
[root@node1 ~]# yum -y install keepalived
查看版本
[root@node1 ~]# keepalived -v
配置 10.0.0.20

[root@node1 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id WEB #集群名称,相同集群名称必须相同
} vrrp_instance VI_1 {
state MASTER #主从设置,这里设置为主
interface eth0
virtual_router_id 51
priority 150 #定义访问优先级,数值越大优先级越高
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.30 #置虚拟IP,需在相同网段
}
}

配置 10.0.0.21

[root@node4 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id WEB #集群名称,必须和web01设置相同
} vrrp_instance VI_1 {
state BACKUP #主从设置,这里设置为从
interface eth0
virtual_router_id 51
priority 100 #定义访问优先级,从要比主的数值小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.30 #设置虚拟IP,必须相同
}
}

重启服务查看10.0.0.20网卡上是否有 10.0.0.30 IP
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:e6:36:7b brd ff:ff:ff:ff:ff:ff
inet 10.0.0.22/24 brd 10.0.0.255 scope global eth0
valid_lft forever preferred_lft forever
inet 10.0.0.30/32 scope global eth0
而此是10.0.0.21的网卡上是没有10.0.0.30 IP
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:15:ba:84 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.23/24 brd 10.0.0.255 scope global eth0
valid_lft forever preferred_lft forever
可以关闭10.0.0.20 的keepalived 服务,查看IP是否转移到了10.0.0.21上
10.0.0.20 上已经没有了10.0.0.30

[root@node1 ~]# systemctl stop keepalived 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:e6:36:7b brd ff:ff:ff:ff:ff:ff
inet 10.0.0.22/24 brd 10.0.0.255 scope global eth0
valid_lft forever preferred_lft forever

查看10.0.0.21

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:15:ba:84 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.23/24 brd 10.0.0.255 scope global eth0
valid_lft forever preferred_lft forever
inet 10.0.0.30/32 scope global eth0
valid_lft forever preferred_lft forever

此时10.0.0.30 已经转移到了 10.0.0.21上