黒縁眼鏡は海を飛ぶ

IT中心にそこはかとなく

haconiwaというコンテナエンジンを試してみた

Docker流行ってるし、バージョンも上がってるしで再開しよう欲が高まってたけど、mruby製のhaconiwaという素敵プロダクトがあるとまつもとりーさんのブログを読んで知ったので使ってみる。

haconiwaのインストール

READMEの通りにやっていきます。
VERSIONは最新の0.6.2を指定します。

$ VERSION=0.6.2
wget https://github.com/haconiwa/haconiwa/releases/download/v${VERSION}/haconiwa-v${VERSION}.x86_64-pc-linux-gnu.tgz
$ tar xzf haconiwa-v${VERSION}.x86_64-pc-linux-gnu.tgz
$ sudo install hacorb hacoirb haconiwa /usr/local/bin

実行できるか確認。

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
    start     - run the container
    attach    - attach to existing container
    kill      - kill the running container
    ps        - list running containers (across the clusterd hosts, etcd needed)
    watch     - (experimental) watch the cluster status and set hooks via mruby file
    version   - show version
    revisions - show mgem/mruby revisions which haconiwa bin uses

Invoke `haconiwa COMMAND -h' for details.

テンプレートを作ってみる

haconiwa new <name>でテンプレートを作ってくれます。

$ haconiwa new -n test test.haco
assign  rootfs location = /var/lib/haconiwa/52d01bc1
create  test.haco

生成されたtest.hacoの中身を雑に見てみると、

# -*- mode: ruby -*-
Haconiwa.define do |config|
  config.name = "test"
  config.init_command = "/bin/bash"
  
  root = Pathname.new("/var/lib/haconiwa/52d01bc1")
  config.chroot_to root
  
  config.bootstrap do |b|
    b.strategy = "lxc"
    b.os_type  = "centos"
  end
  
  config.provision do |p|
  p.run_shell <<-SHELL
yum -y update
  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

こんな感じ。
自動生成される雛形はalpineになっているんですけど、CentOSが好きなのでcentosに書き変えておいた。

b.strategyの部分で指定可能なのは、lxc, debootstrap, tarball, git, shell, mrubyなどなどあるらしい。

gitとかめちゃ便利そう。
雛形だけGithubに置いとけばいつでもコンテナたてられるっぽい。

コンテナ作って動かしてみる

lxc指定しているので、lxcをインストールしておきます。

インストールできたら早速コンテナ作ってみる。

$ haconiwa create test.haco 
Start bootstrapping rootfs with lxc-create...
[bootstrap.lxc]:        lxc_container: utils.c: mkdir_p: 202 Permission denied - failed to create directory '/var/lib/haconiwa/52d01bc1'
[bootstrap.lxc]:        lxc_container: bdev.c: dir_create: 509 Error creating /var/lib/haconiwa/52d01bc1
[bootstrap.lxc]:        lxc_container: lxccontainer.c: do_bdev_create: 819 Failed to create backing store type (null)
[bootstrap.lxc]:        lxc_container: lxccontainer.c: lxcapi_create: 1299 Error creating backing store type (none) for 52d01bc1
[bootstrap.lxc]:        lxc_container: lxc_create.c: main: 274 Error creating container 52d01bc1
RuntimeError: Command failed...: lxc-create -n 52d01bc1 -t centos --dir /var/lib/haconiwa/52d01bc1 exited 1

だめだった。sudoつけよう。

$ sudo /usr/local/bin/haconiwa create test.haco

...

Command success: lxc-create -n 52d01bc1 -t centos --dir /var/lib/haconiwa/52d01bc1 exited 0
Success!
Start provisioning...
Running provisioning with shell script...
[provison.shell-1]:     + yum -y update
[provison.shell-1]:     Loaded plugins: fastestmirror
[provison.shell-1]:     Loading mirror speeds from cached hostfile
[provison.shell-1]:      * base: ftp.riken.jp
[provison.shell-1]:      * extras: ftp.riken.jp
[provison.shell-1]:      * updates: ftp.riken.jp
[provison.shell-1]:     No packages marked for update
Command success: /bin/sh -xe exited 0
Success!

今度は成功した模様。
provisionで指定したyum -y updateとは別にupdateが走ったんですが、haconiwaがやってくれたのか、lxcがやってくれたのか、どちらかは不明。

/var/lib/haconiwaの下を見に行くと、コンテナ起動に必要なファイル一式が作成されている。

$ ls -l /var/lib/haconiwa/52d01bc1/
total 8
lrwxrwxrwx.  1 root root    7 Feb  1 21:37 bin -> usr/bin
dr-xr-xr-x.  2 root root    6 Nov  6 00:38 boot
drwxr-xr-x.  4 root root  206 Feb  1 22:46 dev
drwxr-xr-x. 55 root root 4096 Feb  1 22:46 etc
drwxr-xr-x.  2 root root    6 Nov  6 00:38 home
lrwxrwxrwx.  1 root root    7 Feb  1 21:37 lib -> usr/lib
lrwxrwxrwx.  1 root root    9 Feb  1 21:37 lib64 -> usr/lib64
drwxr-xr-x.  2 root root    6 Nov  6 00:38 media
drwxr-xr-x.  2 root root    6 Nov  6 00:38 mnt
drwxr-xr-x.  2 root root    6 Nov  6 00:38 opt
dr-xr-xr-x.  2 root root    6 Nov  6 00:38 proc
dr-xr-x---.  2 root root   91 Feb  1 21:38 root
drwxr-xr-x. 12 root root  171 Feb  1 22:46 run
lrwxrwxrwx.  1 root root    8 Feb  1 21:37 sbin -> usr/sbin
drwxr-xr-x.  2 root root   21 Feb  1 22:46 selinux
drwxr-xr-x.  2 root root    6 Nov  6 00:38 srv
dr-xr-xr-x.  2 root root    6 Nov  6 00:38 sys
drwxrwxrwt.  7 root root   93 Feb  1 21:38 tmp
drwxr-xr-x. 13 root root  155 Feb  1 21:37 usr
drwxr-xr-x. 18 root root  238 Feb  1 21:37 var

では早速起動してみます。

$ sudo /usr/local/bin/haconiwa start test.haco
Container fork success and going to wait: pid=2349
[root@test /]# uname -n
test

起動でけたー。

tarballとgit

個人的に、わざわざlxc入れるの嫌なのでtarballgit、特にgitが素敵だと思ったので、サンプルを試してみた。

$ sudo /usr/local/bin/haconiwa start timer.haco
Container successfully up. PID={container: 2484, supervisor: 2483}

$ ps aux | grep haco | grep -v grep
$ sudo /usr/local/bin/haconiwa ps
NAME                    HOST                    ROOTFS                  COMMAND                 CREATED_AT                      STATUS          PID     SPID

んんん??成功とは出てるけど、動いてないっぽい?? 調査が必要だ。

test.hacoで出来たイメージをtarで固めて別ホストに持っていって試したところ、同一のコンテナが起動できた。めっちゃ便利。

ただ、hacoファイル内でb.strategy = "tarball"と書いたときにうまく動かなかったので、これまた調査する必要がありそうなのでした。