原理
首先有一个虚拟ip暴露给客户端,虚拟ip对应的mac地址为一台真实服务器,
即用户向虚拟ip发送一个请求,该请求会被分发到真实服务器上。
现在有2台真实服务器,一台master,一台backup,master和backup上都运行着keepalived
当master挂了的时候,backup检测之后,自己成为master,且arp缓存虚拟ip对应的mac地址将变为
backup的mac地址,这样请求虚拟ip的报文会被发送到backup
架构图如下:
安装
主机:192.168.1.227
备机:192.168.1.246
虚拟ip:192.168.1.99
安装命令:
wget http://www.keepalived.org/software/keepalived-1.2.13.tar.gz tar -zxvf keepalived-1.2.13.tar.gz cd keepalived-1.2.13 ./configure --disable-fwmark --prefix=/usr/local/keepalived make && make install
整理:
mkdir /etc/keepalived/ cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ ln -s /usr/local/keepalived/sbin/keepalived /sbin/
配置:
vi /etc/keepalived/keepalived.conf
Master
! 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 LVS_DEVEL } 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.1.99 } }
Backup 和master一样,只要修改如下地方:
vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 99 # 这里改为99,master优先级是100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.99 } }
管理命令:
停止
service keepalived stop
启动
service keepalived start
重启
service keepalived restart
查看状态
service keepalived status
测试
在本机上查看虚拟ip状态
ip add
在同一网段的其他机器上查询arp缓存
arp -a
这里的1.99对应的mac地址是1.227
停掉master的keepalived
service keepalived stop
在同一网段的其他机器上查询arp缓存
arp –a
发现1.99的mac地址已经变为slave的mac地址
那么主备双机热备就完成了
总结:本篇文章实现的功能
1.master主机挂了,或者master 上keepalived挂了之后,可以切换到slave
2.master的网络不通的时候,可以立即切换到slave,但是如果只是master上的应用出现问题的时候,是不会
主动切换的,这个要通过编写脚本来监控应用出问题,这个问题后续再讨论吧。