CentOS 6.5 下keepalived服务的配置
参考网站:
http://zhangxugg-163-com.iteye.com/blog/1665419
http://www.2cto.com/os/201412/362685.html
http://outofmemory.cn/wiki/keepalived-configuration
本文使用两台虚拟机进行Keepalived的配置,其中主Keepalived的Ip地址为192.168.178.2,
备Keepalived的Ip地址为192.168.178.3.
1 Keepalived工作原理
keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于heartbeat,用来防止单点故障。
keepalived工作原理
keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗余协议。
虚拟路由冗余协议,可以认为是实现路由器高可用的协议,即将N台提供相同功能的路由器组成一个路由器组,这个组里面有一个master和多个backup,master上面有一个对外提供服务的vip(该路由器所在局域网内其他机器的默认路由为该vip),master会发组播,当backup收不到vrrp包时就认为master宕掉了,这时就需要根据VRRP的优先级来选举一个backup当master。这样的话就可以保证路由器的高可用了。
keepalived主要有三个模块,分别是core、check和vrrp。core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。check负责健康检查,包括常见的各种检查方式。vrrp模块是来实现VRRP协议的。
2 Keepalived的配置文件说明
keepalived只有一个配置文件keepalived.conf,里面主要包括以下几个配置区域,分别是global_defs、static_ipaddress、static_routes、vrrp_script、vrrp_instance和virtual_server。
global_defs区域
主要是配置故障发生时的通知对象以及机器标识
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 DodeB
}
notification_email 故障发生时给谁发邮件通知。
notification_email_from 通知邮件从哪个地址发出。
smpt_server 通知邮件的smtp地址。
smtp_connect_timeout 连接smtp服务器的超时时间。
enable_traps 开启SNMP陷阱(Simple Network Management Protocol)。
router_id 标识本节点的字条串,通常为hostname,但不一定非得是hostname。故障发生时,邮件通知会用到。
static_ipaddress和static_routes区域
static_ipaddress和static_routes区域配置的是是本节点的IP和路由信息。如果你的机器上已经配置了IP和路由,那么这两个区域可以不用配置。一般情况下你的机器都会有IP地址和路由信息。
static_ipaddress {
10.210.214.163/24 brd 10.210.214.255 dev eth0
...
}
static_routes {
10.0.0.0/8 via 10.210.214.1 dev eth0
...
}
以上分别表示启动/关闭keepalived时在本机执行的如下命令:
# /sbin/ip addr add 10.210.214.163/24 brd 10.210.214.255 dev eth0
# /sbin/ip route add 10.0.0.0/8 via 10.210.214.1 dev eth0
# /sbin/ip addr del 10.210.214.163/24 brd 10.210.214.255 dev eth0
# /sbin/ip route del 10.0.0.0/8 via 10.210.214.1 dev eth0
注意: 请忽略这两个区域,因为你的机器肯定已经配置了IP和路由。
vrrp_script区域
用来做健康检查的,当时检查失败时会将vrrp_instance的priority减少相应的值。
vrrp_script chk_http_port {
script "</dev/tcp/127.0.0.1/80"
interval 1
weight -10
}
以上意思是如果script中的指令执行失败,那么相应的vrrp_instance的优先级会减少10个点。
vrrp_instance和vrrp_sync_group区域
vrrp_instance用来定义对外提供服务的VIP区域及其相关属性。
vrrp_rsync_group用来定义vrrp_intance组,使得这个组内成员动作一致。举个例子来说明一下其功能:
两个vrrp_instance同属于一个vrrp_rsync_group,那么其中一个vrrp_instance发生故障切换时,另一个vrrp_instance也会跟着切换(即使这个instance没有发生故障)。
vrrp_sync_group VG_1 {
group {
inside_network # name of vrrp_instance (below)
outside_network # One for each moveable IP.
...
}
notify_master /path/to_master.sh
notify_backup /path/to_backup.sh
notify_fault "/path/fault.sh VG_1"
notify /path/notify.sh
smtp_alert
}
vrrp_instance VI_1 {
state MASTER
interface eth0
use_vmac <VMAC_INTERFACE>
dont_track_primary
track_interface {
eth0
eth1
}
mcast_src_ip <IPADDR>
lvs_sync_daemon_interface eth1
garp_master_delay 10
virtual_router_id 1
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 12345678
}
virtual_ipaddress {
10.210.214.253/24 brd 10.210.214.255 dev eth0
192.168.1.11/24 brd 192.168.1.255 dev eth1
}
virtual_routes {
172.16.0.0/12 via 10.210.214.1
192.168.1.0/24 via 192.168.1.1 dev eth1
default via 202.102.152.1
}
track_script {
chk_http_port
}
nopreempt
preempt_delay 300
debug
notify_master <STRING>|<QUOTED-STRING>
notify_backup <STRING>|<QUOTED-STRING>
notify_fault <STRING>|<QUOTED-STRING>
notify <STRING>|<QUOTED-STRING>
smtp_alert
}
notify_master/backup/fault 分别表示切换为主/备/出错时所执行的脚本。
notify 表示任何一状态切换时都会调用该脚本,并且该脚本在以上三个脚本执行完成之后进行调用,keepalived会自动传递三个参数($1 = "GROUP"|"INSTANCE",$2 = name of group or instance,$3 = target state of transition(MASTER/BACKUP/FAULT))。
smtp_alert 表示是否开启邮件通知(用全局区域的邮件设置来发通知)。
state 可以是MASTER或BACKUP,不过当其他节点keepalived启动时会将priority比较大的节点选举为MASTER,因此该项其实没有实质用途。
interface 节点固有IP(非VIP)的网卡,用来发VRRP包。
use_vmac 是否使用VRRP的虚拟MAC地址。
dont_track_primary 忽略VRRP网卡错误。(默认未设置)
track_interface 监控以下网卡,如果任何一个不通就会切换到FALT状态。(可选项)
mcast_src_ip 修改vrrp组播包的源地址,默认源地址为master的IP。(由于是组播,因此即使修改了源地址,该master还是能收到回应的)
lvs_sync_daemon_interface 绑定lvs syncd的网卡。
garp_master_delay 当切为主状态后多久更新ARP缓存,默认5秒。
virtual_router_id 取值在0-255之间,用来区分多个instance的VRRP组播。
注意: 同一网段中virtual_router_id的值不能重复,否则会出错,相关错误信息如下。
Keepalived_vrrp[27120]: ip address associated with VRID not present in received packet :
one or more VIP associated with VRID mismatch actual MASTER advert
bogus VRRP packet received on eth1 !!!
receive an invalid ip number count associated with VRID!
VRRP_Instance(xxx) ignoring received advertisment...
可以用这条命令来查看该网络中所存在的vrid:tcpdump -nn -i any net 224.0.0.0/8
priority 用来选举master的,要成为master,那么这个选项的值最好高于其他机器50个点,该项取值范围是1-255(在此范围之外会被识别成默认值100)。
advert_int 发VRRP包的时间间隔,即多久进行一次master选举(可以认为是健康查检时间间隔)。
authentication 认证区域,认证类型有PASS和HA(IPSEC),推荐使用PASS(密码只识别前8位)。
virtual_ipaddress vip,不解释了。
virtual_routes 虚拟路由,当IP漂过来之后需要添加的路由信息。
virtual_ipaddress_excluded 发送的VRRP包里不包含的IP地址,为减少回应VRRP包的个数。在网卡上绑定的IP地址比较多的时候用。
nopreempt 允许一个priority比较低的节点作为master,即使有priority更高的节点启动。
首先nopreemt必须在state为BACKUP的节点上才生效(因为是BACKUP节点决定是否来成为MASTER的),其次要实现类似于关闭auto failback的功能需要将所有节点的state都设置为BACKUP,或者将master节点的priority设置的比BACKUP低。我个人推荐使用将所有节点的state都设置成BACKUP并且都加上nopreempt选项,这样就完成了关于autofailback功能,当想手动将某节点切换为MASTER时只需去掉该节点的nopreempt选项并且将priority改的比其他节点大,然后重新加载配置文件即可(等MASTER切过来之后再将配置文件改回去再reload一下)。
当使用track_script时可以不用加nopreempt,只需要加上preempt_delay 5,这里的间隔时间要大于vrrp_script中定义的时长。
preempt_delay master启动多久之后进行接管资源(VIP/Route信息等),并提是没有nopreempt选项。
virtual_server_group和virtual_server区域
virtual_server_group一般在超大型的LVS中用到,一般LVS用不到这东西,因此不多说。
virtual_server IP Port {
delay_loop <INT>
lb_algo rr|wrr|lc|wlc|lblc|sh|dh
lb_kind NAT|DR|TUN
persistence_timeout <INT>
persistence_granularity <NETMASK>
protocol TCP
ha_suspend
virtualhost <STRING>
alpha
omega
quorum <INT>
hysteresis <INT>
quorum_up <STRING>|<QUOTED-STRING>
quorum_down <STRING>|<QUOTED-STRING>
sorry_server <IPADDR> <PORT>
real_server <IPADDR> <PORT> {
weight <INT>
inhibit_on_failure
notify_up <STRING>|<QUOTED-STRING>
notify_down <STRING>|<QUOTED-STRING>
# HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK
HTTP_GET|SSL_GET {
url {
path <STRING>
# Digest computed with genhash
digest <STRING>
status_code <INT>
}
connect_port <PORT>
connect_timeout <INT>
nb_get_retry <INT>
delay_before_retry <INT>
}
}
}
delay_loop 延迟轮询时间(单位秒)。
lb_algo 后端调试算法(load balancing algorithm)。
virtualhost 用来给HTTP_GET和SSL_GET配置请求header的。
sorry_server 当所有real server宕掉时,sorry server顶替。
real_server 真正提供服务的服务器。
weight 权重。
notify_up/down 当real server宕掉或启动时执行的脚本。
健康检查的方式,N多种方式。
path 请求real serserver上的路径。
digest/status_code 分别表示用genhash算出的结果和http状态码。
connect_port 健康检查,如果端口通则认为服务器正常。
connect_timeout,nb_get_retry,delay_before_retry分别表示超时时长、重试次数,下次重试的时间延迟。
3 Keepalived的安装
[root@xldwhj]# cd /usr/local/src
[root@xldwhj]#wget http://www.keepalived.org/software/keepalived-1.2.6.tar.gz
[root@xldwhj]#tar zxf keepalived-1.2.6.tar.gz
[root@xldwhj]#cd keepalived-1.2.6
[root@xldwhj]#./configure --prefix=/usr/local/keepalived
[root@xldwhj]#make
[root@xldwhj]#make install
上述步骤中,可能为报错,一般为缺少安装keepalived所需要的依赖包,根据提示安装缺少的依赖包,自重新编译即可。
安装完成后,还应进行如下操作:
1. 建立服务启动脚本,以便使用service命令控制之
[root@xldwhj]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
[root@xldwhj]# chmod +x /etc/init.d/keepalived
因为我们使用非默认路径(/usr/local)安装keepalived, 故需要修改几处路径,以保证keepalived能正常启动, 需要修改的文件如下:
2. 修改/etc/init.d/keepalived, 将. /etc/sysconfig/keepalived, 修改为:
. /usr/local/keepalived/etc/sysconfig/keepalived, 即指向正确的文件位置
同时在上述行下添加以下内容(将keepavlied主程序所在路径导入到环境变量PATH中):
PATH="$PATH:/usr/local/keepalived/sbin"
export PATH
3. 修改/usr/local/keepalived/etc/sysconfig/keepalived文件,设置正确的服务启动参数
KEEPALIVED_OPTIONS="-D -f /usr/local/keepalived/etc/keepalived/keepalived.conf"
4. 经过以上修改,keepalived基本安装即可完成,启动测试之:
[root@xldwhj]#service keepalived restart
5. 切勿忘记将此服务设置为开机启动
[root@xldwhj]#chkconfig keepalived on
4 Keepalived主备配置
在这种模式下,虚拟IP在某时刻只能属于某一个节点,另一个节点作为备用节点存在。当主点不可用时,备用节点接管虚拟IP,提供正常服务。
节点A的Ip为192.168.178.2(主节点),节点B的Ip为192.168.178.3(备用节点)。
节点A上的配置文件/usr/local/keepalived/etc/keepalived/keepalived.conf主要修改如下:
global_defs {
notification_email {
root@localhost
}
notification_email_from root@local host
smtp_server localhost
smtp_connect_timeout 30
router_id NodeA
}
vrrp_instance VI_1 {
state MASTER #指定A节点为主节点 备用节点上设置为BACKUP即可
interface eth0 #绑定虚拟IP的网络接口
virtual_router_id 51 #VRRP组名,两个节点的设置必须一样,以指明各个节点属于同一VRRP组
priority 100 #主节点的优先级(1-254之间),备用节点必须比主节点优先级低
advert_int 1 #组播信息发送间隔,两个节点设置必须一样
authentication { #设置验证信息,两个节点必须一致
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #指定虚拟IP, 两个节点设置必须一样
192.168.200.16/24
192.168.200.17 /24
192.168.200.18 /24
}
}
按同样的方法配置节点B并修改配置文件,可将A节点的配置文件复制到B节点,并修改以下几项:
router_id NodeB
state BACKUP
priority 99
4.1 Keepalived主备配置测试
主Keepalived命令
[root@xldwhj ~]# service keepalived start
Starting keepalived: [ OK ]
[root@xldwhj ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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 state UP qlen 1000
link/ether 00:0c:29:ef:76:a8 brd ff:ff:ff:ff:ff:ff
inet 192.168.178.2/24 brd 192.168.178.255 scope global eth0
inet 192.168.200.16/32 scope global eth0
inet 192.168.200.17/32 scope global eth0
inet 192.168.200.18/32 scope global eth0
inet6 fe80::20c:29ff:feef:76a8/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:ef:76:b2 brd ff:ff:ff:ff:ff:ff
inet 192.168.254.129/24 brd 192.168.254.255 scope global eth1
inet6 fe80::20c:29ff:feef:76b2/64 scope link
valid_lft forever preferred_lft forever
可以在主Keepalived看到16,17,18的虚拟IP。
备Keepalived命令
[root@xldmysql ~]# service keepalived start
Starting keepalived: [ OK ]
[root@xldmysql ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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 state UP qlen 1000
link/ether 00:0c:29:53:74:85 brd ff:ff:ff:ff:ff:ff
inet 192.168.178.3/24 brd 192.168.178.255 scope global eth0
inet6 fe80::20c:29ff:fe53:7485/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:53:74:8f brd ff:ff:ff:ff:ff:ff
inet 192.168.254.132/24 brd 192.168.254.255 scope global eth1
inet6 fe80::20c:29ff:fe53:748f/64 scope link
valid_lft forever preferred_lft forever
并没有出现16,17,18的虚拟IP
主Keepalived命令
停止Keepalived服务
[root@xldwhj ~]# service keepalived stop
Stopping keepalived: [ OK ]
[root@xldwhj ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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 state UP qlen 1000
link/ether 00:0c:29:ef:76:a8 brd ff:ff:ff:ff:ff:ff
inet 192.168.178.2/24 brd 192.168.178.255 scope global eth0
inet6 fe80::20c:29ff:feef:76a8/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:ef:76:b2 brd ff:ff:ff:ff:ff:ff
inet 192.168.254.129/24 brd 192.168.254.255 scope global eth1
inet6 fe80::20c:29ff:feef:76b2/64 scope link
valid_lft forever preferred_lft forever
16,17,18的虚拟IP已不存在
备Keepalived命令
[root@xldmysql~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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 state UP qlen 1000
link/ether 00:0c:29:53:74:85 brd ff:ff:ff:ff:ff:ff
inet 192.168.178.3/24 brd 192.168.178.255 scope global eth0
inet 192.168.200.16/32 scope global eth0
inet 192.168.200.17/32 scope global eth0
inet 192.168.200.18/32 scope global eth0
inet6 fe80::20c:29ff:fe53:7485/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:53:74:8f brd ff:ff:ff:ff:ff:ff
inet 192.168.254.132/24 brd 192.168.254.255 scope global eth1
inet6 fe80::20c:29ff:fe53:748f/64 scope link
valid_lft forever preferred_lft forever
关闭主服务上的keepalived服务,观察vip是否会漂移到备用服务,由上述查询结果可知此时在备用服务器上查看到了漂移的vip地址。
此时启动主keepalived服务,vip会自动漂移到主服务器。
由上可知,keepalived主备服务配置成功。
5 Keepalived主主配置
两个节点的配置应该是交叉的,对同个虚拟IP,应该交叉互为主备。
对节点A(192.168.178.2)的关键配置如下:
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51 #本机两个vrrp_instance组的此值不能相同,但对应备用节点的此值必须相同
priority 100 #对应备用节点值应该比此值小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.16
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52 #本机两个vrrp_instance组的此值不能相同,但对应主节点的此值必须相同
priority 90 #主节点的值应该比此值大
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.17
}
}
节点B(192.168.178.3)的关键配置如下:
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.16
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.17
}
}
5.1 Keepalived主主配置测试
从上面的配置文件中可以看出,实际上是增加了一个vrrp实例
测试过程同Keepalived,但是结果可能不同。
主Keepalived命令如下:
重新启动keepalived服务
[root@xldwhj ~]# service keepalived restart
Stopping keepalived: [ OK ]
Starting keepalived: [ OK ]
[root@xldwhj ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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 state UP qlen 1000
link/ether 00:0c:29:ef:76:a8 brd ff:ff:ff:ff:ff:ff
inet 192.168.178.2/24 brd 192.168.178.255 scope global eth0
inet 192.168.200.16/32 scope global eth0
inet6 fe80::20c:29ff:feef:76a8/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:ef:76:b2 brd ff:ff:ff:ff:ff:ff
inet 192.168.254.129/24 brd 192.168.254.255 scope global eth1
inet6 fe80::20c:29ff:feef:76b2/64 scope link
valid_lft forever preferred_lft forever
可以看出,虚拟Ip192.168.200.16存在
备Keepalived命令如下:
[root@xldmysql ~]# service keepalived restart
Stopping keepalived: [ OK ]
Starting keepalived: [ OK ]
[root@xldmysql html]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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 state UP qlen 1000
link/ether 00:0c:29:53:74:85 brd ff:ff:ff:ff:ff:ff
inet 192.168.178.3/24 brd 192.168.178.255 scope global eth0
inet 192.168.200.17/32 scope global eth0
inet6 fe80::20c:29ff:fe53:7485/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:53:74:8f brd ff:ff:ff:ff:ff:ff
inet 192.168.254.132/24 brd 192.168.254.255 scope global eth1
inet6 fe80::20c:29ff:fe53:748f/64 scope link
valid_lft forever preferred_lft forever
可以看出,虚拟Ip192.168.200.17存在
主Keepalived命令如下:
[root@xldwhj ~]# service keepalived stop
Stopping keepalived: [ OK ]
[root@xldwhj ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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 state UP qlen 1000
link/ether 00:0c:29:ef:76:a8 brd ff:ff:ff:ff:ff:ff
inet 192.168.178.2/24 brd 192.168.178.255 scope global eth0
inet6 fe80::20c:29ff:feef:76a8/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:ef:76:b2 brd ff:ff:ff:ff:ff:ff
inet 192.168.254.129/24 brd 192.168.254.255 scope global eth1
inet6 fe80::20c:29ff:feef:76b2/64 scope link
valid_lft forever preferred_lft forever
虚拟Ip192.168.200.16已不存在。
备Keepalived命令如下:
[root@xldmysql ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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 state UP qlen 1000
link/ether 00:0c:29:53:74:85 brd ff:ff:ff:ff:ff:ff
inet 192.168.178.3/24 brd 192.168.178.255 scope global eth0
inet 192.168.200.17/32 scope global eth0
inet 192.168.200.16/32 scope global eth0
inet6 fe80::20c:29ff:fe53:7485/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:53:74:8f brd ff:ff:ff:ff:ff:ff
inet 192.168.254.132/24 brd 192.168.254.255 scope global eth1
inet6 fe80::20c:29ff:fe53:748f/64 scope link
valid_lft forever preferred_lft forever
虚拟Ip192.168.200.16自动漂移到备服务器。
[root@xldmysql ~]# service keepalived stop
Stopping keepalived: [ OK ]
You have new mail in /var/spool/mail/root
[root@xldmysql ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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 state UP qlen 1000
link/ether 00:0c:29:53:74:85 brd ff:ff:ff:ff:ff:ff
inet 192.168.178.3/24 brd 192.168.178.255 scope global eth0
inet6 fe80::20c:29ff:fe53:7485/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:53:74:8f brd ff:ff:ff:ff:ff:ff
inet 192.168.254.132/24 brd 192.168.254.255 scope global eth1
inet6 fe80::20c:29ff:fe53:748f/64 scope link
valid_lft forever preferred_lft forever
备keepalived服务器关闭
主Keepalived命令:
[root@xldwhj ~]# service keepalived start
Starting keepalived: [ OK ]
[root@xldwhj ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
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 state UP qlen 1000
link/ether 00:0c:29:ef:76:a8 brd ff:ff:ff:ff:ff:ff
inet 192.168.178.2/24 brd 192.168.178.255 scope global eth0
inet 192.168.200.16/32 scope global eth0
inet 192.168.200.17/32 scope global eth0
inet6 fe80::20c:29ff:feef:76a8/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:ef:76:b2 brd ff:ff:ff:ff:ff:ff
inet 192.168.254.129/24 brd 192.168.254.255 scope global eth1
inet6 fe80::20c:29ff:feef:76b2/64 scope link
valid_lft forever preferred_lft forever
可以看出虚拟Ip192.168.200.17自动漂浮到主keepalived服务器上。
由上可知。keepalived主主配置已成功。