在 docker 容器中运行 nginx 的性能问题 [英] Performance issues running nginx in a docker container
问题描述
我正在使用 ApacheBench (ab) 来衡量两个 nginx 在 Linux 上的性能.他们有相同的配置文件.唯一的区别是其中一个 nginx 运行在 docker 容器中.
主机系统上的 Nginx:
运行:ab -n 50000 -c 1000 http://172.17.0.2:7082/并发级别:1000测试时间:9.376 秒完成请求:50000失败的请求:0传输总量:8050000 字节传输的 HTML:250000 字节每秒请求数:5332.94 [#/sec](平均值)每个请求的时间:187.514 [ms](平均值)每个请求的时间:0.188 [ms](平均,跨所有并发请求)传输速率:838.48 [Kbytes/sec] 接收
Docker 容器中的 Nginx:
运行:ab -n 50000 -c 1000 http://172.17.0.2:6066/并发级别:1000测试时间:31.274 秒完成请求:50000失败的请求:0传输总量:8050000 字节传输的 HTML:250000 字节每秒请求数:1598.76 [#/sec](平均值)每个请求的时间:625.484 [ms](平均值)每个请求的时间:0.625 [ms](平均,跨所有并发请求)传输速率:251.37 [Kbytes/sec] 接收
只是想知道为什么容器的性能这么差
nginx.conf:
worker_processes auto;worker_rlimit_nofile 10240;事件{使用 epoll;多接受;worker_connections 4096;}http {包括 mime.types;default_type 应用程序/八位字节流;发送文件;keepalive_timeout 10;client_header_timeout 10;client_body_timeout 10;发送超时 10;tcp_nopush 开启;tcp_nodelay 开启;服务器 {听80;server_name 本地主机;地点/{返回 200 '你好';}error_page 500 502 503 504/50x.html;位置 =/50x.html {根html;}}}
我想添加到
放大到nginx
部分:
I'm using ApacheBench (ab) to measure the performance of two nginx on Linux. They have same config file. The Only difference is one of nginx is running in a docker container.
Nginx on Host System:
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
Nginx in docker container:
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
Just wondering why the container one has such a poor performance
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;
}
}
}
I'd like to add to @Andrian Mouat's answer, something I've just found in the docs.
It is written in the Docker run reference:
NETWORK: HOST
Compared to the default
bridge
mode, thehost
mode gives significantly better networking performance since it uses the host’s native networking stack whereas the bridge has to go through one level of virtualization through the docker daemon.It is recommended to run containers in this mode when their networking performance is critical, for example, a production Load Balancer or a High Performance Web Server.
Some tests with Flame Graphs follow:
When using the host’s native networking stack with --net=host
, there are fewer system calls and this is clearly depicted in the following Flame Graphs. Details:
- system wide captures for 30sec:
sudo perf record -F 99 -a -g -- sleep 30
- ab test from another physical machine:
ab -n 50000 -c 1000 http://my-host-ip/
(takes place while capturing)
For more info on Flame Graphs, check Brendan Gregg's website: www.brendangregg.com/
Flame Graph when publishing port -p 80:80
:
Full picture here
Zoomed to nginx
part:
Flame Graph when using --net=host
:
Full picture here
Zoomed to nginx
part:
这篇关于在 docker 容器中运行 nginx 的性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!