在docker容器中运行nginx的性能问题

Sve*_*ith 10 nginx docker

我正在使用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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

只是想知道为什么容器的性能如此差

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

tgo*_*gos 10

我想添加到@Andrian Mouat 的答案中,这是我刚刚在文档中找到的内容。

它写在Docker 运行参考中

网络:主机

与默认bridge模式相比,该host模式提供了明显更好的网络性能,因为它使用主机的本地网络堆栈,而桥接器必须通过 docker daemon 进行一层虚拟化

当容器的网络性能至关重要时,建议在此模式下运行容器,例如,生产负载均衡器或高性能 Web 服务器。


火焰图的一些测试如下:

将主机的本机网络堆栈与 一起使用时--net=host,系统调用较少,这在以下火焰图中清楚地描述。细节:

  • 系统范围捕获 30 秒: sudo perf record -F 99 -a -g -- sleep 30
  • 来自另一台物理机的 ab 测试:(ab -n 50000 -c 1000 http://my-host-ip/在捕获时发生)

有关火焰图的更多信息,请查看 Brendan Gregg 的网站:www.brendangregg.com/

发布端口时的火焰图-p 80:80

全图在这里

放大到nginx部分:

docker nginx 火焰图发布端口放大



使用时的火焰图--net=host

全图在这里

放大到nginx部分:

docker nginx 火焰图网络主机放大


Adr*_*uat 7

您如何运行容器?是否使用默认的Docker桥接网络?如果是这样,请尝试使用运行测试,--net=host然后查看结果。

  • 哇!它获得了大约 50% 的性能提升!`测试时间:16.944秒` (2认同)