`简介`
Keepalived是Linux下一个轻量级别的高可用解决方案。Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。
`重要功能`
管理LVS负载均衡软件
实现LVS集群节点的健康检查中
作为系统网络服务的高可用性
`高可用故障切换原理`
Keepalived高可用服务对之间的故障切换转移,是通过 VRRP (Virtual Router Redundancy Protocol ,虚拟路由器冗余协议)来实现的。
在 Keepalived服务正常工作时,主 Master节点会不断地向备节点发送(多播的方式)心跳消息,用以告诉备Backup节点自己还活看,当主 Master节点发生故障时,就无法发送心跳消息,备节点也就因此无法继续检测到来自主 Master节点的心跳了,于是调用自身的接管程序,接管主Master节点的 IP资源及服务。而当主 Master节点恢复时,备Backup节点又会释放主节点故障时自身接管的IP资源及服务,恢复到原来的备用角色。
1、环境准备
172.18.62.38 nginx+keeplived master
172.18.62.39 nginx+keeplived backup
172.18.62.40 apache
172.18.62.41 apache
172.18.62.42 vip
2、启动容器
[root@iZuf612i9bshiuw3zzlfe9Z keeplived]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a2405e53d1d2 kad "/usr/sbin/init" About a minute ago Up About a minute keepalived_apache1_1
7967b49e3930 kad "/usr/sbin/init" About a minute ago Up About a minute 0.0.0.0:100->80/tcp keepalived_vip_1
ade36b938d94 kad "/usr/sbin/init" About a minute ago Up About a minute keepalived_apache2_1
1d3af026ed63 kad "/usr/sbin/init" About a minute ago Up About a minute 0.0.0.0:80->80/tcp keepalived_nginx-master_1
7a739f103336 kad "/usr/sbin/init" About a minute ago Up About a minute 0.0.0.0:90->80/tcp keepalived_nginx-slave_1
3、安装服务apache
#更新软件包
apt update
apt upgrade -y
#安装apache
apt install apache2 -y
#方便测试,修改首页
vim /var/www/html/index.html
#启动apache服务
/etc/init.d/apache2 start
访问如下:
root@apache1:/var/www/html# curl http://172.18.62.40
<h1>Apache1 172.18.62.40</h1>
root@apache2:/var/www/html# curl http://172.18.62.41
<h1>Apache2 172.18.62.41</h1>
4、安装nginx
#下载安装包
wget -P /usr/local/ http://nginx.org/download/nginx-1.2.8.tar.gz
#安装依赖
apt-get install libpcre3 libpcre3-dev zlib1g-dev -y
#解压
tar xvf nginx-1.2.8.tar.gz
安装
./configure
make CFLAGS='-Wno-implicit-fallthrough'
make install
5、安装keepalived
#更新安装包
apt update
apt upgrade -y
#安装服务
apt install keepalived -y
6、配置nginx
vim /usr/local/nginx/conf/nginx.conf
#负载均衡配置
upstream web{
server 172.18.62.40:80 weight=1;
server 172.18.62.41:80 weight=1;
}
#server配置
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web;
}
}
#启动nginx
/usr/local/nginx/sbin/nginx
7、配置keepalived
vim /etc/keepalived/keepalived.conf
#master
global_defs {
router_id 172.18.62.38
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 2
}
vrrp_instance vrrptest {
state master
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass ufsoft
}
track_script {
check_nginx
}
virtual_ipaddress {
172.18.62.43
}
}
#backup
global_defs {
router_id 172.18.62.39
}
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight 2
}
vrrp_instance vrrptest {
state master
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass ufsoft
}
track_script {
check_nginx
}
virtual_ipaddress {
172.18.62.43
}
}
#启动keepalived
/etc/init.d/keeplived start
8、检查nginx脚本
if [ "$(ps -ef | grep "nginx:master process"| grep -v grep )" == "" ];then
/etc/init.d/keepalived stop
else
echo "nginx is running"
fi
9、测试
#master
http://Ip:80
#backup
http://IP:90
#VIP
http://IP:100
10、故障测试
#停掉master
root@nginx-master:/etc/keepalived# /etc/init.d/keepalived stop
Stopping keepalived (via systemctl): keepalived.service.
http://IP:100依旧正常访问