zoukankan      html  css  js  c++  java
  • Nginx+keepalived 实现Nginx的高可用

    nginx实现请求的负载均衡 + keepalived实现nginx的高可用

    前言

      使用集群是网站解决高并发、海量数据问题的常用手段。当一台服务器的处理能力、存储空间不足时,不要企图去换更强大的服务器,对大型网站而言,不管多么强大的服务器,都满足不了网站持续增长的业务需求。这种情况下,更恰当的做法是增加一台服务器分担原有服务器的访问及存储压力。通过负载均衡调度服务器,将来自浏览器的访问请求分发到应用服务器集群中的任何一台服务器上,如果有更多的用户,就在集群中加入更多的应用服务器,使应用服务器的负载压力不再成为整个网站的瓶颈。

    摘自《大型网站技术架构_核心原理与案例分析》

    环境准备

      server1    192.168.200.111:nginx + keepalived   master

      server2    192.168.200.112:nginx + keepalived   backup

      server3    192.168.200.113:tomcat

      server4    192.168.200.114:tomcat

      虚拟ip(VIP):192.168.200.254,对外提供服务的ip,也可称作浮动ip

      各个组件之间的关系图如下:

    tomcat做应用服务器

    nginx做负载均衡

      nginx.conf内容如下

    user  nginx nginx;            
    worker_processes  2; 
    worker_cpu_affinity 01 10;
    worker_rlimit_nofile 102400;
    error_log logs/error.log; pid logs/nginx.pid; events { use epoll; worker_connections 4096; }
    http { include mime.types; default_type application/octet-stream;

          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  logs/access.log  main;              

          sendfile        on;                                            

          keepalive_timeout  65; 

        upstream tomcat_pool {
         server 192.168.200.112:8080 weight=4 max_fails=2 fail_timeout=30s;   server 192.168.200.113:8080 weight=4 max_fails=2 fail_timeout=30s; } server { listen 80; server_name localhost; charset utf-8;  location / { proxy_pass http://tomcat_pool;
    proxy_set_header Host $http_host; } location ~ .(jsp|jspx|dp)$ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_pass http://tomcat_pool; } location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { expires 30d; }
    location ~ .*.(js|css)${ expires 1h; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }

      配置好后,启动nginx,路径要写自己的

       /usr/local/nginx/sbin/nginx

    keepalived实现nginx高可用(HA)

      keepalived主要起到两个作用:实现VIP到本地ip的映射; 以及检测nginx状态。

      master上的keepalived.conf内容如下:

    global_defs {
        notification_email {
            997914490@qq.com
        }
        notification_email_from sns-lvs@gmail.com
        smtp_server smtp.hysec.com
        smtp_connection_timeout 30
        router_id nginx_master        # 设置nginx master的id,在一个网络应该是唯一的
    }
    vrrp_script chk_http_port {             #引用脚本文件
        script "/usr/local/check_nginx_pid.sh"    #脚本文件绝对路径
        interval 2                          #(检测脚本执行的间隔,单位是秒)
        weight -20                          # 权重要比 BACKUP 小
    }
    vrrp_instance VI_1 {
        state MASTER            # 指定keepalived的角色,MASTER为主,BACKUP为备
        interface ens32            # 当前进行vrrp通讯的网络接口卡(当前centos的网卡)
        virtual_router_id 66        # 虚拟路由编号,主从要相同
        priority 100            # 优先级,数值越大,获取处理请求的优先级越高
        advert_int 1            # 检查间隔,默认为1s(vrrp组播周期秒数)
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        track_script {
        chk_http_port            #(调用检测脚本)
        }
        virtual_ipaddress {
            192.168.200.254           # 定义虚拟ip(VIP),可多设,每行一个
        }
    }

      backup上的keepalived.conf内容如下:

    global_defs {
        notification_email {
            997914490@qq.com
        }
        notification_email_from sns-lvs@gmail.com
        smtp_server smtp.hysec.com
        smtp_connection_timeout 30
        router_id nginx_backup              # 设置nginx backup的id,在一个网络应该是唯一的
    }
    vrrp_script chk_http_port {
        script "/usr/local/check_nginx_pid.sh"
        interval 2                          #(检测脚本执行的间隔)
        weight -20
    }
    vrrp_instance VI_1 {
        state BACKUP                        # 指定keepalived的角色,MASTER为主,BACKUP为备
        interface ens32                      # 当前进行vrrp通讯的网络接口卡(当前centos的网卡)
        virtual_router_id 66                # 虚拟路由编号,主从要相同
        priority 90                         # 优先级,数值越大,获取处理请求的优先级越高
        advert_int 1                        # 检查间隔,默认为1s(vrrp组播周期秒数)
        authentication {
            auth_type PASS
            auth_pass 1111
        }
        track_script {
            chk_http_port                   #(调用检测脚本)
        }
        virtual_ipaddress {
            192.168.200.254                  # 定义虚拟ip(VIP),可多设,每行一个
        }
    }

      nginx检测脚本check_nginx_pid.sh内容如下:

    #!/bin/bash
    count=$(ps -C nginx --no-header |wc -l)      
    if [ $count -eq 0 ];then                            
        /usr/local/nginx/sbin/nginx                #重启nginx
        if [ $(ps -C nginx --no-header |wc -l) -eq 0 ];then    #nginx重启失败
            systemctl stop keepalived
        fi
    fi

      脚本加权限

        chmod +x /usr/local/check_nginx_pid.sh

       启动keepalived

     systemctl start keepalived

     

  • 相关阅读:
    0102-进程操作(面向对象简单工厂模式,打开输入文件)
    0101-进程操作(变量命名)
    win10无法成功完成操作因为文件包含病毒或潜在的垃圾软件如何处理
    序列化和反序列化
    __slot__的用法
    python中typeguard包
    pandas如何将多个DataFrame写入同一个excel工作簿中
    DEAP库遗传算法
    【教程】如何把喜马拉雅音频下载到电脑
    oracle安装路径查看方式
  • 原文地址:https://www.cnblogs.com/2567xl/p/11628335.html
Copyright © 2011-2022 走看看