Nginx + keepalived联动部署
一个合格的集群应该具备以下哪些特征:
- 负载均衡:LVS , Nginx ,Haproxy ,F5
- 健康检查 : for 调度器/节点服务器 Keepalived Heartbeat
- 故障转移 主备切换 : Keepalived解决
操作内容
7-1:nginx+keepalived 联动1 7-5:nginx+keepalived 联动2 7-2 nginx web服务1 7-3 nginx web服务2 7-4 nfs文件共享服务
注:7-2 ; 7-3; 7-4;再LVS+DR模式那边有详细步骤这里不做多余阐述
1.先配置7-1,7-5nginx反向代理
关闭防火墙 nginx反向代理两台机器关闭

两台机器安装nginx
ini
mount /dev/sr0 /mnt
cd /etc/yum.repos.d/
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
yum -y install nginx

🥟

7-5与7-1同样的操作安装nginx
csharp
[root@localhost ~]# vim /etc/yum.repos.d/nginx.repo
[root@localhost ~]# yum -y install nginx

通过yum安装的自带yum模块 nginx -V查看
去7-1nginx配置
ini
cd /etc/nginx/
vim nginx.conf
stream {
upstream backend {
server 192.168.47.102:80;
server 192.168.47.103:80;
}
server {
listen 8080;
proxy_pass backend;
}
}
#用nginx -t检测语法是否有问题
#用netstat查看
nginx是否被误开启,先判断是否是systemctl start开启的,不是就killall nginx
然后再用systemctl start nginx开启,通过systemctl管理
netstat -lntp |grep nginx

🍗

验证访问
192.168.47.100:8080/test.html

bash
scp nginx.conf 192.168.47.105:`pwd`
#将7-1的配置文件,远程拷贝到7-5去

接下来去配置7-5的nginx
bash
systemctl start nginx
#开启nginx

验证

2.两台nginx联合做高可用,实现故障转移
去7-1,7-5安装keepalived
bash
yum -y install keepalived.x86_64
#安装高可用软件

更改keepalived配置
7-1:

然后启动keepalived服务

3.实现 健康检查配置
写一个脚本当nginx一台挂了,会自动将其拉起来
bash
cd /etc/keepalived/
vim check_nginx.sh
#!/bin/bash
if ! killall -0 nginx &> /dev/null
then
systemctl stop keepalived
fi

bash
systemctl start nginx
systemctl status nginx
#启动nginx
chmod +x check_nginx.sh
#给脚本加执行权限
./check_nginx.sh
#执行脚本



再去修改配置文件
sql
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2
weight -5
}
track_script {
check_nginx
}

将7-1的check_nginx.sh keepalived.conf远程拷贝到7-5
bash
scp check_nginx.sh keepalived.conf 192.168.47.105:`pwd`

去7-5修改配置文件

先启动7-1,7-5的nginx

再启动7-1,7-5的leepalived

先启动keepalived 会导致运行脚本 关闭nginx,nginx被关闭所以keepalived跟着就会被关闭,所以要先启动nginx,在启动keepalived
注意启动顺序

查看vipd地址是否出现

测试访问vip地址
http://192.168.47.200:8080/test.html


假如现在nginx的主异常关闭了
arduino
关闭7-1ngin 服务
systemctl stop nginx
keepalived 随着nginx关闭也跟着一起关闭了

去7-5 ip a查看vip第地址是否存在

测试不受影响正常访问
