Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rubyで君もコンテナになる - Haconiwaのごく一部の機能の紹介 / learn-you-a-linux-namespace-for-great-good

Rubyで君もコンテナになる - Haconiwaのごく一部の機能の紹介 / learn-you-a-linux-namespace-for-great-good

ピクシブ福岡オフィス - オープン記念LT祭

KONDO Uchio

March 03, 2017
Tweet

More Decks by KONDO Uchio

Other Decks in Technology

Transcript

  1. 6CVOUV9FOJBMʹΠϯετʔϧ $ curl -s https://packagecloud.io/install/repositories/ udzura/haconiwa/script.deb.sh | sudo bash #

    https://packagecloud.io/udzura/haconiwa/install $ sudo apt install haconiwa $ haconiwa version haconiwa: v0.6.2
  2. ʢࠓճ͸ʣEFCPPUTUSBQͰSPPUGTΛ༻ҙ $ sudo apt install debootstrap $ debootstrap --arch amd64

    jessie \ /var/lib/haconiwa/jessie-box \ http://ftp.jp.debian.org/debian I: Retrieving Release I: Retrieving Release.gpg I: Checking Release signature ...
  3. 654OBNFTQBDFΛ෼͚Δ diff --git a/sample.haco b/sample.haco index f163d45..fa15f50 100644 --- a/sample.haco

    +++ b/sample.haco @@ -2,4 +2,6 @@ Haconiwa.define do |config| config.name = "pixiv-example" config.init_command = ["/bin/bash"] config.chroot_to "/var/lib/haconiwa/jessie-box" + + config.namespace.unshare "uts" end
  4. ϗετ໊͕෼͔Εͨʂ ঃʑʹίϯςφͬΆ͘ $ hostname udzura.example.com # ίϯςφʹೖΔ $ sudo haconiwa

    run sample.haco Container fork success and going to wait: pid=14895 root@pixiv-example:/# hostname pixiv-example
  5. .PVOUOBNFTQBDFΛ෼͚Δɻ diff --git a/sample.haco b/sample.haco index fa15f50..83996c0 100644 --- a/sample.haco

    +++ b/sample.haco @@ -4,4 +4,7 @@ Haconiwa.define do |config| config.chroot_to "/var/lib/haconiwa/jessie-box" config.namespace.unshare "uts" + config.namespace.unshare "mount" + + config.mount_independent "procfs" end ҰॹʹQSPDͳͲΛίϯςφ಺෦ͰϚ΢ϯτ͢Δ
  6. Ϛ΢ϯτϙΠϯτ͕ಠཱͨ͠ QSPD΋ແࣄϚ΢ϯτͰ͖ɺ֎͔Βݟ͑ͳ͍ # on container root@pixiv-example:/# mount proc on /proc

    type proc (rw,relatime) # on host $ mount | grep proc proc on /proc type proc (rw,nosuid,nodev,noexec,relatime) systemd-1 on /proc/sys/fs/binfmt_misc type autofs (rw,relatime,fd=23,pgrp=1,timeout=300,minproto=5,maxproto=5,direct)
  7. 1*%OBNFTQBDFΛ෼͚Δ *1$OBNFTQBDF͸ɺͪΐͬͱྫ͕೉͍͠ͷͰলུ diff --git a/sample.haco b/sample.haco index 83996c0..b27d7be 100644 ---

    a/sample.haco +++ b/sample.haco @@ -5,6 +5,7 @@ Haconiwa.define do |config| config.namespace.unshare "uts" config.namespace.unshare "mount" + config.namespace.unshare "pid" config.mount_independent "procfs" end
  8. 1*%͕͔Βʹͳͬͨ root@pixiv-example:/# sleep 100 & [1] 3 root@pixiv-example:/# ps auxf

    USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.3 20228 3220 ? S 10:10 0:00 /bin/bash root 3 0.0 0.0 4236 668 ? S 10:10 0:00 sleep 100 root 4 0.0 0.2 17496 2128 ? R+ 10:10 0:00 ps auxf ঃʑʹಠཱ͖ͯͨ͠
  9. ࠷ޙʹOFUOTΛ࡞ΓɺWFUIΛҾ͘ ip netns add pixiv001 ip link add haco__991 type

    veth peer name eth2 ip addr add 172.29.89.1/24 broadcast 172.29.89.255 dev haco__991 ip link set haco__991 up ip link set eth2 netns pixiv001 ip netns exec pixiv001 ip addr add 172.29.89.2/24 dev eth2 # ίϯςφͷΞυϨε ip netns exec pixiv001 ip link set lo up ip netns exec pixiv001 ip link set eth2 up # ૄ௨֬ೝ ping 172.29.89.2 ip netns exec pixiv001 ping 172.29.89.1
  10. ͦͷ/FUXPSLOBNFTQBDFʹೖΔ diff --git a/sample.haco b/sample.haco index b27d7be..7835814 100644 --- a/sample.haco

    +++ b/sample.haco @@ -6,6 +6,8 @@ Haconiwa.define do |config| config.namespace.unshare "uts" config.namespace.unshare "mount" config.namespace.unshare "pid" + config.namespace.enter "net", via: "/var/run/netns/pixiv001" config.mount_independent "procfs" + config.mount_independent "sysfs" end TZT഑Լʹ΋ωοτϫʔΫσόΠεͷ৘ใ͕͋ΔͷͰɺϦϚ΢ϯτਪ঑
  11. ೖΕͨʂ $ sudo haconiwa run sample.haco Container fork success and

    going to wait: pid=12554 root@pixiv-example:/# ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 4: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default link/ether 92:80:3b:32:32:6d brd ff:ff:ff:ff:ff:ff inet 172.29.89.2/24 scope global eth2 valid_lft forever preferred_lft forever inet6 fe80::9080:3bff:fe32:326d/64 scope link valid_lft forever preferred_lft forever
  12. શମ Haconiwa.define do |config| config.name = "pixiv-example" config.init_command = ["/bin/bash"]

    config.chroot_to "/var/lib/haconiwa/jessie-box" config.namespace.unshare "uts" config.namespace.unshare "mount" config.namespace.unshare "pid" config.namespace.enter "net", via: "/var/run/netns/pixiv001" config.mount_independent "procfs" config.mount_independent "sysfs" end DHSPVQDBQBCJMJUZSMJNJUͦͷଞ΋%4-ͰઃఆͰ͖·͢