docker 內(nèi)存監(jiān)控與壓測(cè)方式
一直運(yùn)行的docker容器顯示內(nèi)存已經(jīng)耗盡,并且容器內(nèi)存耗盡也沒(méi)出現(xiàn)重啟情況,通過(guò)后臺(tái)查看發(fā)現(xiàn)進(jìn)程沒(méi)有占用多少內(nèi)存。內(nèi)存的監(jiān)控使用的是cadvisor,計(jì)算方式也是使用cadvisor的頁(yè)面計(jì)算方式,所以決定對(duì)docker的內(nèi)存計(jì)算做下研究。
docker version:
Client: Version: 1.12.6 API version: 1.24 Go version: go1.6.4 Git commit: 78d1802 Built: Tue Jan 10 20:20:01 2017 OS/Arch: linux/amd64Server: Version: 1.12.6 API version: 1.24 Go version: go1.6.4 Git commit: 78d1802 Built: Tue Jan 10 20:20:01 2017 OS/Arch: linux/amd64
kubernetes version:
Client Version: version.Info{Major:'1', Minor:'8', GitVersion:'v1.8.2+coreos.0', GitCommit:'4c0769e81ab01f47eec6f34d7f1bb80873ae5c2b', GitTreeState:'clean', BuildDate:'2017-10-25T16:24:46Z', GoVersion:'go1.8.3', Compiler:'gc', Platform:'linux/amd64'}Server Version: version.Info{Major:'1', Minor:'8', GitVersion:'v1.8.2+coreos.0', GitCommit:'4c0769e81ab01f47eec6f34d7f1bb80873ae5c2b', GitTreeState:'clean', BuildDate:'2017-10-25T16:24:46Z', GoVersion:'go1.8.3', Compiler:'gc', Platform:'linux/amd64'}1.創(chuàng)建pod yaml文件,使用busybox鏡像做測(cè)試,對(duì)鏡像設(shè)定2核2G內(nèi)存的限制
[docker@k8s busybox]$ cat busybox.yaml
apiVersion: v1kind: Podmetadata: name: busybox namespace: defaultspec: containers: - image: registry.dcos:8021/public/busybox:latest command: - sleep - '3600' imagePullPolicy: IfNotPresent name: busybox resources: limits: cpu: '2' memory: 2Gi requests: cpu: 100m memory: 64Mi restartPolicy: Always2.通過(guò)kubectl命令生成busybox服務(wù)
[docker@k8s busybox]$ kubectl create -f busybox.yaml
pod 'busybox' created3.進(jìn)入容器的/sys/fs/cgroup/memory目錄,ls查看得到如下文件
-rw-r--r-- 1 root root 0 May 31 03:18 cgroup.clone_children--w--w--w- 1 root root 0 May 31 03:18 cgroup.event_control-rw-r--r-- 1 root root 0 May 31 03:18 cgroup.procs-rw-r--r-- 1 root root 0 May 31 03:18 memory.failcnt--w------- 1 root root 0 May 31 03:18 memory.force_empty-rw-r--r-- 1 root root 0 May 31 03:18 memory.kmem.failcnt-rw-r--r-- 1 root root 0 May 31 03:18 memory.kmem.limit_in_bytes-rw-r--r-- 1 root root 0 May 31 03:18 memory.kmem.max_usage_in_bytes-r--r--r-- 1 root root 0 May 31 03:18 memory.kmem.slabinfo-rw-r--r-- 1 root root 0 May 31 03:18 memory.kmem.tcp.failcnt-rw-r--r-- 1 root root 0 May 31 03:18 memory.kmem.tcp.limit_in_bytes-rw-r--r-- 1 root root 0 May 31 03:18 memory.kmem.tcp.max_usage_in_bytes-r--r--r-- 1 root root 0 May 31 03:18 memory.kmem.tcp.usage_in_bytes-r--r--r-- 1 root root 0 May 31 03:18 memory.kmem.usage_in_bytes-rw-r--r-- 1 root root 0 May 31 03:18 memory.limit_in_bytes-rw-r--r-- 1 root root 0 May 31 03:18 memory.max_usage_in_bytes-rw-r--r-- 1 root root 0 May 31 03:18 memory.memsw.failcnt-rw-r--r-- 1 root root 0 May 31 03:18 memory.memsw.limit_in_bytes-rw-r--r-- 1 root root 0 May 31 03:18 memory.memsw.max_usage_in_bytes-r--r--r-- 1 root root 0 May 31 03:18 memory.memsw.usage_in_bytes-rw-r--r-- 1 root root 0 May 31 03:18 memory.move_charge_at_immigrate-r--r--r-- 1 root root 0 May 31 03:18 memory.numa_stat-rw-r--r-- 1 root root 0 May 31 03:18 memory.oom_control---------- 1 root root 0 May 31 03:18 memory.pressure_level-rw-r--r-- 1 root root 0 May 31 03:18 memory.soft_limit_in_bytes-r--r--r-- 1 root root 0 May 31 03:18 memory.stat-rw-r--r-- 1 root root 0 May 31 03:18 memory.swappiness-r--r--r-- 1 root root 0 May 31 03:18 memory.usage_in_bytes-rw-r--r-- 1 root root 0 May 31 03:18 memory.use_hierarchy-rw-r--r-- 1 root root 0 May 31 03:18 notify_on_release-rw-r--r-- 1 root root 0 May 31 03:18 tasks
我們主要關(guān)注一下幾個(gè)文件
文件名 含義 memory.usage_in_bytes 已使用的內(nèi)存量(包含cache和buffer)(字節(jié)),相當(dāng)于linux的used_meme memory.limit_in_bytes 限制的內(nèi)存總量(字節(jié)),相當(dāng)于linux的total_mem memory.failcnt 申請(qǐng)內(nèi)存失敗次數(shù)計(jì)數(shù) memory.stat 內(nèi)存相關(guān)狀態(tài)memory.stat的文件包含的內(nèi)容
字段 含義 cache 頁(yè)緩存,包括 tmpfs(shmem),單位為字節(jié) rss 匿名和 swap 緩存,不包括 tmpfs(shmem),單位為字節(jié) mapped_file memory-mapped 映射的文件大小,包括 tmpfs(shmem),單位為字節(jié) pgpgin 存入內(nèi)存中的頁(yè)數(shù) pgpgout 從內(nèi)存中讀出的頁(yè)數(shù) swap swap 用量,單位為字節(jié) active_anon 在活躍的最近最少使用(least-recently-used,LRU)列表中的匿名和 swap 緩存,包括 tmpfs(shmem),單位為字節(jié) inactive_anon 不活躍的 LRU 列表中的匿名和 swap 緩存,包括 tmpfs(shmem),單位為字節(jié) active_file 活躍 LRU 列表中的 file-backed 內(nèi)存,以字節(jié)為單位 inactive_file 不活躍 LRU 列表中的 file-backed 內(nèi)存,以字節(jié)為單位 unevictable 無(wú)法再生的內(nèi)存,以字節(jié)為單位 hierarchical_memory_limit 包含 memory cgroup 的層級(jí)的內(nèi)存限制,單位為字節(jié) hierarchical_memsw_limit 包含 memory cgroup 的層級(jí)的內(nèi)存加 swap 限制,單位為字節(jié)查看memory.limit_in_bytes文件
/sys/fs/cgroup/memory # cat memory.limit_in_bytes 2147483648
計(jì)算容器的限制內(nèi)存為2g,和yaml文件里面定義的限制內(nèi)存一樣。查看memory.usag_in_bytes文件
/sys/fs/cgroup/memory # cat memory.usage_in_bytes 2739376
通過(guò)docker stats 容器id查看容器的占用內(nèi)存,和memory.usage_in_bytes的數(shù)據(jù)相符。
4.使用dd命令快速生成1.5g大文件~ # dd if=/dev/zero of=test bs=1M count=15001500+0 records in1500+0 records out1572864000 bytes (1.5GB) copied, 1.279989 seconds, 1.1GB/s
再次通過(guò)docker stats 容器id查看容器的占用內(nèi)存
查看memory.usage_in_bytes文件
/sys/fs/cgroup/memory # cat memory.usage_in_bytes 1619329024
發(fā)現(xiàn)容器的占用內(nèi)存達(dá)到了1.5g,查看memory.stat
/sys/fs/cgroup/memory # cat memory.statcache 1572868096rss 147456rss_huge 0mapped_file 0dirty 1572868096writeback 0swap 0pgpgin 384470pgpgout 433pgfault 607pgmajfault 0inactive_anon 77824active_anon 12288inactive_file 1572864000active_file 4096unevictable 0hierarchical_memory_limit 2147483648hierarchical_memsw_limit 4294967296total_cache 1572868096total_rss 147456total_rss_huge 0total_mapped_file 0total_dirty 1572868096total_writeback 0total_swap 0total_pgpgin 384470total_pgpgout 433total_pgfault 607total_pgmajfault 0total_inactive_anon 77824total_active_anon 12288total_inactive_file 1572864000total_active_file 4096total_unevictable 0
memory.stat文件中的cache字段添加了1.5g,而inactive_file字段為1.5g,因此,dd所產(chǎn)生的文件cache計(jì)算在inactive_file上。這就導(dǎo)致了所看到的容器內(nèi)存的監(jiān)控居高不下,因?yàn)閏ache是可重用的,并不能反映進(jìn)程占用內(nèi)存。
一般情況下,計(jì)算監(jiān)控內(nèi)存可根據(jù)計(jì)算公式:
active_anon + inactive_anon = anonymous memory + file cache for tmpfs + swap cacheThereforeactive_anon + inactive_anon ≠ rss, because rss does not include tmpfs.active_file + inactive_file = cache - size of tmpfs
所以實(shí)際內(nèi)存使用計(jì)算為:
real_used = memory.usage_in_bytes - (active_file + inactive_file)5.壓測(cè)
(1)準(zhǔn)備tomcat鏡像和jmeter壓測(cè)工具,tomcat的yaml文件如下
apiVersion: extensions/v1beta1kind: Deploymentmetadata: name: tomcat-deploymentspec: replicas: 1 template: metadata: labels: app: tomcat spec: containers: - name: tomcat image: registy.dcos:8021/public/tomcat:8 ports: - containerPort: 8080 resources: limits: cpu: '1' memory: 300Mi--- apiVersion: v1kind: Servicemetadata: labels: name: tomcat name: tomcat namespace: defaultspec: ports: - name: tomcat port: 8080 protocol: TCP targetPort: 8080 type: NodePort selector: app: tomcat
yaml文件中限制tomcat鏡像的使用內(nèi)存為300Mi,執(zhí)行命令生成文件。通過(guò)docker stats查看沒(méi)有負(fù)載情況下tomcat容器的內(nèi)存占用。
(2)提取tomcat的service nodePort端口
[docker@ecs-5f72-0006 ~]$ kubectl get svc tomcat -o=custom-columns=nodePort:.spec.ports[0].nodePortnodePort31401
(3)登陸jmeter官網(wǎng)下載壓測(cè)工具
在windows上運(yùn)行jmeter工具,到bin目錄點(diǎn)擊運(yùn)行jmeter,配置jmeter如下:
配置好測(cè)試選項(xiàng)后點(diǎn)擊啟動(dòng)按鈕開(kāi)始?jí)簻y(cè),通過(guò)docker stats查看容器內(nèi)存使用情況發(fā)現(xiàn)已經(jīng)到達(dá)限制。
通過(guò)kubectl get pods查看pod的運(yùn)行情況發(fā)現(xiàn)tomcat由于內(nèi)存超過(guò)限制值被kill掉。
關(guān)于docker stats內(nèi)存監(jiān)控的問(wèn)題一直存在,docker將cache/buffer納入內(nèi)存計(jì)算引起誤解。docker內(nèi)存的計(jì)算方式和linux的內(nèi)存使用計(jì)算方式一致,也包含了cache/buffer。
但是cache是可重復(fù)利用的,經(jīng)常使用在I/O請(qǐng)求上,使用內(nèi)存來(lái)緩解可能被再次訪問(wèn)的數(shù)據(jù),為提高系統(tǒng)性能。
在官方github上,也有很多人提交了關(guān)于內(nèi)存監(jiān)控的issue,直到了Docker 17.06版本,docker stats才解決了這個(gè)問(wèn)題。
但是這也僅僅是docker stats的顯示看起來(lái)正常了,而進(jìn)入容器查看內(nèi)存的使用還是包含的cache,如果直接使用cadvisor搜集的數(shù)據(jù),還是會(huì)出現(xiàn)包含了cache的情況。
通過(guò)壓測(cè)docker,最后發(fā)現(xiàn)當(dāng)壓測(cè)到程序的限制內(nèi)存時(shí),pod出現(xiàn)重啟,這也解釋了我們?cè)谑褂胐ocker監(jiān)控時(shí),即使內(nèi)存占用99%+,卻不出現(xiàn)pod重啟的情況,這里面有相當(dāng)一部分的內(nèi)存是cache占用。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. SXNA RSS Blog 聚合器程序2. JSP的Cookie在登錄中的使用3. ASP常用日期格式化函數(shù) FormatDate()4. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器5. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向6. XML 增、刪、改和查示例7. PHP設(shè)計(jì)模式中工廠模式深入詳解8. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享9. webpack高級(jí)配置與優(yōu)化詳解10. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式
