ubuntu linuxもよくわかっていない状態で、(いまごろ)lxdというものに興味を持ち色々試してみたメモを残しておきます。 そもそも、LXDとは何ってところもわかっていないのですが、VMWAREとか、XEN、KVM等仮想化のコンテナ版というくらいの認識しか私には今のところありません(汗)
今回検証したubuntu linuxは、Ubuntu 18.04.4 LTSで、lxdのバージョンは3.0 です。メモの随所に、lxdの話ではなく、ubuntuの話が混ざっておりますが、どちらもよくわかっていないので、許してください。
まだ勉強途中ですが、意外とこれいいかも!って思っています。
コンテナの作成が速い! ゲスト(って言えばいいのかな?)の起動が速い! ディスクとかメモリとかちまちま考える必要はない。
ちゃんと仕組みを理解すれば、複数台のサーバを一つにまとめることが実現できそうな期待がもてます
LXDのインストール
$ sudo apt install lxd btrfs-progs
コンテナを作成
リモートサーバを確認する
NAMEの部分は後ほどコンテナの作成に利用されます → ①
$ lxc remote list +-----------------+------------------------------------------+---------------+-----------+--------+--------+ | NAME | URL | PROTOCOL | AUTH TYPE | PUBLIC | STATIC | +-----------------+------------------------------------------+---------------+-----------+--------+--------+ | images | https://images.linuxcontainers.org | simplestreams | | YES | NO | +-----------------+------------------------------------------+---------------+-----------+--------+--------+ | local (default) | unix:// | lxd | tls | NO | YES | +-----------------+------------------------------------------+---------------+-----------+--------+--------+ | ubuntu | https://cloud-images.ubuntu.com/releases | simplestreams | | YES | YES | +-----------------+------------------------------------------+---------------+-----------+--------+--------+ | ubuntu-daily | https://cloud-images.ubuntu.com/daily | simplestreams | | YES | YES | +-----------------+------------------------------------------+---------------+-----------+--------+--------+
イメージの一覧を確認
ALIASの部分は後ほどコンテナの作成に利用されます → ②
$ lxc image alias list ubuntu: | head +-----------------+--------------+-------------+ | ALIAS | FINGERPRINT | DESCRIPTION | +-----------------+--------------+-------------+ | 12.04 | be4aa8e56eab | | +-----------------+--------------+-------------+ | 12.04/amd64 | be4aa8e56eab | | +-----------------+--------------+-------------+ | 12.04/armhf | 31a39845ffbf | | +-----------------+--------------+-------------+ | 12.04/i386 | dfcb483c8c20 | |
コンテナの作成
lxc launch ①:② /コンテナのhit前/
- 例えば、リモートサーバhitが、 ubuntu 、イメージが、 18.04、コンテナ名を、 hit としたい場合は、以下のようになります
$ lxc launch ubuntu:18.04 hit ---- hit を作成中 hit を起動中
こんな感じであっさりコンテナが作成されました
作成されたコンテナを確認
lxc listで確認することが可能です
$ lxc list +-----------+---------+---------------------+-----------------------------------------------+------------+-----------+ | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | +-----------+---------+---------------------+-----------------------------------------------+------------+-----------+ | bionic | RUNNING | 10.131.37.10 (eth0) | fd42:fd6b:d6c6:7a71:216:3eff:fe91:d08e (eth0) | PERSISTENT | 0 | +-----------+---------+---------------------+-----------------------------------------------+------------+-----------+ | container | RUNNING | 10.131.37.98 (eth0) | fd42:fd6b:d6c6:7a71:216:3eff:febe:f873 (eth0) | PERSISTENT | 0 | +-----------+---------+---------------------+-----------------------------------------------+------------+-----------+ | hit | RUNNING | 10.131.37.73 (eth0) | fd42:fd6b:d6c6:7a71:216:3eff:fe84:a41e (eth0) | PERSISTENT | 0 | +-----------+---------+---------------------+-----------------------------------------------+------------+-----------+
コンテナへの接続
コンテナ、hitに接続する例です、rootで接続されたようです。
$ lxc exec hit bash --- root@hit:~#
厳密には、ログインしているように見えるだけ。。とのことですが、よくわかっていません(笑)
コンテナにssh接続できるようにする
- パスワードの変更
$ lxc exec hit bash ------ # passwd Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
- sshd_configの編集
とりあえず、あとでちゃんと治すとして。。
root@hit:~# vi /etc/ssh/sshd_config ----- PermitRootLogin yes PasswordAuthentication yes root@hit:~# /etc/init.d/ssh restart ----- [ ok ] Restarting ssh (via systemctl): ssh.service. root@hit:~# exit
- ssh接続してみる
とりあえず、ssh接続はできるようになりました!
$ ssh root@10.131.37.73 root@10.131.37.73's password: ----- Welcome to Ubuntu 18.04.4 LTS (GNU/Linux 5.3.0-42-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage System information as of Thu Mar 19 04:55:31 UTC 2020 System load: 0.06 Processes: 23 Usage of /home: unknown Users logged in: 0 Memory usage: 2% IP address for eth0: 10.131.37.73 Swap usage: 0% 0 packages can be updated. 0 updates are security updates. Last login: Thu Mar 19 04:51:35 2020 from 10.131.37.1
- root以外のアカウントを作成する
$ lxc exec hit bash ------ # groupadd -g 3001 admen # useradd -g admen -u 1007 user01 # mkdir -p /home/user01/.ssh # touch /home/user01/.ssh/authorized_keys # chown -Rf user01:admen /home/user01/.ssh # chown -Rf user01:admen /home/user01/.ssh/authorized_keys # passwd user01
- pemの設定
# su - user01 $ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/user01/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/user01/.ssh/id_rsa. Your public key has been saved in /home/user01/.ssh/id_rsa.pub. The key fingerprint is: SHA256:WJ7GvRwMkkTOg7gYH5RCWjO72tTo5WfGWIDLFLVZfZ0 user01@hit The key's randomart image is: +---[RSA 2048]----+ |..*o..+. . . | |oo.B O .. . E | |o.* = * o. | | * B . B = | |. O o o S + | | = o + . . o | |. o o = o | | + | | | +----[SHA256]-----+
- pemの作成
$ cd .ssh $ cat id_rsa.pub >> authorized_keys $ chmod 600 authorized_keys $ rm id_rsa.pub $ cp -a id_rsa /tmp/
- pemの取得(ホスト側で)
$ scp -r root@10.131.37.73:/tmp/id_rsa ./ $ mv id_rsa hit.pem $ ssh -i hit.pem user01@10.131.37.73 --- $
コンテナの停止
$ lxc stop hit $ lxc list +-----------+---------+---------------------+-----------------------------------------------+------------+-----------+ | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | +-----------+---------+---------------------+-----------------------------------------------+------------+-----------+ | bionic | RUNNING | 10.131.37.10 (eth0) | fd42:fd6b:d6c6:7a71:216:3eff:fe91:d08e (eth0) | PERSISTENT | 0 | +-----------+---------+---------------------+-----------------------------------------------+------------+-----------+ | container | RUNNING | 10.131.37.98 (eth0) | fd42:fd6b:d6c6:7a71:216:3eff:febe:f873 (eth0) | PERSISTENT | 0 | +-----------+---------+---------------------+-----------------------------------------------+------------+-----------+ | hit | STOPPED | | | PERSISTENT | 0 | +-----------+---------+---------------------+-----------------------------------------------+------------+-----------+
コンテナの開始
$ lxc start hit $ lxc list +-----------+---------+---------------------+-----------------------------------------------+------------+-----------+ | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | +-----------+---------+---------------------+-----------------------------------------------+------------+-----------+ | bionic | RUNNING | 10.131.37.10 (eth0) | fd42:fd6b:d6c6:7a71:216:3eff:fe91:d08e (eth0) | PERSISTENT | 0 | +-----------+---------+---------------------+-----------------------------------------------+------------+-----------+ | container | RUNNING | 10.131.37.98 (eth0) | fd42:fd6b:d6c6:7a71:216:3eff:febe:f873 (eth0) | PERSISTENT | 0 | +-----------+---------+---------------------+-----------------------------------------------+------------+-----------+ | hit | RUNNING | 10.131.37.73 (eth0) | fd42:fd6b:d6c6:7a71:216:3eff:fe84:a41e (eth0) | PERSISTENT | 0 | +-----------+---------+---------------------+-----------------------------------------------+------------+-----------+
コンテナの削除
起動中は削除できません、停止してから削除(あたりまえか)
$ lxc delete bionic Error: コンテナは実行中です。先に停止させるか、--force を指定してください $ lxc stop bionic $ lxc delete bionic $ lxc stop container $ lxc delete container $ lxc list +------+---------+---------------------+-----------------------------------------------+------------+-----------+ | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | +------+---------+---------------------+-----------------------------------------------+------------+-----------+ | hit | RUNNING | 10.131.37.73 (eth0) | fd42:fd6b:d6c6:7a71:216:3eff:fe84:a41e (eth0) | PERSISTENT | 0 | +------+---------+---------------------+-----------------------------------------------+------------+-----------+
lxcコンテナに固定IPを割り振る方法
ipアドレスを固定したい場合があると思う。。そんな時はこんな感じで
現在の状況を確認
$ lxc list +--------+---------+----------------------+-----------------------------------------------+------------+-----------+ | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | +--------+---------+----------------------+-----------------------------------------------+------------+-----------+ | hit | RUNNING | 10.131.37.117 (eth0) | fd42:fd6b:d6c6:7a71:216:3eff:fe91:d08e (eth0) | PERSISTENT | 0 | +--------+---------+----------------------+-----------------------------------------------+------------+-----------+
10.131.37.10のIP ADDRを固定
$ lxc network attach lxdbr0 hit eth0 eth0 $ lxc config device set hit eth0 ipv4.address 10.131.37.10 $ lxc stop hit $ lxc start hit
lxc network attach lxdbr0 hit eth0 eth0にはeth0が2つ指定します
・1つ目はlxd listコマンド等で表示されるインターフェース名 ・2つ目はコンテナ内から見たインターフェース名
設定後のIPが固定になっていることを確認
$ lxc list +--------+---------+---------------------+-----------------------------------------------+------------+-----------+ | NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS | +--------+---------+---------------------+-----------------------------------------------+------------+-----------+ | hit | RUNNING | 10.131.37.10 (eth0) | fd42:fd6b:d6c6:7a71:216:3eff:fe91:d08e (eth0) | PERSISTENT | 0 | +--------+---------+---------------------+-----------------------------------------------+------------+-----------+
nicの追加
macvlanで既存のLANと同じIP帯でネットワークを設定してみる
nicの追加
$ lxc config device add hit eth1 nic nictype=macvlan parent=enp0s25 $ lxc exec hit ip addr show eth1 $ lxc exec hit dhclient eth1 $ lxc exec hit ip addr show eth1 --------------- 16: eth1@if2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 00:16:3e:37:74:3e brd ff:ff:ff:ff:ff:ff link-netnsid 0 nakayama@gate:~$ lxc exec hit dhclient eth1 cmp: EOF on /tmp/tmp.YmGMl8SifI which is empty nakayama@gate:~$ lxc exec hit ip addr show eth1 16: eth1@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether 00:16:3e:37:74:3e brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 192.168.0.26/24 brd 192.168.0.255 scope global eth1 valid_lft forever preferred_lft forever inet6 240f:38:80f8:1:216:3eff:fe37:743e/64 scope global dynamic mngtmpaddr valid_lft 296sec preferred_lft 296sec inet6 fe80::216:3eff:fe37:743e/64 scope link valid_lft forever preferred_lft forever
nicの削除
$ lxc config device remove hit eth1
後ほど、このページは、更新していこうと思います。