keepalived是什么?
keepalived是集群管理中保证集群高可用的一个服务软件,用来防止单点故障。
keepalived工作原理
keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗余协议。
虚拟路由冗余协议:可以认为是实现路由器高可用的协议,即将N台提供相同功能的路由器组成一个路由器组,这个组里面有一个master和多个backup,master上面有一个对外提供服务的vip(该路由器所在局域网内其他机器的默认路由为该vip),master会发组播,当backup收不到vrrp包时就认为master宕掉了,这时就需要根据VRRP的优先级来选举一个backup当master。这样的话就可以保证路由器的高可用了。
keepalived主要有三个模块,分别是core、check和vrrp。core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。check负责健康检查,包括常见的各种检查方式。vrrp模块是来实现VRRP协议的。
脑裂 split barin:
Keepalived的BACKUP主机在收到不MASTER主机报文后就会切换成为master,如果是它们之间的通信线路出现问题,无法接收到彼此的组播通知,但是两个节点实际都处于正常工作状态,这时两个节点均为master强行绑定虚拟IP,导致不可预料的后果,这就是脑裂。
关于脑裂问题的解决方法:
通过Nginx的upstream实现负载均衡
proxy-master: 192.168.134.165
proxy-slave: 192.168.134.166
real-server1: 192.168.134.163
real-server2: 192.168.134.164
VIP 192.168.1345.160
systemctl stop firewalld
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/sysconfig/selinux //关闭selinux,重启生效
[root@proxy-master ~]# cd /etc/yum.repos.d/
[root@proxy-master yum.repos.d]# vim nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
[root@proxy-master ~]# yum install yum-utils -y
[root@proxy-master ~]# yum install nginx -y
[root@proxy-master ~]# systemctl start nginx
proxy-master:192.168.134.165和proxy-slave:192.168.134.166做代理。
在两台代理机器上都配置:
[root@master ~]# vim /etc/nginx/conf.d/default.conf
upstream aren {
server 192.168.134.163:80 weight=1 max_fails=3 fail_timeout=20s;
server 192.168.134.164:80 weight=1 max_fails=3 fail_timeout=20s;
}
server {
listen 80;
root /usr/share/nginx/html;
location /{
proxy_pass http://aren;
proxy_set_header Host $host:$proxy_port;
proxy_set_header X-Forwarded-For $remote_addr;
}
[root@master ~]# nginx -s reload
注:主/备调度器均能够实现正常调度
1. 主/备调度器安装软件
主:
[root@proxy-master ~]# yum install -y keepalived
[root@proxy-slave ~]# yum install -y keepalived
[root@proxy-master ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak ##备份
! Configuration File for keepalived
global_defs {
router_id directory1
}
vrrp_instance VI_1 {
state MASTER #定义为主
interface ens33 #VIP绑定接口
virtual_router_id 80 #整个集群的调度器一致
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123
}
virtual_ipaddress {
192.168.134.160/24 #VIP
}
}
备:
[root@proxy-slave ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@proxy-slave ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id directory2 #
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 80
priority 50 #back的优先级为50 (小于master)
advert_int 1
authentication {
auth_type PASS
auth_pass 123
}
virtual_ipaddress {
192.168.134.160/24
}
}
此时可以看到VIP在master上:
[root@server03 ~]# echo "lvs-RS1" > /usr/share/nginx/html/index.html
[root@server03 ~]# systemctl start nginx
[root@server04 ~]# echo "lvs-RS2" > /usr/share/nginx/html/index.html
[root@server04 ~]# systemctl start nginx
##关闭master的nginx 和 keepalived
[root@master ~]# systemctl stop keepalived
思路:
让Keepalived以一定时间间隔执行一个外部脚本,脚本的功能是当Nginx失败,则关闭本机的Keepalived
[root@proxy-master ~]# vim /etc/keepalived/check_nginx_status.sh
#!/bin/bash
/usr/bin/curl -I http://localhost &>/dev/null
if [ $? -ne 0 ];then
# /etc/init.d/keepalived stop
systemctl stop keepalived
fi
! Configuration File for keepalived
global_defs {
router_id directory1
}
vrrp_script check {
script "/etc/keepalived/check-nginx.sh"
interval 5 #每5秒检测一次
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 80
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123
}
virtual_ipaddress {
192.168.134.160/24
}
track_script {
check
}
}
关闭master上的nginx服务,过5秒后会检测到nginx挂掉,随后会关闭master上的keepalived;并且VIP会转移到slave上面,让slave继续提供服务。
master:
slave: