【问题标题】:Performance issues running nginx in a docker container在 docker 容器中运行 nginx 的性能问题
【发布时间】:2018-08-07 23:49:48
【问题描述】:

我正在使用 ApacheBench (ab) 来衡量两个 nginx 在 Linux 上的性能。他们有相同的配置文件。唯一的区别是 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;
        }
    }
}

【问题讨论】:

    标签: docker nginx


    【解决方案1】:

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

    写在Docker run reference:

    网络:主机

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

    当容器的网络性能至关重要时,建议在此模式下运行容器,例如生产负载均衡器或高性能 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时的火焰图:

    全图here

    放大到nginx 部分:



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

    全图here

    放大到nginx部分:

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 哇!它获得了大约 50% 的性能提升! Time taken for tests: 16.944 seconds
      • 是的,桥接网络很容易让事情正常工作,但它不利于效率。仍然存在相当大的差异,弄清楚它的来源会很有趣。由于覆盖 fs,写入日志和缓存文件也可能更慢。
      • 我试过这个,但仍然只有大约 60% 直接在主机上运行 nginx(当我没有 --net=host 参数时,我只得到 35%)。
      • 我还会检查您是否使用相同的库(glibc 与 musl)以及容器文件系统的影响(尝试对任何数据/文件使用卷)。
      • 请注意,--net host 不幸的是,Mac 版 Docker 上的 does not work - 似乎只在 Linux 上受支持
      猜你喜欢
      • 2018-07-28
      • 2019-05-29
      • 2021-07-14
      • 2018-08-29
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      相关资源
      最近更新 更多