RHEL 6.5----Nginx负载均衡
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源里面添加 [[email protected] ~]# 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 完事之后,执行下面命令 [[email protected] ~]# yum install -y zlib zlib-devel openssl openssl-devel pcre pcre-devel nginx
如果是源码编译安装的,可能没有nginx用户需要手动添加,但是这里yum安装的,就不需要添加了
[[email protected] ~]# id nginx uid=496(nginx) gid=492(nginx) groups=492(nginx)
启动nginx
[[email protected] ~]# /etc/init.d/nginx start Starting nginx: [ OK ] [[email protected] ~]# /etc/init.d/nginx status -b (pid 2501) is running...
测试nginx
配置分发器实现动静分离
[[email protected] ~]# cp /etc/nginx/conf.d/default.conf{,.bak} [[email protected] ~]# cd /etc/nginx/ [[email protected] nginx]# ls conf.d koi-utf mime.types nginx.conf uwsgi_params fastcgi_params koi-win modules scgi_params win-utf [[email protected] nginx]# cp nginx.conf{,.bak} [[email protected] 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 [[email protected] 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; }
测试配置文件是否配置正确
[[email protected] ~]# /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上
[[email protected]1 ~]# yum install -y php httpd [[email protected]-1 ~]# echo "<h1>Static-Web-Server:192.168.30.131</h1>" > /var/www/html/index.html [[email protected]-1 ~]# echo "<h1>Web-Server:192.168.30.131</h1><?php phpinfo(); ?>" > /var/www/html/index.php [[email protected]-1 ~]# rz [[email protected]-1 ~]# mv 131.jpg /var/www/html/ [[email protected]-1 ~]# service httpd restart Stopping httpd: [ OK ] Starting httpd: [ OK ] [[email protected]-1 ~]# ls /var/www/html/ 131.jpg index.html index.php
node-2做相同操作,只是将测试页面的内容改下
[[email protected]2 ~]# echo "<h1>Web-Server:192.168.30.132</h1>" > /var/www/html/index.html [[email protected]-2 ~]# echo "<h1>Web-Server:192.168.30.132</h1><?php phpinfo(); ?>" > /var/www/html/index.php [[email protected]-2 ~]# rz [[email protected]-2 ~]# mv 132.jpg /var/www/html/ [[email protected]-2 ~]# ls /var/www/html/ 132.jpg index.html index.php [[email protected]-2 ~]# service httpd restart
测试
[[email protected] ~]# /etc/init.d/nginx reload Reloading nginx: [ OK ]
首先直接测试web-server,看是否正常
然后测试分发器
刷新之后
按F5刷新
压力测试
[[email protected]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)
[[email protected]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) 解决办法 [[email protected]-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 [[email protected]-2 ~]# ulimit -n #系统默认值 1024 [[email protected]-2 ~]# ulimit -n 1024000 [[email protected]-2 ~]# ulimit -n 1024000 [[email protected]-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) [[email protected]-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)