三者的關(guān)系
嚴格的說度苔,Container技術(shù)并非虛擬化,更準確的說它是一種資源隔離機制。VMM或者叫Hypervisor是標準的虛擬化技術(shù)竣付,這種技術(shù)通過虛擬層(也就是VMM或叫Hypervisor)把上層虛擬機的指令轉(zhuǎn)換成底層Host操作系統(tǒng)所認識的指令。所以滞欠,虛擬化技術(shù)可以在主機上虛擬出各種類型的操作系統(tǒng)古胆,而Container技術(shù)只能“虛擬”出與其主機操作系統(tǒng)相同或很類似的“虛擬機”。
LXC和Warden以及最近火熱的Docker都是基于CGroup在其上封裝了一層筛璧。所以逸绎,我們還是一起學(xué)習(xí)CGroup吧:)
一、CGroup
(1)CGroup簡介及用法
http://linuxaria.com/article/introduction-to-cgroups-the-linux-conrol-group?lang=en
控制資源的三種方法:
First method it’s to use the meta-filesystem of cgroups.
|
<pre class="bash" name="code" style="margin: 0px 0px 24px; padding: 0px; font-weight: 400; box-sizing: border-box; background-color: rgb(240, 240, 240); overflow-x: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0);">$ echo 2048 >> /sys/fs/cgroup/cpu/Multimedia/cpu.shares</pre>
|
Second method it’s to use a set of commands
|
<pre class="bash" name="code" style="margin: 0px 0px 24px; padding: 0px; font-weight: 400; box-sizing: border-box; background-color: rgb(240, 240, 240); overflow-x: auto; font-family: Consolas, Inconsolata, Courier, monospace; font-size: 14px; line-height: 22px; color: rgb(0, 0, 0);">cgset -r cpu.shares=1024 Browsers</pre>
|
** Notice, the value of memory.memsw.limit_in_bytes must not less than that of memory.limit_in_bytes. See the following validation:**
root@app-node-v-o:/home/test/memleak# echo 1G >> /sys/fs/cgroup/memory/zhxue/memory.limit_in_bytes
root@app-node-v-o:/home/test/memleak# echo 200M >> /sys/fs/cgroup/memory/zhxue/memory.memsw.limit_in_bytes
bash: echo: write error: Invalid argument
root@app-node-v-o:/home/test/memleak# echo 100M >> /sys/fs/cgroup/memory/zhxue/memory.memsw.limit_in_bytes
bash: echo: write error: Invalid argument
root@app-node-v-o:/home/test/memleak# echo 10G >> /sys/fs/cgroup/memory/zhxue/memory.memsw.limit_in_bytes
The cgred Daemon
Cgred is a daemon that moves tasks into cgroups according to parameters set in the /etc/cgrules.conf file. Entries in the /etc/cgrules.conf file can take one of the two forms:
user
hierarchies
control_group
user
:command
hierarchies
control_group
For example:
<pre style="margin: 1.5em 0px; padding: 1em; font-weight: 400; box-sizing: border-box; background-color: rgb(247, 247, 247); overflow: auto; font-family: "Courier New", Courier, Monaco, "Lucida Console", monospace; font-size: 14px; line-height: 1.5em; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; border: 1px solid rgb(221, 221, 221);">maria devices /usergroup/staff</pre>
This entry specifies that any processes that belong to the user named maria
access the devices subsystem according to the parameters specified in the /usergroup/staff
cgroup. To associate particular commands with particular cgroups, add the command
parameter, as follows:
<pre style="margin: 1.5em 0px; padding: 1em; font-weight: 400; box-sizing: border-box; background-color: rgb(247, 247, 247); overflow: auto; font-family: "Courier New", Courier, Monaco, "Lucida Console", monospace; font-size: 14px; line-height: 1.5em; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial; border: 1px solid rgb(221, 221, 221);">maria:ftp devices /usergroup/staff/ftp</pre>
The entry now specifies that when the user named maria
uses the ftp
command, the process is automatically moved to the /usergroup/staff/ftp
cgroup in the hierarchy that contains the devices
subsystem.
/etc/cgrules.conf
文???件???中???的???條???目???可???包???括???以???下???額???外???符???號???:
@
- 當???在???user
使???用???前???綴???時???夭谤,代???表???是???一???個???組???群???而???不???是???單???獨???用???戶???棺牧。???例???如???:@admins
是???admins
組???群???中???的???所???有???用???戶???。???*
- 代???表???“???所???有???”???朗儒。???例???如???:subsystem
字???段???中???的???*
代???表???所???有???子???系???統(tǒng)???颊乘。???%
- 代???表???與???以???上???行???中???項???目???相???同???的???項???目???参淹。???例???如???:
問題:
1)在ubuntu高版本(13.04)上無法更改memsw值:
a. 直接該文件不行
root@ubuntudev:/sys/fs/cgroup/memory# echo 100M >> zhxue/memory.limit_in_bytes root@ubuntudev:/sys/fs/cgroup/memory# echo 100M >> zhxue/memory.memsw.limit_in_bytes
bash: echo: write error: Operation not supported
root@ubuntudev:/sys/fs/cgroup/memory# cat zhxue/memory.limit_in_bytes
104857600
root@ubuntudev:/sys/fs/cgroup/memory# cat zhxue/memory.memsw.limit_in_bytes
cat: zhxue/memory.memsw.limit_in_bytes: Operation not supported
b. 命令更改也無效:
root@ubuntudev:/sys/fs/cgroup/memory# cgset -r memory.memsw.limit_in_bytes=10MB zhxue
cgset: the group can't be modified
注意到memory.memsw.limit_in_bytes和memory.limit_in_bytes文件屬性一模一樣,為什么不能改乏悄?很奇怪浙值。
(2) CGroup原理——很全面的文章
從底層數(shù)據(jù)結(jié)構(gòu)到上層文件系統(tǒng),非常有價值檩小。
Linux Cgroups詳解 by 王喆鋒
http://files.cnblogs.com/lisperl/cgroups%E4%BB%8B%E7%BB%8D.pdf
(3) CGroup文件及參數(shù)詳解
https://www.kernel.org/doc/Documentation/cgroups/
在內(nèi)存這塊开呐,CGroup默認分配的內(nèi)存為8PB,請看:
root@ubuntudev:/sys/fs/cgroup/memory# cgcreate -g memory:zhxue
root@ubuntudev:/sys/fs/cgroup/memory# ll zhxue/
cgroup.clone_children memory.memsw.usage_in_bytes
cgroup.event_control memory.move_charge_at_immigrate
cgroup.procs memory.oom_control
memory.failcnt memory.soft_limit_in_bytes
memory.force_empty memory.stat
memory.limit_in_bytes memory.swappiness
memory.max_usage_in_bytes memory.usage_in_bytes
memory.memsw.failcnt memory.use_hierarchy
memory.memsw.limit_in_bytes notify_on_release
memory.memsw.max_usage_in_bytes tasks
root@ubuntudev:/sys/fs/cgroup/memory# cat zhxue/memory.limit_in_bytes
9223372036854775807
root@ubuntudev:/sys/fs/cgroup/memory# cgset -r memory.limit_in_bytes=10M zhxue
root@ubuntudev:/sys/fs/cgroup/memory# cat zhxue/memory.limit_in_bytes
10485760
二识啦、LXC
LXC(Linux containers)配置文件簡介
http://www.cnblogs.com/lisperl/archive/2012/04/16/2451215.html
LXC:Linux 容器工具
http://www.ibm.com/developerworks/cn/linux/l-lxc-containers/
Linux 內(nèi)存不足的解決辦法 – 增加虛擬內(nèi)存 Swap Space
http://www.cpanelcn.info/linux-%E5%86%85%E5%AD%98%E4%B8%8D%E8%B6%B3%E7%9A%84%E8%A7%A3%E5%86%B3%E5%8A%9E%E6%B3%95-%E5%A2%9E%E5%8A%A0%E8%99%9A%E6%8B%9F%E5%86%85%E5%AD%98-swap-space.html
轉(zhuǎn)載自:http://blog.csdn.net/zhxue123/article/details/8945236