使用nginx实现动静分离的负载均衡集群

一、概述:

LB负载均衡集群分两类: LVS (四层)和 nginx或haproxy (七层)

客户端通过访问分发器的VIP来访问网站
      |
现在应用更复杂,比如现在网站页面有: .php .html .png .jpeg .jsp 等, 有动态页面有静态页面。
静态页面一般是不变的,想访问更快些,可以学习SQUID。
      |
但是前面的LVS是四层的。基于IP的。现在需要在应用层基于不同的应用进行分发。
      |
七层LB , Nginx / Haproxy都可以支持7层LB

现在实现以下功能,拓扑图:

工作中,希望这样:
  静态文件处理:可以使用nginx 或apache
  动文件处理: apache ,tomcat
  图片文件处理: squid

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

 二、实验操作:

 1、源码编译安装nginx 

1、安装nginx时必须先安装相应的编译工具和相关依赖
[root@xuegod63 ~]#yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel
zlib:nginx提供gzip模块,需要zlib库支持
openssl:nginx提供ssl功能
pcre:支持地址重写rewrite功能

安装nginx:
[root@xuegod63 ~]# ll nginx-1.12.2.tar.gz  -h  #整个nginx文件不到只813K,很小
-rw-r--r-- 1 root root 813K Jul 14 20:17 nginx-1.12.2.tar.gz 
[root@xuegod63 ~]# tar -zxvf nginx-1.12.2.tar.gz  -C /usr/local/src/
[root@xuegod63 ~]# cd /usr/local/src/nginx-1.12.2/
[root@xuegod63 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx  --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module  --with-http_mp4_module

查看参数:
[root@xuegod63 nginx-1.12.2]# ./configure  --help | grep mp4

参数:
--with-http_dav_module         启用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)默认情况下为关闭,需编译开启
--with-http_stub_status_module 启用ngx_http_stub_status_module支持(获取nginx自上次启动以来的工作状态)
--with-http_addition_module    启用ngx_http_addition_module支持(作为一个输出过滤器,支持不完全缓冲,分部分响应请求)
--with-http_sub_module         启用ngx_http_sub_module支持(允许用一些其他文本替换nginx响应中的一些文本)
--with-http_flv_module         启用ngx_http_flv_module支持(提供寻求内存使用基于时间的偏移量文件)
--with-http_mp4_module         启用对mp4文件支持(提供寻求内存使用基于时间的偏移量文件)

编译和安装: (查看CPU逻辑数cat /proc/cpuinfo | grep processor | wc -l)
[root@xuegod63 nginx-1.12.2]# make -j 4 
[root@xuegod63 nginx-1.12.2]# make  install

生成运行nginx的用户:
[root@xuegod63 nginx-1.12.2]# useradd -u 8000 -s /sbin/nologin  nginx
[root@xuegod63 nginx-1.12.2]# id !$
id nginx
uid=8000(nginx) gid=8000(nginx) groups=8000(nginx)

nginx主要目录结构:
[root@xuegod63 nginx-1.12.2]# cd /usr/local/nginx/
[root@xuegod63 nginx]# ls
conf  html  logs  sbin
conf  #配置文件
html  #网站根目录
logs  #日志
sbin  #nginx启动脚本

主配置文件:
[root@xuegod63 nginx]# ls /usr/local/nginx/conf/
fastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default   uwsgi_params
fastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.default
fastcgi_params        koi-win                 nginx.conf          scgi_params.default  win-utf

启动nginx:
[root@xuegod63 nginx]# ls
conf  html  logs  sbin
[root@xuegod63 nginx]# ./sbin/nginx 
[root@xuegod63 nginx]# netstat -antup|grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7857/nginx: master  
[root@xuegod63 nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@xuegod63 nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@xuegod63 nginx]# nginx -v
nginx version: nginx/1.12.2

测试:
http://192.168.1.63/

2.nginx服务日常操作

 配置nginx成为分发器,实现动静分离

[root@xuegod63 conf]# cd  /usr/local/nginx/conf       #配置文件目录 
[root@xuegod63 conf]# cp nginx.conf nginx.conf.bak    #备份一下配置文件
[root@xuegod63 conf]# vim nginx.conf
[root@xuegod63 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@xuegod70 conf]# nginx -s reload

改:# user nobody;
为:user nginx nginx;   
  
改:
 43         location / {
 44             root   html;
 45             index  index.html index.htm;          #在location / { 。。。} 中添加以下内容  #定义分发策略
location / {
            root   html;
            index  index.html index.htm;
     
        if ($request_uri ~* \.html$){
                   proxy_pass http://htmlservers;
           }   
        if ($request_uri ~* \.php$){
                   proxy_pass http://phpservers;
           }   
                   proxy_pass http://picservers;

      }

把以下内容注释掉,否则php文件直接在nginx服务器上解析了,不再解析给后端服务器:
 72 #       location ~ \.php$ {
 73 #           root           html;
 74 #           fastcgi_pass   127.0.0.1:9000;
 75 #           fastcgi_index  index.php;
 76 #           fastcgi_param  SCRIPT_FILENAME  /server/nginx-1.8.0/html$fastcgi_script_name;
 77 #           include        fastcgi_params;
 78 #       }

#定义负载均衡设备的 Ip
#定义负载均衡设备的 Ip
在配置文件nginx.conf的最后一行}前,添加以下内容:
  upstream  htmlservers {   #定义负载均衡服务器组名称
         server 192.168.1.62:80;   
         server 192.168.1.64:80;
 }
 upstream  phpservers{
         server 192.168.1.62:80;
         server 192.168.1.64:80;
 }
 upstream  picservers {
         server 192.168.1.62:80;
         server 192.168.1.64:80;
 }
#后期工作中,根据工作中的需要,配置成具体业务的IP地址

保存退出。
重新加载nginx服务器配置文件

[root@xuegod63 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@xuegod63 conf]# nginx -s reload

3.配置后端服务器  

[root@xuegod62 ~]## yum install httpd  php -y
生成静态测试文件:
[root@xuegod62 ~]# echo 192.168.10.71 > /var/www/html/index.html
[root@xuegod62 ~]# vim /var/www/html/test.php

生成动态测试文件:
[root@xuegod62 html]#vim  /var/www/html/test.php   #写如以下内容:
192.168.1.62-php
<?php
phpinfo();
?>
启动apache服务器:
[root@xuegod62 html]# service httpd restart

生成图片文件:
上传如下图片,到xuegod62网站/var/www/html/目录下:

然后进行测试:http://192.168.1.63

 

三、测试性能  

扩展: 文件打开数过多
[root@xuegod64 html]# ab -n 1000 -c 1000 http://192.168.1.62/index.html  #运行正常
[root@xuegod64 html]# ab -n 2000 -c 2000 http://192.168.1.62/index.html  #报错
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.1.62 (be patient)
socket: Too many open files (24)   #  测试时,一次打开的socket文件太多。

#ulimit -a   #查看
#ulimit -n
1024
系统默认一个进程最多同时允许打开1024的文件
解决:
#ulimit -n 10240  #报错的解决方法

四、Nginx负载的5种策略设置方法:

1、轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
upstream backserver {
server 192.168.1.62;
server 192.168.1.64;
}

2、指定权重

指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
upstream backserver {
server 192.168.1.62 weight=1;
server 192.168.1.64 weight=2;
}

3、IP绑定 ip_hash

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
upstream backserver {
ip_hash;
server 192.168.1.62:80;
server 192.168.1.64:80;
}

4、fair(第三方)

按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backserver {
server server1;
server server2;
fair;
}

5、url_hash(第三方)

按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
upstream backserver {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}

总结,扩展:

如有tomcat ,apache,squid 配置为如下:
[root@xuegod63 conf]# vim nginx.conf # 在最后添加以下内容。 定义服务器组
upstream tomcat_servers {
server 192.168.1.2:8080;
server 192.168.1.1:8080;
server 192.168.1.11:8080;
}
upstream apache_servers {
server 192.168.1.5:80;
server 192.168.1.177:80;
server 192.168.1.15:80;
}
upstream squid_servers {
server 192.168.1.26:3128;
server 192.168.1.55:3128;
server 192.168.1.18:3128;
}

posted @ 2018-07-04 20:59  走天涯  阅读(2013)  评论(0编辑  收藏  举报