Nginx keepalived一主一从高可用,手把手带你一步一步配置!
本文作者:墨篱弦
文章目录
一、 实验环境
1.准备两台NGINX环境的服务器
- Nginx40.14 IPADDR=192.168.40.14
- Nginx_40.15 IPADDR=192.168.40.15
2.准备一台tomcat服务器
- Docker40.16 Tomcat8081 192.168.40.16:8081
- Tomcat8082 192.168.40.16:8082
二、 配置环境
Nginx40.14
IPADDR=192.168.40.14
Keepalived.conf配置:
vrrp_instance VI_1
state MASTER
interface ens33
virtual_router_id 17
priority 150
virtual_ipaddress 192.168.40.17
- 1
- 2
- 3
- 4
- 5
- 6
Nginx_40.15
IPADDR=192.168.40.15
Keepalived.conf配置:
vrrp_instance VI_1
state BACKUP
interface ens33
virtual_router_id 17
priority 99
virtual_ipaddress 192.168.40.17
- 1
- 2
- 3
- 4
- 5
- 6
Docker40.16
利用docker部署两个tomcat容器,并启动容器。
Tomcat8081
192.168.40.16:8081
Tomcat8082
192.168.40.16:8082
- 1
- 2
- 3
- 4
- 5
三、 Keepalived安装部署
Keepalived下载地址https://www.keepalived.org/download.html
,下载keepalived-2.1.5.tar.gz安装包
1. 安装依赖环境
yum –y install curl gcc openssl-devel libnl3-devel net-snmp-devel libnfnetlink-devel
- 1
2. 创建目录/data/keepalived
mkdir /software
- 1
此为软件存放目录
3. 上传安装包到/software
4. 解压安装包
tar -zxvf keepalived-2.1.5.tar.gz
- 1
5. 进入keepalived-2.1.5目录
cd keepalived-2.1.5
./configure --prefix=/data/keepalived --sysconf=/etc
- 1
- 2
6. 编译安装
make & manke install
- 1
确保依赖都安装的情况下更换keepalived低版本
四、 进入安装后的路径
cd /data/keepalived/
- 1
1. 创建软连接
ln -s sbin/keepalived /sbin/
- 1
2. 覆盖软连接
ln -snf sbin/keepalived /sbin
- 1
3. 复制运行命令
cp /software/keepalived-2.1.5/keepalived/etc/init.d/keepalived /etc/init.d
chkconfig --add keepalived
- 1
- 2
4. 添加到系统服务
chkconfig keepalived on
- 1
5. 启动服务
service keepalived start
- 1
6. 查看启动状态
service keepalived status
- 1
五、 修改keepalived 的配置
Master配置
vim /etc/keepalived/
(最小配置)
! Configuration File for keepalived
global_defs {
router_id NGINX
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 17
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.40.17
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
Backup配置
vim /etc/keepalived/
(最小配置)
! Configuration File for keepalived
global_defs {
router_id NGINX
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 17
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.40.17
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
六、 启动nginx
/usr/local/nginx/sbin/nginx
- 1
启动keepalived
service keepalived start
- 1
查看网卡是否获取到虚拟IP
Ip add
- 1
验证试验
curl http://192.168.40.17
- 1
七、 验证冗余
手动停止 nginx服务
/usr/local/nginx/sbin/nginx –s stop
- 1
网页无法开启
手动停止keepalived服务
service keepalived stop
- 1
网页正常
八、 利用脚本实现自动切换
1. Keepalived检测nginx运行状态的脚本
#!/bin/bash
A=`ps -C nginx --no-header|wc -l` -->判断nginx的服务进程数并统计
if [ $A -eq 0 ];then -->假如数量为0则执行以下脚本
/usr/local/nginx/sbin/./nginx
sleep 2 -->进程休眠2秒
if [ `ps -C nginx --no-header|wc -l` -eq 0 ];then -->假如nginx服务进程总数还是0则执行以下脚本
killall keepalived -->结束keepalived所有进程
fi
fi
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
2. 修改keepalived.conf配置
Nginx-keepalived-MASTER
! Configuration File for keepalived
global_defs {
router_id NGINX #路由标识同一个主备必须相同
}
vrrp_script chk_nginx {
script "/root/chk_nginx.sh" #检测脚本的路径
interval 2 #脚本执行间隔
weight -5 #失败一次自己优先级降低5
}
vrrp_instance VI_1 {
state MASTER #节点状态,主节点为MASTER,备节点为BACKUP
interface ens33 #vip绑定的网络接口
virtual_router_id 17 #虚拟路由ID,方便记录可以使用IP最后一位VRID区间是0~255正证书
priority 100 #节点优先级,优先级是0~255正整数
advert_int 1
authentication { #认证
auth_type PASS #明文认证
auth_pass 1111 #认证密码
}
virtual_ipaddress {
192.168.40.17/24 #虚拟IP/掩码
}
track script { #追踪脚本
chk_nginx
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
Nginx-keepalived-BACKUP
! Configuration File for keepalived
global_defs {
router_id NGINX #路由标识同一个主备必须相同
}
vrrp_instance VI_1 {
state BACKUP #节点状态,主节点为MASTER,备节点为BACKUP
interface ens33 #vip绑定的网络接口
virtual_router_id 17 #虚拟路由ID,方便记录可以使用IP最后一位VRID区间是0~255正证书
priority 99 #节点优先级,优先级是0~255正整数
advert_int 1
authentication { #认证
auth_type PASS #明文认证
auth_pass 1111 #认证密码
}
virtual_ipaddress {
192.168.40.17/24 #虚拟IP/掩码
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
九、 测试keepalived利用脚本能否让nginx自动重启
停止nginx
/usr/local/nginx/sbin/nginx -s stop
- 1
查看进程
netstat -nulpt
netstat –nulpt
查看网页状态
修改nginx配置使nginx无法启动验证主备是否会自动切换
查看主备网络接口信息
Nginx-MASTER
Nginx-BACKUP
Kill -9 nginx
进程
有可能需要kill两边
查看网口信息
ip a
- 1
验证网页
查看nginx-BACKUP的网络接口信息
再次恢复nginx-MASTER的nginx.conf文件配置,重启keepalived,验证主服务器正常上线后是否抢占MASTER
Keepalived+nginx 主备能自动切换。
文章来源: blog.csdn.net,作者:wljslmz,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/weixin_43025343/article/details/109112117
- 点赞
- 收藏
- 关注作者
评论(0)