仮想化ツール >
目次 †
インストール †
lxcs クックブックを利用する †
以下で説明しているセットアップ方法は、筆者が作成した lxcs Chefクックブックの lxcs::lxd レシピで同様に行うことができます。
クイックセットアップ †
最も簡単なセットアップは以下のように chef-solo を利用して行うことができます。設定のカスタマイズは lxd ロールの編集によって行います。
$ sudo apt-get install ca-certificates curl git
$ curl -L https://omnitruck.chef.io/install.sh | sudo bash
$ git clone git://git.osdn.net/gitroot/metasearch/grid-chef-repo.git
$ cd grid-chef-repo/
$ chef-solo -c solo.rb -j nodes/local-lxd.json
サンプルロール †
roles/lxd.rb
name 'lxd'
description 'LXD role.'
run_list(
'recipe[lxcs::lxd]' ,
)
ssh_pub_key = 'ssh-rsa AAAAB3Nz ...'
override_attributes(
'lxcs' => {
'lxd' => {
'with_zfs' => true ,
'users_allow' => [
'alice' ,
#'bob',
] ,
'init' => {
'enabled' => true ,
'options' => [
#"--network-address #{node['ipaddress']}",
#'--network-port 8443',
#'--storage-backend dir',
'--storage-backend zfs' ,
'--storage-create-loop 20' , # 20GB file
'--storage-pool lxd' ,
# NOT recommended, use 'trust_password_vault_item' attribute.
#'--trust-password secret',
] .join ( ' ' ) ,
} ,
# Ubuntu >= 16.10
'network' => {
'lxdbr0' => {
'config' => {
'dns.mode' => 'dynamic' ,
'ipv4.address' => '10.1.3.1/24' ,
'ipv4.dhcp.ranges' => '10.1.3.100-10.1.3.254' ,
'ipv4.nat' => 'true' ,
'ipv6.address' => 'none' ,
} ,
'managed' => 'true' ,
'type' => 'bridge' ,
} ,
} ,
# Deprecated: Ubuntu <= 16.04
# use `lxc network set` command on 16.10 (yakkety) or later
'bridge' => {
'enabled' => true ,
'conf' => {
'USE_LXD_BRIDGE' => 'true' ,
'LXD_BRIDGE' => 'lxdbr0' ,
'UPDATE_PROFILE' => 'true' ,
'LXD_CONFILE' => '' ,
'LXD_DOMAIN' => 'lxd' ,
'LXD_IPV4_ADDR' => '10.1.3.1' ,
'LXD_IPV4_NETMASK' => '255.255.255.0' ,
'LXD_IPV4_NETWORK' => '10.1.3.1/24' ,
'LXD_IPV4_DHCP_RANGE' => '10.1.3.100,10.1.3.254' ,
'LXD_IPV4_DHCP_MAX' => '155' ,
'LXD_IPV4_NAT' => 'true' ,
'LXD_IPV6_ADDR' => '' ,
'LXD_IPV6_MASK' => '' ,
'LXD_IPV6_NETWORK' => '' ,
'LXD_IPV6_NAT' => 'false' ,
'LXD_IPV6_PROXY' => 'false' ,
} ,
} ,
'profile' => {
'default-br0' => {
'config' => {
'user.user-data' => <<- "EOS" ,
#cloud-config
users:
- name: alice
groups: sudo,wheel
ssh- authorized- keys:
- #{ssh_pub_key}
EOS
} ,
'description' => 'Default LXD profile for real NIC bridged' ,
'devices' => {
'eth0' => {
'name' => 'eth0' ,
'nictype' => 'bridged' ,
'parent' => 'br0' ,
'type' => 'nic' ,
} ,
} ,
} ,
'docker-lxdbr0' => {
'config' => {
'linux.kernel_modules' => 'overlay, nf_nat' ,
'security.nesting' => 'true' ,
} ,
'description' => 'Profile supporting docker in containers' ,
'devices' => {
'aadisable' => {
'path' => '/sys/module/apparmor/parameters/enabled' ,
'source' => '/dev/null' ,
'type' => 'disk' ,
} ,
'eth0' => {
'name' => 'eth0' ,
'nictype' => 'bridged' ,
'parent' => 'lxdbr0' ,
'type' => 'nic' ,
} ,
'fuse' => {
'path' => '/dev/fuse' ,
'type' => 'unix-char' ,
} ,
} ,
} ,
} ,
} ,
} ,
)
snappy コマンドでインストールします。SnappyUbuntuCore の場合、LXDの初期設定も完了しています。
$ sudo snappy install lxd
初期設定の内容は以下の通りです。ストレージバックエンドは通常のディレクトリで、LXDサーバはネットワークに公開されていないようです。
$ lxc info
apicompat : 0
auth : trusted
environment :
addresses : [ ]
architectures :
- x86_64
- i686
certificate : |
-----BEGIN CERTIFICATE-----
MIIGATCCA+mgAwIBAgIQH+VJmjNOcp1DMx87NmYOTDANBgkqhkiG9w0BAQsFADBD
...
-----END CERTIFICATE-----
driver : lxc
driverversion : 2.0.0
kernel : Linux
kernelarchitecture : x86_64
kernelversion : 3.19.0-51 -generic
server : lxd
serverpid : 957
serverversion : 2.0.0
storage : dir
storageversion : ""
config : { }
public : false
通常のUbuntu(デスクトップ、サーバ) †
apt-get コマンドでインストールします。
$ sudo apt-get install lxd lxd-client
ストレージバックエンドとしてベターなZFSを使用する場合には、以下のパッケージもインストールします。 Ubuntu 16.04からzfsカーネルモジュールが標準で含まれています。
$ sudo apt-get install zfsutils-linux
初期設定を行います。
以下の例ではストレージバックエンドにZFSのブロックデバイスを使い、LXDをネットワークに公開しています。
$ sudo lxd init
Name of the storage backend to use (dir or zfs): zfs
Create a new ZFS pool (yes/no)? yes
Name of the new ZFS pool: lxd
Would you like to use an existing block device (yes/no)? yes
Path to the existing block device: /dev/sdb1
Would you like LXD to be available over the network (yes/no)? yes
Address to bind LXD to (not including port): 192.168.56.99
Port to bind LXD to (8443 recommended): 8443
Trust password for new clients:
Again:
Do you want to configure the LXD bridge (yes/no)? yes
ここから先はブリッジ設定の別のウィザードになります。設定初期値はランダムなIPアドレスになっていますので、既存のサブネットと衝突しないように適宜変更します。以下の例では、LXCのデフォルト設定(10.0.3.1/24)との衝突を避けています。
Would you like to setup a network bridge for LXD containers now? yes
Bridge interface name: lxdbr0
Do you want to setup an IPv4 subnet? yes
IPv4 address: 10.0.13.1
IPv4 CIDR mask: 24
First DHCP address: 10.0.13.100
Last DHCP address: 10.0.13.254
Max number of DHCP clients: 155
Do you want to NAT the IPv4 traffic? yes
Do you want to setup an IPv6 subnet? no
以下のメッセージ表示で初期化が完了します。
Warning: Stopping lxd.service, but it can still be activated by:
lxd.socket
LXD has been successfully configured.
初期化の結果を確認します。
$ lxc info
apicompat : 0
auth : trusted
environment :
addresses :
- 192.168.56.99:8443
architectures :
- x86_64
- i686
certificate : |
-----BEGIN CERTIFICATE-----
MIIF5jCCA86gAwIBAgIRAMzzVTKxAzRurnQgc+4NjycwDQYJKoZIhvcNAQELBQAw
...
-----END CERTIFICATE-----
driver : lxc
driverversion : 2.0.1
kernel : Linux
kernelarchitecture : x86_64
kernelversion : 4.4.0-24 -generic
server : lxd
serverpid : 16366
serverversion : 2.0.2
storage : zfs
storageversion : "5"
config :
core.https_address : 192.168.56.99:8443
core.trust_password : true
storage.zfs_pool_name : lxd
public : false
14.04 LTS (trusty) †
trusty-backports で提供されていますので、それを利用します。
Kernelを16.04と同じものにアップデートします。
$ sudo apt-get install linux-image-generic-lts-xenial linux-image-extra-virtual-lts-xenial
$ sudo reboot
trusty-backports を指定してlxdパッケージをインストールします。
$ sudo apt-get install -t trusty-backports lxd
以降のセットアップ手順は、16.04の場合と同様です(上記参照)。
再設定 †
コンテナとイメージが残存していなければ、初期化(lxd init)を再実行できます。
バージョン毎の注意点 †
16.10 (yakkety) †
ブリッジインタフェイスのセットアップ。lxd init のみを利用している限り違いを意識することはありませんが、ブリッジインタフェイスのセットアップが dpkg-reconfigure から lxc network コマンドに変更になりました。
16.10以降:
$ lxc network -h
Usage: Manage networks.
lxc network list List available networks.
lxc network show <network> Show details of a network.
lxc network create <network> [key=value]... Create a network.
lxc network get <network> <key> Get network configuration.
lxc network set <network> <key> <value> Set network configuration.
lxc network unset <network> <key> Unset network configuration.
lxc network delete <network> Delete a network.
lxc network edit <network>
Edit network, either by launching external editor or reading STDIN.
Example: lxc network edit <network> # launch editor
cat network.yml | lxc network edit <network> # read from network.yml
lxc network attach <network> <container> [device name]
lxc network attach-profile <network> <profile> [device name]
lxc network detach <network> <container> [device name]
lxc network detach-profile <network> <container> [device name]
Options:
--debug (= false)
Enables debug mode.
--force-local (= false)
Force using the local unix socket.
--no-alias (= false)
Ignore aliases when determining what command to run.
--verbose (= false)
Enables verbose mode.
16.04以前: 設定結果は、/etc/default/lxd-bridge に書き出されます。あまり推奨されませんが、この設定ファイルを直接編集し、lxd-bridgeサービスを再起動させても構いません。
$ sudo dpkg-reconfigure -p medium lxd
...
アップグレード後のコンテナ起動エラー
16.04以前で作成したコンテナが以下のエラーにより起動に失敗します。
$ lxc start xenial00
error: Bad key: volatile.idmap.base
Try `lxc info --show-log xenial00` for more info
lxc config edit コマンドで古いキーを削除してから起動させます。古いキーが複数ある場合がありますので、unset ではなく edit コマンドを使い一括して削除します。筆者の環境では、volatile.idmap.base と volatile.idmap.next が不要でした。
$ lxc config edit xenial00
利用方法 †
ひとめぐり †
ローカルユーザはUNIXソケット経由でLXDと通信しますので、あらかじめ lxd グループに追加しておく必要があります。現在のシェルですぐに使い始めたい場合には、グループ追加後に以下のコマンドを実行します。
$ newgrp lxd
Ubuntuではデフォルトで以下のリモートイメージサーバが登録されています。
$ lxc remote list
+-----------------+------------------------------------------+---------------+--------+--------+
| NAME | URL | PROTOCOL | PUBLIC | STATIC |
+-----------------+------------------------------------------+---------------+--------+--------+
| images | https://images.linuxcontainers.org | lxd | YES | NO |
+-----------------+------------------------------------------+---------------+--------+--------+
| local (default) | unix:// | lxd | NO | YES |
+-----------------+------------------------------------------+---------------+--------+--------+
| ubuntu | https://cloud-images.ubuntu.com/releases | simplestreams | YES | YES |
+-----------------+------------------------------------------+---------------+--------+--------+
| ubuntu-daily | https://cloud-images.ubuntu.com/daily | simplestreams | YES | YES |
+-----------------+------------------------------------------+---------------+--------+--------+
imagesサーバ上のイメージをリストしてみます。
$ lxc image list images: | head
+---------------------------------+--------------+--------+-------------------------------------------+---------+----------+------------------------------+
| ALIAS | FINGERPRINT | PUBLIC | DESCRIPTION | ARCH | SIZE | UPLOAD DATE |
+---------------------------------+--------------+--------+-------------------------------------------+---------+----------+------------------------------+
| alpine/3.0/amd64 (1 more) | 850b792748af | yes | Alpine 3.0 (amd64) (20160608_18:03) | x86_64 | 2.09MB | Jun 8, 2016 at 6:15pm (UTC) |
+---------------------------------+--------------+--------+-------------------------------------------+---------+----------+------------------------------+
| alpine/3.0/i386 (1 more) | 2f52b23c9540 | yes | Alpine 3.0 (i386) (20160608_17:50) | i686 | 2.06MB | Jun 8, 2016 at 6:15pm (UTC) |
+---------------------------------+--------------+--------+-------------------------------------------+---------+----------+------------------------------+
| alpine/3.1/amd64 (1 more) | 0977fe56e366 | yes | Alpine 3.1 (amd64) (20160608_17:50) | x86_64 | 2.32MB | Jun 8, 2016 at 6:15pm (UTC) |
+---------------------------------+--------------+--------+-------------------------------------------+---------+----------+------------------------------+
| alpine/3.1/i386 (1 more) | 6fb50b272f54 | yes | Alpine 3.1 (i386) (20160608_18:03) | i686 | 2.26MB | Jun 8, 2016 at 6:15pm (UTC) |
Ubuntuの最新安定版をシステムコンテナとして作成し、起動させてみます。
$ lxc launch ubuntu:16.04 my-xenial
Creating my-xenial
Retrieving image: 100%
Starting my-xenial
$ lxc list
+-----------+---------+-------------------+------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+-----------+---------+-------------------+------+------------+-----------+
| my-xenial | RUNNING | 10.0.3.254 (eth0) | | PERSISTENT | 0 |
+-----------+---------+-------------------+------+------------+-----------+
コンテナの中でBashを実行してみます。
$ lxc exec my-xenial -- /bin/bash
root@my-xenial:~# uname -a
Linux my-xenial 3.19.0-51-generic #57-Ubuntu SMP Fri Feb 19 14:05:05 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
root@my-xenial:~# exit
exit
直接システムコンテナを更新してみます。
$ lxc exec my-xenial -- apt-get update
...
$ lxc exec my-xenial -- apt-get upgrade
...
53 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 22.4 MB of archives.
After this operation, 45.1 kB of additional disk space will be used.
Do you want to continue? [Y/n]
...
$ lxc stop my-xenial
$ lxc list
+-----------+---------+------+------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+-----------+---------+------+------+------------+-----------+
| my-xenial | STOPPED | | | PERSISTENT | 0 |
+-----------+---------+------+------+------------+-----------+
$ lxc start my-xenial
$ lxc exec my-xenial -- apt-get update
...
$ lxc exec my-xenial -- apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
コンテナ内のrootは、ホストのrootと分離されていることが分かります。
$ ps -fe | grep lxc
nobody 951 1 0 11:05 ? 00:00:00 dnsmasq -u nobody --strict-order --bind-interfaces --pid-file=/var/lib/apps/lxd/2.0.0-2/dnsmasq.pid
--listen-address 10.0.3.1 --dhcp-range 10.0.3.2,10.0.3.254 --dhcp-lease-max=253 --dhcp-no-override --except-interface=lo --interface=lxcbr0
--dhcp-authoritative --dhcp-leasefile=/var/lib/apps/lxd/2.0.0-2/dnsmasq.lxcbr0.leases
root 954 885 0 11:05 ? 00:00:13 /apps/lxd/2.0.0-2/bin/x86_64/lxcfs/lxcfs /var/lib/apps/lxd/2.0.0-2/lxcfs
root 21348 1 0 11:35 ? 00:00:00 [lxc monitor] /var/lib/apps/lxd/2.0.0-2/lxd/containers my-xenial
$ ps -fe | grep 21348
root 21348 1 0 11:35 ? 00:00:00 [lxc monitor] /var/lib/apps/lxd/2.0.0-2/lxd/containers my-xenial
1000000 21365 21348 0 11:35 ? 00:00:00 /sbin/init
$ ps -fe | grep 1000000
1000000 21365 21348 0 11:35 ? 00:00:00 /sbin/init
1000000 21672 21365 0 11:35 ? 00:00:00 /lib/systemd/systemd-udevd
1000000 21687 21365 0 11:35 ? 00:00:00 /lib/systemd/systemd-journald
1000000 21944 21365 0 11:35 ? 00:00:00 /usr/sbin/cron -f
1000000 21974 21365 0 11:35 ? 00:00:00 /lib/systemd/systemd-logind
1000000 22031 21365 0 11:35 ? 00:00:00 /usr/lib/accountsservice/accounts-daemon
1000000 22087 21365 0 11:35 ? 00:00:00 /usr/lib/policykit-1/polkitd --no-debug
1000000 22257 21365 0 11:35 ? 00:00:00 /sbin/dhclient -1 -v -pf /run/dhclient.eth0.pid -lf /var/lib/dhcp/dhclient.eth0.leases -I
-df /var/lib/dhcp/dhclient6.eth0.leases eth0
1000000 22369 21365 0 11:35 ? 00:00:00 /usr/sbin/sshd -D
1000000 22576 21365 0 11:35 pts/1 00:00:00 /sbin/agetty --noclear --keep-baud console 115200 38400 9600 linux
追加でCentOS 7のシステムコンテナを作成し、起動させてみます。
$ lxc launch images:centos/7/amd64 my-centos7
Creating my-centos7
Retrieving image: 100%
Starting my-centos7
$ lxc list
+------------+---------+-------------------+------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+------------+---------+-------------------+------+------------+-----------+
| my-centos7 | RUNNING | 10.0.3.90 (eth0) | | PERSISTENT | 0 |
+------------+---------+-------------------+------+------------+-----------+
| my-xenial | RUNNING | 10.0.3.254 (eth0) | | PERSISTENT | 0 |
+------------+---------+-------------------+------+------------+-----------+
当然ながら、Linuxカーネルはホストのものですが、システムはCentOSであることが分かります。
$ lxc exec my-centos7 -- /bin/bash
[root@my-centos7 ~]# uname -a
Linux my-centos7 3.19.0-51-generic #57-Ubuntu SMP Fri Feb 19 14:05:05 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[root@my-centos7 ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"
非永続コンテナ †
デフォルトでは作成したコンテナは(停止させても)永続化されますが、-e, --ephemeral オプションを追加すると使い捨てのコンテナを起動させることができます。
$ lxc launch ubuntu:16.04 xenial-1time -e
...
$ lxc list
+--------------+---------+--------------------+------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+--------------+---------+--------------------+------+------------+-----------+
| xenial-1time | RUNNING | 10.0.13.187 (eth0) | | EPHEMERAL | 0 |
+--------------+---------+--------------------+------+------------+-----------+
| xenial00 | RUNNING | 10.0.13.133 (eth0) | | PERSISTENT | 0 |
+--------------+---------+--------------------+------+------------+-----------+
コンテナを停止させると、同時に削除されていることが分かります。ちなみに、コンテナ内のシステムをシャットダウンするとコンテナも終了しますので削除されますが、システムの再起動はそのまま可能です。
$ lxc stop xenial-1time
$ lxc list
+----------+---------+--------------------+------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+----------+---------+--------------------+------+------------+-----------+
| xenial00 | RUNNING | 10.0.13.133 (eth0) | | PERSISTENT | 0 |
+----------+---------+--------------------+------+------------+-----------+
ZFSストレージバックエンド †
ストレージバックエンドをZFSに設定する方法は上記の通りですが、ZFSプールの状態は以下のように確認できます。
$ sudo zpool list
NAME SIZE ALLOC FREE EXPANDSZ FRAG CAP DEDUP HEALTH ALTROOT
lxd 15.9G 752M 15.1G - 3% 4% 1.00x ONLINE -
$ sudo zpool status
pool: lxd
state: ONLINE
scan: none requested
config:
NAME STATE READ WRITE CKSUM
lxd ONLINE 0 0 0
sdb1 ONLINE 0 0 0
errors: No known data errors
ZFSプールの破棄 †
イメージやコンテナが存在しなければZFSプールを破棄して再セットアップすることができます。
$ sudo zpool status -v lxd
pool: lxd
state: ONLINE
scan: none requested
config:
NAME STATE READ WRITE CKSUM
lxd ONLINE 0 0 0
sda6 ONLINE 0 0 0
errors: No known data errors
$ sudo zpool destroy lxd
ローカルイメージを使う †
一度ダウンロードしたイメージはローカルストレージに保存されます。
$ lxc image list local:
+-------+--------------+--------+---------------------------------------------+--------+----------+------------------------------+
| ALIAS | FINGERPRINT | PUBLIC | DESCRIPTION | ARCH | SIZE | UPLOAD DATE |
+-------+--------------+--------+---------------------------------------------+--------+----------+------------------------------+
| | fa935b47f205 | no | Centos 7 (amd64) (20160614_02:16) | x86_64 | 62.95MB | Jun 14, 2016 at 4:56am (UTC) |
+-------+--------------+--------+---------------------------------------------+--------+----------+------------------------------+
| | 4b747164eaff | no | Debian jessie (amd64) (20160613_22:42) | x86_64 | 104.10MB | Jun 14, 2016 at 5:06am (UTC) |
+-------+--------------+--------+---------------------------------------------+--------+----------+------------------------------+
| | 98dc4bd7cca2 | no | ubuntu 16.04 LTS amd64 (release) (20160610) | x86_64 | 139.21MB | Jun 14, 2016 at 3:07am (UTC) |
+-------+--------------+--------+---------------------------------------------+--------+----------+------------------------------+
以降、同じバージョンのシステムコンテナを起動させる場合には、このローカルイメージを用いて高速に起動させることが可能です。
$ for i in $(seq 1 5); do time lxc launch 98dc4bd7cca2 -e; done
Creating southern-cari
Starting southern-cari
real 0m7.903s
user 0m0.008s
sys 0m0.004s
Creating unbigoted-danelle
Starting unbigoted-danelle
real 0m5.145s
user 0m0.008s
sys 0m0.000s
Creating selenographical-yuri
Starting selenographical-yuri
real 0m4.255s
user 0m0.008s
sys 0m0.000s
Creating radiographical-marlo
Starting radiographical-marlo
real 0m5.691s
user 0m0.008s
sys 0m0.000s
Creating nonprojecting-altagracia
Starting nonprojecting-altagracia
real 0m4.979s
user 0m0.008s
sys 0m0.000s
$ lxc list
+--------------------------+---------+-------------------+------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+--------------------------+---------+-------------------+------+------------+-----------+
| centos00 | STOPPED | | | PERSISTENT | 0 |
+--------------------------+---------+-------------------+------+------------+-----------+
| deb00 | STOPPED | | | PERSISTENT | 0 |
+--------------------------+---------+-------------------+------+------------+-----------+
| nonprojecting-altagracia | RUNNING | 10.1.3.181 (eth0) | | EPHEMERAL | 0 |
+--------------------------+---------+-------------------+------+------------+-----------+
| radiographical-marlo | RUNNING | 10.1.3.119 (eth0) | | EPHEMERAL | 0 |
+--------------------------+---------+-------------------+------+------------+-----------+
| selenographical-yuri | RUNNING | 10.1.3.200 (eth0) | | EPHEMERAL | 0 |
+--------------------------+---------+-------------------+------+------------+-----------+
| southern-cari | RUNNING | 10.1.3.104 (eth0) | | EPHEMERAL | 0 |
+--------------------------+---------+-------------------+------+------------+-----------+
| unbigoted-danelle | RUNNING | 10.1.3.190 (eth0) | | EPHEMERAL | 0 |
+--------------------------+---------+-------------------+------+------------+-----------+
| xenial00 | STOPPED | | | PERSISTENT | 0 |
+--------------------------+---------+-------------------+------+------------+-----------+
ホストの属するネットワークに参加させる(ブリッジ) †
bridge-utils パッケージがインストールされていることをあらかじめ確認しておきます。
以下の要領でブリッジインタフェイス(br*)を定義し、物理インタフェイス(eth*、p3p1等)にブリッジします。
/etc/netplan/01-netcfg.yaml (18.04以前): netplan がデフォルト設定ツールになっています。設定ファイル名は任意です。netplan apply コマンドで設定を反映させます。
network :
version : 2
renderer : networkd
ethernets :
eth0 :
dhcp4 : no
dhcp6 : no
bridges :
br0 :
interfaces :
- eth0
dhcp4 : no
dhcp6 : no
addresses :
- 192.168.0.123/24
gateway4 : 192.168.0.1
nameservers :
addresses :
- 192.168.0.53
parameters :
forward-delay : 0
stp : no
optional : yes
/etc/network/interfaces (17.10以前): ifupコマンドやOSの再起動によりインタフェイスを有効にします。なお、最近のUbuntuでは、NetworkManager? が起動していても自動でこれらの静的設定を避け、干渉することはありません。
auto lo
iface lo inet loopback
auto br0
iface br0 inet static
address 192.168.0.123
netmask 255.255.255.0
gateway 192.168.0.1
bridge_ports eth0
bridge_stp off
このブリッジを利用するLXDプロファイルを用意します。
$ lxc profile list
default
docker
$ lxc profile copy default{,-br0}
$ lxc profile edit default-br0
default-br0プロファイルの内容
name : default-br0
config : { }
description : Default LXD profile for real NIC bridged
devices :
eth0 :
name : eth0
nictype : bridged
parent : br0
type : nic
コンテナ作成時に、このプロファイルを指定します。
$ lxc launch ubuntu:16.04 xenial00-br0 -p default-br0
Creating xenial00-br0
Starting xenial00-br0
$ lxc list
+--------------+---------+----------------------+------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+--------------+---------+----------------------+------+------------+-----------+
| xenial00 | RUNNING | 10.0.4.221 (eth0) | | PERSISTENT | 0 |
+--------------+---------+----------------------+------+------------+-----------+
| xenial00-br0 | RUNNING | 192.168.0.223 (eth0) | | PERSISTENT | 0 |
+--------------+---------+----------------------+------+------------+-----------+
リモートLXDのコンテナを操作する †
LXDはネットワークに公開することができ、リモートからlxcコマンドやREST API経由で操作することが可能です。
LXDの設定 †
lxd init で設定が完了しているかもしれませんが、そうでない場合には以下のコマンドでリスンするアドレスと管理パスワードを設定します。
$ sudo lxc config set core.https_address "[::]" # すべてのネットワークインタフェイス
or
$ sudo lxc config set core.https_address "192.168.56.99:8443" # 特定のアドレス、ポートの例
$ sudo lxc config set core.trust_password some-password
ワークステーション(LXDクライアント)からの操作 †
ワークステーション上でリモートのLXDを追加登録します。表示の通り、サーバのフィンガープリントを確認の上、初回は管理パスワードの入力によりクライアントが認証され、リモートLXDにはそのクライアント証明書が登録されます。
ubuntu@lxd-ws:~$ lxc remote add lxd99 192.168.56.99
Generating a client certificate. This may take a minute...
If this is your first time using LXD, you should also run: sudo lxd init
Certificate fingerprint: 62f3016b553e8cd69910448ed3d306d2dfd5 ...
ok (y/n)? y
Admin password for lxd99:
Client certificate stored at server: lxd99
リモートサーバ一覧を確認します。
ubuntu@lxd-ws:~$ lxc remote list
+-----------------+------------------------------------------+---------------+--------+--------+
| NAME | URL | PROTOCOL | PUBLIC | STATIC |
+-----------------+------------------------------------------+---------------+--------+--------+
| images | https://images.linuxcontainers.org | lxd | YES | NO |
+-----------------+------------------------------------------+---------------+--------+--------+
| local (default) | unix:// | lxd | NO | YES |
+-----------------+------------------------------------------+---------------+--------+--------+
| lxd99 | https://192.168.56.99:8443 | lxd | NO | NO |
+-----------------+------------------------------------------+---------------+--------+--------+
| ubuntu | https://cloud-images.ubuntu.com/releases | simplestreams | YES | YES |
+-----------------+------------------------------------------+---------------+--------+--------+
| ubuntu-daily | https://cloud-images.ubuntu.com/daily | simplestreams | YES | YES |
+-----------------+------------------------------------------+---------------+--------+--------+
リモートのコンテナを操作してみます。
ubuntu@lxd-ws:~$ lxc exec lxd99:xenial00 -- /bin/bash
error: Container is not running.
ubuntu@lxd-ws:~$ lxc list lxd99:
+----------+---------+--------------------+------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+----------+---------+--------------------+------+------------+-----------+
| xenial00 | STOPPED | | | PERSISTENT | 0 |
+----------+---------+--------------------+------+------------+-----------+
ubuntu@lxd-ws:~$ lxc start lxd99:xenial00
ubuntu@lxd-ws:~$ lxc exec lxd99:xenial00 -- /bin/bash
root@xenial00:~#
cloud-initによるノードの初期設定 †
Ubuntuのイメージは cloud-init に対応していますので、コンテナの起動と同時に自動でノードの初期設定を行うことができます。ただし、CentOSなど cloud-init に対応していない他のイメージでは無効です。
以下のようにコンテナプロファイルに、cloud-init.yml の内容を追加しておくと便利です。
$ lxc profile show default
name : default
config :
user.user-data : |
#cloud-config
users:
- name: alice
groups: sudo,wheel
ssh-authorized-keys:
- ssh-rsa AAAAB3NzaC1yc2E ... == alice@wonderland.example.com
description : Default LXD profile
devices :
eth0 :
name : eth0
nictype : bridged
parent : lxdbr0
type : nic
usedby:
LXDとcloud-init †
コンテナのネスト(LXD in LXD) †
まずクリーンに最初の(親)システムコンテナを作成し、最新に更新しておきます。 追加設定オプションで security.nesting を有効にしておきます。
(amd64)ubuntu@snappy00:~$ lxc delete xenial00
(amd64)ubuntu@snappy00:~$ lxc launch ubuntu:16.04 xenial00 -c security.nesting=true
Creating xenial00
Starting xenial00
(amd64)ubuntu@snappy00:~$ lxc exec xenial00 -- /bin/bash
root@xenial00:~# apt-get update
...
root@xenial00:~# apt-get upgrade
...
もしシステムコンテナにLXDがインストールされていない場合にはインストールします。
root@xenial00:~# apt-get install lxd lxd-client
...
LXDの初期設定を行います。
通常のストレージバックエンドはZFSを選択するのがベターですが、すでにコンテナ内でZFS用にカーネルモジュールを新たにロードすることができませんので単純なディレクトリを選択しています。テストなので、以下の例ではLXDはネットワークに公開していません。
root@xenial00:~# lxd init
Name of the storage backend to use (dir or zfs): dir
We detected that you are running inside an unprivileged container.
This means that unless you manually configured your host otherwise,
you will not have enough uid and gid to allocate to your containers.
LXD can re-use your container's own allocation to avoid the problem.
Doing so makes your nested containers slightly less safe as they could
in theory attack their parent container and gain more privileges than
they otherwise would.
Would you like to have your containers share their parent's allocation (yes/no)? yes
Would you like LXD to be available over the network (yes/no)? no
Do you want to configure the LXD bridge (yes/no)? yes
uidとgidのアロケーションを親のそれと共有しない場合、以下のようなエラーが発生します。
root@xenial00:~# lxc launch ubuntu:16.04 xenial10
Creating xenial10
Retrieving image: 100%
error: Failed to change ownership of: /var/lib/lxd/containers/xenial10/rootfs
ここから先はブリッジ設定の別のウィザードになります。設定初期値はランダムなIPアドレスになっていますので、既存のサブネットと衝突しないように適宜変更します。
Would you like to setup a network bridge for LXD containers now? yes
Bridge interface name: lxdbr0
Do you want to setup an IPv4 subnet? yes
IPv4 address: 10.0.4.1
IPv4 CIDR mask: 24
First DHCP address: 10.0.4.100
Last DHCP address: 10.0.4.254
Max number of DHCP clients: 155
Do you want to NAT the IPv4 traffic? yes
Do you want to setup an IPv6 subnet? no
以下のメッセージ表示で初期化が完了します。
Warning: Stopping lxd.service, but it can still be activated by:
lxd.socket
LXD has been successfully configured.
折角なので非特権ユーザになって、ネストされた(子)システムコンテナを作成します。
root@xenial00:~# su ubuntu
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
ubuntu@xenial00:/root$ lxc launch ubuntu:16.04 xenial10
Generating a client certificate. This may take a minute...
If this is your first time using LXD, you should also run: sudo lxd init
To start your first container, try: lxc launch ubuntu:16.04
Creating xenial10
Retrieving image: 100%
Starting xenial10
ubuntu@xenial00:/root$ lxc list
+----------+---------+-------------------+------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+----------+---------+-------------------+------+------------+-----------+
| xenial10 | RUNNING | 10.0.4.205 (eth0) | | PERSISTENT | 0 |
+----------+---------+-------------------+------+------------+-----------+
問題ないようです。コンテナの仕組上、パフォーマンスの劣化もありません。
ubuntu@xenial00:/root$ lxc exec xenial10 -- /bin/bash
root@xenial10:~# apt-get update
...
root@xenial10:~# apt-get upgrade
...
もう一段ネストしても動作に遜色ありません。
root@xenial10:~# lxd init
...
root@xenial10:~# su ubuntu
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
ubuntu@xenial10:/root$ lxc launch ubuntu:16.04 xenial20
Generating a client certificate. This may take a minute...
If this is your first time using LXD, you should also run: sudo lxd init
To start your first container, try: lxc launch ubuntu:16.04
Creating xenial20
Retrieving image: 100%
Starting xenial20
ubuntu@xenial10:/root$ lxc list
+----------+---------+-------------------+------+------------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+----------+---------+-------------------+------+------------+-----------+
| xenial20 | RUNNING | 10.0.5.131 (eth0) | | PERSISTENT | 0 |
+----------+---------+-------------------+------+------------+-----------+
ubuntu@xenial10:/root$ lxc exec xenial20 -- /bin/bash
root@xenial20:~#
リソース †