keepalived实现双机热备的高可用
一、 Keepalived 详解
1 Keepalived 定义
Keepalived 是一个基于VRRP协议来实现的LVS服务高可用方案,可以利用其来避免单点故障。一个LVS服务会有2台服务器运行Keepalived,一台为主服务器(MASTER),一台为备份服务器(BACKUP),但是对外表现为一个虚拟IP,主服务器会发送特定的消息给备份服务器,当备份服务器收不到这个消息的时候,即主服务器宕机的时候, 备份服务器就会接管虚拟IP,继续提供服务,从而保证了高可用性。Keepalived是VRRP的完美实现,因此在介绍keepalived之前,先介绍一下VRRP的原理。
2 VRRP 协议简介
在现实的网络环境中,两台需要通信的主机大多数情况下并没有直接的物理连接。对于这样的情况,它们之间路由怎样选择?主机如何选定到达目的主机的下一跳路由,这个问题通常的解决方法有二种:
在主机上使用动态路由协议(RIP、OSPF等)
在主机上配置静态路由
很明显,在主机上配置动态路由是非常不切实际的,因为管理、维护成本以及是否支持等诸多问题。配置静态路由就变得十分流行,但路由器(或者说默认网关default gateway)却经常成为单点故障。VRRP的目的就是为了解决静态路由单点故障问题,VRRP通过一竞选(election)协议来动态的将路由任务交给LAN中虚拟路由器中的某台VRRP路由器。
3 VRRP 工作机制
在一个VRRP虚拟路由器中,有多台物理的VRRP路由器,但是这多台的物理的机器并不能同时工作,而是由一台称为MASTER的负责路由工作,其它的都是BACKUP,MASTER并非一成不变,VRRP让每个VRRP路由器参与竞选,最终获胜的就是MASTER。MASTER拥有一些特权,比如,拥有虚拟路由器的IP地址,我们的主机就是用这个IP地址作为静态路由的。拥有特权的MASTER要负责转发发送给网关地址的包和响应ARP请求。
VRRP通过竞选协议来实现虚拟路由器的功能,所有的协议报文都是通过IP多播(multicast)包(多播地址224.0.0.18)形式发送的。虚拟路由器由VRID(范围0-255)和一组IP地址组成,对外表现为一个周知的MAC地址。所以,在一个虚拟路由 器中,不管谁是MASTER,对外都是相同的MAC和IP(称之为VIP)。客户端主机并不需要因为MASTER的改变而修改自己的路由配置,对客户端来说,这种主从的切换是透明的。
在一个虚拟路由器中,只有作为MASTER的VRRP路由器会一直发送VRRP通告信息(VRRPAdvertisement message),BACKUP不会抢占MASTER,除非它的优先级(priority)更高。当MASTER不可用时(BACKUP收不到通告信息), 多台BACKUP中优先级最高的这台会被抢占为MASTER。这种抢占是非常快速的(<1s),以保证服务的连续性。由于安全性考虑,VRRP包使用了加密协议进行加密。
4 VRRP 工作流程
4.1 初始化:
路由器启动时,如果路由器的优先级是255(最高优先级,路由器拥有路由器地址),要发送VRRP通告信息,并发送广播ARP信息通告路由器IP地址对应的MAC地址为路由虚拟MAC,设置通告信息定时器准备定时发送VRRP通告信息,转为MASTER状态;否则进入BACKUP状态,设置定时器检查定时检查是否收到MASTER的通告信息。
4.2 Master
设置定时通告定时器;
用VRRP虚拟MAC地址响应路由器IP地址的ARP请求;
转发目的MAC是VRRP虚拟MAC的数据包;
如果是虚拟路由器IP的拥有者,将接受目的地址是虚拟路由器IP的数据包,否则丢弃;
当收到shutdown的事件时删除定时通告定时器,发送优先权级为0的通告包,转初始化状态;
如果定时通告定时器超时时,发送VRRP通告信息;
收到VRRP通告信息时,如果优先权为0,发送VRRP通告信息;否则判断数据的优先级是否高于本机,或相等而且实际IP地址大于本地实际IP,设置定时通告定时器,复位主机超时定时器,转BACKUP状态;否则的话,丢弃该通告包;
4.3 Backup
设置主机超时定时器;
不能响应针对虚拟路由器IP的ARP请求信息;
丢弃所有目的MAC地址是虚拟路由器MAC地址的数据包;
不接受目的是虚拟路由器IP的所有数据包;
当收到shutdown的事件时删除主机超时定时器,转初始化状态;
主机超时定时器超时的时候,发送VRRP通告信息,广播ARP地址信息,转MASTER状态;
收到VRRP通告信息时,如果优先权为0,表示进入MASTER选举;否则判断数据的优先级是否高于本机,如果高的话承认MASTER有效,复位主机超时定时器;否则的话,丢弃该通告包;
5 ARP查询处理
当内部主机通过ARP查询虚拟路由器IP地址对应的MAC地址时,MASTER路由器回复的MAC地址为虚拟的VRRP的MAC地址,而不是实际网卡的 MAC地址,这样在路由器切换时让内网机器觉察不到;而在路由器重新启动时,不能主动发送本机网卡的实际MAC地址。如果虚拟路由器开启的ARP代理 (proxy_arp)功能,代理的ARP回应也回应VRRP虚拟MAC地址;好了VRRP的简单讲解就到这里,我们下来讲解一下Keepalived的案例。
二、 环境准备
1 操作系统
RHEL7 3.10.0-327.18.2.el7.x86_64
2 软件版本
httpd 2.4.6-40.el7
Keepalived v1.2.21
3 实验拓扑
4 主服务器安装步骤
4.1 主服务器httpd(大家根据实际情况应用,这里简单安装便于测试)
4.1.1 安装httpd
[root@master ~]# yum install -y httpd
4.1.2 配置httpd
[root@master ~]# vim /var/www/html/index.html
<h1>master<h1>
4.1.3 设置开机自启动
[root@master ~]# systemctl enable httpd.service
4.1.4 防火墙设置
[root@master ~]# firewall-cmd --permanent --add-service=http
4.2 主服务器keepalived
4.2.1 下载keepalived软件
[root@master ~]# wget http://www.keepalived.org/software/keepalived-1.2.21.tar.gz
4.2.2 解压软件
[root@master ~]# tar xzvf keepalived-1.2.21.tar.gz
4.2.3 进入目录
[root@master ~]# cd keepalived-1.2.21/
4.2.4 编译
[root@master keepalived-1.2.21]# ./configure --prefix=/usr/local/keepalived
4.2.5 错误信息
configure: error:
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!!
4.2.6 安装OpenSSL软件
[root@master keepalived-1.2.21]# yum install openssl-devel -y
4.2.7 重新编译
[root@master keepalived-1.2.21]# ./configure --prefix=/usr/local/keepalived
4.2.8 编译成功
Keepalived configuration
------------------------
Keepalived version : 1.2.21
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lssl -lcrypto -lcrypt
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
IPVS use libnl : No
fwmark socket support : Yes
Use VRRP Framework : Yes
Use VRRP VMAC : Yes
Use VRRP authentication : Yes
SNMP keepalived support : No
SNMP checker support : No
SNMP RFCv2 support : No
SNMP RFCv3 support : No
SHA1 support : No
Use Debug flags : No
libnl version : None
Use IPv4 devconf : No
Use libiptc : No
Use libipset : No
4.2.9 安装
[root@master keepalived-1.2.21]# make && make install
4.2.10 创建软连接
[root@master keepalived-1.2.21]# ln -s /usr/local/keepalived/sbin/keepalived /usr/bin/keepalived
4.2.11 复制文件
[root@masterkeepalived-1.2.21]#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
4.2.12 权限修改
[root@master keepalived-1.2.21]# chmod 755 /etc/init.d/keepalived
4.2.13 启动keepalived
[root@master keepalived-1.2.21]# chkconfig keepalived on
4.2.14 配置文件
[root@master ~]# vim /etc/init.d/keepalived
# Source configuration file (we set KEEPALIVED_OPTIONS there)
. /etc/sysconfig/keepalived
改为:
# Source configuration file (we set KEEPALIVED_OPTIONS there)
. /usr/local/keepalived/etc/sysconfig/keepalived
4.2.15 创建目录
[root@master keepalived-1.2.21]# mkdir /etc/keepalived
4.2.16 创建软连接
[root@masterkeepalived-1.2.21]#ln –s /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
4.2.17 重启服务
[root@master keepalived-1.2.21]# service keepalived restart
4.3 Keepalived主服务器配置文件
[root@master ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
65556505@qq.com#配置管理员邮箱
}
notification_email_from root#配置发件人
smtp_server 127.0.0.1#配置邮件服务器
smtp_connect_timeout 30#邮箱超时时间
router_id LVS_DEVEL#标识,保持唯一性
}
vrrp_instance VI_1 {#配置实例,名称主备服务器一致
state MASTER#配置模式,主服务器
interface eno16777736#网卡接口
virtual_router_id 51#虚拟路由标识,主备一致
priority 100#优先级,主服务器数值大于备服务器
advert_int 1#主备通讯间隔时间
authentication {#认证配置
auth_type PASS#认证类型,主备一致
auth_pass 1111#认证密码,主备一致
}
virtual_ipaddress {#虚拟ip地址,可以多个
192.168.1.70/24 dev eno16777736 label eno16777736:1
192.168.1.80/24 dev eno16777736 label eno16777736:1
}
}
5 备服务器安装步骤
5.1 备服务器httpd(大家根据实际情况应用,这里简单安装便于测试)
5.1.1 安装httpd
[root@backup ~]# yum install -y httpd
5.1.2 配置httpd
[root@backup ~]# vim /var/www/html/index.html
<h1>backup<h1>
5.1.3 设置开机自启动
[root@backup ~]# systemctl enable httpd.service
5.1.4 防火墙设置
[root@backup ~]# firewall-cmd --permanent --add-service=http
5.2 备服务器keepalived
5.2.1 下载keepalived软件
[root@backup ~]# wget http://www.keepalived.org/software/keepalived-1.2.21.tar.gz
5.2.2 解压软件
[root@backup ~]# tar xzvf keepalived-1.2.21.tar.gz
5.2.3 进入目录
[root@backup ~]# cd keepalived-1.2.21/
5.2.4 编译
[root@backup keepalived-1.2.21]# ./configure --prefix=/usr/local/keepalived
5.2.5 错误信息
configure: error:
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!!
5.2.6 安装OpenSSL软件
[root@backup keepalived-1.2.21]# yum install openssl-devel -y
5.2.7 重新编译
[root@backup keepalived-1.2.21]# ./configure --prefix=/usr/local/keepalived
5.2.8 编译成功
Keepalived configuration
------------------------
Keepalived version : 1.2.21
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lssl -lcrypto -lcrypt
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
IPVS use libnl : No
fwmark socket support : Yes
Use VRRP Framework : Yes
Use VRRP VMAC : Yes
Use VRRP authentication : Yes
SNMP keepalived support : No
SNMP checker support : No
SNMP RFCv2 support : No
SNMP RFCv3 support : No
SHA1 support : No
Use Debug flags : No
libnl version : None
Use IPv4 devconf : No
Use libiptc : No
Use libipset : No
5.2.9 安装
[root@backup keepalived-1.2.21]# make && make install
5.2.10 创建软连接
[root@backup keepalived-1.2.21]# ln -s /usr/local/keepalived/sbin/keepalived /usr/bin/keepalived
5.2.11 复制文件
[root@backupkeepalived-1.2.21]#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
5.2.12 权限修改
[root@backup keepalived-1.2.21]# chmod 755 /etc/init.d/keepalived
5.2.13 启动keepalived
[root@backup keepalived-1.2.21]# chkconfig keepalived on
5.2.14 配置文件
[root@backup keepalived-1.2.21]# vim /etc/init.d/keepalived
# Source configuration file (we set KEEPALIVED_OPTIONS there)
. /etc/sysconfig/keepalived
改为:
# Source configuration file (we set KEEPALIVED_OPTIONS there)
. /usr/local/keepalived/etc/sysconfig/keepalived
5.2.15 创建目录
[root@backup keepalived-1.2.21]# mkdir /etc/keepalived
5.2.16 创建软连接
[root@backupkeepalived-1.2.21]#
ln–s /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
5.2.17 重启服务
[root@backup keepalived-1.2.21]# service keepalived restart
5.3 Keepalived备服务器配置文件
[root@backup keepalived-1.2.21]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
65556505@qq.com#配置管理员邮箱
}
notification_email_from root#配置发件人
smtp_server 127.0.0.1#配置邮件服务器
smtp_connect_timeout 30#邮箱超时时间
router_id LVS_DEVEL#标识,保持唯一性
}
vrrp_instance VI_1 {#配置实例,名称主备服务器一致
state BACKUP#配置模式,备服务器
interface eno16777736#网卡接口
virtual_router_id 51#虚拟路由标识,主备一致
priority 99#优先级,备服务器数值小于主服务器
advert_int 1#主备通讯间隔时间
authentication {#认证配置
auth_type PASS#认证类型,主备一致
auth_pass 1111#认证密码,主备一致
}
virtual_ipaddress {#虚拟ip地址,可以多个
192.168.1.70/24 dev eno16777736 label eno16777736:1
192.168.1.80/24 dev eno16777736 label eno16777736:1
}
}
6 Keepalived测试
6.1 主服务器ip查看
[root@master ~]# ip add show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:9a:75:2e brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/24 brd 192.168.1.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet 192.168.1.70/24 scope global secondary eno16777736:1
valid_lft forever preferred_lft forever
inet 192.168.1.80/24 scope global secondary eno16777736:1
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe9a:752e/64 scope link
valid_lft forever preferred_lft forever
6.2 备服务器ip查看
[root@backup ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:1b:3b:d8 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.20/24 brd 192.168.1.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe1b:3bd8/64 scope link
valid_lft forever preferred_lft forever
6.3 主服务器宕机
[root@master ~]# service keepalived stop
[root@master ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:9a:75:2e brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/24 brd 192.168.1.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe9a:752e/64 scope link
valid_lft forever preferred_lft forever
6.4 再查看备服务器ip
[root@backup ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:1b:3b:d8 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.20/24 brd 192.168.1.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet 192.168.1.70/24 scope global secondary eno16777736:1
valid_lft forever preferred_lft forever
inet 192.168.1.80/24 scope global secondary eno16777736:1
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe1b:3bd8/64 scope link
valid_lft forever preferred_lft forever
6.5 主服务器启动
[root@master ~]# service keepalived start
[root@master ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:9a:75:2e brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/24 brd 192.168.1.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet 192.168.1.70/24 scope global secondary eno16777736:1
valid_lft forever preferred_lft forever
inet 192.168.1.80/24 scope global secondary eno16777736:1
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe9a:752e/64 scope link
valid_lft forever preferred_lft forever
6.6 再查看备服务器
[root@backup ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:1b:3b:d8 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.20/24 brd 192.168.1.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe1b:3bd8/64 scope link
valid_lft forever preferred_lft forever
三、 keepalived互为主备配置
1 keepalived主服务器配置文件
global_defs {
notification_email {
65556505@qq.com
}
notification_email_from root
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1
}
vrrp_instance VI_1 {
state MASTER
interface eno16777736
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.70/24 dev eno16777736 label eno16777736:1
192.168.1.80/24 dev eno16777736 label eno16777736:1
}
}
vrrp_instance VI_2 {
state BACKUP
interface eno16777736
virtual_router_id 52
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.70/24 dev eno16777736 label eno16777736:1
192.168.1.80/24 dev eno16777736 label eno16777736:1
}
}
2 Keepalived备服务器配置文件
global_defs {
notification_email {
65556505@qq.com
}
notification_email_from root
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node2
}
vrrp_instance VI_1 {
state BACKUP
interface eno16777736
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.70/24 dev eno16777736 label eno16777736:1
192.168.1.80/24 dev eno16777736 label eno16777736:1
}
}
vrrp_instance VI_2 {
state MASTER
interface eno16777736
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.70/24 dev eno16777736 label eno16777736:1
192.168.1.80/24 dev eno16777736 label eno16777736:1
}
}
3 Keepalived备机接管后主机恢复不抢占VIP
3.1 只需修改主服务器配置文件
[root@master ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
65556505@qq.com#配置管理员邮箱
}
notification_email_from root#配置发件人
smtp_server 127.0.0.1#配置邮件服务器
smtp_connect_timeout 30#邮箱超时时间
router_id LVS_DEVEL#标识,保持唯一性
}
vrrp_instance VI_1 {#配置实例,名称主备服务器一致
state BACKUP#主服务器MASTER改为 BACKUP
interface eno16777736#网卡接口
virtual_router_id 51#虚拟路由标识,主备一致
priority 100#优先级,主服务器数值大于备服务器
advert_int 1#主备通讯间隔时间
authentication {#认证配置
auth_type PASS#认证类型,主备一致
auth_pass 1111#认证密码,主备一致
}
virtual_ipaddress {#虚拟ip地址,可以多个
192.168.1.70/24 dev eno16777736 label eno16777736:1
192.168.1.80/24 dev eno16777736 label eno16777736:1
}
}
4 使用keepalived时iptables需要开放的协议
#!/bin/bash
iptables -F
iptables -X
iptables -Z
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 9419 -j ACCEPT
iptables -A INPUT -p tcp --dport 9420 -j ACCEPT
iptables -A INPUT -p tcp --dport 9421 -j ACCEPT
iptables -A INPUT -p tcp --dport 9425 -j ACCEPT
iptables -A INPUT -p tcp --dport 873 -j ACCEPT
iptables -A INPUT -p icmp -j ACCEPT
iptables -A INPUT -p vrrp -j ACCEPT
service iptables save
service iptables restart
exit
5 解决Keepalived只针对物理服务器问题
5.1 主服务器编写脚本
[root@master /]# vim chk_httpd.sh
#!bin/sh
if [ 'netstat -lntup | grep httpd | wc -l' -ne 1];then
/etc/init.d/keepalived stop
fi
[root@master /]# chmod +x chk_httpd.sh
5.2 主服务器Keepalived配置文件
! Configuration File for keepalived
global_defs {
notification_email {
65556505@qq.com
}
notification_email_from root
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id note1
}
vrrp_script chk_httpd{
script "/chk_httpd.sh"
interval 1
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eno16777736
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.70/24 dev eno16777736 label eno16777736:1
192.168.1.80/24 dev eno16777736 label eno16777736:1
}
track_script{
chk_httpd
}
}
5.3 备服务器编写脚本
[root@backup ~]# vim /chk_httpd.sh
#!bin/sh
if [ 'netstat -lntup | grep httpd | wc -l' -ne 1];then
/etc/init.d/keepalived stop
fi
[root@backup ~]# chmod +x chk_httpd.sh
5.4 备服务器Keepalived配置文件
! Configuration File for keepalived
global_defs {
notification_email {
65556505@qq.com
}
notification_email_from root
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id note2
}
vrrp_script chk_httpd{
script "/chk_httpd.sh"
interval 1
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eno16777736
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.70/24 dev eno16777736 label eno16777736:1
192.168.1.80/24 dev eno16777736 label eno16777736:1
}
track_script{
chk_httpd
}
}
5.5 配置ipvs(主备一样)
virtual_server192.168.1.70 80{
delay_loop 6
lb_algo rr
lb_kind NAT
nat_mask 255.255.0.0
persistence_timeout 0
protocol TCP
#
real_server 172.16.2.10 80 {
weight 1
HTTP_GET {
url {
path /
state_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.16.2.20 80 {
weight 1
HTTP_GET {
url {
path /
state_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
virtual_server192.168.1.80 80{
delay_loop 6
lb_algo rr
lb_kind NAT
nat_mask 255.255.0.0
persistence_timeout 0
protocol TCP
#
real_server 172.16.2.10 80 {
weight 1
HTTP_GET {
url {
path /
state_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.16.2.20 80 {
weight 1
HTTP_GET {
url {
path /
state_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}