我也查了一下LXC/cgroups,所以记了一些笔记
当我在调查Docker时,对其底层的LXC和cgroup产生了兴趣,因此进行了一些研究。我只是稍微尝试了一下。
什么是LXC? shì LXC?)
-
- LinuX Containerの略で、システムのコンテナ化を行なうための機構
-
- OSレベルで提供されている
-
- Linux kernelの機構であるcgroups/namespaceなどに依存している
-
- 低レイヤな機構であるcgroupsなどをとりまとめる中間レイヤ的な側面を持つが、さらに高レイヤのDockerから利用されることが多い
- 同様の技術にFreeBSD jailやSolaris Containersなど
如何使用
请在下述位置进行安装。
ubuntu@ubuntu:~$ sudo apt-get install lxc
以下是已经准备好的模板。
ubuntu@ubuntu:~$ ls -l /usr/lib/lxc/templates/
total 88
-rwxr-xr-x 1 root root 8227 Oct 29 2013 lxc-busybox
-rwxr-xr-x 1 root root 9806 Oct 29 2013 lxc-debian
-rwxr-xr-x 1 root root 10520 Oct 29 2013 lxc-fedora
-rwxr-xr-x 1 root root 9094 Oct 29 2013 lxc-opensuse
-rwxr-xr-x 1 root root 5115 Oct 29 2013 lxc-sshd
-rwxr-xr-x 1 root root 19736 Oct 29 2013 lxc-ubuntu
-rwxr-xr-x 1 root root 10590 Oct 29 2013 lxc-ubuntu-cloud
尝试安装Debian操作系统。
请注意,这可能需要十分以上的时间。
ubuntu@ubuntu:~$ sudo lxc-create -t debian -n debian0
No config file specified, using the default config
debootstrap is /usr/sbin/debootstrap
Checking cache download in /var/cache/lxc/debian/rootfs-squeeze-i386 ...
Downloading debian minimal ...
I: Retrieving Release
W: Cannot check Release signature; keyring file not available /usr/share/keyrings
/debian-archive-keyring.gpg
I: Retrieving Packages
I: Validating Packages
I: Resolving dependencies of required packages...
I: Resolving dependencies of base packages...
...
当显示列表时,即表示存在。
ubuntu@ubuntu:~$ sudo lxc list
RUNNING
FROZEN
STOPPED
debian0
开始尝试。
ubuntu@ubuntu:~$ sudo lxc start debian0
INIT: version 2.88 booting
...
Starting OpenBSD Secure Shell server: sshd.
Debian GNU/Linux 6.0 debian0 console
debian0 login: root
Password:
Linux debian0 3.2.0-24-generic-pae #39-Ubuntu SMP Mon May 21 18:54:21 UTC 2012 i686
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
root@debian0:~#
尽管分布不同,但可以看出它们在相同的内核上运行。
ubuntu@ubuntu:~$ uname -a
Linux ubuntu 3.2.0-24-generic-pae #39-Ubuntu SMP Mon May 21 18:54:21 UTC 2012 i686 i686 i386 GNU/Linux
root@debian0:~# uname -a
Linux debian0 3.2.0-24-generic-pae #39-Ubuntu SMP Mon May 21 18:54:21 UTC 2012 i686 GNU/Linux
顺便说一句,通过以下命令也可以安装Fedora,但与Debian相比,所需时间较长。可能需要大约1小时的时间。由于基础系统差异较大,似乎需要单独安装各种rpm软件包。
ubuntu@ubuntu:~$ sudo lxc-create -t fedora -n fedora0
No config file specified, using the default config
...
'fedora' template installed
'fedora0' created
ubuntu@ubuntu:~$ sudo lxc-start -n fedora0
Fedora release 14 (Laughlin)
Kernel 3.2.0-24-generic-pae on an i686 (tty1)
fedora0 login: root
Password:
Last login: Tue Jul 15 01:01:39 on console
[root@fedora0 ~]# uname -a
Linux fedora0 3.2.0-24-generic-pae #39-Ubuntu SMP Mon May 21 18:54:21 UTC 2012 i686 i686 i386 GNU/Linux
cgroups是什么?
-
- リソースの制限
グループ単位でメモリ割り当てを制限
優先順位の変動
グループ単位でCPU, Disk I/Oの優先順位を変更
監視
グループ単位で使用しているリソースを計上
各グループは階層構造を持つ
子グループは親グループの属性を一定引き継ぐ
另外,先前的版本似乎支持命名空间的分离,但现在已被删除。这意味着命名空间作为内核功能是独立的吗?
如何使用
首先,让我们讨论一下资源限制的问题。首先要进行安装。
$ sudo apt-get install cgroup-bin
相关命令包括:
– cgcreate
– cgexec
– cgclassify
创建cgroup。可以限制内存和CPU资源。
$ sudo cgcreate -g memory,cpu:test
可以通过下面的sysfs进行确认。
$ ls -l -l /sys/fs/cgroup/cpu/test/
total 0
-rw-r--r-- 1 root root 0 Jul 15 13:17 cgroup.clone_children
--w--w--w- 1 root root 0 Jul 15 13:17 cgroup.event_control
-rw-r--r-- 1 root root 0 Jul 15 13:17 cgroup.procs
-rw-r--r-- 1 root root 0 Jul 15 13:17 cpu.cfs_period_us
-rw-r--r-- 1 root root 0 Jul 15 13:17 cpu.cfs_quota_us
-rw-r--r-- 1 root root 0 Jul 15 13:17 cpu.rt_period_us
-rw-r--r-- 1 root root 0 Jul 15 13:17 cpu.rt_runtime_us
-rw-r--r-- 1 root root 0 Jul 15 13:17 cpu.shares
-r--r--r-- 1 root root 0 Jul 15 13:17 cpu.stat
-rw-r--r-- 1 root root 0 Jul 15 13:17 notify_on_release
-rw-r--r-- 1 root root 0 Jul 15 13:17 tasks
在特定的cgroup中启动进程。
$ sudo cgexec -g memory,cpu:test /bin/bash
可以看出它属于指定的CGroup。
# cat /proc/self/cgroup
5:freezer:/sysdefault
4:memory:/test
3:devices:/sysdefault
2:cpuacct:/sysdefault
1:cpu:/test
CPU的优先级可以设定。以100为单位,10%的优先级。
# cat /sys/fs/cgroup/cpu/test/cpu.shares
1024
# echo 100 >/sys/fs/cgroup/cpu/test/cpu.shares
内存使用限制。尝试申请更多内存将导致交换出内存。
# cat /sys/fs/cgroup/memory/test/memory.limit_in_bytes
9223372036854775807
# echo $((1024*1024)) >/sys/fs/cgroup/memory/test/memory.limit_in_bytes
可以看到统计数据。
cat /sys/fs/cgroup/cpu/test/cgroup.procs
9239
# ps auxw | grep 9239
root 9239 0.0 0.4 6960 2216 pts/1 S 13:35 0:00 /bin/bash
Docker / LXC / cgroups的含义是相似的。
不知怎么的,感觉这些系统有些相似,但最终这些关系是如何的呢?关于这个疑问,这里提供详细解答。
-
- Dockerは下位レイヤとしてLXCを利用する
-
- コンテナの可搬性(LXCでは環境が異なるとコンテナの互換性がない)
-
- アプリの配布に最適化している
-
- 自動ビルド(Dockerfile)
-
- バージョン管理
-
- コンテナ再利用
-
- コンテナ共有
- 様々なツール/システムとの連携
如果按照功能分类,
- 文件系统的分离由LXC(namespace)提供,资源的分离由LXC(cgroup)提供,网络的分离由LXC(cgroup)提供。文件系统的写时复制由AUFS提供,日志记录由Docker提供,文件的修改管理和重用由Docker提供,交互式Shell由LXC提供。