centos nginx重启_Nginx+Keepalived 实现 Nginx 高可用负载均衡

猿码利剑

出处 :http://dwz.date/a85w
一、说明

在架构设计中,可以利用 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.131web应用(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.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 231b27e5d9d41e44b794a3150e552dfa.gif

●招人(内推岗)

●资料分享

●zhao'ren

●停课不停学,线上免费课程在家也能轻松学习

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值