-m … –memory-swap …

-m(–memory=) 選項可以完成這樣的配置:

$ docker run -it -m 300M --memory-swap -1 --name con1 u-stress /bin/bash

下面的 stress 命令會創建一個進程并通過 malloc 函數分配內存:

# stress --vm 1 --vm-bytes 500M

通過 docker stats 命令查看實際情況:

上面的 docker run 命令中通過 -m 選項限制容器使用的內存上限為 300M。同時設置 memory-swap 值為 -1,它表示容器程序使用內存的受限,而可以使用的 swap 空間使用不受限制(宿主機有多少 swap 容器就可以使用多少)。如果 –memory-swap 設置小于 –memory則設置不生效,使用默認設置)。

下面我們通過 top 命令來查看 stress 進程內存的實際情況:

上面的截圖中先通過 pgrep 命令查詢 stress 命令相關的進程,進程號比較大的那個是用來消耗內存的進程,我們就查看它的內存信息。VIRT 是進程虛擬內存的大小,所以它應該是 500M。RES 為實際分配的物理內存數量,我們看到這個值就在 300M 上下浮動。看樣子我們已經成功的限制了容器能夠使用的物理內存數量。

也可以通過如下命令獲取 stress 進程的 swap 占用:

for file in /proc/*/status ; do awk \\\'/VmSwap|Name/{printf $2 $3}END{ print }\\\' $file; done | sort -k 2 -n -r | grep stress

限制可用的 swap 大小

強調一下 –memory-swap 是必須要與 –memory 一起使用的。但是凡事沒有絕對,如果你非要不添加–memory-swap 選項呢?

如:docker run -it --rm -m 100M ubuntu-stress:latest /bin/bash

按照官方文檔的理解,如果指定 -m 內存限制時不添加 –memory-swap 選項或 –memory-swap 設置為 0 ,則表示容器中程序可以使用 100M 內存和 100M swap 內存。默認情況下,–memory-swap 會被設置成 memory 的 2倍。

We set memory limit(300M) only, this means the processes in the container can use 300M memory and 300M swap memory, by default, the total virtual memory size --memory-swapwill be set as double of memory, in this case, memory   swap would be 2*300M, so processes can use 300M swap memory as well.

如果按照以上方式運行容器提示如下信息:

WARNING: Your kernel does not support swap limit capabilities, memory limited without swap.

可參考 Adjust memory and swap accounting 獲取解決方案:

To enable memory and swap on system using GNU GRUB (GNU GRand Unified Bootloader), do the following:

(1)Log into Ubuntu as a user with sudo privileges.

(2)Edit the /etc/default/grub file.

(3)Set the GRUB_CMDLINE_LINUX value as follows: GRUB_CMDLINE_LINUX=cgroup_enable=memory swapaccount=1

(4)Save and close the file.

(5)Update GRUB. $ sudo update-grub

(6)Reboot your system.

正常情況下, –memory-swap 的值包含容器可用內存和可用 swap。所以 –memory="300m" –memory-swap="1g" 的含義為:

容器可以使用 300M 的物理內存,并且可以使用 700M(1G -300M) 的 swap。–memory-swap 居然是容器可以使用的物理內存和可以使用的 swap 之和!

如果 –memory-swap 的值和 –memory 相同,則容器不能使用 swap。

下面的 demo 演示了在沒有 swap 可用的情況下向系統申請大量內存的場景:

$ docker run -it --rm -m 300M --memory-swap=300M u-stress /bin/bash
# stress --vm 1 --vm-bytes 500M

demo 中容器的物理內存被限制在 300M,但是進程卻希望申請到 500M 的物理內存。在沒有 swap 可用的情況下,進程直接被 OOM kill 了。如果有足夠的 swap,程序至少還可以正常的運行。

我們可以通過 –oom-kill-disable 選項強行阻止 OOM kill 的發生,但是筆者認為 OOM kill 是一種健康的行為,為什么要阻止它呢?

-m … –memory-swappiness …

swappiness 可以認為是宿主/proc/sys/vm/swappiness設定:

Swappiness is a Linux kernel parameter that controls the relative weight given to swapping out runtime memory, as opposed to dropping pages from the system page cache. Swappiness can be set to values between 0 and 100 inclusive. A low value causes the kernel to avoid swapping, a higher value causes the kernel to try to use swap space.Swappiness

–memory-swappiness=0 表示禁用容器 swap 功能(這點不同于宿主機,宿主機 swappiness 設置為 0 也不保證 swap 不會被使用):

docker run -it --rm -m 100M --memory-swappiness=0 ubuntu-stress:latest /bin/bash

?  ~ docker run -it --rm -m 100M --memory-swappiness=0 ubuntu-stress:latest /bin/bash
root@e3fd6cc73849:/# stress --vm 1 --vm-bytes 100M  # 沒有任何商量的余地,到達 100M 直接被 kill
stress: info: [18] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
stress: FAIL: [18] (416) <-- worker 19 got signal 9
stress: WARN: [18] (418) now reaping child worker processes
stress: FAIL: [18] (452) failed run completed in 0s
root@e3fd6cc73849:/#

–memory-reservation …

--memory-reservation ...選項可以理解為內存的軟限制。如果不設置 -m 選項,那么容器使用內存可以理解為是不受限的。按照官方的說法,memory reservation 設置可以確保容器不會長時間占用大量內存。

–oom-kill-disable

?  ~ docker run -it --rm -m 100M --memory-swappiness=0 --oom-kill-disable ubuntu-stress:latest /bin/bash
root@f54f93440a04:/# stress --vm 1 --vm-bytes 200M  # 正常情況不添加 --oom-kill-disable 則會直接 OOM kill,加上之后則達到限制內存之后也不會被 kill
stress: info: [17] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd

但是如果是以下的這種沒有對容器作任何資源限制的情況,添加 –oom-kill-disable 選項就比較 危險 了:

$ docker run -it --oom-kill-disable ubuntu:14.04 /bin/bash

因為此時容器內存沒有限制,并且不會被 oom kill,此時系統則會 kill 系統進程用于釋放內存。

–kernel-memory

Kernel memory is fundamentally different than user memory as kernel memory can’t be swapped out. The inability to swap makes it possible for the container to block system services by consuming too much kernel memory. Kernel memory includes:

stack pages

slab pages

sockets memory pressure

tcp memory pressure

這里直接引用 Docker 官方介紹,如果無特殊需求,kernel-memory 一般無需設置,這里不作過多說明。

參考文檔

Docker內存資源限制

進程不見了,Linux 的OOM Killer

十問 Linux 虛擬內存管理

Docker: 限制容器可用的內存

更多關于云服務器域名注冊,虛擬主機的問題,請訪問三五互聯官網:m.shinetop.cn

贊(0)
聲明:本網站發布的內容(圖片、視頻和文字)以原創、轉載和分享網絡內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。郵箱:3140448839@qq.com。本站原創內容未經允許不得轉載,或轉載時需注明出處:三五互聯知識庫 » Docker內存使用

登錄

找回密碼

注冊

主站蜘蛛池模板: 新婚少妇无套内谢国语播放| 岛国最新亚洲伦理成人| 亚洲欧洲av人一区二区| 亚洲av男人电影天堂热app| 狠狠色婷婷久久综合频道日韩 | 26uuu另类亚洲欧美日本| 中文字幕无码免费久久| 激情内射亚洲一区二区三区| 中国性欧美videofree精品| 天天摸天天操免费播放小视频| 庆元县| 激情综合网激情综合| 国产精品综合av一区二区| 国产精品无码无卡在线观看久| 浮山县| 伊人中文在线最新版天堂| 天堂网av一区二区三区| 精品一区二区三区在线视频观看| 久久人妻少妇嫩草av无码专区| 国产精品制服丝袜无码| 亚洲日本欧美日韩中文字幕| av无码免费一区二区三区| 亚洲 欧美 综合 另类 中字| 日本免费最新高清不卡视频| 精品国偷自产在线视频99| 女子spa高潮呻吟抽搐| 欧洲国产成人久久精品综合| 18成人片黄网站www| 日日碰狠狠躁久久躁96avv| 亚洲男人第一无码av网| 亚洲国产良家在线观看| 精品免费国产一区二区三区四区介绍| 国产成人无码免费视频麻豆| 97人妻蜜臀中文字幕| 中文字幕日韩精品无码内射| 亚洲精品欧美综合二区| 99re在线视频观看| 成人动漫综合网| 99精品热在线在线观看视| 丰镇市| 亚洲av与日韩av在线|