目录
Keepalived是一款基于VRRP协议的高可用集群软件,通过虚拟IP(VIP)对外提供服务,能够实时监控集群中服务器的运行状态并自动进行故障隔离,这些服务器都启动着相同的服务,当主服务器发生故障时,会自动将虚拟IP漂移到备份服务器,从而实现业务高可用。
一、服务器环境
两台服务器:10.100.1.123、10.100.1.125
虚拟IP:10.100.1.155
二、流程图
三、部署Nginx
1、获取镜像
docker pull nginx:1.20.0
2、配置文件
mkdir -p /home/docker/nginx/conf
cd /home/docker/nginx/conf
vi nginx.conf
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
keepalive_timeout 65;
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /home/nginx-demo/html;
}
}
}
3、设计默认页面
mkdir -p /home/docker/nginx/html
cd /home/docker/nginx/html
vi index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>Load One</p>
</body>
</html>
同理,另一台服务器也做上述操作
4、启动容器
docker run -d \
--name nginx \
-v /home/docker/nginx/html:/usr/share/nginx/html \
-v /home/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-p 9000:80 \
nginx:1.20.0
三、部署Keepalived
1、获取镜像
docker pull osixia/keepalived:2.0.20
osixia/keepalived DockerHub:Docker Hub
osixia/keepalived GitHub:GitHub - osixia/docker-keepalived: Keepalived container image 🐳🌴
2、查看服务器网关
ifconfig
netstat -rn
cat /etc/sysconfig/network-scripts/ifcfg-eth160
3、启动容器
docker run --cap-add=NET_ADMIN --cap-add=NET_BROADCAST --cap-add=NET_RAW --net=host -d --name keepalived \
-v /etc/localtime:/etc/localtime \
-e KEEPALIVED_INTERFACE='ens160' \
-e KEEPALIVED_PASSWORD='d0cker' \
-e KEEPALIVED_STATE='BACKUP' \
-e KEEPALIVED_ROUTER_ID='51' \
-e KEEPALIVED_UNICAST_PEERS="#PYTHON2BASH:['10.100.1.123','10.100.1.125']" \
-e KEEPALIVED_VIRTUAL_IPS='10.100.1.155' \
osixia/keepalived:2.0.20
四、Nginx状态监听脚本
#!/bin/bash
#检测nginx是否启动了
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ];then #如果nginx没有启动就启动nginx
docker restart nginx #重启nginx
sleep 5
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ];then #nginx重启失败,则停掉keepalived服务,进行VIP转移
docker stop keepalived
fi
fi
Nginx状态监听脚本,添加关闭Keepalived的命令,Nginx出现问题时能正常完成主备切换
五、验证服务
访问虚拟IP
关停 10.100.1.123 服务器的 Keepalived,再次访问
修复 10.100.1.123 服务器的 Keepalived,并一同重启 10.100.1.125 服务器的 Keepalived。
相关学习链接:
进入容器的keepalived目录下
/container/service/keepalived/assets
keepalived.conf
global_defs {
default_interface enp160
}
vrrp_script check_nginx {
script "/container/service/keepalived/assets/check_nginx.sh" #检测脚本文件
interval 3 #检测时间间隔
weight -10 #权重
}
vrrp_instance VI_1 {
interface enp160 #设置实例绑定的网卡
state BACKUP #设置实例初始状态,实际的MASTER和BACKUP是选举决定的
virtual_router_id 51 #同一实例下virtual_router_id必须相同
priority 120 #设置优先级,优先级高的会被竞选为Master
nopreempt #非抢占模式
unicast_peer { #单播模式,设置对端ip
10.100.1.123
}
virtual_ipaddress { #设置VIP,可以设置多个
10.100.1.155
}
authentication { #设置认证
auth_type PASS #认证方式,支持PASS和AH
auth_pass d0cker #认证密码
}
track_script { #设置追踪脚本
check_nginx
}
notify "/container/service/keepalived/assets/notify.sh"
}