zoukankan      html  css  js  c++  java
  • Nginx负载均衡

    • 环境准备

    四台服务器、一台客户机、一台nginx负载均衡器、三台web(其中一台web主机为备节点)

    • 安装软件

    三台web(使用nginx或apache)

    web01

    [root@web01 /]# /etc/init.d/iptables stop
    iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
    iptables: Flushing firewall rules:                         [  OK  ]
    iptables: Unloading modules:                               [  OK  ]
    
    [root@web01 /]# yum -y install httpd
    [root@web01 /]# vim /etc/httpd/conf/httpd.conf 
    ServerName  www.web01.com:80
    [root@web01 /]# echo "web01_192.168.119.130" > /var/www/html/index.html
    [root@web01 /]# /etc/init.d/httpd start
    Starting httpd:                                            [  OK  ]
    [root@web01 /]# curl 192.168.119.130
    web01_192.168.119.130
    [root@web01 /]# 
    [root@web01 /]# curl 192.168.119.130
    web01_192.168.119.130

    web02

    [root@web02 /]# /etc/init.d/iptables stop
    iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
    iptables: Flushing firewall rules:                         [  OK  ]
    iptables: Unloading modules:                               [  OK  ]
    
    [root@web02 /]# yum -y install httpd
    [root@web02 /]# vim /etc/httpd/conf/httpd.conf 
    ServerName  www.web02.com:80
    [root@web02 /]# echo "web02_192.168.119.133" > /var/www/html/index.html
    [root@web02 /]# /etc/init.d/httpd start
    Starting httpd:                                            [  OK  ]
    [root@web02 /]# curl 192.168.119.133
    web02_192.168.119.133

    web_backup

    [root@web_backup /]# /etc/init.d/iptables stop
    iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
    iptables: Flushing firewall rules:                         [  OK  ]
    iptables: Unloading modules:                               [  OK  ]
    
    [root@web_backup /]# yum -y install httpd
    [root@web_backup /]# vim /etc/httpd/conf/httpd.conf
    ServerName www.web03.com:80
    [root@web_backup /]# echo "web03_backup" > /var/www/html/index.html
    [root@web_backup /]# /etc/init.d/httpd start
    Starting httpd:                                            [  OK  ]
    [root@web_backup /]# curl 192.168.119.131
    web03_backup

    LB nginx负载均衡器

    [root@lb01 conf]# useradd nginx -s /sbin/nologin -M
    [root@lb01 /]#wget http://nginx.org/download/nginx-1.6.3.tar.gz [root@lb01 /]#yum -y install pcre pcre-devel [root@lb01 /]#yum -y install gcc gcc-c++ [root@lb01 /]#yum -y install openssl openssl-devel [root@lb01 /]#tar zxvf nginx-1.6.3.tar.gz [root@lb01 /]#cd nginx-1.6.3 [root@lb01 nginx-1.6.3]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module [root@lb01 nginx-1.6.3]#make && make install [root@lb01 nginx-1.6.3]#ln -s /usr/local/nginx/sbin/* /usr/local/sbin [root@lb01 nginx-1.6.3]# cd /usr/local/nginx/conf/ [root@lb01 conf]# ll nginx.conf nginx.conf.default -rw-r--r--. 1 root root 576 Sep 26 07:20 nginx.conf -rw-r--r--. 1 root root 2656 Sep 26 06:33 nginx.conf.default [root@lb01 conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf [root@lb01 conf]#vim nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream web_pools { server 192.168.119.130:80 weight=5; server 192.168.119.133:80 weight=5; server 192.168.119.131:80 weight=5 backup; } server { listen 80; server_name www.test.com; location / { root html; index index.html index.htm; proxy_pass http://web_pools; } } } [root@lb01 conf]#cd / [root@lb01 /]#vim /etc/hosts 192.168.119.128 www.test.com [root@lb01 /]# nginx #启动服务 [root@lb01 /]#

    测试

    [root@lb01 /]# curl www.test.com
    web02_192.168.119.133
    [root@lb01 /]# curl www.test.com
    web01_192.168.119.130
    [root@lb01 /]# curl www.test.com
    web01_192.168.119.130
    [root@lb01 /]# curl www.test.com
    web02_192.168.119.133
    [root@lb01 /]# curl www.test.com
    web01_192.168.119.130
    [root@lb01 /]# curl www.test.com
    web02_192.168.119.133
    [root@lb01 /]# curl www.test.com
    web01_192.168.119.130

    测试backup是否正常

    先关闭web01、web02的web服务

    [root@web01 /]# /etc/init.d/httpd stop
    Stopping httpd:                                            [  OK  ]
    [root@web02 /]# /etc/init.d/httpd stop
    Stopping httpd:                                            [  OK  ]

     测试

    [root@lb01 /]# curl www.test.com
    web03_backup
    [root@lb01 /]# curl www.test.com
    web03_backup
    [root@lb01 /]# curl www.test.com
    web03_backup
    [root@lb01 /]# curl www.test.com
    web03_backup
    [root@lb01 /]# curl www.test.com
    web03_backup

    当web01和web02恢复正常时(nginx负载均衡器是否会自动切换主节点)

    [root@web01 /]# /etc/init.d/httpd start
    Starting httpd:                                            [  OK  ]
    [root@web01 /]# curl 192.168.119.130
    web01_192.168.119.130
    
    [root@web02 /]# /etc/init.d/httpd start
    Starting httpd:                                            [  OK  ]
    [root@web02 /]# curl 192.168.119.133
    web02_192.168.119.133

    测试(可以自动切换回主节点)

    [root@lb01 /]# curl www.test.com
    web01_192.168.119.130
    [root@lb01 /]# curl www.test.com
    web01_192.168.119.130
    [root@lb01 /]# curl www.test.com
    web01_192.168.119.130
    [root@lb01 /]# curl www.test.com
    web01_192.168.119.130
    [root@lb01 /]# curl www.test.com
    web02_192.168.119.133
    [root@lb01 /]# curl www.test.com
    web01_192.168.119.130
    [root@lb01 /]# curl www.test.com
    web02_192.168.119.133
    [root@lb01 /]# curl www.test.com
    web01_192.168.119.130
    [root@lb01 /]# curl www.test.com
    web02_192.168.119.133
    [root@lb01 /]# curl www.test.com
    web01_192.168.119.130
    [root@lb01 /]# curl www.test.com
    web02_192.168.119.133
    [root@lb01 /]# curl www.test.com
    web01_192.168.119.130
    [root@lb01 /]# curl www.test.com
    web02_192.168.119.133
    [root@lb01 /]# curl www.test.com
    web01_192.168.119.130
    •  upstream模块

    upstream模块介绍

      Nginx的负载均衡功能依赖于ngx_http_upstream_module模块,所支持的代理方式有proxy_pass,fastcgi_pass,memcached_pass。

    官方地址:http://nginx.org/en/docs/http/ngx_http_upstream_module.html

    The ngx_http_upstream_module module is used to define groups of servers that can be referenced by the proxy_passfastcgi_passuwsgi_passscgi_pass, and memcached_pass directives.

    • upstream模块相关说明

    upstream模块应放于nginx.conf配置的http{}标签内。

    upstream模块默认算法是wrr(权重轮询weighted round-robin)

    upstream模块内部参数说明

    server 负载均衡后面的RS配置,可以是IP或域名,端口不写,默认是80端口。高并发场景要换成域名,通过DNS做负载均衡。
    weight 是权重,默认是1.权重大接受的请求越多
    max_fails=2 最大尝试失败的次数,默认为1,0表示禁止失败尝试。企业场景:建议2-3、京东1次,蓝汛10次,根据业务需求去配置。
    backup 热备配置(RS节点的高可用),当前面激活的RS都失败后会自动启用热备RS。
    fail_timeout=20s 失败超时时间,默认是10s。京东1次,蓝汛10次,根据业务需求去配置。常规业务2-3秒。
    down 这标志着服务器永远不可用,这个参数一直配合ip_hash使用

     upstream模块调度算法

    • rr轮询(默认)

      按客户端请求顺序把客户端的请求逐一分配到不同的后端的服务器,这相当于LVS中rr算法,如果后端服务器宕机(默认情况下只检测80端口,如果后端报502,404,403,503,还是会直接返回给用户),宕机服务器会被自动剔除,使用户访问不影响。请求会分配给正常的服务器。

    • weight(权重)

      在轮询算法的基础上加上权重(默认是rr+weight),权重轮询和访问成正比,权重越大,转发的请求也就越多。可以根据服务器的配置和性能指定权重值大小,可以有效解决新旧服务器性能不均进行分配问题。

    • ip_hash

      每个请求按访问的IP的hash结果分配,当新的请求到达时,先将其客户端IP通过哈希算法哈希出一个值,在随后请求客户端,IP的哈希值只要相同,就会分配至同一台服务器(LVS负载均衡的-p参数,keepalived的配置里的persistence_timeout 50),该调度算法可以解决动态网页session共享问题,但有时会导致请求分配不均,即无法保证1:1的负载均衡。在国内所有的公司都是NAT上网,多个PC对应一个外部IP

      提示:必须是最前段的服务器,后端也必须直接应用服务器多数情况不能和权重参数一起使用。

      注意:当负载调度算法为ip_hash时,后端服务器在负载调度中的状态不能是weight和backup

    • fair(第三方,NO)动态算法

      按照后端服务器RS的响应时间来分配请求,响应时间短的优先分配。

    比上面两个更智能的负载均衡算法。此种算法可以根据页面大小和加载时间长短只能地进行负载均衡,也就是根据后端服务器的响应时间来分配请求,响应时间的优先分配。Nginx本身是不支持fair的,如果需要使用这种调度算法,必须下载Nginx的upstream_fair模块。

    • url_hash(第三方,NO)

      按访问url的hash结果来分配请求,让每个url定向到同一个后端服务器,后端服务器为缓存服务器时效果显著。在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法。

      url_hash。按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器。可以进一步提高后端缓存服务器的效率命中率。Nginx本身是不支持url_hash的,如果需要使用这种调度算法,必须安装Nginx的hash软件包。

      

     

     

      

      

  • 相关阅读:
    Windows系统的DOS常用命令
    常用pom
    ssm整合
    pom依赖集合
    json
    软件项目管理笔记-软件项目计划
    CSS
    网络配置
    用户组
    用户管理
  • 原文地址:https://www.cnblogs.com/hwlong/p/6032095.html
Copyright © 2011-2022 走看看