Nginx+Keepalived实现Nginx高可用负载均衡
一、说明
在架构设计中,可以利用NGINX的反向代理和负载均衡实现后端应用的高可用性,同时我们还需要考虑Nginx的单点故障。真正做到架构高可用性。
主要考虑以下几点:
1、Nginx服务因为意外现象挂掉
2、服务器宕机导致NGINX不可用
二、环境准备
# 准备四台机器,两台nginx主备,两台web
环境名称 | 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) |
三、安装配置相关服务
1、安装nginx(192.168.182.128,192.168.182.129)
# 添加Nginx到YUM源
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.servicesystemctl 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 { } notification_email_from 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 { } notification_email_from 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
~~~以上就是nginx+keepalived高可用负载均衡了~~~
相关推荐
liupengqwert 2020-10-28
快乐de馒头 2020-06-16
CurrentJ 2020-05-12
swtIrene 2020-08-14
slovyz 2020-08-14
tinydu 2020-08-03
yungame 2020-07-04
廖先贵 2020-06-23
畅聊架构 2020-06-10
魏莉的微 2020-06-07
后厂村老司机 2020-06-01
后厂村老司机 2020-05-08
azhuye 2020-11-12
YzhilongY 2020-08-31
crazyjingling 2020-08-16
tinydu 2020-08-09
Studynutlee 2020-08-03
快乐de馒头 2020-07-29