为什么 docker 在内存使用率过高时会崩溃?

Ant*_*ony 4 python scalability flask docker

我有一个 docker 容器,运行用 python Flask 编写的 REST 服务。我在 OSx 上使用 VirtualBox 运行容器。

\n\n

这是容器启动时 OSx 上的内存统计信息:

\n\n

在此输入图像描述

\n\n

所以,我还有大约 3GB 的空闲内存。所以我运行内存限制为 2 GB 的容器

\n\n
docker run -d -m 2g --name mycontainer -p 5000:5000 foobar\n
Run Code Online (Sandbox Code Playgroud)\n\n

现在,我向容器上运行的服务发送约 100 个 REST 请求,同时运行docker stats.

\n\n

最终,docker 容器崩溃了。

\n\n

docker stats我将容器崩溃之前的数据粘贴到下面。

\n\n

崩溃 1:运行 100 个不同的请求时(容器几乎立即崩溃。

\n\n
CONTAINER           CPU %               MEM USAGE / LIMIT     MEM %               NET I/O               BLOCK I/O\n27ee4ed4f98a        99.27%              256.9 MB / 2.147 GB   11.96%              163.2 kB / 7.958 kB   107.4 MB / 0 B\nCONTAINER           CPU %               MEM USAGE / LIMIT   MEM %               NET I/O               BLOCK I/O\n27ee4ed4f98a        99.77%              324 MB / 2.147 GB   15.09%              163.2 kB / 7.958 kB   107.4 MB / 0 B\nCONTAINER           CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O\nCONTAINER           CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O\nCONTAINER           CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O\n
Run Code Online (Sandbox Code Playgroud)\n\n

崩溃 2:运行 1 个请求 100 次时(容器在大约 30 次后崩溃)

\n\n
CONTAINER           CPU %               MEM USAGE / LIMIT     MEM %               NET I/O               BLOCK I/O\n41fc484677fb        79.00%              891.5 MB / 2.147 GB   41.52%              12.13 MB / 429.8 kB   2.379 GB / 61.85 MB\nCONTAINER           CPU %               MEM USAGE / LIMIT   MEM %               NET I/O               BLOCK I/O\n41fc484677fb        85.83%              892 MB / 2.147 GB   41.54%              12.13 MB / 429.8 kB   3.071 GB / 61.85 MB\nCONTAINER           CPU %               MEM USAGE / LIMIT   MEM %               NET I/O               BLOCK I/O\n41fc484677fb        85.83%              892 MB / 2.147 GB   41.54%              12.13 MB / 429.8 kB   3.071 GB / 61.85 MB\nCONTAINER           CPU %               MEM USAGE / LIMIT   MEM %               NET I/O               BLOCK I/O\n41fc484677fb        86.01%              892 MB / 2.147 GB   41.54%              12.13 MB / 429.8 kB   3.81 GB / 61.85 MB\nCONTAINER           CPU %               MEM USAGE / LIMIT   MEM %               NET I/O               BLOCK I/O\n41fc484677fb        86.01%              892 MB / 2.147 GB   41.54%              12.13 MB / 429.8 kB   3.81 GB / 61.85 MB\nCONTAINER           CPU %               MEM USAGE / LIMIT     MEM %               NET I/O               BLOCK I/O\n41fc484677fb        86.28%              892.2 MB / 2.147 GB   41.55%              12.13 MB / 429.8 kB   4.508 GB / 61.85 MB\nCONTAINER           CPU %               MEM USAGE / LIMIT     MEM %               NET I/O               BLOCK I/O\n41fc484677fb        86.28%              892.2 MB / 2.147 GB   41.55%              12.13 MB / 429.8 kB   4.508 GB / 61.85 MB\nCONTAINER           CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O\nCONTAINER           CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O\nCONTAINER           CPU %               MEM USAGE / LIMIT   MEM %               NET I/O             BLOCK I/O\n
Run Code Online (Sandbox Code Playgroud)\n\n

docker ps -a崩溃后显示以下内容

\n\n
CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS                       PORTS               NAMES\n41fc484677fb        foobar   "python service.py"    7 minutes ago       Exited (137) 2 minutes ago                       mycontainer\n
Run Code Online (Sandbox Code Playgroud)\n\n

运行 dmesg 显示几个内存不足错误:

\n\n
\xe2\x9e\x9c  ~ docker exec -it mycontainer dmesg | grep "Out of memory"\nOut of memory: Kill process 2006 (python) score 872 or sacrifice child\nOut of memory: Kill process 2496 (python) score 873 or sacrifice child\nOut of memory: Kill process 2807 (python) score 879 or sacrifice child\nOut of memory: Kill process 3101 (python) score 875 or sacrifice child\nOut of memory: Kill process 5393 (python) score 868 or sacrifice child\nOut of memory: Kill process 5647 (python) score 868 or sacrifice child\nOut of memory: Kill process 5926 (python) score 877 or sacrifice child\nOut of memory: Kill process 6328 (python) score 873 or sacrifice child\nOut of memory: Kill process 7923 (python) score 872 or sacrifice child\nOut of memory: Kill process 10183 (python) score 873 or sacrifice child\n
Run Code Online (Sandbox Code Playgroud)\n\n

问题

\n\n
    \n
  1. 我怎样才能避免类似的崩溃?

  2. \n
  3. 这仅在我的本地计算机上,但最终我计划将此容器部署到生产环境中。我应该采取哪些方法来防止崩溃?我应该在 Nginx 负载均衡器后面放置此容器的多个克隆吗?

  4. \n
  5. 在生产中,我计划在单个服务器上运行单个容器。如果我在服务器上运行单个容器并且不在该服务器上运行任何其他内容,该容器是否能够使用其可用的所有计算资源?

  6. \n
\n

Tim*_*kin 5

欢迎来到精彩的资源世界:)

对容器设置限制并不会让你保持在限制之下,它只是告诉内核何时开始挤压你以及何时杀死你。你实际上必须保持在你的极限以下。在许多情况下,这意味着观察您的内存占用情况,并在您无法在预算范围内满足请求时排队或删除请求。又称减载。

不过,好处是,当您需要更多容器副本时,您现在可以得到一个非常清晰的信号。