第一章 Keepalived简介
Keepalived软件起初是专为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能。因此,Keepalived除了能够管理LVS软件外,还可以作为其它服务(例如nginx、Haproxy、MySQL等)的高可用解决方案软件。
Keepalived软件主要是通过VRRP协议实现高可用功能的,VRRP是Virtual Router Redundancy Protocol(虚拟路由器冗余协议)的缩写,VRRP出现的目的就是为了解决静态路由单点故障问题的,它能够保证当别的节点宕机时,整个网络可以不间断的运行。所以Keepalived一方面具有配置管理LVS的功能,同时还具有LVS下面节点进行健康检查的功能,另一方面也可实现系统网络服务的高可用功能。
第二章 Keepalived服务三个重要功能
1. 管理LVS负载均衡软件
早期的LVS软件,需要通过命令行或脚本实现管理,并且没有针对LVS节点的健康检查功能。为了解LVS的这些使用不便的问题, Keepalived就诞生了,可以说Keepalived软件起初是专为解决LVS的问题而诞生的。因此Keepalived和LVS的感情很深,可以紧密的结合,愉快的工作。Keepalived可以通过读取自身的配置文件实现通过更底层的接口直接管理LVS的配置以及控制服务的启动、停止等功能,这使得LVS的应用更加简单方便了。
2. 实现对LVS集群节点健康检查功能(healthcheck)
Keepalived可以通过自身的keepalived.conf文件里配置LVS的节点IP和相关参数实现对LVS的直接管理;除此之外,当LVS集群中的某一个甚至几个节点服务器同时发生故障无法提供服务时,Keepalived服务会自动将失效的节点服务器从LVS的正常转发队列中清楚出去,并转换到别的正常节点服务器上,从而保证最终用户的访问不受影响;当故障的节点服务器被修复以后,Keepalived服务又会自动地把他们加入到正常转发队列中,对客户提供服务。
3. 作为系统服务的高可用功能(failover)
第三章 VRRP工作原理
- VRRP协议,全称Virtual Router RedundancyProtocol,中文名为虚拟路由冗余协议,VRRP的出现是为了解决静态路由点单故障。
- VRRP是通过一种竞选协议机制来将路由任务交给某台VRRP路由器的。
- VRRP是用过IP多播的方式(默认多播地址224.0.0.18)实现高可用对之间通信的。
- 工作时主节点发包,备节点接包,当备节点接收不到主节点发的数据包的时候,就启动接管程序接管主节点的资源。备节点可以有多个,通过优先级竞选,但一般Keepalived系统运维工作中都是一对。
- VRRP使用了加密协议加密数据,但Keepalived官方目前还是推荐用明文的方式配置认证类型和密码。
第四章 安装Keepalived环境
1. 使用yum源下载
yum install keepalived -y
! 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 { ##主 backup备
state MASTER ##主 backup备
interface ens33 ##组名 家庭名
virtual_router_id 51 ##weight 相当于权重 主大于备 一般为100
priority 150 ##发送间隔时间 1s
advert_int 1
authentication {
auth_type PASS ##认证类型密码
auth_pass 1111 ##密码
}
virtual_ipaddress {
10.0.0.1/24 dev ens33 label ens33:0 ##vip地址 虚拟IP地址
}
}
第五章 LB01/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
第六章 LB02/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL1
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
第七章 导致脑裂发生的原因
- 高可用服务器对之间心跳线链路故障,导致无法正常通信,例如1) 心跳线坏了(包括断了,老化),2) 网卡及相关驱动坏了,IP配置及冲突问题(网卡直连),3) 心跳线间连接的设备故障(网卡交换机等)。
- 高可用服务器对上开启了iptables防火墙阻挡了心跳消息传输。
- 高可用服务器对上心跳网卡地址等信息配置不正确,导致发送心跳失败。
- 其他服务配置不当等原因,如心跳方式不同,心跳广播冲突、软件BUG等。
提示:keepalived配置里统一VRRPVirtual_router_id参数两端配置不一致,也会导致脑裂问题发生。
第八章 配置文件LB01LB02 nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream server_pools {
server 10.0.0.7:80 weight=4 max_fails=3 fail_timeout=30s;
server 10.0.0.8:80 weight=4 max_fails=3 fail_timeout=30s;
# server 10.0.0.9:80 weight=4 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name www.etiantian.org;
location / {
proxy_pass http://server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 80;
server_name bbs.etiantian.org;
location / {
proxy_pass http://server_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
第九章 web01 nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
access_log logs/access_www.log main;
}
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
access_log logs/access_bbs.log main;
}
}
第十章 web02 nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
access_log logs/access_www.log main;
}
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
access_log logs/access_bbs.log main;
}
}
第十一章 keepalived双主模式配置文件
1. LB01配置keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24 dev eth0 label eth0:2
}
}
2. LB02 配置keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL1
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 52
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.4/24 dev eth0 label eth0:2
}
}
第十二章 监听本机没有的网卡ip 修改内核参数
echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
sysctl -p
第十三章 执行监控脚本nginx关闭则停止keepalived
cat /server/scripts/chk_web_proxy.sh
if [ `ss -lntup|grep nginx|wc -l` -ne 1 ];then
/etc/init.d/keepalived stop
fi
chmod +x /server/scripts/chk_web_proxy.sh
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_script chk_web_proxy { #<==定义vrrp脚本,检测HTTP端口。
script "/server/script/chk_web_proxy.sh" #<==执行脚本,当nginx服务有问题,就停掉keepalived服务。
interval 2 #<==间隔2秒
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
track_script {
chk_web_proxy #<==触发检查
}
}