Skip to content

Instantly share code, notes, and snippets.

@kjunichi
Last active February 17, 2017 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjunichi/e2f6b07da4feb8fdea5b9b53664fe67b to your computer and use it in GitHub Desktop.
Save kjunichi/e2f6b07da4feb8fdea5b9b53664fe67b to your computer and use it in GitHub Desktop.
haconiwaを試す(haconiwaのコンテナでh2oのmrubyをうごかす)

試している環境

Virutalbox上のUbuntu 16.04ltsだと信じてる

以下に出てくる設定ファイル中の文字列「your_rootfs」は文字通り、この名称のディレクトリで試した。

IP フォワーディングの設定

以下、関係ないかもだが、やってた。

echo 1 > /proc/sys/net/ipv4/ip_forward

haconiwaの設定ファイル

よく分ってないからReadme.mdを参考につくったやつ。たぶんlxcをつかってるっぽい。

Haconiwa::Base.define do |config|
  config.name = "new-haconiwa001" # to be hostname

  config.bootstrap do |b|
    b.strategy = "lxc"
    b.os_type = "ubuntu"
  end

  config.provision do |p|
    p.run_shell "apt install -y git"
  end

  config.cgroup["cpu.shares"] = 2048
  config.cgroup["memory.limit_in_bytes"] = 256 * 1024 * 1024
  config.cgroup["pids.max"] = 1024

  config.add_mount_point "/var/another/root/etc", to: "/var/your_rootfs/etc", readonly: true
  config.add_mount_point "/var/another/root/home", to: "/var/your_rootfs/home"
  config.mount_independent "procfs"
  config.mount_independent "sysfs"
  config.chroot_to "/var/your_rootfs"

  config.namespace.unshare "ipc"
  config.namespace.unshare "uts"
  config.namespace.unshare "mount"
  config.namespace.unshare "pid"

  config.capabilities.allow :all
  config.capabilities.drop "cap_sys_admin"
end

# h2oのビルド

ubuntu16.04だとlibuvが古く(h2oが期待しているバージョンに比べて)、git cloneして入れる必要がった。

libuv.aをリンクするので、haconiwa側でとくに考慮する必要はない。

mkdir build
cd build
PKG_CONFIG_PATH=/var/another/root/home/local/lib/pkgconfig cmake -DCMAKE_INSTALL_PREFIX=/var/another/root/home/local -DWITH_BUNDLED_SSL=on  .. 

h2o.conf

コンテナにユーザー管理のリソース入れるのも手間かかりそうだったので、rootで動かす方法が有ったのでこれで試した。

user: root
listen: 8080
listen:
  port: 8081
  ssl:
    certificate-file: examples/h2o/server.crt
    key-file: examples/h2o/server.key
hosts:
  "127.0.0.1.xip.io:8080":
    paths:
      /:
        file.dir: examples/doc_root
        mruby.handler-file: examples/h2o_mruby/hello.rb
    access-log: /dev/stdout
  "alternate.127.0.0.1.xip.io:8081":
    listen:
      port: 8081
      ssl:
        certificate-file: examples/h2o/alternate.crt
        key-file: examples/h2o/alternate.key
    paths:
      /:
        file.dir: examples/doc_root.alternate
    access-log: /dev/stdout

haconiwaでh2oを起動

いろいろ、文句言われるので、/etc配下を中心にファイルをなんとか置くなりする。

mruby関連はPrefixに埋め込まれたパスを参照しているので、

haconiwaのコンテナ内からこれらを埋め込まれたパス(/var/another/root/home/local)で読み込めるようにする必要があった。

mkdir -p /var/another/root/home
cd /var/another/root/home/
ln -s /home/local .
/home/local/bin/h2o -c examples/h2o_mruby/h2o.conf 

動き出すと

[INFO] raised RLIMIT_NOFILE to 65536
h2o server (pid:157) is ready to serve requests
fetch-ocsp-response (using OpenSSL 1.0.2g  1 Mar 2016)
failed to extract ocsp URI from examples/h2o/server.crt
fetch-ocsp-response (using OpenSSL 1.0.2g  1 Mar 2016)
[OCSP Stapling] disabled for certificate file:examples/h2o/server.crt
failed to extract ocsp URI from examples/h2o/server.crt
[OCSP Stapling] disabled for certificate file:examples/h2o/server.crt
fetch-ocsp-response (using OpenSSL 1.0.2g  1 Mar 2016)
failed to extract ocsp URI from examples/h2o/alternate.crt
[OCSP Stapling] disabled for certificate file:examples/h2o/alternate.crt
fetch-ocsp-response (using OpenSSL 1.0.2g  1 Mar 2016)
failed to extract ocsp URI from examples/h2o/alternate.crt
[OCSP Stapling] disabled for certificate file:examples/h2o/alternate.crt

10.0.3.1 - - [15/Feb/2017:03:45:11 +0000] "GET / HTTP/2" 200 177 "-" "nghttp2/1.7.1"
10.0.3.1 - - [15/Feb/2017:03:46:18 +0000] "GET /hge.rb HTTP/2" 200 147 "-" "nghttp2/1.7.1"

h2oにアクセス

nghttp https://10.0.3.1:8081/hge.rb
hello from h2o_mruby. User-Agent:nghttp2/1.7.1 New User-Agent:new-nghttp2/1.7.1-h2o_mruby path:/hge.rb host:10.0.3.1:8081 method:GET query: input:

アクセス解析タグ

@udzura
Copy link

udzura commented Feb 15, 2017

たぶんlxcをつかってるっぽい

使っていません^^ mrubyでコンテナ関連のmgemをそれぞれ作っています。
中身についてはちょっと前に日本語で発表しております https://speakerdeck.com/udzura/the-haconiwa-internals

@kjunichi
Copy link
Author

kjunichi commented Feb 15, 2017

b.strategy = "lxc"
っててっきり、

で、lxc-createコマンドを実行しているのだと斜め読みしてました。

あ、lxc-createコマンドはDockerで言うところのイメージを作るだけで、コンテナとして動かすのはlxcを使っている訳ではないってことですかね。

@udzura
Copy link

udzura commented Feb 17, 2017

あ、lxc-createコマンドはDockerで言うところのイメージを作るだけで、コンテナとして動かすのはlxcを使っている訳ではないってことですかね

あ、そうですそれは正しいです。Haconiwa全体でlxc利用されていると思われてるのかと誤読しておりました。
ちなみにlxc-create、 debootstrap、任意のシェルスクリプトなどでイメージ(rootfs)を作成できます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment