文章目录
-
- 一、架构图
- 二、搭建nginx服务
-
- 1、yum安装nginx
- 2、优化配置文件
- 3、搭建博客服务
- 4、启动服务,并将博客应用复制到php,配置文件复制到nginx2
- 三、搭建PHP服务器
- 四、搭建MySQL服务器
- 五、搭建负载均衡服务器
- 六、搭建代理服务器
- 七、实现代理服务器高可用
-
-
- 1、lb1安装keepalived服务
- 2、lb2安装nginx服务和keepalived
- 3、验证飘移地址
- 八、压力测试
-
- 1、ab测试
-
- 1、进行160个并发访问,发出10000个请求
- 2、进行1000个并发访问,发出1000000个请求
- 2、webbench测试
-
- 1、模拟300个并发连接持续60s
- 2、模拟5000个并发连接持续60s
一、架构图
主机名 |
IP地址 |
作用 |
nginx1 |
192.168.1.10 |
提供nginx服务 |
php |
192.168.1.20 |
web服务 |
mysql |
192.168.1.30 |
数据库 |
nginx2 |
192.168.1.40 |
提供nginx负载均衡 |
lb2 |
192.168.1.50 |
反向代理、高可用 |
lb2 |
192.168.1.60 |
反向代理、高可用 |
二、搭建nginx服务
1、yum安装nginx
[root@nginx1 ~]
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@nginx1 ~]
[root@nginx1 ~]
[root@nginx1 ~]
2、优化配置文件
[root@nginx1 ~]
user nginx;
worker_processes 4;
worker_cpu_affinity 01 10 01 10;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_buffering on;
proxy_temp_path /etc/nginx/proxy_temp;
proxy_cache_path /etc/nginx/proxy_cache levels=1:2 keys_zone=my-cache:100m inactive=600m max_size=2g;
server_tokens off;
sendfile on;
tcp_nopush on;
client_header_buffer_size 4k;
open_file_cache max=102400 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
client_header_timeout 15;
client_body_timeout 15;
reset_timedout_connection on;
send_timeout 15;
keepalive_timeout 65;
fastcgi_connect_timeout 600;
fastcgi_send_timeout 600;
fastcgi_read_timeout 600;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_temp_path /etc/nginx/nginx_tmp;
fastcgi_intercept_errors on;
fastcgi_cache_path /etc/nginx/fastcgi_cache levels=1:2 keys_zone=cache_fastcgi:128m inactive=1d max_size=10g;
gzip on;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_proxied any;
gzip_min_length 1k;
gzip_buffers 16 8k;
gzip_types text/htm;
gzip_vary on;
include /etc/nginx/conf.d/*.conf;
}
3、搭建博客服务
[root@nginx1 ~]
[root@nginx1 ~]
[root@nginx1 blog]
[root@nginx1 blog]
[root@nginx1 ~]
server {
listen 80;
server_name blog.benet.com;
root /blog/wordpress;
index index.php index.html;
location ~\.php$ {
root /blog/wordpress;
fastcgi_pass 192.168.1.20:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_cache cache_fastcgi;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_cache_key http://$host$request_uri;
}
location ~* \.(ico|jpe?g|gif|png|bmp|swf|flv)$ {
expires 30d;
access_log off;
}
location ~* \.(js|css)$ {
expires 7d;
log_not_found off;
access_log off;
}
location ~* ^.+\.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {
valid_referers none blocked www.benet.com benet.com;
if ($invalid_referer) {
return 404;
break;
}
access_log off;
}
}
4、启动服务,并将博客应用复制到php,配置文件复制到nginx2
[root@nginx1 ~]
[root@nginx1 ~]
[root@nginx1 ~]
[root@nginx1 ~]
[root@nginx1 ~]
三、搭建PHP服务器
[root@php ~]
[root@php ~]
[root@php ~]
[root@php ~]
> php72w-mysqlnd php72w-opcache
[root@php ~]
[root@php ~]
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
[root@php ~]
listen = 192.168.1.20:9000
listen.allowed_clients = 127.0.0.1,192.168.1.10,192.168.1.40,192.168.1.254
[root@php ~]
四、搭建MySQL服务器
[root@mysql ~]
[root@mysql ~]
[root@mysql ~]
[root@mysql ~]
[root@mysql ~]
[root@mysql ~]
[root@mysql soft]
[root@mysql soft]
[root@mysql soft]
[root@mysql soft]
[root@mysql soft]
[root@mysql soft]
[root@mysql soft]
[root@mysql soft]
[root@mysql soft]
[root@mysql soft]
五、搭建负载均衡服务器
[root@nginx2 ~]
[root@nginx2 ~]
[root@nginx2 ~]
[root@nginx2 ~]
[root@localhost ~]
upstream webcluster {
server 192.168.1.10:80;
server 192.168.1.40:80;
}
server {
listen 80;
server_name blog.benet.com;
location / {
proxy_pass http://webcluster;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
六、搭建代理服务器
[root@lb1 ~]
[root@lb1 ~]
[root@nginx1 ~]
[root@nginx1 ~]
[root@lb1 ~]
upstream webcluster {
server 192.168.1.10:80;
server 192.168.1.40:80;
}
server {
listen 80;
server_name blog.benet.com;
location / {
proxy_pass http://webcluster;
include nginx_params;
}
}
[root@lb1 ~]
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 128k;
[root@lb1 ~]
七、实现代理服务器高可用
1、lb1安装keepalived服务
[root@lb1 ~]
[root@lb1 ~]
global_defs {
router_id lb1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254
}
}
[root@lb1 ~]
[root@lb1 ~]
2、lb2安装nginx服务和keepalived
[root@lb2 ~]
[root@lb2 ~]
[root@lb2 ~]
[root@lb2 ~]
[root@lb2 ~]
global_defs {
router_id lb2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254
}
}
[root@lb2 ~]
[root@lb2 ~]
3、验证飘移地址
[root@lb1 ~]
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:88:26:30 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.50/24 brd 192.168.1.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.1.254/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::c13d:1021:3463:e652/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::7b62:b4f3:e4e4:d24c/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::47b0:c0f7:8abb:d142/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
[root@lb2 ~]
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:dc:b1:e9 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.60/24 brd 192.168.1.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::c13d:1021:3463:e652/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::7b62:b4f3:e4e4:d24c/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::47b0:c0f7:8abb:d142/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
八、压力测试
1、ab测试
1、进行160个并发访问,发出10000个请求
[root@lb1 ~]
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking blog.benet.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: nginx
Server Hostname: blog.benet.com
Server Port: 80
Document Path: /index.html
Document Length: 146 bytes
Concurrency Level: 160
Time taken for tests: 1.552 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Non-2xx responses: 10000
Total transferred: 2890000 bytes
HTML transferred: 1460000 bytes
Requests per second: 6441.86 [
Time per request: 24.838 [ms] (mean)
Time per request: 0.155 [ms] (mean, across all concurrent requests)
Transfer rate: 1818.06 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 6 35.1 4 1014
Processing: 4 18 12.7 16 227
Waiting: 1 16 11.9 14 223
Total: 6 25 37.5 21 1039
Percentage of the requests served within a certain time (ms)
50% 21
66% 26
75% 28
80% 29
90% 34
95% 43
98% 71
99% 86
100% 1039 (longest request)
2、进行1000个并发访问,发出1000000个请求
[root@lb1 ~]
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking blog.benet.com (be patient)
Completed 100000 requests
Completed 200000 requests
Completed 300000 requests
Completed 400000 requests
Completed 500000 requests
Completed 600000 requests
Completed 700000 requests
Completed 800000 requests
Completed 900000 requests
Completed 1000000 requests
Finished 1000000 requests
Server Software: nginx
Server Hostname: blog.benet.com
Server Port: 80
Document Path: /index.html
Document Length: 146 bytes
Concurrency Level: 1000
Time taken for tests: 169.644 seconds
Complete requests: 1000000
Failed requests: 0
Write errors: 0
Non-2xx responses: 1000000
Total transferred: 289000000 bytes
HTML transferred: 146000000 bytes
Requests per second: 5894.71 [
Time per request: 169.644 [ms] (mean)
Time per request: 0.170 [ms] (mean, across all concurrent requests)
Transfer rate: 1663.64 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 127 430.7 12 63127
Processing: 3 42 48.5 30 2015
Waiting: 1 37 47.3 25 2004
Total: 5 169 437.0 44 63176
Percentage of the requests served within a certain time (ms)
50% 44
66% 56
75% 69
80% 82
90% 258
95% 1056
98% 1113
99% 1260
100% 63176 (longest request)
[root@localhost ~]
top - 22:58:19 up 3:52, 3 users, load average: 0.81, 0.23, 0.11
Tasks: 113 total, 2 running, 111 sleeping, 0 stopped, 0 zombie
%Cpu0 : 6.6 us, 31.5 sy, 0.0 ni, 61.9 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
%Cpu1 : 3.7 us, 15.8 sy, 0.0 ni, 46.5 id, 0.0 wa, 0.0 hi, 34.1 si, 0.0 st
KiB Mem : 1863252 total, 1240008 free, 120596 used, 502648 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 1550180 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
31295 root 20 0 281228 3248 968 S 22.6 0.2 0:10.92 nginx
31297 root 20 0 281180 3260 1060 S 22.6 0.2 0:10.91 nginx
31296 root 20 0 281048 2988 1056 S 15.9 0.2 0:07.40 nginx
31298 root 20 0 280916 2996 1036 S 15.9 0.2 0:07.39 nginx
[root@localhost ~]
top - 22:58:31 up 2:28, 2 users, load average: 1.52, 0.44, 0.19
Tasks: 111 total, 5 running, 106 sleeping, 0 stopped, 0 zombie
%Cpu0 : 8.1 us, 28.2 sy, 0.0 ni, 63.4 id, 0.0 wa, 0.0 hi, 0.4 si, 0.0 st
%Cpu1 : 3.6 us, 14.9 sy, 0.0 ni, 45.5 id, 0.0 wa, 0.0 hi, 36.0 si, 0.0 st
KiB Mem : 1863252 total, 1232120 free, 123276 used, 507856 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 1544800 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
31782 root 20 0 280432 2632 976 S 21.9 0.1 0:13.91 nginx
31784 root 20 0 280432 2692 1036 R 21.9 0.1 0:13.87 nginx
31785 root 20 0 281016 2996 988 R 16.9 0.2 0:09.72 nginx
31783 root 20 0 281044 2996 960 R 16.6 0.2 0:09.70 nginx
14 root 20 0 0 0 0 R 10.3 0.0 0:06.80 ksoftirqd/1
2、webbench测试
1、模拟300个并发连接持续60s
[root@lb1 ~]
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://blog.benet.com/index.html
300 clients, running 60 sec.
Speed=405924 pages/min, 1955195 bytes/sec.
Requests: 405924 susceed, 0 failed.
[root@localhost ~]
top - 23:09:54 up 4:04, 3 users, load average: 0.89, 0.54, 0.35
Tasks: 113 total, 4 running, 109 sleeping, 0 stopped, 0 zombie
%Cpu(s): 9.1 us, 31.9 sy, 0.0 ni, 34.0 id, 0.0 wa, 0.0 hi, 25.0 si, 0.0 st
%Node0 : 9.3 us, 31.9 sy, 0.0 ni, 34.0 id, 0.0 wa, 0.0 hi, 24.8 si, 0.0 st
KiB Mem : 1863252 total, 1013728 free, 120636 used, 728888 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 1547000 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
31297 root 20 0 281180 3260 1060 R 32.2 0.2 1:06.54 nginx
31295 root 20 0 281228 3248 968 R 31.9 0.2 1:06.43 nginx
31296 root 20 0 281048 2988 1056 R 24.3 0.2 0:48.36 nginx
31298 root 20 0 280916 2996 1036 S 23.6 0.2 0:48.18 nginx
[root@localhost ~]
top - 23:10:09 up 2:40, 2 users, load average: 1.66, 0.79, 0.47
Tasks: 111 total, 5 running, 106 sleeping, 0 stopped, 0 zombie
%Cpu0 : 13.1 us, 42.2 sy, 0.0 ni, 44.3 id, 0.0 wa, 0.0 hi, 0.4 si, 0.0 st
%Cpu1 : 6.8 us, 19.4 sy, 0.0 ni, 25.5 id, 0.0 wa, 0.0 hi, 48.2 si, 0.0 st
KiB Mem : 1863252 total, 1003164 free, 123860 used, 736228 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 1541224 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
31784 root 20 0 280432 2692 1036 R 31.3 0.1 1:10.83 nginx
31782 root 20 0 280432 2632 976 R 30.7 0.1 1:10.77 nginx
31783 root 20 0 281044 2996 960 S 25.0 0.2 0:52.58 nginx
31785 root 20 0 281016 2996 988 R 24.0 0.2 0:52.68 nginx
2、模拟5000个并发连接持续60s
[root@lb1 ~]
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.
Benchmarking: GET http://blog.benet.com/index.html
5000 clients, running 60 sec.
Speed=956522 pages/min, 4599838 bytes/sec.
Requests: 956512 susceed, 10 failed.
[root@localhost ~]
top - 23:15:06 up 4:09, 3 users, load average: 0.65, 0.93, 0.62
Tasks: 114 total, 5 running, 109 sleeping, 0 stopped, 0 zombie
%Cpu(s): 8.6 us, 31.5 sy, 0.0 ni, 36.8 id, 0.0 wa, 0.0 hi, 23.1 si, 0.0 st
KiB Mem : 1863252 total, 796524 free, 120420 used, 946308 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 1544416 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
31295 root 20 0 281228 3248 968 R 30.9 0.2 2:07.21 nginx
31297 root 20 0 281180 3260 1060 R 30.2 0.2 2:07.58 nginx
31296 root 20 0 281048 2988 1056 R 23.3 0.2 1:34.33 nginx
31298 root 20 0 280916 2996 1036 R 22.6 0.2 1:34.18 nginx
[root@localhost ~]
top - 23:15:34 up 2:45, 2 users, load average: 1.12, 1.21, 0.80
Tasks: 112 total, 8 running, 104 sleeping, 0 stopped, 0 zombie
%Cpu0 : 13.9 us, 41.6 sy, 0.0 ni, 44.1 id, 0.0 wa, 0.0 hi, 0.4 si, 0.0 st
%Cpu1 : 6.0 us, 22.5 sy, 0.0 ni, 23.2 id, 0.0 wa, 0.0 hi, 48.2 si, 0.0 st
KiB Mem : 1863252 total, 771432 free, 123768 used, 968052 buff/cache
KiB Swap: 2097148 total, 2097148 free, 0 used. 1538088 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
31782 root 20 0 280432 2632 976 R 31.2 0.1 2:15.77 nginx
31784 root 20 0 280432 2692 1036 R 31.2 0.1 2:16.02 nginx
31783 root 20 0 281044 2996 960 R 25.2 0.2 1:42.50 nginx
31785 root 20 0 281016 2996 988 R 25.2 0.2 1:42.55 nginx
31784 root 20 0 280432 2692 1036 R 31.2 0.1 2:16.02 nginx
31783 root 20 0 281044 2996 960 R 25.2 0.2 1:42.50 nginx
31785 root 20 0 281016 2996 988 R 25.2 0.2 1:42.55 nginx