我使用ApacheBench(ab)测量Linux上两个nginx的性能。他们有相同的配置文件。唯一的区别是nginx在docker容器中运行。
主机系统上的Nginx:
Running: ab -n 50000 -c 1000 http://172.17.0.2:7082/
Concurrency Level: 1000
Time taken for tests: 9.376 seconds
Complete requests: 50000
Failed requests: 0
Total transferred: 8050000 bytes
HTML transferred: 250000 bytes
Requests per second: 5332.94 [#/sec] (mean)
Time per request: 187.514 [ms] (mean)
Time per request: 0.188 [ms] (mean, across all concurrent requests)
Transfer rate: 838.48 [Kbytes/sec] received
Docker容器中的Nginx:
Running: ab -n 50000 -c 1000 http://172.17.0.2:6066/
Concurrency Level: 1000
Time taken for tests: 31.274 seconds
Complete requests: 50000
Failed requests: 0
Total transferred: 8050000 bytes
HTML transferred: 250000 bytes
Requests per second: 1598.76 [#/sec] (mean)
Time per request: 625.484 [ms] (mean)
Time per request: 0.625 [ms] (mean, across all concurrent requests)
Transfer rate: 251.37 [Kbytes/sec] received
只是想知道容器为什么会有这么差的性能
nginx.conf:
worker_processes auto;
worker_rlimit_nofile 10240;
events {
use epoll;
multi_accept on;
worker_connections 4096;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 10;
client_header_timeout 10;
client_body_timeout 10;
send_timeout 10;
tcp_nopush on;
tcp_nodelay on;
server {
listen 80;
server_name localhost;
location / {
return 200 'hello';
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
答案 0 :(得分:3)
你是如何运行容器的?它是否使用默认的Docker桥接网络?如果是这样,请尝试使用--net=host
运行测试,看看结果如何。
答案 1 :(得分:1)
我想添加@Andrian Mouat's回答,这是我刚在文档中找到的内容。
中NETWORK:HOST
与默认
bridge
模式相比,host
模式提供显着更好的网络性能,因为它使用主机的本机网络堆栈,而桥必须去通过docker守护程序进行一级虚拟化。建议在网络性能至关重要时以此模式运行容器,例如,生产负载均衡器或高性能Web服务器。
将主机的本机网络堆栈与--net=host
一起使用时,系统调用较少,这在下面的Flame Graph中有清晰描述。详细说明:
sudo perf record -F 99 -a -g -- sleep 30
ab -n 50000 -c 1000 http://my-host-ip/
(在捕获时发生)有关Flame Graphs的更多信息,请查看Brendan Gregg的网站:www.brendangregg.com/
-p 80:80
时的Flame Graph:全图here
放大nginx
部分:
--net=host
时的火焰图:全图here
放大nginx
部分: