一、说明 在架构设计中,可以利用 Nginx 的反向代理和负载均衡实现后端应用的高可用性,同时我们还需要考虑 Nginx 的单点故障。真正做到架构高可用性。 主要考虑以下几点: 1、Nginx 服务因为意外现象挂掉 2、服务器宕机导致 Nginx 不可用 二、环境准备 准备四台机器,两台 Nginx 主备,两台 Web猿码利剑
出处 :http://dwz.date/a85w
环境名称 | IP 地址 | 环境介绍 |
nginx备+keepalived 主 | 192.168.182.128 | 反向代理 nginx高可用主;VIP:192.168.182.230 |
nginx 主+keepalived主 | 192.168.182.129 | 反向代理 nginx高可用备;VIP:192.168.182.230 |
web 应用服务器 | 192.168.182.130 | web应用(apache) |
web 应用服务器 | 192.168.182.131 | web应用(apache) |
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
安装 Nginx
yum install -y nginx
启动服务
systemctl start nginx.service
2、安装 Web 服务,Apache(192.168.182.130,192.168.182.131)
# 关闭防护墙
systemctl stop firewalld.service
systemctl enable firewalld.service
# 安装
yum -y install httpd
# 修改默认页(192.168.182.130)
echo '192.168.182.130'> /usr/share/httpd/noindex/index.html
# 修改默认页(192.168.182.131)
echo '192.168.182.131'> /usr/share/httpd/noindex/index.html
# 启动服务
systemctl start httpd.service
3、配置 Nginx 反向代理,实现负载均衡
配置文件
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
#include /etc/nginx/conf.d/*.conf;
upstream myServer {
server 192.168.182.130:80 weight=1;
server 192.168.182.131:80 weight=1;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
# root /usr/share/nginx/html;
# include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://myServer;
index index.html index.htm;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
重启 Nginx 服务器
systemctl restart nginx.service
测试
C:\Users\29209>curl 192.168.182.128
192.168.182.130
C:\Users\29209>curl 192.168.182.128
192.168.182.131
C:\Users\29209>curl 192.168.182.128
192.168.182.130
C:\Users\29209>curl 192.168.182.128
192.168.182.131
3、安装配置keepalived,实现高可用(192.168.182.130,192.168.182.131)
yum install -y keepalived
配置主
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id nginx01 # router_id 唯一标识符
vrrp_skip_check_adv_addr
# vrrp_strict # 如果访问不了VIP,就去掉,这里我注释掉了
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script check_nginx {
script "/etc/keepalived/chknginx.sh" #nginx服务检查脚本
interval 1
weight -2
}
vrrp_instance VI_1 {
state MASTER
interface ens33 # 网卡名
virtual_router_id 52 # 默认为51 配置完发现主备切换有问题 更改为52 发现好了 原因未知
priority 150 # 主备的优先级priority,数字越大,优先级越高
advert_int 1 # 检查时间1秒
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.182.230/24 dev ens33 label ens33:7 #vip地址
}
}
}
检测脚本文件/etc/keepalived/chknginx.sh
#!/bin/bash
# auto check nginx process
# 20200425 by zxg
killall -0 nginx
if [[ $? -ne 0 ]];then
systemctl stop keepalived.service #这个这里应该有问题,yum安装的怎么停掉服务那?
fi
配置备
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id nginx01 # router_id 唯一标识符
vrrp_skip_check_adv_addr
# vrrp_strict # 如果访问不了VIP就去掉,这里我注释掉了
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script check_nginx {
script "/etc/keepalived/chknginx.sh" #nginx服务检查脚本
interval 1
weight -2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33 #网卡
virtual_router_id 52 #默认为51 配置完发现主备切换有问题 更改为52 发现好了 原因未知
priority 100 #主备的优先级priority
advert_int 1 #检查时间1秒
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.182.230/24 dev ens33 label ens33:7 #vip地址
}
}
}
重启 keepalived 服务
systemctl restart keepalived.service
测试负载均衡
访问 VIP:192.168.182.230
C:\Users\29209>curl 192.168.182.230
192.168.182.130
C:\Users\29209>curl 192.168.182.230
192.168.182.131
C:\Users\29209>curl 192.168.182.230
192.168.182.130
C:\Users\29209>curl 192.168.182.230
192.168.182.131
测试高可用
# 停掉nginx,因为有检测脚本,自然也会停掉keepalived服务
systemctl stop nginx
# 当然也可以直接stopkeepalived服务
systemctl stop keepalived.service
# 接下来就查看VIP是否飘逸到备机器上,但是对客户而已,是没有任何影响,依旧是访问VIP
ifconfig ens33:7
扫码关注我们
专注于运维开发
![5edec75b61a49fe202622c02f95ef462.png](https://i-blog.csdnimg.cn/blog_migrate/382b043d9e05a5178737a701d1bc6690.jpeg)
![231b27e5d9d41e44b794a3150e552dfa.gif](https://i-blog.csdnimg.cn/blog_migrate/69b76251ea79e7ff1af614bc8da4e5e2.gif)
●招人(内推岗)
●资料分享
●zhao'ren
●停课不停学,线上免费课程在家也能轻松学习