Raspberry Pi4にLXDをセットアップする

lxd の初期設定

今回はlxdのストレージにzfsを使うので、最初にデータセットを作成

ubuntu@ubuntu:~$ sudo zfs create pool01/lxd_default
ubuntu@ubuntu:~$ zfs list
NAME                 USED  AVAIL     REFER  MOUNTPOINT
pool01              17.9G   373G     5.97G  /pool01
pool01/NFS          12.0G   373G     12.0G  /pool01/NFS
pool01/lxd_default    24K   373G       24K  /pool01/lxd_default

初期設定コマンドで適当にセットアップ

ubuntu@ubuntu:~$ sudo lxd init
Would you like to use LXD clustering? (yes/no) [default=no]:
Do you want to configure a new storage pool? (yes/no) [default=yes]:
Name of the new storage pool [default=default]:
Name of the storage backend to use (btrfs, dir, lvm, zfs, ceph) [default=zfs]: zfs
Create a new ZFS pool? (yes/no) [default=yes]: no
Name of the existing ZFS pool or dataset: pool01/lxd_default
Would you like to connect to a MAAS server? (yes/no) [default=no]:
Would you like to create a new local network bridge? (yes/no) [default=yes]:
What should the new bridge be called? [default=lxdbr0]:
What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:
What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: none
Would you like the LXD server to be available over the network? (yes/no) [default=no]:
Would you like stale cached images to be updated automatically? (yes/no) [default=yes]:
Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]:

テスト用にコンテナを作成する。ubuntu20.04のイメージでubuntu-containerという名前で作る

ubuntu@ubuntu:~$ sudo lxc launch images:ubuntu/20.04 ubuntu-container
Creating ubuntu-container
Starting ubuntu-container
ubuntu@ubuntu:~$ lxc list
+------------------+---------+-----------------------+------+-----------+-----------+
|       NAME       |  STATE  |         IPV4          | IPV6 |   TYPE    | SNAPSHOTS |
+------------------+---------+-----------------------+------+-----------+-----------+
| ubuntu-container | RUNNING | 10.206.161.151 (eth0) |      | CONTAINER | 0         |
+------------------+---------+-----------------------+------+-----------+-----------+

とりあえず動くことが分かったので消しておく

ubuntu@ubuntu:~$ lxc stop ubuntu-container
ubuntu@ubuntu:~$ lxc list
+------------------+---------+------+------+-----------+-----------+
|       NAME       |  STATE  | IPV4 | IPV6 |   TYPE    | SNAPSHOTS |
+------------------+---------+------+------+-----------+-----------+
| ubuntu-container | STOPPED |      |      | CONTAINER | 0         |
+------------------+---------+------+------+-----------+-----------+
ubuntu@ubuntu:~$ lxc delete ubuntu-container
ubuntu@ubuntu:~$ lxc list
+------+-------+------+------+------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+------+-------+------+------+------+-----------+

ネットワーク設定

今の設定だとコンテナから外部に通信できるけれど、外部からコンテナに通信はできない。外部から通信で起用にmacvlanの設定を作成する。 最初に設定ファイルをコピー

ubuntu@ubuntu:~$ lxc profile copy default macvlan_profile

ファイルを編集してmacvlan用の設定にする。nictypeをmacvlanにしてparentにホストのNICを指定する。

ubuntu@ubuntu:~$ lxc profile edit macvlan_profile
config: {}
description: macvlan profile
devices:
  eth0:
    name: eth0
    nictype: macvlan
    parent: eth0
    type: nic
  root:
    path: /
    pool: default
    type: disk
name: macvlan_profile
used_by: []

作成したプロファイルを指定してコンテナを作成。今回はホストと同じサブネットのIPになっている。

ubuntu@ubuntu:~$ sudo lxc launch -p macvlan_profile images:ubuntu/20.04 macvlan-container
Creating macvlan-container
Starting macvlan-container
ubuntu@ubuntu:~$ lxc list
+-------------------+---------+----------------------+---------------------------------------------+-----------+-----------+
|       NAME        |  STATE  |         IPV4         |                    IPV6                     |   TYPE    | SNAPSHOTS |
+-------------------+---------+----------------------+---------------------------------------------+-----------+-----------+
| macvlan-container | RUNNING | 192.168.11.39 (eth0) | 240b:11:a102:9400:216:3eff:feb6:83ed (eth0) | CONTAINER | 0         |
+-------------------+---------+----------------------+---------------------------------------------+-----------+-----------+
ubuntu@ubuntu:~$ lxc exec macvlan-container /bin/bash
root@macvlan-container:~# ip -4 a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
7: eth0@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link-netnsid 0
    inet 192.168.11.39/24 brd 192.168.11.255 scope global dynamic eth0
       valid_lft 172754sec preferred_lft 172754sec

ホストのIPv4フォワード設定を変える。コメント解除してsysctl -p

ubuntu@ubuntu:~$ sudo vi /etc/sysctl.conf
#net.ipv4.ip_forward=1
↓
net.ipv4.ip_forward=1

テスト

試しにApacheを入れてクライアントのブラウザから見えるか試してみる

root@macvlan-container:~# apt install apache2
root@macvlan-container:~# systemctl status httpd
Unit httpd.service could not be found.
root@macvlan-container:~# systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
    Drop-In: /run/systemd/system/service.d
             mqzzz-lxc-service.conf
     Active: active (running) since Fri 2022-04-29 02:57:04 UTC; 17s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 1167 (apache2)
      Tasks: 55 (limit: 4421)
     Memory: 6.3M
        CPU: 284ms
     CGroup: /system.slice/apache2.service
             tq1167 /usr/sbin/apache2 -k start
             tq1168 /usr/sbin/apache2 -k start
             mq1169 /usr/sbin/apache2 -k start

ブラウザからアクセスして表示されていれば成功