RHEL 6.5----Nginx负载均衡

Posted Luckyzd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RHEL 6.5----Nginx负载均衡相关的知识,希望对你有一定的参考价值。

实验环境

主机名 IP
master 192.168.30.130
node-1 192.168.30.131
node-2 192.168.30.132

 

 

 

 

 

 

在master上安装

本次安装过程统一采用YUM的方式,比较快,也省的出各种问题
首先在本地yum源里面添加
[root@master ~]# vim /etc/yum.repos.d/rhel-source.repo 
[rhel-source]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=file:///media/cdrom
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/rhel/6/$basearch/
gpgcheck=0
enabled=1
或者直接新建个nginx.repo文件在文件中写入
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/rhel/6/$basearch/
gpgcheck=0
enabled=1
完事之后,执行下面命令
[root@master ~]# yum install -y zlib zlib-devel openssl openssl-devel pcre pcre-devel nginx

如果是源码编译安装的,可能没有nginx用户需要手动添加,但是这里yum安装的,就不需要添加了

[root@master ~]# id nginx
uid=496(nginx) gid=492(nginx) groups=492(nginx)

启动nginx

[root@master ~]# /etc/init.d/nginx start
Starting nginx:                                            [  OK  ]
[root@master ~]# /etc/init.d/nginx status
-b (pid  2501) is running...

测试nginx

配置分发器实现动静分离

[root@master ~]# cp /etc/nginx/conf.d/default.conf{,.bak}
[root@master ~]# cd /etc/nginx/
[root@master nginx]# ls
conf.d          koi-utf  mime.types  nginx.conf   uwsgi_params
fastcgi_params  koi-win  modules     scgi_params  win-utf
[root@master nginx]# cp nginx.conf{,.bak}
[root@master nginx]# vim nginx.conf  #末尾添加
 upstream htmlservers {
        server 192.168.30.131:80;
        server 192.168.30.132:80;
    }   
    upstream phpservers {
        server 192.168.30.131:80;
        server 192.168.30.132:80;
    }
    upstream picservers {
        server 192.168.30.131:80;
        server 192.168.30.132:80;
    }
}
"nginx.conf" 44L, 902C written
[root@master conf.d]# vim default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/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;
    }

测试配置文件是否配置正确

[root@master ~]# /etc/init.d/nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

在node-1上

[root@node-1 ~]# yum install -y php httpd 
[root@node-1 ~]# echo "<h1>Static-Web-Server:192.168.30.131</h1>" > /var/www/html/index.html
[root@node-1 ~]# echo "<h1>Web-Server:192.168.30.131</h1><?php phpinfo(); ?>" > /var/www/html/index.php
[root@node-1 ~]# rz
[root@node-1 ~]# mv 131.jpg /var/www/html/
[root@node-1 ~]# service httpd restart 
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
[root@node-1 ~]# ls /var/www/html/
131.jpg  index.html  index.php

node-2做相同操作,只是将测试页面的内容改下

[root@node-2 ~]# echo "<h1>Web-Server:192.168.30.132</h1>" > /var/www/html/index.html
[root@node-2 ~]# echo "<h1>Web-Server:192.168.30.132</h1><?php phpinfo(); ?>" > /var/www/html/index.php
[root@node-2 ~]# rz
[root@node-2 ~]# mv 132.jpg /var/www/html/
[root@node-2 ~]# ls /var/www/html/
132.jpg  index.html  index.php
[root@node-2 ~]# service httpd restart

测试

[root@master ~]# /etc/init.d/nginx reload
Reloading nginx:                                           [  OK  ]

首先直接测试web-server,看是否正常

 

 

然后测试分发器

刷新之后

按F5刷新

 

 

压力测试

[root@node-2 ~]# ab -c 1000 -n 1000 http://192.168.30.130/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.30.130 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:        nginx/1.14.0
Server Hostname:        192.168.30.130
Server Port:            80

Document Path:          /index.html
Document Length:        42 bytes

Concurrency Level:      1000
Time taken for tests:   1.020 seconds
Complete requests:      1000
Failed requests:        500
   (Connect: 0, Receive: 0, Length: 500, Exceptions: 0)
Write errors:           0
Total transferred:      295500 bytes
HTML transferred:       38500 bytes
Requests per second:    980.42 [#/sec] (mean)
Time per request:       1019.966 [ms] (mean)
Time per request:       1.020 [ms] (mean, across all concurrent requests)
Transfer rate:          282.93 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    8  32.1      8    1004
Processing:    13   78  73.1     47     244
Waiting:       13   78  73.1     47     244
Total:         25   86  80.8     58    1017

Percentage of the requests served within a certain time (ms)
  50%     58
  66%     76
  75%     91
  80%     97
  90%    246
  95%    250
  98%    252
  99%    253
 100%   1017 (longest request)
[root@node-2 ~]# ab -c 2000 -n 2000 -q http://192.168.30.130/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.30.130 (be patient)...socket: Too many open files (24)
解决办法
[root@node-2 ~]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 31406
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 31406
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
[root@node-2 ~]# ulimit -n  #系统默认值
1024
[root@node-2 ~]# ulimit -n 1024000
[root@node-2 ~]# ulimit -n
1024000
[root@node-2 ~]# ab -c 2000 -n 2000 -q http://192.168.30.131/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.30.131 (be patient).....done


Server Software:        Apache/2.2.15
Server Hostname:        192.168.30.131
Server Port:            80

Document Path:          /index.html
Document Length:        42 bytes

Concurrency Level:      2000
Time taken for tests:   1.274 seconds
Complete requests:      2000
Failed requests:        0
Write errors:           0
Total transferred:      620310 bytes
HTML transferred:       84042 bytes
Requests per second:    1570.26 [#/sec] (mean)
Time per request:       1273.678 [ms] (mean)
Time per request:       0.637 [ms] (mean, across all concurrent requests)
Transfer rate:          475.61 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  137 329.6     28    1036
Processing:     3  166 228.9     14     661
Waiting:        3  165 228.9     14     661
Total:          7  302 386.9     47    1256

Percentage of the requests served within a certain time (ms)
  50%     47
  66%    255
  75%    672
  80%    684
  90%   1040
  95%   1239
  98%   1253
  99%   1255
 100%   1256 (longest request)
[root@node-2 ~]# ab -c 2000 -n 2000 -q http://192.168.30.130/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.30.130 (be patient).....done


Server Software:        nginx/1.14.0
Server Hostname:        192.168.30.130
Server Port:            80

Document Path:          /index.html
Document Length:        193 bytes

Concurrency Level:      2000
Time taken for tests:   1.081 seconds
Complete requests:      2000
Failed requests:        1553
   (Connect: 0, Receive: 0, Length: 1553, Exceptions: 0)
Write errors:           0
Non-2xx responses:      447
Total transferred:      618860 bytes
HTML transferred:       147325 bytes
Requests per second:    1849.81 [#/sec] (mean)
Time per request:       1081.193 [ms] (mean)
Time per request:       0.541 [ms] (mean, across all concurrent requests)
Transfer rate:          558.97 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  141 327.6     22    1002
Processing:     2  147 212.8     39     692
Waiting:        1  147 212.8     39     692
Total:         23  288 351.5     96    1037

Percentage of the requests served within a certain time (ms)
  50%     96
  66%    245
  75%    642
  80%    653
  90%   1015
  95%   1028
  98%   1032
  99%   1034
 100%   1037 (longest request)

 

以上是关于RHEL 6.5----Nginx负载均衡的主要内容,如果未能解决你的问题,请参考以下文章

RHEL 7配置HAProxy实现Web负载均衡

RHEL6 搭建LVS/DR 负载均衡集群 案例

RHEL 6.5----haproxy实现负载均衡

RHEL服务器配置BIND以及实现DNS负载均衡

RHEL6 搭建LVS/NAT 负载均衡集群 案例

RHEL 5.4下部署LVS(DR)+keepalived实现高性能高可用负载均衡