服务器信息:
master : 192.168.1.51
backup : 192.168.1.50
VIP : 192.168.1.55
主服务器配置 /etc/keepalived/keepalived.conf :
global_defs {
notification_email {
root@networkquestions.org //定义报警邮件
}
notification_email_from warn@networkquestions.org //定义发件人
smtp_server localhost //指定smtp服务器,这里直接改为localhost
smtp_connect_timeout 30 //smtp连接超时时间
router_id node1 //节点名称
}
vrrp_instance VI_1 {
state MASTER //设置为主服务器
interface eth0 //定义虚拟ip绑定接口
virtual_router_id 51 //VRRP组名,两个节点必须一样,指明各个节点属于同一VRRP组 mcast_src_ip 192.168.1.51 //发送多播包的地址,如果不设置默认使用绑定的网卡
priority 100 //优先级,必须高于从服务器
advert_int 1 //组播信息发送间隔,两个节点设置必须一样
authentication { //认证,默认即可。主从需一致。
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.55 //绑定的虚拟ip
}
}
我这里只保留了以上部分,其它参数全部删除。
从服务器配置 /etc/keepalived/keepalived.conf :
global_defs {
notification_email {
acassen@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server localhost
smtp_connect_timeout 30
router_id node2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.1.50 //从服务器ip
priority 99 //小于主服务器
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.55
}
}
保存退出,重启keepalived。
主服务器执行:
[root@rhel ~]# 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:d4:de:01 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.51/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.55/32 scope global eth0
inet6 fe80::20c:29ff:fed4:de01/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000
link/ether 00:0c:29:d4:de:0b brd ff:ff:ff:ff:ff:ff
4: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
看到192.168.1.55 已经绑定到eth0中。
tail -f /var/log/messages 会有相关信息输出。
现在拔掉主服务器网线,去从服务器查看:
[root@rhel ~]# 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:7f:5b:93 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.50/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.55/32 scope global eth0
inet6 fe80::20c:29ff:fe7f:5b93/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000
link/ether 00:0c:29:7f:5b:9d brd ff:ff:ff:ff:ff:ff
4: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
接管了192.168.1.55 绑定到eth0
插上主服务器的网线,重启keepalived,再次执行ip a 发现重新接管VIP。
测试完成。
PS:
keepalived 互为主备,当同一时间只有一台节点接管vip时,另一台处于备份状态,利用率不高,所以配置两个VIP,互为主备。
节点1:
global_defs {
notification_email {
xxx@qq.com
}
notification_email_from root@localhost
smtp_server localhost
smtp_connect_timeout 30
router_id node1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
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
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.26
}
}
节点2:
global_defs { notification_email { xxx@qq.com } notification_email_from root@localhost smtp_server localhost smtp_connect_timeout 30 router_id node2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 99 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.26 } }