tl;dr
Stable 扱ã„ã§ã¯ç„¡ãã€çµ¶è³›é–‹ç™ºä¸ã® LXD ã® Rest API を触ã£ã¦ã¿ã¦ã€ç›®çš„別ã«æ•´ç†ã—ã¦ã¿ãŸã€‚
ã¯ã˜ã‚ã«
注æ„
LXD ã® REST API ã¯çµ¶è³›é–‹ç™ºä¸ã¨ã®ã“ã¨ã§ã™ã®ã§ã€ã“ã“ã«æ›¸ã„ã¦ã‚る内容ã¯æœ¬è¨˜äº‹ã‚’書ã„ãŸæ™‚点ã®å†…容ã¨ãªã‚Šã¾ã™ã®ã§ã”注æ„ãã ã•ã„ã¾ã›ã€‚
å‚考
メモ
å‰æ
- ホスト OS
$ cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=14.04 DISTRIB_CODENAME=trusty DISTRIB_DESCRIPTION="Ubuntu 14.04.3 LTS"
- LXD ãƒãƒ¼ã‚¸ãƒ§ãƒ³
$ lxd --version
0.25
- コンテナイメージ
$ lxc image list +--------------------+--------------+--------+-----------------------+--------+----------+------------------------------+ | ALIAS | FINGERPRINT | PUBLIC | DESCRIPTION | ARCH | SIZE | UPLOAD DATE | +--------------------+--------------+--------+-----------------------+--------+----------+------------------------------+ | oreno-ubuntu-image | 1b9ba7cb50ac | no | | x86_64 | 126.72MB | Dec 27, 2015 at 8:10am (JST) | | | 4712c42e3fca | no | Ubuntu trusty (amd64) | x86_64 | 64.43MB | Dec 27, 2015 at 8:00am (JST) | +--------------------+--------------+--------+-----------------------+--------+----------+------------------------------+
ã‚‚ã¡ã‚ã‚“ã€API ã§ã‚‚確èªã§ãる。
$ curl -s -k --cert $HOME/.config/lxc/client.crt --key $HOME/.config/lxc/client.key https://127.0.0.1:8443/1.0/images | jq . { "operation": "", "metadata": [ "/1.0/images/1b9ba7cb50acxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "/1.0/images/4712c42e3fcaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ], "status_code": 200, "status": "Success", "type": "sync" }
REST API ã§æ“作出æ¥ã‚‹ã‚ˆã†ã«ã™ã‚‹
$ lxc config set core.trust_password ${password} $ lxc config set core.https_address [::] $ lxc remote add oreno-local 127.0.0.1:8443
以下ã®ã‚ˆã†ã«å‡ºåŠ›ã•ã‚Œã‚‹ã€‚
Certificate fingerprint: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ok (y/n)? y # y を入力ã™ã‚‹
trust_password
ã®å…¥åŠ›ã‚’求ã‚られるã®ã§å…¥åŠ›ã™ã‚‹ã€‚
lxc remote list
ã§ä¸€è¦§ã‚’確èªã™ã‚‹ã€‚
$ lxc remote list +-------------+-----------------------------------------+--------+ | NAME | URL | PUBLIC | +-------------+-----------------------------------------+--------+ | images | https://images.linuxcontainers.org:8443 | YES | | local | unix:// | NO | | oreno-local | https://127.0.0.1:8443 | NO | +-------------+-----------------------------------------+--------+
API ã®ã‚¨ãƒ³ãƒ‰ãƒã‚¤ãƒ³ãƒˆã«ã‚¢ã‚¯ã‚»ã‚¹ã™ã‚‹
/1.0/ ã‚’å‚考ã«ã—ãŸã€‚
以下ã®ã‚ˆã†ã«å®Ÿè¡Œã™ã‚‹ã€‚
$ curl -s -k --cert $HOME/.config/lxc/client.crt --key $HOME/.config/lxc/client.key https://127.0.0.1:8443/1.0 | jq . { "operation": "", "metadata": { "environment": { "storage_version": "", "storage": "dir", "server_version": "0.25", "server_pid": 8168, "addresses": [ "192.168.xxx.xxx:8443", "[2001:a452:48b:ed00:309f:497e:b743:655c]:8443", "[2001:a452:48b:ed00:8e70:5aff:fe7c:11a4]:8443", "10.0.x.x:8443", "172.17.x.x:8443" ], "architectures": [ 2, 1 ], (snip) "config": { "core.trust_password": true, "core.https_address": "[::]:8443" }, "auth": "trusted", "api_compat": 1 }, "status_code": 200, "status": "Success", "type": "sync" }
コンテナを作æˆã™ã‚‹
/1.0/containers ã‚’å‚考ã«ã—ãŸã€‚
以下ã®ã‚ˆã†ã«å®Ÿè¡Œã™ã‚‹ã€‚
$ curl --s X POST -k --cert $HOME/.config/lxc/client.crt --key $HOME/.config/lxc/client.key https://127.0.0.1:8443/1.0/containers -d ' { "name": "oreno-container", "architecture": 2, "profiles": ["default"], "ephemeral": true, "config": {"limits.cpu": "1"}, "source": {"type": "image", "alias": "oreno-ubuntu-image"} }'
以下ã®ã‚ˆã†ã«å‡ºåŠ›ã•ã‚Œã‚‹ã€‚
{ "operation": "/1.0/operations/66a6d8e8-bc20-43d2-8266-4c801ee856df", "metadata": { "err": "", "may_cancel": false, "id": "66a6d8e8-bc20-43d2-8266-4c801ee856df", "class": "task", "created_at": "2015-12-27T21:04:04.449438237+09:00", "updated_at": "2015-12-27T21:04:04.449438237+09:00", "status": "Running", "status_code": 103, "resources": { "containers": [ "/1.0/containers/oreno-container" ] }, "metadata": null }, "status_code": 100, "status": "OK", "type": "async" }
コンテナ一覧を確èªã™ã‚‹
/1.0/containers ã‚’å‚考ã«ã—ãŸã€‚
以下ã®ã‚ˆã†ã«å®Ÿè¡Œã™ã‚‹ã€‚
$ curl -s -k --cert $HOME/.config/lxc/client.crt --key $HOME/.config/lxc/client.key https://127.0.0.1:8443/1.0/containers | jq . { "operation": "", "metadata": [ "/1.0/containers/oreno-container", "/1.0/containers/oreno-ubuntu", "/1.0/containers/oreno-ubuntu01" ], "status_code": 200, "status": "Success", "type": "sync" }
lxc
コマンドã§ç¢ºèªã™ã‚‹ã€‚
$ lxc list +-----------------+---------+------+------+-----------+-----------+ | NAME | STATE | IPV4 | IPV6 | EPHEMERAL | SNAPSHOTS | +-----------------+---------+------+------+-----------+-----------+ | oreno-container | STOPPED | | | YES | 0 | +-----------------+---------+------+------+-----------+-----------+ | oreno-ubuntu | STOPPED | | | NO | 0 | +-----------------+---------+------+------+-----------+-----------+ | oreno-ubuntu01 | STOPPED | | | NO | 0 | +-----------------+---------+------+------+-----------+-----------+
EPHEMERAL
㌠YES
ã¨ãªã£ã¦ã„るコンテナã¯ã‚³ãƒ³ãƒ†ãƒŠã‚’åœæ¢ã™ã‚‹ã¨è‡ªå‹•çš„ã«å‰Šé™¤ã•ã‚Œã‚‹ã€‚
コンテナを起動ã™ã‚‹
/1.0/containers/
以下ã®ã‚ˆã†ã«å®Ÿè¡Œã™ã‚‹ã€‚
$ CONTAINER_NAME="oreno-container" $ curl -X PUT -k --cert $HOME/.config/lxc/client.crt --key $HOME/.config/lxc/client.key https://127.0.0.1:8443/1.0/containers/${CONTAINER_NAME}/state -d ' { "action": "start", "timeout": 30, "force": true }'
以下ã®ã‚ˆã†ã«å‡ºåŠ›ã•ã‚Œã‚‹ã€‚
{ "operation": "/1.0/operations/31517205-e64f-4b23-92fa-b2634f408885", "metadata": { "err": "", "may_cancel": false, "id": "31517205-e64f-4b23-92fa-b2634f408885", "class": "task", "created_at": "2015-12-27T21:07:34.618819353+09:00", "updated_at": "2015-12-27T21:07:34.618819353+09:00", "status": "Running", "status_code": 103, "resources": { "containers": [ "/1.0/containers/oreno-container" ] }, "metadata": null }, "status_code": 100, "status": "OK", "type": "async" }
コンテナã®ã‚¹ãƒ†ãƒ¼ã‚¿ã‚¹ã‚’確èªã™ã‚‹
/1.0/containers/
以下ã®ã‚ˆã†ã«å®Ÿè¡Œã™ã‚‹ã€‚
$ CONTAINER_NAME="oreno-container" $ curl -s -k --cert $HOME/.config/lxc/client.crt --key $HOME/.config/lxc/client.key https://127.0.0.1:8443/1.0/containers/${CONTAINER_NAME} | jq . { "operation": "", "metadata": { "status": { "ips": [ { "host_veth": "veth7GH2NJ", "address": "10.0.x.xxx", "protocol": "IPV4", "interface": "eth0" }, { "host_veth": "", "address": "127.0.0.1", "protocol": "IPV4", "interface": "lo" }, { "host_veth": "", "address": "::1", "protocol": "IPV6", "interface": "lo" } ], "processcount": 9, "init": 11358, "status_code": 103, "status": "Running" }, (snip) }, "expanded_devices": { "eth0": { "type": "nic", "parent": "lxcbr0", "nictype": "bridged" } }, "name": "oreno-container", "profiles": [ "default" ] }, "status_code": 200, "status": "Success", "type": "sync" }
"status": "Running"
ã¨ãªã£ã¦ã„ã‚‹ã®ã‚³ãƒ³ãƒ†ãƒŠã¯èµ·å‹•ã—ã¦ã„ã‚‹ã¯ãš...
念ã®ç‚ºã€lxc
コマンドã§ã‚‚確èªã—ã¦ã¿ã‚‹ã€‚
$ lxc list +-----------------+---------+-------------------+------+-----------+-----------+ | NAME | STATE | IPV4 | IPV6 | EPHEMERAL | SNAPSHOTS | +-----------------+---------+-------------------+------+-----------+-----------+ | oreno-container | RUNNING | 10.0.x.xxx (eth0) | | YES | 0 | +-----------------+---------+-------------------+------+-----------+-----------+ | oreno-ubuntu | STOPPED | | | NO | 0 | +-----------------+---------+-------------------+------+-----------+-----------+ | oreno-ubuntu01 | STOPPED | | | NO | 0 | +-----------------+---------+-------------------+------+-----------+-----------+
èµ·å‹•ã—ã¦ã„る。
念ã®ç‚ºã€exec
サブコマンドも実行ã—ã¦ã¿ã‚‹ã€‚
$ lxc exec oreno-container -- hostname
oreno-container
よã—。
コンテナã§ã‚³ãƒžãƒ³ãƒ‰ã‚’実行ã™ã‚‹
/1.0/containers/
以下ã®ã‚ˆã†ã«å®Ÿè¡Œã™ã‚‹ã€‚
$ CONTAINER_NAME="oreno-container" $ curl -s -X POST -k --cert $HOME/.config/lxc/client.crt --key $HOME/.config/lxc/client.key https://127.0.0.1:8443/1.0/containers/${CONTAINER_NAME}/exec -d ' { "command": ["/bin/sh", "-c", "while true; do echo \"foo\"; sleep 5; done"], "wait-for-websocket": true, "interactive": true }'
以下ã®ã‚ˆã†ã«å‡ºåŠ›ã•ã‚Œã‚‹ã€‚
{ "operation": "/1.0/operations/83940f18-7646-458e-b1dd-cb65ea9ddb07", "metadata": { "err": "", "may_cancel": false, "id": "83940f18-7646-458e-b1dd-cb65ea9ddb07", "class": "websocket", "created_at": "2015-12-27T21:21:47.852563283+09:00", "updated_at": "2015-12-27T21:21:47.852563283+09:00", "status": "Running", "status_code": 103, "resources": { "containers": [ "/1.0/containers/oreno-container" ] }, "metadata": { "fds": { "control": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "0": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" } } }, "status_code": 100, "status": "OK", "type": "async" }
実行ã—ãŸã‚³ãƒžãƒ³ãƒ‰ã®å‡ºåŠ›ã‚’確èªã™ã‚‹
/1.0/operations/
以下ã®ã‚ˆã†ã«å®Ÿè¡Œã™ã‚‹ã€‚
$ ID="83940f18-7646-458e-b1dd-cb65ea9ddb07" $ SECRET="bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" $ curl \ -H "Sec-WebSocket-Key: `ruby -r "base64" -e "puts Base64.encode64('oreno_container')"`" \ -H "Upgrade: websocket" \ -H "Connection: upgrade" \ -H "Sec-WebSocket-Version: 13" \ -k --cert $HOME/.config/lxc/client.crt --key $HOME/.config/lxc/client.key \ "https://127.0.0.1:8443/1.0/operations/${ID}/websocket?secret=${SECRET}" �foo �foo �foo �foo �foo
Ctrl+c
ã§å‡ºåŠ›ã‚’æ¢ã‚ã‚‹ã¨ã‚³ãƒ³ãƒ†ãƒŠä¸Šã®ã‚³ãƒžãƒ³ãƒ‰ã‚‚åœæ¢ã™ã‚‹ã€‚
å°šã€Websocket ã¸ã®ã‚¢ã‚¯ã‚»ã‚¹ã«ã¤ã„ã¦ã¯ä»¥ä¸‹ã®è¨˜äº‹ã‚’å‚考ã«ã•ã›ã¦é ‚ã„ãŸã€‚
hwmemo01.tumblr.com有難ã†ã”ã–ã„ã¾ã™ã€‚
コンテナをåœæ¢ã™ã‚‹
/1.0/containers/
以下ã®ã‚ˆã†ã«å®Ÿè¡Œã™ã‚‹ã€‚
$ curl -X PUT -k --cert $HOME/.config/lxc/client.crt --key $HOME/.config/lxc/client.key https://127.0.0.1:8443/1.0/containers/${CONTAINER_NAME}/state -d ' { "action": "stop", "timeout": 30, "force": true }'
以下ã®ã‚ˆã†ã«å‡ºåŠ›ã•ã‚Œã‚‹ã€‚
{ "operation": "/1.0/operations/9b506e26-1962-4b7e-ac9d-b59968e41192", "metadata": { "err": "", "may_cancel": false, "id": "9b506e26-1962-4b7e-ac9d-b59968e41192", "class": "task", "created_at": "2015-12-27T21:29:33.198590419+09:00", "updated_at": "2015-12-27T21:29:33.198590419+09:00", "status": "Running", "status_code": 103, "resources": { "containers": [ "/1.0/containers/oreno-container" ] }, "metadata": null }, "status_code": 100, "status": "OK", "type": "async" }
エフェメラルãªã‚³ãƒ³ãƒ†ãƒŠãªã®ã§ã‚³ãƒ³ãƒ†ãƒŠã‚’åœæ¢ã—ãŸæ™‚点ã§ã‚³ãƒ³ãƒ†ãƒŠã¯å‰Šé™¤ã•ã‚Œã¦ã„る。
$ curl -s -k --cert $HOME/.config/lxc/client.crt --key $HOME/.config/lxc/client.key https://127.0.0.1:8443/1.0/containers | jq . { "operation": "", "metadata": [ "/1.0/containers/oreno-ubuntu", "/1.0/containers/oreno-ubuntu01" ], "status_code": 200, "status": "Success", "type": "sync" }
コンテナを削除ã™ã‚‹
/1.0/containers/
コンテナを削除ã™ã‚‹éš›ã«ã¯ä»¥ä¸‹ã®ã‚ˆã†ã«å®Ÿè¡Œã™ã‚‹ã€‚
$ CONTAINER_NAME="oreno-container" $ curl -X DELETE -s -k --cert $HOME/.config/lxc/client.crt --key $HOME/.config/lxc/client.key https://127.0.0.1:8443/1.0/containers/${CONTAINER_NAME}
以下ã®ã‚ˆã†ã«å‡ºåŠ›ã•ã‚Œã‚‹ã€‚
{ "operation": "/1.0/operations/ac1e30e5-ae88-44ec-b053-b6d5a489f668", "metadata": { "err": "", "may_cancel": false, "id": "ac1e30e5-ae88-44ec-b053-b6d5a489f668", "class": "task", "created_at": "2015-12-27T21:34:07.358316581+09:00", "updated_at": "2015-12-27T21:34:07.358316581+09:00", "status": "Running", "status_code": 103, "resources": { "containers": [ "/1.0/containers/oreno-container" ] }, "metadata": null }, "status_code": 100, "status": "OK", "type": "async" }
以上
メモã§ã—ãŸã€‚