Nginx集群架构图
环境准备
2台虚拟机(安装 Nginx,keepalived)
172.16.183.18 nginx keepalived
172.16.183.20 nginx keepalived
VIP:172.16.183.220(虚拟出来的ip,此ip必须在2台虚拟机的ip段范围内)
关闭防火墙(为了测试)
systemctl stop firewalld.service (关闭防火墙)
systemctl start firewalld.service (开启防火墙)
systemctl disable firewalld.service (禁止防火墙自启动)
systemctl enable firewalld.service (防火墙随系统开启启动)
在Nginx安装之前需要一些环境依赖库
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
Nginx 安装步骤
- 直接下载.tar.gz安装包,下载地址https://nginx.org/en/download.html
cd /usr/local/soft/
wgte https://nginx.org/download/nginx-1.21.6.tar.gz
- 解压
tar xvf nginx-1.21.6.tar.gz
cd nginx-1.21.6
- 配置(带有https模块)
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
- 编译和安装
编译:make
安装:make install
- 设置开机启动
编辑服务文件
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx service
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
- 加入开机自启动
systemctl enable nginx.service
服务的启动/停止/刷新配置文件/查看状态
#启动nginx服务
systemctl start nginx.service
#停止服务
systemctl stop nginx.service
#重新启动服务
systemctl restart nginx.service
#查看所有已启动的服务
systemctl list-units --type=service
#查看服务当前状态
systemctl status nginx.service
#设置开机自启动
systemctl enable nginx.service
# 取消开机自启动
systemctl disable nginx.service
- 查看Nginx启动状态
- 查看启动页面
在2台机器上重复操作即可
至此 Nginx安装完毕
keepliaved安装步骤
- yum安装命令
yum -y install keepalived
安装路径
/etc/keepalived/
keepalived.conf 配置
- 查看本虚拟机网卡
查看的原因是要确定VIP,只要虚假出来的ip在机器的ip段即可
- keepalived.conf文件配置
主节点
! Configuration File for keepalived
global_defs {
#设备标识
router_id LVS_DEVEL
script_user root
enable_script_security
}
vrrp_script chk_nginx {
#健康检查nginx状态的脚本文件
script "/etc/keepalived/nginx_check.sh"
#检测脚本执行的间隔时间
interval 2
weight -20
# 运行脚本的用户和组。
user root root
}
#虚拟IP的配置
vrrp_instance VI_1 {
#服务器属性(主)
state MASTER
#网卡名称
interface ens33
#主备机的虚拟路由器ID即VRRP-ID必须相同
virtual_router_id 51
#主备机配置不同优先级,主机优先级>备机优先级
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
#按照指定的网卡ip段随意写的ip
172.16.183.220
}
#调用健康监测模板,一定要放在vip的配置下面
track_script {
chk_nginx
}
}
备节点
! Configuration File for keepalived
global_defs {
#设备标识
router_id LVS_DEVEL
script_user root
enable_script_security
}
vrrp_script chk_nginx {
#健康检查nginx状态的脚本文件
script "/etc/keepalived/nginx_check.sh"
#检测脚本执行的间隔时间
interval 2
weight -20
# 运行脚本的用户和组。
user root root
}
#虚拟IP的配置
vrrp_instance VI_1 {
#服务器属性(备)
state BACKUP
#网卡名称
interface ens33
#主备机的虚拟路由器ID即VRRP-ID必须相同
virtual_router_id 51
#主备机配置不同优先级,主机优先级>备机优先级
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
#按照指定的网卡ip段随意写的ip
172.16.183.220
}
#调用健康监测模板,一定要放在vip的配置下面
track_script {
chk_nginx
}
}
- nginx_check.sh编辑
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
/usr/local/nginx/sbin/nginx
sleep 2
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
killall -9 keepalived
fi
fi
如果没有安装kiilall 命令,则安装命令:yum install -y psmisc
编辑好之后,放在/etc/keepalived目录下并给执行权限
chmod 755?nginx_check.sh
- 设置开机启动
#设置开机启动
systemctl enable keepalived.service
#启动keepalived服务
systemctl start keepalived.service
#停止keepalived服务
systemctl stop keepalived.service
#查看keepalived服务状态
systemctl status keepalived.service
访问Nginx集群
在浏览器上输入:http://172.16.183.220/
故障转移
Nginx + keepalived 组成的高可用集群,默认访问主节点,当主节点出现故障后,从节点自动切换为主节点,此后的访问在备节点上,当主节点再次启动后正常后,此后的访问节点回到主节点。
本示例使用ip:172.16.183.18这台作为主节点,ip:172.16.183.20作为备节点
- 正常访问集群效果如下:
- 当主节点停机或者挂掉后,访问备节点效果如下:
- 当主节点恢复正常后访问效果如下:
自此Nginx + keepalived 高可用集群搭建完成