cgroups 系統(tǒng)是 linux 內(nèi)核提供的一個(gè)基于進(jìn)程組的資源管理的框架,可以為特定的進(jìn)程組限定可以使用的資源。其具體介紹可以參考周明耀先生的CGroup 介紹、應(yīng)用實(shí)例及原理描述【1】,該文詳細(xì)講述了cgroup的概念、結(jié)構(gòu)、原理,并給出了應(yīng)用實(shí)例,是不可多得的精品級(jí)入門文檔,因此建議移步學(xué)習(xí),其內(nèi)容在此也不作復(fù)述。
本文主要解答對(duì)cgroup的認(rèn)知和運(yùn)用上的一些常見問題。
1. namespace與hierarchy、cgroup是什么關(guān)系,容器=層級(jí)?
為了更好的理解他們的關(guān)系,我們先看一下曹江華先生的Linux 容器的建立和簡單管理【2】中對(duì)cgroup子系統(tǒng)的介紹,其中提到了這樣一個(gè)子系統(tǒng)——ns:
ns 子系統(tǒng)提供了一個(gè)將進(jìn)程分組到不同名稱空間的方法。在具體名稱空間中,進(jìn)程可彼此互動(dòng),但會(huì)與在其它名稱空間中運(yùn)行的進(jìn)程隔絕。這些分開的名稱空間在用于操作系統(tǒng)級(jí)別的虛擬化時(shí),有時(shí)也稱之為容器。
可見,容器和層級(jí)并沒有直接的關(guān)系,容器在cgroup系統(tǒng)中只是作為一個(gè)子系統(tǒng)(資源管理器)為hierarchy中的進(jìn)程劃分命名空間,將之隔離開來。
值得一提的是,提到ns子系統(tǒng)的文章都是2013年之前的,這是因?yàn)樵?013年linux內(nèi)核中的cgroup功能做了很多改動(dòng),直接把ns子系統(tǒng)剔除了。參考Wiki中提到的:
Redesign of cgroups started in 2013, with additional changes brought by versions 3.15 and 3.16 of the Linux kernel.……The "ns" subsystem was added early in cgroups development to integrate namespaces and control groups. If the "ns" cgroup was mounted, each namespace would also create a new group in the cgroup hierarchy. This was an experiment that was later judged to be a poor fit for the cgroups API, and removed from the kernel.
為什么要剔除ns子系統(tǒng)呢?筆者的理解是這樣的,用過cgroup的人會(huì)清楚,cgroup就是對(duì)系統(tǒng)資源進(jìn)行分配,而容器不僅對(duì)進(jìn)程使用的資源做隔離,還要禁止不同容器間進(jìn)程的通信,這超出了cgroup的職責(zé)范圍,使其功能變得更加復(fù)雜和紊亂。因此,容器不再由cgroup管理,而是交由其他技術(shù)實(shí)現(xiàn)了。
2. cgroup有沒有ID?單個(gè)cgroup可在不同hierarchy中重復(fù)嗎?
cgroup沒有ID,不過有指向hierarchy(結(jié)構(gòu)體)的指針和指向top-cgroup的指針,還包含有分別組織起兄弟cgroup和子cgroup的鏈表的表頭,因此每個(gè)cgroup在系統(tǒng)中都是唯一的。
Hierarchy本質(zhì)上是文件系統(tǒng)中的一個(gè)目錄結(jié)構(gòu),cgroup在hierarchy中是以目錄的形式存在的,不同的hierarchy中的cgroup均是不同的目錄,目錄中包含的是該cgroup和該hierarchy子系統(tǒng)的一系列配置文件,以及子cgroup。
3. hierarchy與subsystem的關(guān)系
hierarchy可有多個(gè)不同的subsystem依附,如cpu、memory同時(shí)依附;一個(gè)subsystem只能依附一個(gè)hierarchy,但如果第二個(gè)hierarchy沒有其他subsystem,那么就可以依附,如可同時(shí)存在兩個(gè)僅有cpu依附的hierarchy。詳細(xì)規(guī)則可參考Red Hat EnterPRise Linux 6 Resource Management Guide【3】中的Relationships Between Subsystems, Hierarchies, Control Groups and Tasks一節(jié)。
4.root-cgroup是否包含hierarchy中所有進(jìn)程?
不是,同一hierarchy內(nèi)每個(gè)cgroup的進(jìn)程都不會(huì)出現(xiàn)在其他cgroup中。
root-cgroup默認(rèn)的設(shè)定是,系統(tǒng)一旦有新的進(jìn)程出現(xiàn),即刻會(huì)被root-cgroup包含進(jìn)去(除了子進(jìn)程,子進(jìn)程生成時(shí)會(huì)自動(dòng)與父進(jìn)程同在一個(gè)cgroup)。某個(gè)cgroup管理的進(jìn)程可在其目錄下的tasks文件中查看,操作進(jìn)程加入cgroup也僅需將pid添加進(jìn)該文件中。
*注:一旦把一個(gè)進(jìn)程放進(jìn)某個(gè)cgroup,不把它移到其他cgroup是不會(huì)離開該cgroup的;而只要加入到其他cgroup,原cgroup將會(huì)自動(dòng)釋放該進(jìn)程。
5.子cgroup能否繼承父cgroup資源配置?
會(huì)自動(dòng)繼承,因?yàn)樽觕group的資源配置不能超出父cgroup的限定范圍。正如Ubuntu Document中提到:
In general, the kernel enforces the hierarchical constraints on limits, so that for instance if devices cgroup /child1 cannot access a disk drive, then/child1/child2 cannot give itself those rights.
而在父cgroup的限定下,子cgroup的資源配置相互之間可以相同。
6.如果給多個(gè)cgroup分配的資源有重疊會(huì)怎樣?譬如說給A分配了50%的資源,給B分配了100%的資源,它們會(huì)如何競爭?
這里給出筆者針對(duì)CPU資源的實(shí)驗(yàn)結(jié)果:假設(shè)A需要使用10%的資源,B需要用80%的資源,那么當(dāng)限制A使用50%的資源的時(shí)候,B是能用80%的;但A需要使用80%的資源的話,那么限定A使用50%的時(shí)候,A和B運(yùn)行時(shí)都使用50%的資源。
可見,cgroup的功能是限定資源,而不是劃分資源。
操作cgroup的命令和子系統(tǒng)配置文件的功能可以參考how to use cgroup【4】。
7.如何在更改容器狀態(tài)后快速切換資源的分配?
修改該容器對(duì)應(yīng)cgroup的配置文件。
8.為什么每個(gè)cgroup里都會(huì)自動(dòng)創(chuàng)建一個(gè)user目錄?- Ubuntu 14.04
參考help.ubuntu.com中的解釋:
As of Ubuntu 14.04 (Linux Kernel 3.16), users are automatically placed in a set of cgroups which they own, safely allowing them to constrain their own jobs using child cgroups. This feature is relied upon, for instance, for unprivileged container creation in lxc.
參考資料
【1】CGroup 介紹、應(yīng)用實(shí)例及原理描述. http://www.ibm.com/developerworks/cn/linux/1506_cgroup/index.html
【2】Linux 容器的建立和簡單管理. https://www.ibm.com/developerworks/cn/linux/1312_caojh_linuxlxc/
【3】Red Hat Enterprise Linux 6 Resource Management Guide. https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Resource_Management_Guide/index.html
【4】how to use cgroup. http://tiewei.github.io/devops/howto-use-cgroup/
【5】CGROUPS. https://www.kernel.org/doc/Documentation/cgroups/cgroups.txt
【6】wiki/Cgroups. https://en.wikipedia.org/wiki/Cgroups
【7】All About the Linux Kernel: Cgroup’s Redesign. http://www.linux.com/news/featured-blogs/200-libby-clark/733595-all-about-the-linux-kernel-cgroups-redesign
【8】Ubuntu Document. https://help.ubuntu.com/lts/serverguide/cgroups-delegation.html
新聞熱點(diǎn)
疑難解答
圖片精選