Skip to content

Instantly share code, notes, and snippets.

@mapk0y
Last active October 22, 2015 06:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mapk0y/5d6dce5d821cffebc6f5 to your computer and use it in GitHub Desktop.
Save mapk0y/5d6dce5d821cffebc6f5 to your computer and use it in GitHub Desktop.
runC で CRIU をやってみる

準備

OS の準備

jessie の標準の kernel だと runc が動かない(cgroup の CPU 制御の問題)。また、CRIU を動かすにも足りないので自前で build(4.0.6)。

CRIU は github からとってきて build。buld に必要な物はすべて debian packege で補った。何が必要かは CRIU についてくる Dockerfile を見ればよい。

Ubuntu あたりを使えばもっと楽にできるかもしれない。

イメージ/コンテナの準備

memcached のイメージで動かす(NW設定が面倒だったので unix domain socket 経由でアクセス)

jessie:~# mkdir -p mcrunc/rootfs
jessie:~# cd mcrunc/
jessie:~/mcrunc# docker run memcached echo && docker export $(docker ps -aql) | tar xf - -C rootfs/
Unable to find image 'memcached:latest' locally
latest: Pulling from memcached
30e38a0d02bb: Pull complete 
d0a946e5b34b: Pull complete 
0e22c7635271: Pull complete 
cbc89630facd: Pull complete 
31f79787e46c: Pull complete 
353189480dc4: Pull complete 
6708d05ca977: Pull complete 
3a51cc675c6c: Already exists 
64e5325c0d9d: Already exists 
bf84c1d84a8f: Already exists 
memcached:latest: The image you are pulling has been verified. Important: image verification is a tech preview feature and should not be relied on to provide security.
Digest: sha256:7e492627975a37c89ffdce040233491bb8c92cc20e33bbe29064d3ef61ce9290
Status: Downloaded newer image for memcached:latest

jessie:~/mcrunc# ls -l rootfs/
total 76
drwxr-xr-x  2 root root 4096 Jun 15 18:37 bin
drwxr-xr-x  2 root root 4096 May  4 13:20 boot
drwxr-xr-x  4 root root 4096 Jun 23 15:54 dev
drwxr-xr-x 42 root root 4096 Jun 23 15:54 etc
drwxr-xr-x  2 root root 4096 May  4 13:20 home
drwxr-xr-x  9 root root 4096 Jun 16 00:30 lib
drwxr-xr-x  2 root root 4096 Jun 15 18:33 lib64
drwxr-xr-x  2 root root 4096 Jun 15 18:32 media
drwxr-xr-x  2 root root 4096 Jun 15 18:32 mnt
drwxr-xr-x  2 root root 4096 Jun 15 18:32 opt
drwxr-xr-x  2 root root 4096 Jun 15 18:37 proc
drwx------  2 root root 4096 Jun 15 18:32 root
drwxr-xr-x  3 root root 4096 Jun 15 18:32 run
drwxr-xr-x  2 root root 4096 Jun 15 18:37 sbin
drwxr-xr-x  2 root root 4096 Jun 15 18:32 srv
drwxr-xr-x  2 root root 4096 Apr  6 18:44 sys
drwxrwxrwt  2 root root 4096 Jun 16 00:30 tmp
drwxr-xr-x 10 root root 4096 Jun 16 00:30 usr
drwxr-xr-x 11 root root 4096 Jun 16 00:30 var

jessie:~/mcrunc# /vagrant/runc spec >container.json
jessie:~/mcrunc# ls -l
total 8
-rw-r--r--  1 root root 1457 Jun 23 15:55 container.json
drwxr-xr-x 21 root root 4096 Jun 23 15:54 rootfs

jessie:~/mcrunc# vi container.json 
jessie:~/mcrunc# diff -U1 <(/vagrant/runc spec) container.json                                                                                                                                                                               
--- /dev/fd/63  2015-06-23 15:58:52.250817014 +0000
+++ container.json      2015-06-23 15:58:49.829606938 +0000
@@ -6,6 +6,8 @@
                {
-                       "tty": true,
-                       "user": "daemon",
+                       "tty": false,
+                       "user": "memcache",
                        "args": [
-                               "sh"
+                               "/usr/local/bin/memcached",
+                                "-s",
+                                "/tmp/mem.sock"
                        ],
@@ -20,3 +22,3 @@
                "path": "rootfs",
-               "readonly": true
+               "readonly": false 
        },
@@ -93,2 +95,2 @@
        ]
-}
\ No newline at end of file
+}
jessie:~/mcrunc# /vagrant/runc                                                                                                                                                                                                               

別の端末から unix domain socket 経由でテスト

jessie:~/mcrunc# socat - unix:rootfs/tmp/mem.sock,crnl
stats
STAT pid 1
STAT uptime 61
STAT time 1435075206
STAT version 1.4.24
(snip)
set foo 0 0 3
bar
STORED
get foo
VALUE foo 0 3
bar
END
quit
jessie:~/mcrunc# 

CRIU

別の端末側から checkpoint コマンドの実行(memcached 側止まる) checkpoint ディレクトリに dump されたファイルが保存される。 おそらくベースディレクトリ一式(rootfs と container.json と checkpoint) で別マシンでに移動して動かすことができるはず

jessie:~/mcrunc# /vagrant/runc checkpoint
jessie:~/mcrunc# ls -l
total 12
drw-r-xr-x  2 root root 4096 Jun 23 16:03 checkpoint
-rw-r--r--  1 root root 1572 Jun 23 15:58 container.json
drwxr-xr-x 21 root root 4096 Jun 23 15:59 rootfs
jessie:~/mcrunc# ls -l checkpoint
total 1624
-rw-r--r-- 1 root root    1284 Jun 23 16:03 cgroup.img
-rw-r--r-- 1 root root     750 Jun 23 16:03 core-1.img
-rw-r--r-- 1 root root     445 Jun 23 16:03 core-5.img
-rw-r--r-- 1 root root     432 Jun 23 16:03 core-6.img
-rw-r--r-- 1 root root     432 Jun 23 16:03 core-7.img
-rw-r--r-- 1 root root     432 Jun 23 16:03 core-8.img
-rw-r--r-- 1 root root     412 Jun 23 16:03 core-9.img
-rw-r--r-- 1 root root      62 Jun 23 16:03 creds-1.img
-rw-r-xr-x 1 root root      46 Jun 23 16:03 descriptors.json
-rw-r--r-- 1 root root     168 Jun 23 16:03 eventpoll.img
-rw-r--r-- 1 root root     332 Jun 23 16:03 fdinfo-2.img
-rw-r--r-- 1 root root      18 Jun 23 16:03 fs-1.img
-rw-r--r-- 1 root root      32 Jun 23 16:03 ids-1.img
-rw-r--r-- 1 root root       4 Jun 23 16:03 ifaddr-8.img
-rw-r--r-- 1 root root      38 Jun 23 16:03 inventory.img
-rw-r--r-- 1 root root      77 Jun 23 16:03 ipcns-var-9.img
-rw-r--r-- 1 root root     392 Jun 23 16:03 iptables-8.img
-rw-r--r-- 1 root root    1524 Jun 23 16:03 mm-1.img
-rw-r--r-- 1 root root     427 Jun 23 16:03 mountpoints-11.img
-rw-r--r-- 1 root root      82 Jun 23 16:03 netdev-8.img
-rw-r--r-- 1 root root     124 Jun 23 16:03 netns-8.img
-rw-r--r-- 1 root root     297 Jun 23 16:03 pagemap-1.img
-rw-r--r-- 1 root root 1531904 Jun 23 16:03 pages-1.img
-rw-r--r-- 1 root root      78 Jun 23 16:03 pipes-data.img
-rw-r--r-- 1 root root     272 Jun 23 16:03 pipes.img
-rw-r--r-- 1 root root      32 Jun 23 16:03 pstree.img
-rw-r--r-- 1 root root     368 Jun 23 16:03 reg-files.img
-rw-r--r-- 1 root root       4 Jun 23 16:03 route-8.img
-rw-r--r-- 1 root root     820 Jun 23 16:03 sigacts-1.img
-rw-r--r-- 1 root root     384 Jun 23 16:03 tmpfs-dev-46.tar.gz.img
-rw-r--r-- 1 root root      98 Jun 23 16:03 tmpfs-dev-48.tar.gz.img
-rw-r--r-- 1 root root     846 Jun 23 16:03 unixsk.img
-rw-r--r-- 1 root root      27 Jun 23 16:03 utsns-10.img

checkpoint から復帰させてみる

jessie:~/mcrunc# /vagrant/runc restore

別の端末からアクセス

jessie:~/mcrunc# socat - unix:rootfs/tmp/mem.sock,crnl
stats
STAT pid 1
STAT uptime 736
STAT time 1435075881
STAT version 1.4.24
(snip)
get foo
VALUE foo 0 3
bar
END

先ほど入れていたデータがとれることがわかる。

使いドコロ

  • Java アプリなど起動が遅いもの(起動した後で checkpoint 作って、それをオリジナルとして利用。filesystem は差分ファイルシステムを使うのがよい)
  • CI や検証環境

困ったトコロ

  • container.json が決まるまで辛い(NW設定どうしようかな)
  • attach 状態(bash などでインタラクティブな状態)の時に /dev/console を dump するタイミングでエラーが出る。

tips

  • runc のエラーがあまりに味気ないが、criu コマンドのログは /var/run/ocf/ 以下に保持される(今のところはこちら)のでそちらで debug 頑張る。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment