目录
一、Keepalived及其工作原理
1.Keepalived 是一个基于VRRP协议来实现的LVS服务高可用方案,可以解决静态路由出现的单点故障问题。
2.Keepalived体系主要模块及其作用:
二、LVS+Keepalived 高可用群集的部署
三、解决脑裂现象
在一个LVS服务集群中通常有主服务器(MASTER)和备份服务器(BACKUP)两种角色的服务器,但是对外表现为一个虚拟IP(VIP),主服务器会发送VRRP通告信息给备份服务器,当备份服务器收不到VRRP消息的时候,即主服务器异常的时候,备份服务器就会接管虚拟IP,继续提供服务,从而保证了高可用性。
keepalived体系架构中主要有三个模块,分别是core、check和vrrp。
●core模块:为keepalived的核心,负责主进程的启动、维护及全局配置文件的加载和解析。
●vrrp模块:是来实现VRRP协议的。(调度器之间的健康检查和主备切换)
●check模块:负责健康检查,常见的方式有端口检查及URL检查。(节点服务器的健康检查)
配置keeplived(主、备DR 服务器)
1.主
yum install -y keepalived
systemctl disable --now firewalld
setenforce 0
cd /etc/keepalived/
cp keepalived.conf{,.bak}
vim keepalived.conf
smtp_server 127.0.0.1 #修改,邮件服务指向本地
smtp_connect_timeout 30
router_id LVS_01 #修改,指定服务器(路由器)的名称,主备服务器名称须不同,主为LVS_01,备为LVS_02 并注释或删除掉后四行,取消严格遵守VRRP协议功能,否则VIP无法被连接
}
vrrp_instance VI_1 { #定义VRRP热备实例参数
state MASTER #指定热备状态,主为MASTER,备为BACKUP
interface ens33 #指定承载vip地址的物理接口
virtual_router_id 51 #指定虚拟路由器的ID号,每个热备组保持一致
priority 100 #指定优先级,数值越大优先级越高,这里设置主为100,备为90
advert_int 1 #通告间隔秒数(心跳频率)
authentication { #定义认证信息,每个热备组保持一致
auth_type PASS #认证类型
auth_pass 1111 #指定验证密码,主备服务器保持一致
}
virtual_ipaddress {
172.16.114.200 #指定群集vip地址
}
}
virtual_server 172.16.114.200 80 { #指定虚拟服务器地址(VIP)、端口,定义虚拟服务器和Web服务器池参数
delay_loop 6 #健康检查的间隔时间(秒)
lb_algo rr #指定调度算法,轮询(rr)
lb_kind DR #指定群集工作模式,直接路由(DR)
persistence_timeout 0 #连接保持时间(秒)
protocol TCP #应用服务采用的是 TCP协议
real_server 172.16.114.10 80 { #指定第一个Web节点的地址、端口
weight 1 #节点的权重
#删除下面几行,添加以下健康检查方式
TCP_CHECK {
connect_port 80 #添加检查的目标端口
connect_timeout 3 #添加连接超时(秒)
nb_get_retry 3 #添加重试次数
delay_before_retry 3 #添加重试间隔
}
}
real_server 172.16.114.20 80 { #添加第二个 Web节点的地址、端口
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
#删除后面多余的配置
yum install -y ipvsadm
ipvsadm-save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm
systemctl enable ipvsadm
调整 proc 响应参数,关闭Linux 内核的重定向参数响应
vim /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
sysctl -p
systemctl restart keepalived.service
systemctl enable keepalived.service
systemctl status keepalived.service
ipvsadm -ln
scp keepalived.conf 172.16.114.70:`pwd`
ip addr #查看虚拟网卡vip
2.备
yum install -y keepalived
systemctl disable --now firewalld
setenforce 0
yum install -y ipvsadm
ipvsadm-save > /etc/sysconfig/ipvsadm
systemctl start ipvsadm
systemctl enable ipvsadm
调整 proc 响应参数,关闭Linux 内核的重定向参数响应
vim /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
sysctl -p
cd /etc/keepalived/
vim keepalived.conf
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_02
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.114.200
}
}
virtual_server 172.16.114.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 0
protocol TCP
real_server 172.16.114.10 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.16.114.20 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
systemctl restart keepalived.service
ipvsadm -ln #如没有VIP 的分发策略,则重启 keepalived 服务
ip a
节点服务器1
cd /etc/yum.repos.d/
rz -E
yum install -y nginx
cd /etc/nginx/
vim nginx.conf
keepalive_timeout 0; 长连接时间修改为0
cd /usr/share/nginx/html/
echo 'this is web01 text page
' > text.html
vim /etc/sysctl.conf
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
sysctl -p
route add -host 172.16.114.200 dev lo:0
route -n
cd /etc/sysconfig/network-scripts/
cp ifcfg-ens33 ifcfg-lo:0
vim ifcfg-ens33
TYPE=Ethernet
DEVICE=ens33
ONBOOT=yes
BOOTPROTO=static
IPADDR=172.16.114.10
NETMASK=255.255.255.0
#GATEWAY=172.16.114.2
#DNS1=218.2.135.1
vim ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=172.16.114.200
NETMASK=255.255.255.255
systemctl restart network nginx
节点服务器2
cd /etc/yum.repos.d/
rz -E
yum install -y nginx
cd /usr/share/nginx/html
echo 'this is web02 text page
' > text.html
vim /etc/sysctl.conf
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
sysctl -p
vim /etc/nginx/nginx.conf
keepalive_timeout 0; 长连接时间修改为0
cd /etc/sysconfig/network-scripts/
cp ifcfg-ens33 ifcfg-lo:0
vim ifcfg-ens33
TYPE=Ethernet
DEVICE=ens33
ONBOOT=yes
BOOTPROTO=static
IPADDR=172.16.114.20
NETMASK=255.255.255.0
#GATEWAY=172.16.114.2
#DNS1=218.2.135.1
vim ifcfg-lo:0
DEVICE=lo:0
ONBOOT=yes
IPADDR=172.16.114.200
NETMASK=255.255.255.255
systemctl restart network nginx
route add -host 172.16.114.200 dev lo:0
route -n
测试结果访问vip172.16.114.200
http:172.16.114.200/text.html
关闭主再次测试
systemctl stop keepalived
服务正常运行部署成功
1)负载均衡 LVS Nginx HAProxy F5
2)健康检查(探针) for调度器/节点服务器 Keepalived Heartbeat
3)故障转移 通过VIP漂移实现主备切换
1)发送心跳消息 ping/pong
2)TCP端口检查 向目标主机的 IP:PORT 发起TCP连接请求,如果TCP连接三次握手成功则认为健康检查探测成功,否则认为健康检查探测失败
3)HTTP URL检查 向目标主机的 http://IP:PORT/URL路径 发送 HTTP GET 请求方法,如果响应消息是2XX 3XX状态码则认为健康检查探测成功;如果响应消息是4XX 5XX状态码则认为健康检查探测失败
现象:主服务器和备服务器同时拥有VIP
原因:因为主服务器和备服务器之间的通信链路中断,导致备服务器无法收到主服务器发送的VRRP通告消息,备服务器误认为主服务器故障了并通过IP命令生成VIP
解决:关闭主服务器或备服务器其中一个的keepalived服务
预防:
(1)主服务器和备服务器之间添加双链路通信
(2)在主服务器上添加脚本进行判断与备服务器通信链路是否中断,如果确实是链路中断则自行关闭keepalived服务
(3)利用第三方应用或监控系统检测是否发送脑裂故障,如果发送脑裂故障则通过第三方应用或监控系统来关闭主服务器或备服务器上的keepalived服务
主
cd /etc/yum.repos.d/
rz -E
yum install -y nginx keepalived
cd /etc/nginx/
vim nginx.conf
stream {
upstream backends {
server 172.16.114.10:80 weight=1 max_fails=2 fail_timeout=30s;
server 172.16.114.20:80 weight=1 max_fails=2 fail_timeout=30s;
}
server {
listen 9527;
proxy_pass backends;
}
nginx -t
systemctl start nginx
systemctl enable nginx
scp nginx.conf 172.16.114.70:`pwd`
cd /etc/keepalived/
vim check_nginx.sh
#!/bin/bash
if ! killall -0 nginx &> /dev/null
then
systemctl stop keepalived
fi
chmod +x check_nginx.sh
vim keepalived.conf
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id NGINX_01
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 2
}
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 {
172.16.114.200
}
track_script {
check_nginx
}
}
virtual_server 172.16.114.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 0
protocol TCP
real_server 172.16.114.10 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.16.114.20 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
scp keepalived.conf 172.16.114.70:`pwd`
scp check_nginx.sh 172.16.114.70:`pwd`
systemctl enable --now keepalived.service
ip a
备
yum install -y nginx keepalived
systemctl start nginx
systemctl enable nginx
cd /etc/keepalived/
vim keepalived.conf
router_id NGINX_02 #修改这两个
priority 90
systemctl enable --now keepalived.service
ip a
chmod +x check_nginx.sh
主
上面都配置好了
测试脚本
killall nginx
ps -C nginx
systemctl status keepalived.service
12月 19 19:27:32 localhost.localdomain Keepalived[60201]: Stopping
访问测试结果
http://172.16.114.200:9527/text.html
主服务器挂了,备服务器正常使用,并且脚本运行成功。