目录
一、 Keeplive服务介绍1.1 Keepalived的用途1.2 LVS cluster nodes healthchecks功能1.3 keepalive故障切换转换原理1.4 VRRP协议的简单介绍二、配置Keepalived实现高可用2.1 安装keepalive2.2 配置文件2.3 启动看效果2.4 keepalive的VIP实现形式2.5 检测keepalibve效果三、 结合nginx实现高可用3.1 配置3.2 发现不能访问的问题3.3 测试四、keepalived的其他特性4.1 监控自动迁移脚本 4.2 keepalive高可用脑裂脚本4.3 修改日志文件路径4.4 keepalived多实例
回到顶部一、 Keeplive服务介绍
Keeplive期初是专门为LVS设计的,专门用来监控LVS集群系统中各个服务节点的状态,后来又加入VRRP的功能,因此除了配合LVS服务以外,也可以作为其他服务(nginx,haroxy)的高可用软件,VRRP是Virtual Router Redundancy Protocol(虚拟路由冗余协议)的缩写,VRRP出现的目的就是为了解决静态路由出现的单点故障问题,它能够保证网络的不间断,稳定的运行。所以keepalive一方面具有LVS cluster nodes healthchecks功能,另一方面也具有LVS directors failover
1.1 Keepalived的用途
Keepalive服务的两大用途:healthcheck和failover
ha failover功能:实现LB Master主机和Backup主机之间故障转移和自动切换
这是针对有两个负载均衡器Direator同时工作而采取的故障转移措施,当主负载均衡器失效或者出现故障时,备份的负载均衡器(BACKUP)将自动接管主负载均衡器的所有工作(vip资源以及相关服务):一旦主负载均衡器故障恢复,MASTER又会接管回它原来的工作,二备份复杂均衡器(BACKUP)会释放master是小事它接管的工作,此时两者将恢复到最初各自的角色
1.2 LVS cluster nodes healthchecks功能
在keeplive.conf配置记忆可以实现LVS的功能
keeplive可以对LVS下面的集群节点进行健康检查
rs healthcheck功能:负载均衡定期检查RS的可用性决定是否给其分发请求
当虚拟的服务器中的某一个甚至是几个真实的服务器同时出现故障无法提供服务时,负载均衡器会自动将失效的RS服务器从转发队列中清除出去,从而保证用户的访问不收影响;当故障的RS服务器被修复后,系统又自动的将他们加入转发队列,分发请求提供正常服务。
工作原理
1.3 keepalive故障切换转换原理
Keepalived高可用对之间是通过 VRRP进行通信的, VRRP是遑过竞选机制来确定主备的,主的优先级高于备,因此,工作时主会优先获得所有的资源,备节点处于等待状态,当主挂了的时候,备节点就会接管主节点的资源,然后顶替主节点对外提供服务。
在 Keepalived服务对之间,只有作为主的服务器会一直发送 VRRP广播包,告诉备它还活着,此时备不会枪占主,当主不可用时,即备监听不到主发送的广播包时,就会启动相关服务接管资源,保证业务的连续性.接管速度最快可以小于1秒。
1.4 VRRP协议的简单介绍
1) VRRP,全称 Virtual Router Redundancy Protocol,中文名为虚拟路由冗余协议,VRRP的出现是为了解决静态路由的单点故障。
2) VRRP是通过一种竟选协议机制来将路由任务交给某台 VRRP路由器的。
3) VRRP用 IP多播的方式(默认多播地址(224.0_0.18))实现高可用对之间通信。
4) 工作时主节点发包,备节点接包,当备节点接收不到主节点发的数据包的时候,就启动接管程序接管主节点的开源。备节点可以有多个,通过优先级竞选,但一般 Keepalived系统运维工作中都是一对。
5) VRRP使用了加密协议加密数据,但Keepalived官方目前还是推荐用明文的方式配置认证类型和密码
回到顶部二、配置Keepalived实现高可用
2.1 安装keepalive
【root@lb01 ~】# cd /usr/local/src/
【root@lb01 src】# wget
【root@lb01 src】# tar -xf keepalived-2.0.15.tar.gz
【root@lb01 src】# cd keepalived-2.0.15
【root@lb01 keepalived-2.0.15】# ./configure
Linker flags : -pie
Extra Lib : -lm -lcrypto -lssl
Use IPVS Framework : Yes
IPVS use libnl : No
IPVS syncd attributes : No
IPVS 64 bit stats : No
HTTP_GET regex support : No
fwmark socket support : Yes
Use VRRP Framework : Yes
Use VRRP VMAC : Yes
Use VRRP authentication : Yes
With ip rules/routes : Yes
With track_process : Yes
With linkbeat : Yes
Use BFD Framework : No
SNMP vrrp support : No
SNMP checker support : No
SNMP RFCv2 support : No
SNMP RFCv3 support : No
DBUS support : No
SHA1 support : No
Use JSON output : No
libnl version : None
Use IPv4 devconf : No
Use iptables : Yes
Use libiptc : No
Use libipset : No
Use nftables : No
init type : systemd
Strict config checks : No
Build genhash : Yes
Build documentation : No
【root@lb01 keepalived-2.0.15】# make
【root@lb01 keepalived-2.0.15】# make install
lb02相同操作
【root@lb02 ~】# cd /usr/local/src/
【root@lb02 src】# wget
【root@lb02 src】# tar -xf keepalived-2.0.15.tar.gz
【root@lb02 src】# cd keepalived-2.0.15
【root@lb02 keepalived-2.0.15】# ./configure
【root@lb02 keepalived-2.0.15】# make
【root@lb02 keepalived-2.0.15】# make install
【root@lb01 keepalived-2.0.15】# vim /usr/lib/systemd/system/keepalived.service
【Unit】
Description=LVS and VRRP High Availability Monitor
After=syslog.target network-online.target
【Service】
Type=forking
PIDFile=/var/run/keepalived.pid
KillMode=process
EnvironmentFile=-/etc/sysconfig/keepalived
ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
【Install】
WantedBy=multi-user.target
2.2 配置文件
【root@lb01 keepalived-2.0.15】# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived #!注释
global_defs { #全局变量
notification_email {
} #邮件发件人
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1 #邮件服务器地址
smtp_connect_timeout //代码效果参考:http://www.lyjsj.net.cn/wx/art_23314.html
30 #超时时间router_id LVS_01
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 { #keepalive或者vrrp的一个实例
state MASTER #状态
interface ens33 #通信端口
virtual_router_id 51 #实例ID
priority 150 #优先级
advert_int 1 #心跳的间隔
authentication { #服务器之间通过密码验证
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.131 #VIP
}
}
2.3 启动看效果
【root@lb01 keepalived】# systemctl start keepalived
【root@lb01 keepalived】# ps -ef|grep keep
【root@lb01 keepalived】# ip addr
1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1
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: ens33:
mtu 1500 qdisc pfifo_fast state UP qlen 10//代码效果参考:http://www.lyjsj.net.cn/wx/art_23312.html
00link/ether 00:0c:29:12:2e:59 brd ff:ff:ff:ff:ff:ff
inet 172.25.254.131/24 brd 172.25.254.255 scope global dynamic ens33
valid_lft 1085sec preferred_lft 1085sec
inet 10.0.0.131/24 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::8068:96e2:b57b:be1d/64 scope link
valid_lft forever preferred_lft forever
3: ens34:
mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:12:2e:63 brd ff:ff:ff:ff:ff:ff
2.4 keepalive的VIP实现形式
【root@lb01 keepalived】# ifconfig ens33:0 10.0.0.18 up
【root@lb01 keepalived】# ip addr add 10.0.0.19 dev ens33
【root@lb01 keepalived】# ip addr
inet 172.25.254.131/24 brd 172.25.254.255 scope global dynamic ens33
valid_lft 1583sec preferred_lft 1583sec
inet 10.0.0.131/24 scope global ens33
valid_lft forever preferred_lft forever
inet 10.0.0.18/8 brd 10.255.255.255 scope global ens33:0
valid_lft forever preferred_lft forever
inet 10.0.0.19/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::8068:96e2:b57b:be1d/64 scope link
valid_lft forever preferred_lft forever
【root@lb01 keepalived】# ip addr del 10.0.0.19 dev ens33
【root@lb01 keepalived】# ifconfig ens33:0 10.0.0.18 down
【root@lb01 keepalived】# scp /etc/keepalived/keepalived.conf 172.25.254.133:/etc/keepalived/
【root@lb02 ~】# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_02
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.131/24
}
}
【root@lb02 ~】# systemctl start keepalived
【root@lb02 ~】# ps -ef |grep keep
配置成功
2.5 检测keepalibve效果
关闭MASTER的keepalive服务
【root@lb01 keepalived】# systemctl stop keepalived
【root@lb01 keepalived】# ip addr|grep 10.0.0.131
查看BACKUP端,是否有10.0.0.131
【root@lb02 ~】# ip addr|grep 10.0.0.131
【root@lb01 keepalived】# systemctl start keepalived
成功
回到顶部三、 结合nginx实现高可用
3.1 配置
【root@lb01 keepalived】# cd /usr/local/nginx/conf/
【root@lb01 conf】# vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream web_pools {
server 172.25.254.134:80 weight=5;
server 172.25.254.135:80 weight=5;
# server 172.25.254.158:80 weight=5 backup;
}
server {
listen 80;
server_name
location / {
# root html;
# index index.html index.htm;
proxy_set_header Host $host;
proxy_pass
}
}
}
【root@lb01 conf】# scp nginx.conf 172.25.254.133:/usr/local/nginx/conf/
【root@lb01 conf】# nginx -s reload
【root@lb01 conf】# curl 172.25.254.134
172.25.254.134
【root@lb01 conf】# curl 172.25.254.135
172.25.254.135
【root@lb01 conf】# nginx -s reload
【root@lb02 ~】# curl 172.25.254.134
172.25.254.134
【root@lb02 ~】# curl 172.25.254.135
172.25.254.135
在获取到VIP后,不在同一网段,为了方便测试,把VIP设置为172.25.254.254
【root@lb01 ~】# ip addr|grep 172.25.254.254
3.2 发现不能访问的问题
访问测试,发现不能访问,也不能ping通
【root@lb01 conf】# curl 172.25.254.254
curl: (7) Failed connect to 172.25.254.254:80; Connection timed out
【root@lb01 conf】# ping 172.25.254.254
2 packets transmitted, 0 received, 100% packet loss, time 999ms
解决
【root@lb01 conf】# vim /etc/keepalived/keepalived.conf
# vrrp_strict # 注释掉vrrp_strict
【root@lb01 conf】# systemctl restart keepalived
3.3 测试
【root@lb01 conf】# curl 172.25.254.254
172.25.254.135
【root@lb01 conf】# curl 172.25.254.254
172.25.254.134
【root@lb01 conf】# curl 172.25.254.254
172.25.254.135
【root@lb01 conf】# curl 172.25.254.254
172.25.254.134
关闭MASTER测试
【root@lb01 conf】# systemctl stop keepalived #这时VIP已经在BACKUP上,但是可以正常访问
【root@lb01 conf】# curl 172.25.254.254
172.25.254.135
【root@lb01 conf】# curl 172.25.254.254
172.25.254.134
【root@lb01 conf】# curl 172.25.254.254
172.25.254.135
【root@lb01 conf】# curl 172.25.254.254
172.25.254.134
【root@lb01 conf】# curl 172.25.254.254
172.25.254.135
<a href="javascript:void(0);" onclick="copyCnblog