zoukankan      html  css  js  c++  java
  • Day 58 应用工具_nginx集群及分发

    Nginx集群

    集群介绍

    传统web访问模型

    完成一次请求的步骤
    1)用户发起请求
    2)服务器接受请求
    3)服务器处理理请求(压力最大)
    4)服务器响应请求
    缺点:单点故障
    单台服务器资源有限
    单台服务器处理耗时长

    单点故障解决⽅方案

    1) 部署一台备份服务器,宕机直接切换
    2) 部署多台服务器,根据DNS的轮询解析机制去实现用户分发

    问题:
    1方案:服务器利用率低,成本高,切换不不及时,服务器压力依然大
    2方案: 优势是用户处理速度得到了提升,但是当其中一台故障,就会有一部分用户访问不了网站

    并行处理解决方案

    1) 上述的DNS轮询解析方案
    2)多机阵列---集群模式

     集群

    将多个物理机器组成一个逻辑计算机,实现负载均衡和容错
    •  计算机集群简称集群,是一种计算机系统, 它通过一组松散集成的计算机软件或硬件连接起来高度紧密地协作完成计算
    工作。在某种意义上,他们可以被看作是一台计算机。 (百度解释)
    •  组成要素
    1)VIP: ⼀一个IP地址
    2)分发器器: nginx
    3)数据服务器器: Web服务器器

    Nginx集群原理

    Nginx集群
    •  在该集群中Nginx扮演的角色是: 分发器
    •  任务:接受请求、分发请求、响应请求
    •  功能模块:

    1) ngx_http_upstream_module 基于应用层分发模块
    2) ngx_stream_core_module 基于传输层分发模块 (1.9开始提供)

    Nginx集群其实是:虚拟主机+反向代理+upstream分发模块组成的
    虚拟主机:接受和响应请求
    反向代理: 带用户去数据服务器拿数据
    upstream: 告诉Nginx去哪个数据服务器拿数据
    •  数据⾛走向
    1)虚拟主机接受用户请求
    2)虚拟主机去找反向代理
    3) 反向代理让去找upstream
    4)upstream 告诉 ⼀一个数据服务器IP
    5)Nginx去找数据服务器并发起用户的请求
    6) 数据服务器接受请求并处理请求
    7)数据服务器响应请求给Nginx
    8)Nginx响应请求给用户

    Nginx集群实现

    声明

    •  实验机器器: Vmware 虚拟机 2核4G
    •  ⽹网卡:桥接
    •  系统:centos7.5
    •  防火墙:关闭
    •  Selinux:关闭
    •  ⽹网段:192.168.10.0/24
    主机名                            IP                    角色
    Master.ayitula.com 192.168.10.40 主分发器
    Backup.ayitula.com 192.168.10.41 备分发器
    Web01.ayitula.com 192.168.10.42 数据服务器1
    Web02.ayitula.com 192.168.10.43 数据服务器2

    配置一个web集群

    1) Nginx 安装
    2) 配置业务服务器页面
    3)配置Nginx分发器
    4) 测试分发

    配置web业务机器

    #web02
    [root@web02 ~]# sh nginx_install
    [root@web02 ~]# echo web02 > /usr/local/nginx/html/index.html
    [root@web02 ~]# yum -y install elinks &>/dev/null
    [root@web02 ~]# /usr/local/nginx/sbin/nginx
    [root@web02 ~]# elinks http://localhost -dump
    web02

    配置分发器

    Syntax: upstream name { ... }
    Default: —
    Context: http
    #upstream 模块
    upstream web {
        server 192.168.10.42;
        server 192.168.10.43;
    }
    server {
        listen 80;
        server_name localhost;
        location / {
            proxy_pass http://web;
            }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
            }
        }
    }

    Nginx集群测试

    集群分发测试
    [root@web02 ~]# elinks http://192.168.10.40 -dump
    web01
    [root@web02 ~]# elinks http://192.168.10.40 -dump
    web02
    [root@web02 ~]# elinks http://192.168.10.40 -dump
    web01
    [root@web02 ~]# elinks http://192.168.10.40 -dump
    web02

    Nginx 分发算法

    集群分发算法介绍

    分发算法
    如何将用户请求按照一定的规律分发给业务服务器
    •  思考
    假如你有1000块钱,你怎么分配给身边的3个好朋友呢?

    Nginx集群默认算法

    upstream module
    nginx的upstream目前支持4种方式的分配
    1、轮询(默认)
    每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
    2、weight
    指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
    3、ip_hash
    每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
    4、fair(第三⽅方)
    按后端服务器的响应时间来分配请求,响应时间短的优先分配。
    5、url_hash(第三⽅方)
    按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

    Nginx业务服务器状态

    每个设备的状态设置为:
    1.down 表示单前的server暂时不参与负载
    2.weight 默认为1.weight越大,负载的权重就越大。
    3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
    4.fail_timeout: 失败超时时间,在连接Server时,如果在超时间之内超过max_fails指定的失败次数,会认为在fail_timeout时间内Server不可用。默认为10s。
    5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

    基于请求头的分发

    1)基于host分发
    2)基于开发语言分发
    3)基于浏览器的分发
    4)  基于源ip

    Nginx算法测试

    声明

    •  实验机器器: Vmware 虚拟机 2核4G
    •  ⽹网卡:桥接
    •  系统:centos7.5
    •  防火墙:关闭
    •  Selinux:关闭
    •  ⽹网段:192.168.10.0/24
    主机名                            IP                    角色
    Master.ayitula.com 192.168.10.40 主分发器
    Backup.ayitula.com 192.168.10.41 备分发器
    Web01.ayitula.com 192.168.10.42 数据服务器1
    Web02.ayitula.com 192.168.10.43 数据服务器2

    轮询分发

    upstream web {
        server 192.168.10.42;
        server 192.168.10.43;
    }
    server {
        listen 80;
        server_name localhost;
        location / {
            proxy_pass http://web;
        }
    }

    基于权重的分发

    upstream web {
        server 192.168.10.42 weight=1;
        server 192.168.10.43 weight=2;
    }
    server {
        listen 80;
        server_name localhost;
        location / {
            proxy_pass http://web;
        }
    }

    ip_hash

    upstream web {
        ip_hash;
        server 192.168.10.42;
        server 192.168.10.43;
    }
    server {
        listen 80;
        server_name localhost;
        location / {
            proxy_pass http://web;
        }
    }

    ip_hash算法能够保证来自同样源地址的请求,都分发到同一台主机

    基于host分发

    http {
        upstream web1 {
            server 192.168.10.42;
        }
        upstream web2 {
            server 192.168.10.43;
        }
        server {
            listen 80;
            server_name www.web1.com;
            location / {
                proxy_pass http://web1; 
            }
        }
        server {
            listen 80;
            server_name www.web2.com;
            location / {
                proxy_pass http://web2; 
            }
        }
    }

    基于开发语言分发

    http {
        upstream php {
            server 192.168.10.42;
        }
        upstream html {
            server 192.168.10.43;
        }
        server {
            location ~* .php$ {
                proxy_pass http://php;
            }
        }
        location ~* .html$ {
            proxy_pass http://html;
        }
    }

    基于浏览器分发

    upstream elinks { server 192.168.10.42; }
    upstream chrome { server 192.168.10.43; }
    upstream any { server 192.168.10.42:81; }
    server {
        listen 80;
        server_name www.web1.com;
        location / {
            proxy_pass http://any;
            if ( $http_user_agent ~* Elinks ) {
            proxy_pass http://elinks;
            }
        if ( $http_user_agent ~* chrome ) {
            proxy_pass http://chrome;
            }
        }
    }

    基于源ip分发

    upstream bj.server {
        server 192.168.10.42;
    }
    upstream sh.server {
        server 192.168.10.43;
    }
    upstream default.server {
        server 192.168.10.42:81;
    }
    geo $geo {
        default default;
        192.168.10.241/32 bj;
        192.168.10.242/32 sh;
    }
    location / {
        proxy_pass http://$geo.server$request_uri;
    }
  • 相关阅读:
    eclipse-SDK-3.7-win32;eclipse-java-indigo-win32;eclipse-jee-indigo-win32 区别(ZZ)
    Marketplace Client- Download
    Log4J2基本配置
    Map 迭代 两种方法
    Python Argparse模块
    Python操作Memcached
    MySQL参数调优
    Nginx调优
    JavaScript知识点总结[部分]
    python optparser模块
  • 原文地址:https://www.cnblogs.com/ysging/p/12869492.html
Copyright © 2011-2022 走看看