Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement memory Monitoring of Docker Container

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to implement Docker container memory monitoring". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to implement Docker container memory monitoring".

Linux memory monitoring

To understand how docker container memory is calculated, you must first understand the concepts of memory in linux.

Use the free command to view the current memory usage.

[root@localhost] $free total used free shared buffers cachedMem: 264420684 213853512 50567172 71822688 2095364 175733516 Muhammad + buffers/cache: 36024632 228396052Swap: 16777212 1277964 15499248

Here are a few concepts:

Mem: physical memory

Swap: virtual memory. That is, the data can be stored on the hard disk.

Shared: shared memory. Exists in physical memory.

Buffers: used to store data to be output to disk (block device)

Cached: stores data read from disk

For ease of illustration, I made a response to the results of free.

[root@localhost ~] $free total used free shared buffers cachedMem: total_mem used_mem free_mem shared_mem buffer cache-/+ buffers/cache: real_used real_freeSwap: total_swap used_swap free_swap

It is generally believed that buffer and cache are reusable memory, so when calculating free memory, it will be removed.

So here are a few equations:

Real_used = used_mem-buffer-cachereal_free = free_mem + buffer + cachetotal_mem = used_mem + free_mem

With this in mind, let's take a look at the data source of free. In fact, the data source is from the / proc/memeinfo file.

[root@localhost ~] $cat / proc/meminfo MemTotal: 264420684 kBMemFree: 50566436 kBBuffers: 2095356 kBCached: 175732644 kBSwapCached: 123688 kBActive: 165515340 kBInactive: 37004224 kBActive (anon): 92066880 kBInactive (anon): 4455076 kBActive (file): 73448460 kBInactive (file): 32549148 kBUnevictable: 362900 kBMlocked: 74696 kBSwapTotal: 16777212 kBSwapFree: 15499248 kBDirty: 2860 kBWriteback: 0 kBAnonPages: 24932928 kBMapped: 58165040 kBShmem: 71822688 kBSlab: 8374496 kBSReclaimable: 8163096 kBSUnreclaim: 211400 kBKernelStack: 45824 kBPageTables: 606296 kBNFS_Unstable: 0 kBBounce: 0 kBWritebackTmp: 0 kBCommitLimit: 148987552 kBCommitted_AS: 114755628 kBVmallocTotal: 34359738367 kBVmallocUsed: 772092 kBVmallocChunk: 34225428328 kBHardwareCorrupted: 0 kBAnonHugePages: 22083584 kBHugePages_Total: 0HugePages_Free: 0HugePages_Rsvd: 0HugePages_Surp: 0Hugepagesize: 2048 kBDirectMap4k: 7168 kBDirectMap2M: 2015232 kBDirectMap1G: 266338304 kBdocker

After talking about linux's memory, let's take a look at docker's memory monitoring. Docker itself provides a way of memory monitoring, that is, container memory can be monitored through docker stats.

This method is actually calculated by taking the value of the relevant data in cgroup.

Cgroup

The memory subsystem in cgroup provides the following files for hierarchy.

[root@localhost] $ll / cgroup/memory/docker/53a11f13c08099dd6d21030dd2ddade54d5cdd7ae7e9e68f5ba055ad28498b6f/ total usage 0kw root root w-1 root root 0 February 22 12:51 cgroup.event_control-rw-r--r-- 1 root root 0 May 25 17:07 cgroup.procs-rw-r--r-- 1 root root 0 February 22 12:51 memory.failcnt--w- 1 root root 0 February 22 12:51 memory.force_empty- Rw-r--r-- 1 root root 0 March 30 17:06 memory.limit_in_bytes-rw-r--r-- 1 root root 0 February 22 12:51 memory.max_usage_in_bytes-rw-r--r-- 1 root root 0 February 22 12:51 memory.memsw.failcnt-rw-r--r-- 1 root root 0 March 30 17:06 memory.memsw.limit_in_bytes-rw-r--r-- 1 root Root 0 February 22 12:51 memory.memsw.max_usage_in_bytes-r--r--r-- 1 root root 0 February 22 12:51 memory.memsw.usage_in_bytes-rw-r--r-- 1 root root 0 February 22 12:51 memory.move_charge_at_immigrate-rw-r--r-- 1 root root 0 February 22 12:51 memory.oom_control-rw-r--r-- 1 root root 0 March 30 17:06 memory.soft_limit_in_bytes-r--r--r-- 1 root root 0 February 22 12:51 memory.stat-rw-r--r-- 1 root root 0 February 22 12:51 memory.swappiness-r--r--r-- 1 root root 0 February 22 12:51 memory.usage_in_bytes-rw-r--r-- 1 root root 0 February 22 12:51 memory.use_hierarchy-rw-r--r -- 1 root root 0 February 22 12:51 notify_on_release-rw-r--r-- 1 root root 0 February 22 12:51 tasks

The specific meaning of these documents can be found in the relevant information cgroup memory.

This paper mainly introduces several related to docker monitoring.

[root@localhost 53a11f13c08099dd6d21030dd2ddade54d5cdd7ae7e9e68f5ba055ad28498b6f] $cat memory.usage_in_bytes 135021858816 [root@localhost 53a11f13c08099dd6d21030dd2ddade54d5cdd7ae7e9e68f5ba055ad28498b6f] $cat memory.memsw.usage_in_bytes 135679291392 [root@localhost 53a11f13c08099dd6d21030dd2ddade54d5cdd7ae7e9e68f5ba055ad28498b6f] $cat memory.stat cache 134325506048rss 695980032mapped_file 16155119616pgpgin 21654116032pgpgout 21705492352swap 655171584inactive_anon 4218880active_anon 74202603520inactive_file 8365199360active_file 52449439744unevictable 0hierarchical_memory_limit 137438953472hierarchical_memsw_limit 274877906944total_cache 134325506048total_rss 695980032total_mapped_file 16155119616total_pgpgin 21654116032total_pgpgout 21705492352total_swap 655171584total_inactive_anon 4218880total_active_anon 74202603520total_inactive_file 8365199360total_active_file 52449439744total_unevictable 0memory.stat

Memory.stat contains the richest

Active_anon + inactive_anon = anonymous memory + file cache for tmpfs + swap cache

Active_file + inactive_file = cache-size of tmpfs

Docker native memory monitoring

Let's turn to docker's native docker stats. Its implementation can be seen in libcontainer. It divides the memory monitoring of the container into cache,usage,swap usage,kernel usage,kernel tcp usage.

Where cache is obtained from cache in memory.stat.

Usage uses the division of memory.usage_in_bytes and memory.limit_in_bytes to calculate usage. One drawback of this approach is that it is not detailed enough to distinguish the cache part and can not really reflect the memory usage. Because cache is generally a reusable part of memory, it is generally counted as part of the usable part.

An improved calculation method that can be considered

The improved method excludes cache calculations when counting memory usage. Similar to excluding buffer and cache when calculating real_used in linux.

Cache cannot directly apply cache in memory.stat because it includes tmpfs, and tmpfs is the actual memory part used.

Tmpfs is share memory, shared memory

Because in memory.stat, there are

Active_file + inactive_file = cache-size of tmpfs

Therefore, the amount of memory actually used can be calculated as

Real_used = memory.usage_in_bytes-(rss + active_file + inactive_file) at this point, I believe you have a deeper understanding of "how to implement Docker container memory monitoring". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report