- 追è¨
- ç´è¿ã® Docker çéã«ã¤ãã¦
- Linux ã³ã³ãã
- haconiwa
- haconiwa ã§å¦ã¶ Linux ã³ã³ãã
- 俺㯠Linux ã³ã³ããã«ã¤ãã¦ãªãã«ã解ã£ã¦ããªãã£ã(ã¾ã¨ã)
- åè
追è¨
@udzura ããããã³ã¡ã³ããé ãã¾ããï¼
ãªãOKç§å ´ãã¨ããã¨ãNetwork namespaceã®unshareè¨å®ãå ¥ã£ã¦ããªãã®ã§ããã¹ãã¨ãããè¨å®ãå ±æãã¦ããã ããªã®ã§ãã£ã... (ãã®ãã¡vethå¼ã£å¼µãæé ãæ¸ãã¾ããæ¤è¨¼ã¬ãã¼ãèª ã«æè¬ã / â俺â¦â https://t.co/yuwoLfeLCo
— Uchio KONDO (@udzura) 2017å¹´5æ1æ¥
æé£ããããã¾ããï¼
ç´è¿ã® Docker çéã«ã¤ãã¦
以ä¸ã®ãããªãã¼ã¯ã¼ããã¤ã³ã¿ã¼ãããä¸ãé§ãå·¡ã£ã¦ããæ°ãããã
èªåèªèº«ãããã®ãã®ãã¼ã¯ã¼ãã«ã¤ãã¦ä¸èº«ãå ¨ãç解åºæ¥ã¦ããªãã
ã¾ããDocker ã触ãæ©ä¼ãæ¸ã£ã¦ããã®ãäºå®ã§ããã°ããªãããã°ããªãã¨æããªãããã´ã¼ã«ãã³ã¦ã£ã¼ã¯çªå ¥ã¨ãªã£ãã®ã§ãæ¹ãã¦ãDocker ã¨ããããã Linux ã³ã³ããæè¡ã«ã¤ãã¦åå¼·ãã¦ã¿ããã¨æãã
Linux ã³ã³ãã
åè
Linux ã³ã³ããã¨ã¯
Linux ã«ã¼ãã«ã®æ©è½ã§ä»¥ä¸ã®ãããªæ©è½ãæä¾ããã
- éé¢ããã空éã§ããã»ã¹ãå®è¡ãã
- ããã»ã¹ã«å¯¾ãã¦ãªã½ã¼ã¹å¶éãè¨å®ãã
Linux ã³ã³ãããæ§æãã主ãªæ©è½
Linux ã³ã³ããã¯ã«ã¼ãã«ã«å«ã¾ãã以ä¸ã®ãããªæ©è½ãå©ç¨ãã¦å®è£ ããã¦ããã
åå | å½¹å² |
---|---|
namespace | OS ãªã½ã¼ã¹ã®éé¢(ããã»ã¹ãã°ã«ã¼ãåãã¦ä»ã®ãªã½ã¼ã¹ã¨éé¢) |
cgroup | ãã¹ãã®ç©çãªã½ã¼ã¹ã«å¯¾ããå¶é(ã°ã«ã¼ãåããããã»ã¹å¯¾ãããªã½ã¼ã¹å¶é) |
capability | root 権éãããã»ã¹ããã¡ã¤ã«ã«å²ãå½ã¦ã |
ä¸è¨ä»¥å¤ã«ã Bind mount/chroot
ã Resource limit(rlimit)
åã³ setuid/setgid
çã®æ©è½ãå©ç¨ããã¦ããã
OS ãªã½ã¼ã¹æ¯ã® Namespace
OS ãªã½ã¼ã¹æ¯ã«ä»¥ä¸ã®ãã㪠Namespace ãæä¾ããã¦ããã
- Mount Namespace
- UTS Namespace
- PID Namespace
- IPC Namespace
- User Namespace
- Network Namespace
- cgroup Namespace
å Namespace ã¯ã«ã¼ãã«ã®ãã¼ã¸ã§ã³ã追ãæ¯ã«è¿½å å®è£ ããã¦ããã
cgroup ãµãã·ã¹ãã
cgroup 㯠cgroup ãã¡ã¤ã«ã·ã¹ãã ã¨ããä»®æ³çãªãã¡ã¤ã«ã·ã¹ãã ã使ã£ã¦æä½ããNamespace åæ§ã«ä»¥ä¸ã®ãããªãµãã·ã¹ãã ã¨å¼ã°ããæ©è½ã§ãªã½ã¼ã¹ãæ±ãã
- cpu
- cpuacct
- cpuset
- device
- freezer
- memory
- blkio
ä»ã«ã hugetlb ã perf_event çãæä¾ããã¦ããã
ãããªã«ãã£ããã§ã¯ãLinux ã³ã³ããã¯èªããªãã¨æããã©
æä½éãä¸è¨ã®ãããªãã¼ã¯ã¼ãã ãã¯ãã£ããã¨è¦ãã¦ããããã
haconiwa
haconiwa ã¨ã¯
- @udzura ãããã¡ã¤ã³ã¨ãªã£ã¦å®è£ ããã¦ãã mruby ã§å®è£ ããã Linux ã³ã³ããã®ã©ã³ã¿ã¤ã
- Ruby DSL 㧠Linux ã³ã³ããã®è¨å®ãè¨è¿°ãããã¨ãåºæ¥ã¦ãèªåã ãã® Linux ã³ã³ãããä½æãããã¨ãåºæ¥ã
haconiwa ã§ä½ãåºæ¥ãã¨ï¼
- å©ç¨ããã³ã³ããè¦ç´ ãèªç±ã«é¸æåºæ¥ã
- Ruby DSL ã使ã£ã¦ããã°ã©ããã«ã« Linux ã³ã³ãããä½æåºæ¥ã
- ããã°ã©ããã«ã«ãªããã¨ã§åçãªãã½ã¼ã¹å²ãå½ã¦ãåºæ¥ãçã®æè»ãªéç¨ãå¯è½
haconiwa å°å ¥
haconiwa 㯠mruby-cli ã§ãã«ãããããã¤ããªã§é å¸ããã¦ãããpackagecloud ã«ã¦ããã±ã¼ã¸é å¸ããã¦ããã®ã§ã以ä¸ã®ããã«ã¤ã³ã¹ãã¼ã«ããã
# # Ubunt/xenial ã«å°å ¥ãã # $ cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=16.04 DISTRIB_CODENAME=xenial DISTRIB_DESCRIPTION="Ubuntu 16.04.2 LTS" # # äºåã« lxc ãå°å ¥æ¸ã¿ # $ dpkg --list | grep lxc ii liblxc1 2.0.7-0ubuntu1~16.04.2 amd64 Linux Containers userspace tools (library) ii lxc 2.0.7-0ubuntu1~16.04.2 all Transitional package for lxc1 ii lxc-common 2.0.7-0ubuntu1~16.04.2 amd64 Linux Containers userspace tools (common tools) ii lxc-templates 2.0.7-0ubuntu1~16.04.2 amd64 Linux Containers userspace tools (templates) ii lxc1 2.0.7-0ubuntu1~16.04.2 amd64 Linux Containers userspace tools ii lxcfs 2.0.6-0ubuntu1~16.04.1 amd64 FUSE based filesystem for LXC ii python3-lxc 2.0.7-0ubuntu1~16.04.2 amd64 Linux Containers userspace tools (Python 3.x bindings) # # haconiwa ããã±ã¼ã¸ã®å°å ¥ # $ curl -s https://packagecloud.io/install/repositories/udzura/haconiwa/script.deb.sh | sudo bash $ sudo apt-get install haconiwa=0.8.5-1 # # ãã«ãã®ç¢ºèª # ubuntu@ubuntu-xenial:~$ haconiwa haconiwa - The MRuby on Container commands: new - generate haconiwa's config DSL file template create - create the container rootfs provision - provision already booted container rootfs archive - create, provision, then archive rootfs to image start - run the container attach - attach to existing container reload - reload running container parameters, following its current config kill - kill the running container version - show version revisions - show mgem/mruby revisions which haconiwa bin uses Invoke `haconiwa COMMAND -h' for details.
ã¯ããã¦ã® haconiwa (1)
以ä¸ã®ããã« haconiwa new
ãå®è¡ããã¨å®ç¾©ãã¡ã¤ã«ã®éå½¢ãçæããäºãåºæ¥ãã
haconiwa new \ --name=my-first-container \ --root=/var/lib/haconiwa/my-first-container my-first-container.haco
以ä¸ã®ããã«åºåãããmy-first-container.haco
ã¨ãããã¡ã¤ã«ãã«ã¬ã³ããã£ã¬ã¯ããªã«çæããã¦ããã
create my-first-container.haco
.haco
ãã¡ã¤ã«ã®ä¸èº«ã¯ä»¥ä¸ã®ããã«ãªã£ã¦ããã
$ grep -v -e '^\s*#' -e '^\s*$' my-first-container.haco Haconiwa.define do |config| config.name = "my-first-container" config.init_command = "/bin/bash" root = Pathname.new("/var/lib/haconiwa/my-first-container") config.chroot_to root config.bootstrap do |b| b.strategy = "lxc" b.os_type = "alpine" end config.provision do |p| p.run_shell <<-SHELL apk add --update bash SHELL end config.add_mount_point "tmpfs", to: root.join("tmp"), fs: "tmpfs" config.mount_network_etc(root, host_root: "/etc") config.mount_independent "procfs" config.mount_independent "sysfs" config.mount_independent "devtmpfs" config.mount_independent "devpts" config.mount_independent "shm" config.namespace.unshare "mount" config.namespace.unshare "ipc" config.namespace.unshare "uts" config.namespace.unshare "pid" end
ã¯ããã¦ã® haconiwa (2) ã ã³ã³ããä½æ ã
以ä¸ã®ããã« create
ãªãã·ã§ã³ãæå®ã㦠haconiwa ãå®è¡ããã¨ãlxc-create
ãå®è¡ãã㦠alpine linux ã®ã³ã³ããã®ä½æãéå§ãããã
sudo haconiwa create my-first-container.haco
以ä¸ã®ããã«åºåãããã
ubuntu@ubuntu-xenial:~$ sudo haconiwa create my-first-container.haco Creating rootfs of my-first-container... Start bootstrapping rootfs with lxc-create... [bootstrap.lxc]: Obtaining an exclusive lock... done ... Command success: lxc-create -n my-first-container -t alpine --dir /var/lib/haconiwa/my-first-container exited 0 Success! Start provisioning... Running provisioning with shell script... [provison.shell-1]: + apk add --update bash [provison.shell-1]: fetch http://mirror.yandex.ru/mirrors/alpine//v3.5/main/x86_64/APKINDEX.tar.gz [provison.shell-1]: (1/5) Installing ncurses-terminfo-base (6.0-r7) [provison.shell-1]: (2/5) Installing ncurses-terminfo (6.0-r7) [provison.shell-1]: (3/5) Installing ncurses-libs (6.0-r7) [provison.shell-1]: (4/5) Installing readline (6.3.008-r4) [provison.shell-1]: (5/5) Installing bash (4.3.46-r5) [provison.shell-1]: Executing bash-4.3.46-r5.post-install [provison.shell-1]: Executing busybox-1.25.1-r0.trigger [provison.shell-1]: OK: 14 MiB in 21 packages Command success: /bin/sh -xe exited 0 Success!
ã¯ããã¦ã® haconiwa (3) ã ã³ã³ããèµ·å ã
以ä¸ã®ããã« run
å㯠start
ãªãã·ã§ã³ãæå®ã㦠haconiwa ãå®è¡ããã¨ã³ã³ãããèµ·åããï¼ï¼
$ sudo haconiwa run my-first-container.haco Container fork success and going to wait: pid=1893 bash-4.3# ps PID USER TIME COMMAND 1 root 0:00 /bin/bash 2 root 0:00 ps bash-4.3# exit exit Container(1893) finish detected: #<Process::Status: pid=1893,exited(0)> Container successfully exited: #<Process::Status: pid=1893,exited(0)> One of supervisors finished: 1892, #<Process::Status: pid=1892,exited(0)> $ sudo haconiwa start my-first-container.haco Container fork success and going to wait: pid=1906 bash-4.3# ps PID USER TIME COMMAND 1 root 0:00 /bin/bash 2 root 0:00 ps bash-4.3# exit exit Container(1906) finish detected: #<Process::Status: pid=1906,exited(0)> Container successfully exited: #<Process::Status: pid=1906,exited(0)> One of supervisors finished: 1905, #<Process::Status: pid=1905,exited(0)>
ã¡ãªã¿ã«ãèµ·åããã³ã³ããããã¤ã³ã¿ã¼ãããã¸ã®ã¢ã¯ã»ã¹ã ã£ã¦ OK ç§å ´ã
$ sudo haconiwa start my-first-container.haco Container fork success and going to wait: pid=1919 bash-4.3# bash-4.3# ping www.yahoo.com -c 3 PING www.yahoo.com (106.10.139.246): 56 data bytes 64 bytes from 106.10.139.246: seq=0 ttl=63 time=137.592 ms 64 bytes from 106.10.139.246: seq=1 ttl=63 time=158.620 ms 64 bytes from 106.10.139.246: seq=2 ttl=63 time=106.147 ms --- www.yahoo.com ping statistics --- 3 packets transmitted, 3 packets received, 0% packet loss round-trip min/avg/max = 106.147/134.119/158.620 ms bash-4.3#
haconiwa ã§å¦ã¶ Linux ã³ã³ãã
.haco ãã¡ã¤ã«åæ²
ããããã¯ã以ä¸ã® .haco
ãã¡ã¤ã«ãå©ç¨ã㦠Linux ã³ã³ãããå¼ã£ã¦ã¿ããã¨æãã
Haconiwa.define do |config| config.name = "my-first-container" config.init_command = "/bin/bash" root = Pathname.new("/var/lib/haconiwa/my-first-container") config.chroot_to root config.bootstrap do |b| b.strategy = "lxc" b.os_type = "alpine" end config.provision do |p| p.run_shell <<-SHELL apk add --update bash SHELL end config.add_mount_point "tmpfs", to: root.join("tmp"), fs: "tmpfs" config.mount_network_etc(root, host_root: "/etc") config.mount_independent "procfs" config.mount_independent "sysfs" config.mount_independent "devtmpfs" config.mount_independent "devpts" config.mount_independent "shm" config.namespace.unshare "mount" config.namespace.unshare "ipc" config.namespace.unshare "uts" config.namespace.unshare "pid" end
namespace ãå¼ãåã«
Namespace ã®æ©è½ã«ã¤ãã¦ããããã
namespace | æ©è½ |
---|---|
mount | Namespace å
ã® mount / umount ãä»ã® Namespace ã«å½±é¿ãä¸ããªãããã«ãã |
ipc | SysV IPC ãªãã¸ã§ã¯ãã POSIX ã¡ãã»ã¼ã¸ãã¥ã¼ã®éé¢ |
uts | hostname ã uname ã®çµæãåé¢ |
pid | PID 空éã®åé¢ãæ°ãã PID Namespace ã§ã¯ PID 1 ããå§ã¾ã |
uts
㨠pid
ã«ã¤ãã¦å¼ã£ã¦ã¿ãã
namespace uts
以ä¸ã®ããã« config.namespace.unshare "uts"
ãã³ã¡ã³ãã¢ã¦ãã
ubuntu@ubuntu-xenial:~$ diff -u my-first-container.haco.bk my-first-container.haco --- my-first-container.haco.bk 2017-04-29 23:35:21.313501998 +0000 +++ my-first-container.haco 2017-04-29 23:35:32.883713999 +0000 @@ -59,7 +59,7 @@ # The namespaces to unshare: config.namespace.unshare "mount" config.namespace.unshare "ipc" - config.namespace.unshare "uts" + # config.namespace.unshare "uts" config.namespace.unshare "pid" # You can use existing namespace via symlink file. e.g.:
ä½ãèµ·ããã®ãã
ubuntu@ubuntu-xenial:~$ hostname ubuntu-xenial ubuntu@ubuntu-xenial:~$ sudo haconiwa run my-first-container.haco Container fork success and going to wait: pid=3449 bash-4.3# hostname ubuntu-xenial
uts
㯠hostname
ã uname
ã®çµæãéé¢ãã namespace ã§ããããunshare
ãè¡ã£ã¦ããªãçºãhostname
ã®çµæãéé¢ããã¦ãããããã¹ãã¨åã hostname
ã®çµæãè¿ãã¦ããç¶æ
ã
ã¡ãªã¿ã«ããã®ç¶æ ã§ã³ã³ããå ãããã¹ãåãå¤æ´ãã¦ã¿ãã¨ã以ä¸ã®ããã«æãããã
bash-4.3# hostname foo hostname: sethostname: Operation not permitted
ããã¯ãsethostname(2) ã®
EPERM sethostname() ã«ããã¦ãå¼ã³åºãã人ã CAP_SYS_ADMIN ã±ã¼ãããªã㣠(capability) ãæã£ã¦ããªãã£ãã
ã¨ããäºããå¯ããã«ãhaconiwa ã«ããã¦ã³ã³ããå é¨ããã®ãã¹ãåå¤æ´ã¯ Linux capability ã«ããå¶å¾¡ããã¦ããã¨æãããã
æ¹ãã¦ãconfig.namespace.unshare "uts"
ãã³ã¡ã³ãã¤ã³ã㦠haconiwa run
ãã¦ã¿ãã
ubuntu@ubuntu-xenial:~$ hostname ubuntu-xenial ubuntu@ubuntu-xenial:~$ sudo haconiwa run my-first-container.haco Container fork success and going to wait: pid=3651 bash-4.3# hostname my-first-container
hostname
ã³ãã³ãã®å®è¡çµæããã¹ãå´ã¨ã³ã³ããå
ã§ç°ãªã£ã¦ãããã³ã³ããã«éé¢ãããç¶æ
ã¨ãªã£ã¦ããã
å°ãunshare
ã³ãã³ã㧠uts
ã®ã¿ãåé¢ããå ´åã«ã¯ä»¥ä¸ã®ãããªæåã¨ãªããéé¢ããç°å¢ã«ããã¦å®æ½ãã hostname
ã³ãã³ãã®å½±é¿ã¯éå®çã¨ãªã£ã¦ãããã¨ã解ãã
# # uts namespace ãéé¢ # ubuntu@ubuntu-xenial:~$ sudo unshare --uts -- /bin/bash root@ubuntu-xenial:~# hostname ubuntu-xenial root@ubuntu-xenial:~# hostname foo root@ubuntu-xenial:~# hostname foo # # å¥ç«¯æ«ã§ hostname ã確èª(hostname å¤æ´ã®å½±é¿ã¯åºã¦ããªã) # ubuntu@ubuntu-xenial:~$ hostname ubuntu-xenial ubuntu@ubuntu-xenial:~$ hostname ubuntu-xenial
namespace pid
以ä¸ã®ããã« config.namespace.unshare "pid"
ãã³ã¡ã³ãã¢ã¦ãã
$ diff -u my-first-container.haco.bk my-first-container.haco --- my-first-container.haco.bk 2017-04-29 23:35:21.313501998 +0000 +++ my-first-container.haco 2017-04-29 23:56:19.213720735 +0000 @@ -60,7 +60,7 @@ config.namespace.unshare "mount" config.namespace.unshare "ipc" config.namespace.unshare "uts" - config.namespace.unshare "pid" + # config.namespace.unshare "pid" # You can use existing namespace via symlink file. e.g.: # config.namespace.enter "net", via: "/var/run/netns/sample001"
ä½ãèµ·ããã®ã…
ubuntu@ubuntu-xenial:~$ sudo haconiwa run my-first-container.haco Container fork success and going to wait: pid=3596 bash-4.3# ps aux | less PID USER TIME COMMAND 1 root 0:13 {systemd} /sbin/init 2 root 0:00 [kthreadd] 3 root 0:00 [ksoftirqd/0] ... 3596 root 0:00 /bin/bash 3602 root 0:00 [kworker/u4:2] 3605 root 0:00 ps aux 3606 root 0:00 less
ãã¹ãå´ã®ããã»ã¹ã丸è¦ãç¶æ ã«ãªã£ã¦ããã
æ¹ãã¦ãconfig.namespace.unshare "pid"
ãã³ã¡ã³ãã¤ã³ã㦠haconiwa run
ãã¦ã¿ãã
ubuntu@ubuntu-xenial:~$ sudo haconiwa run my-first-container.haco Container fork success and going to wait: pid=3623 bash-4.3# ps aux PID USER TIME COMMAND 1 root 0:00 /bin/bash 2 root 0:00 ps aux
ããã»ã¹ ID 㯠1
ããéå§ããããã¹ãå´ã®ããã»ã¹ ID ãè¦ããã¨ã¯åºæ¥ãªãç¶æ
ã«ãªã£ã¦ããã³ã³ããã«éé¢ãããç¶æ
ã¨ãªã£ã¦ããã
俺㯠Linux ã³ã³ããã«ã¤ãã¦ãªãã«ã解ã£ã¦ããªãã£ã(ã¾ã¨ã)
haconiwa ã¯
- Ruby DSL ã§ã³ã³ããå®ç¾©ãæ¸ããã¨ãåºæ¥ã Linux ã³ã³ããã©ã³ã¿ã¤ã
- æ軽ã«è²ã ãªç¶æ ã®ã³ã³ãããä½æãããã¨ãåºæ¥ãã®ã§ Linux ã³ã³ãããå¦ã¶ã®ã«æã£ã¦ã¤ã
Linux ã³ã³ãã
- èªåã¯æ¬å½ã«ãªãã«ã解ã£ã¦ããªãã£ã
- Linux ã³ã³ããã¨ã¯ namespace ã«ãã OS ãªã½ã¼ã¹ã®éé¢ãcgroup ã«ãããã·ã³ãªã½ã¼ã¹ã®å¶éçã® Linux ã«ã¼ãã«ã®æ©è½ãå¯ãéãã¦å®è£ ããã¦ãã
- Docker ã¯è¯ããªã« Linux ã³ã³ãããã©ãããã¦ä½¿ããããããã ã(ãããåããã¨ãªãã ãã©)ã§ãã³ã³ãããåããã®ã« Docker ã¯å¿ ãããå¿ è¦ç¡ããã ãªã£ã¦ãã¨ã解ã£ã
次åã¯
cgroup ã capability ã«è§¦ããããã°è¯ãããªã¨èãã¦ããã
åè
Linux ã³ã³ããç¥è³æ
Linux ã³ã³ãããèªãä¸ã§æ¬ ãããªãç¥è³æéã
- https://speakerdeck.com/tenforward/osc2017-osaka
- https://speakerdeck.com/tenforward/kof-2016
- http://gihyo.jp/admin/serial/01/linux_containers
- https://speakerdeck.com/hayajo/tukututexue-bulinuxkontenafalseli-ce
haconiwa ç¥è³æ
haconiwa ã触ãä¸ã§æ¬ ããäºã®åºæ¥ãªãç¥è³æéã
- https://speakerdeck.com/udzura/learn-you-a-linux-namespace-for-great-good
- https://speakerdeck.com/udzura/haconiwa-fukuoka-ruby-award-presentation-2017
- https://speakerdeck.com/udzura/haconiwa-and-future-os
- https://speakerdeck.com/udzura/the-haconiwa-internals
- https://speakerdeck.com/udzura/haconiwa-on-the-pavement
- https://speakerdeck.com/udzura/mruby-on-container
- https://speakerdeck.com/udzura/haconiwa-intro