Linux系统——keepalived高可用集群服务
Keepalived服务介绍
起初的设计是专门给LVS服务的,专门用来监控LVS集群系统中各个服务节点的状态,后来又加入了VRRP(Virtual Router Redundancy Protocol)虚拟路由器冗余协议,VRRP是为了解决静态路由单点故障,它有两大功能:健康检查与故障切换(主备之间快速切换)
HA的功能:
在主备LB之间相互监控运行状态,一旦主LB宕机,备LB会立马接收主LB的所有资源(IP资源与VIP资源),然后接管主LB来运行LB的功能,一旦主LB恢复,它将资源还给主LB
健康检查:
主要是对RS节点的运行健康性检查,一旦有节点宕机,它会不再将主求发给这个RS节点
Keepalived故障切换原理
这种故障切换是通过VRRP协议来实现的,主节点会按一定的时间间隔发生心跳信息广播包,告诉备节点自己的存活状态,当主节点出现故障时,备节点就无法继续检测到主节点的心跳信息,从而调用自身的接管程序,接管主节点的IP资源及服务,当主节点恢复时,备节点会释放所接管的资源,恢复到接管前的状态
VRRP协议:
Virtual Router Redundancy Protocol虚拟路由器冗余协议,能过竞选的方式来实现的
Keepalived安装布署
官方网站:http://www.keepalived.org
下载并安装
wget http://www.keepalived.org/software/keepalived-1.1.7.tar.gz
代码语言:javascript复制tar zxf keepalived-1.1.7.tar.gz
cd keepalived-1.1.7
./configure
Keepalived configuration
------------------------
Keepalived version : 1.1.17
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lpopt -lssl -lcrypto
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
Use VRRP Framework : Yes
Use LinkWatch : No
Use Debug flags : No
make && make install
拷贝配置文件
代码语言:javascript复制/bin/cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
#启动脚本
代码语言:javascript复制/bin/cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
#配置启动脚本参数
代码语言:javascript复制mkdir /etc/keepalived -p
#创建配置文件目录
代码语言:javascript复制/bin/cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
#配置文件模板
代码语言:javascript复制/bin/cp /usr/local/sbin/keepalived /usr/sbin/
#启动命令
启动服务
代码语言:javascript复制/etc/init.d/keepalived start
配置Keepalived单实例
主负载均衡器配置如下
代码语言:javascript复制[root@master keepalived]# vi keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
abc@abc.com
}
smtp_server 192.168.1.1
smtp_connect_timeout 30
router_id LVS_7
}
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 7
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254/24
}
}
备负载均衡器配置
代码语言:javascript复制[root@backup keepalived]# vi keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
abc@abc.com
}
smtp_server 192.168.1.1
smtp_connect_timeout 30
router_id LVS_8
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 8
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254/24
}
}
两端服务重启
代码语言:javascript复制/etc/init.d/keepalived restart
检查主备负载均衡器VIP情况
代码语言:javascript复制[root@master keepalived]# ip add|grep 192.168.1.24
inet 192.168.1.254/24 scope global eth1
[root@backup keepalived]# ip add|grep 192.168.1.254
备服务器上没有出现VIP表明配置正确
测试故障切换
首先检查VIP联通性
停掉主服务器keepalived服务
/etc/init.d/keepalived stop
只会有短暂的超时就立马恢复了
安装WEB服务器测试故障切换
yum install httpd -y
##分别安装在两台主备服务器上
配置WEB-001
代码语言:javascript复制echo "welcome to web of mingongge">/var/www/html/index.htm
配置WEB-002
代码语言:javascript复制echo "welcome to web of mingongge">/var/www/html/index.html
为了测试两台服务器显示内容配置一致
代码语言:javascript复制for n in `echl {1..3}`;do curl http://192.168.1.254;done
welcome to web of mingongge
welcome to web of mingongge
welcome to web of mingongge
测试WEB故障切换
直接关闭主服务器测试
代码语言:javascript复制[root@master keepalived]#halt
for n in `echl {1..5}`;do curl http://192.168.1.254;done
welcome to web of mingongge
welcome to web of mingongge
welcome to web of mingongge
welcome to web of mingongge
welcome to web of mingongge
实现服务器故障切换
备注:实际生产环境,需要注意两台WEB服务器上的数据一致性问题,使用NFS共享存储或其它共享存储就可以解决此问题