LXDでクラスタを構築する

## 目的
LXD基盤でクラスタを構築して、多マシンにVMを配置する様な構成にしたい

## 環境
今回はubuntu-baseでクラスタを作成し、残りをそのクラスタに参加させる。
```bash
+--------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
|     NAME     |  STATE  |         IPV4         |                     IPV6                      |   TYPE    | SNAPSHOTS |
+--------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| ubuntu-lxd   | RUNNING | 10.85.165.233 (eth0) | fd42:d04c:aa87:84fb:216:3eff:fef1:2238 (eth0) | CONTAINER | 0         |
+--------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| ubuntu-lxd-0 | RUNNING | 10.85.165.153 (eth0) | fd42:d04c:aa87:84fb:216:3eff:fee1:edd8 (eth0) | CONTAINER | 0         |
+--------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| ubuntu-lxd-1 | RUNNING | 10.85.165.163 (eth0) | fd42:d04c:aa87:84fb:216:3eff:fef5:f85c (eth0) | CONTAINER | 0         |
+--------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| ubuntu-lxd-2 | RUNNING | 10.85.165.156 (eth0) | fd42:d04c:aa87:84fb:216:3eff:fe1a:c2ea (eth0) | CONTAINER | 0         |
+--------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
```

![特研資料.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2738475/2a0cc989-001c-41f2-0d65-a267309fee4d.jpeg)

## 構築
### master上での作業
ubuntu-base上で作業する

lxd initを実行し、`Would you like to use LXD clustering? (yes/no) [default=no]:`を`yes`と入力。それ以外はデフォルトの設定

```bash
root@ubuntu-lxd:~# lxd init
Would you like to use LXD clustering? (yes/no) [default=no]: yes
What IP address or DNS name should be used to reach this server? [default=10.85.165.233]:
Are you joining an existing cluster? (yes/no) [default=no]:
What member name should be used to identify this server in the cluster? [default=ubuntu-lxd]:
Do you want to configure a new local storage pool? (yes/no) [default=yes]:
Do you want to configure a new remote storage pool? (yes/no) [default=no]:
Would you like to connect to a MAAS server? (yes/no) [default=no]:
Would you like to configure LXD to use an existing bridge or host interface? (yes/no) [default=no]:
Would you like to create a new Fan overlay network? (yes/no) [default=yes]:
What subnet should be used as the Fan underlay? [default=auto]:
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]:
```


クラスタのマスターノードを構築
```bash
root@ubuntu-lxd:~# lxc cluster add ubuntu-lxd
To start your first container, try: lxc launch ubuntu:22.04
Or for a virtual machine: lxc launch ubuntu:22.04 --vm

Member ubuntu-lxd join token:
eyJzZXJ2ZXJfbmFtZSI6InVidW50dS1seGQiLCJmaW5nZXJwcmludCI6IjU0YzAzNTViMWIxODA3Nzk3MGI3NTZlYTcwNWMzYmUwNmY1NWFlYWMxMzcwZjhhNzhmMmJmMjM5ZDA5ZDA3ZDIiLCJhZGRyZXNzZXMiOlsiMTAuODUuMTY1LjIzMzo4NDQzIl0sInNlY3JldCI6IjBiZjY1MDQxNTAyOTA1MjRkMTJmM2FhMzk2OGJmMWY2OWRkNjAxZDNlMDc3OTA2OTUzMTc3MjY5ZGE5ZTJlNGYiLCJleHBpcmVzX2F0IjoiMjAyMy0wOS0yN1QxMjowOTowNi40MjQyODAwMTFaIn0=
```

クラスタに参加するマシンのトークンを取得(master node上で作業)
```bash
root@ubuntu-lxd:~# lxc cluster add ubuntu-lxd-0
Member ubuntu-lxd-0 join token:
eyJzZXJ2ZXJfbmFtZSI6InVidW50dS1seGQtMCIsImZpbmdlcnByaW50IjoiNTRjMDM1NWIxYjE4MDc3OTcwYjc1NmVhNzA1YzNiZTA2ZjU1YWVhYzEzNzBmOGE3OGYyYmYyMzlkMDlkMDdkMiIsImFkZHJlc3NlcyI6WyIxMC44NS4xNjUuMjMzOjg0NDMiXSwic2VjcmV0IjoiYmFkMjgyMTJlZjU4Yjg1ZTc1MmMyY2IxOGY3ZGE4NGVlNjExYTE0YWQ4OWNlNTViY2NkNmQ1NjFjODdjNTI3MyIsImV4cGlyZXNfYXQiOiIyMDIzLTA5LTI3VDEyOjA5OjIxLjg4MTk0MjEzM1oifQ==


root@ubuntu-lxd:~# lxc cluster add ubuntu-lxd-1
Member ubuntu-lxd-1 join token:
eyJzZXJ2ZXJfbmFtZSI6InVidW50dS1seGQtMSIsImZpbmdlcnByaW50IjoiNTRjMDM1NWIxYjE4MDc3OTcwYjc1NmVhNzA1YzNiZTA2ZjU1YWVhYzEzNzBmOGE3OGYyYmYyMzlkMDlkMDdkMiIsImFkZHJlc3NlcyI6WyIxMC44NS4xNjUuMjMzOjg0NDMiXSwic2VjcmV0IjoiZTkyZDQ2MjQ2Nzc1NWE1YzgzZDk2ODE5MDg3NWExODYwODNmNzhlMmIyMDU1OGYxMWUyZGI5NDFiZWNlOGMwNyIsImV4cGlyZXNfYXQiOiIyMDIzLTA5LTI3VDEyOjA5OjI0LjIyNDc3NzMyWiJ9


root@ubuntu-lxd:~# lxc cluster add ubuntu-lxd-2
Member ubuntu-lxd-2 join token:
eyJzZXJ2ZXJfbmFtZSI6InVidW50dS1seGQtMiIsImZpbmdlcnByaW50IjoiNTRjMDM1NWIxYjE4MDc3OTcwYjc1NmVhNzA1YzNiZTA2ZjU1YWVhYzEzNzBmOGE3OGYyYmYyMzlkMDlkMDdkMiIsImFkZHJlc3NlcyI6WyIxMC44NS4xNjUuMjMzOjg0NDMiXSwic2VjcmV0IjoiYWI3MTBjNDQxMzU2MWY3ZTY0ZjI4ZjE2OGY0NGIyMWFmZTI1Zjc0YWE0YzcyNTNjMTIyY2FhNGE0N2M1NDYxOSIsImV4cGlyZXNfYXQiOiIyMDIzLTA5LTI3VDEyOjA5OjI2Ljc0NDA1OTQ4NVoifQ==
```



### Worker上での作業
lxd initを実行し、`Would you like to use LXD clustering? (yes/no) [default=no]:`を`yes`と入力。`Are you joining an existing cluster? (yes/no) [default=no]:`で`yes`を入力。

**ubuntu-lxd-0**
```bash
root@ubuntu-lxd-0:~# lxd init
Would you like to use LXD clustering? (yes/no) [default=no]: yes
What IP address or DNS name should be used to reach this server? [default=10.85.165.153]:
Are you joining an existing cluster? (yes/no) [default=no]: yes
Do you have a join token? (yes/no/[token]) [default=no]: yes
Please provide join token: eyJzZXJ2ZXJfbmFtZSI6InVidW50dS1seGQtMCIsImZpbmdlcnByaW50IjoiNTRjMDM1NWIxYjE4MDc3OTcwYjc1NmVhNzA1YzNiZTA2ZjU1YWVhYzEzNzBmOGE3OGYyYmYyMzlkMDlkMDdkMiIsImFkZHJlc3NlcyI6WyIxMC44NS4xNjUuMjMzOjg0NDMiXSwic2VjcmV0IjoiYmFkMjgyMTJlZjU4Yjg1ZTc1MmMyY2IxOGY3ZGE4NGVlNjExYTE0YWQ4OWNlNTViY2NkNmQ1NjFjODdjNTI3MyIsImV4cGlyZXNfYXQiOiIyMDIzLTA5LTI3VDEyOjA5OjIxLjg4MTk0MjEzM1oifQ==
All existing data is lost when joining a cluster, continue? (yes/no) [default=no] yes
Choose "source" property for storage pool "local":
Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]:
```
**ubuntu-lxd-1**
```bash
root@ubuntu-lxd-1:~# lxd init
Would you like to use LXD clustering? (yes/no) [default=no]: yes
What IP address or DNS name should be used to reach this server? [default=10.85.165.163]:
Are you joining an existing cluster? (yes/no) [default=no]: yes
Do you have a join token? (yes/no/[token]) [default=no]: eyJzZXJ2ZXJfbmFtZSI6InVidW50dS1seGQtMSIsImZpbmdlcnByaW50IjoiNTRjMDM1NWIxYjE4MDc3OTcwYjc1NmVhNzA1YzNiZTA2ZjU1YWVhYzEzNzBmOGE3OGYyYmYyMzlkMDlkMDdkMiIsImFkZHJlc3NlcyI6WyIxMC44NS4xNjUuMjMzOjg0NDMiXSwic2VjcmV0IjoiZTkyZDQ2MjQ2Nzc1NWE1YzgzZDk2ODE5MDg3NWExODYwODNmNzhlMmIyMDU1OGYxMWUyZGI5NDFiZWNlOGMwNyIsImV4cGlyZXNfYXQiOiIyMDIzLTA5LTI3VDEyOjA5OjI0LjIyNDc3NzMyWiJ9
All existing data is lost when joining a cluster, continue? (yes/no) [default=no] yes
Choose "source" property for storage pool "local":
Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]:
```

**ubuntu-lxd-2**
```bash
root@ubuntu-lxd-2:~# lxd init
Would you like to use LXD clustering? (yes/no) [default=no]: yes
What IP address or DNS name should be used to reach this server? [default=10.85.165.156]:
Are you joining an existing cluster? (yes/no) [default=no]: yes
Do you have a join token? (yes/no/[token]) [default=no]: eyJzZXJ2ZXJfbmFtZSI6InVidW50dS1seGQtMiIsImZpbmdlcnByaW50IjoiNTRjMDM1NWIxYjE4MDc3OTcwYjc1NmVhNzA1YzNiZTA2ZjU1YWVhYzEzNzBmOGE3OGYyYmYyMzlkMDlkMDdkMiIsImFkZHJlc3NlcyI6WyIxMC44NS4xNjUuMjMzOjg0NDMiXSwic2VjcmV0IjoiYWI3MTBjNDQxMzU2MWY3ZTY0ZjI4ZjE2OGY0NGIyMWFmZTI1Zjc0YWE0YzcyNTNjMTIyY2FhNGE0N2M1NDYxOSIsImV4cGlyZXNfYXQiOiIyMDIzLTA5LTI3VDEyOjA5OjI2Ljc0NDA1OTQ4NVoifQ==
All existing data is lost when joining a cluster, continue? (yes/no) [default=no] yes
Choose "source" property for storage pool "local":
Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]:
```

## 確認
master-node上で確認
```bash
+--------------+----------------------------+------------------+--------------+----------------+-------------+--------+-------------------+
|     NAME     |            URL             |      ROLES       | ARCHITECTURE | FAILURE DOMAIN | DESCRIPTION | STATE  |      MESSAGE      |
+--------------+----------------------------+------------------+--------------+----------------+-------------+--------+-------------------+
| ubuntu-lxd   | https://10.85.165.233:8443 | database-leader  | x86_64       | default        |             | ONLINE | Fully operational |
|              |                            | database         |              |                |             |        |                   |
+--------------+----------------------------+------------------+--------------+----------------+-------------+--------+-------------------+
| ubuntu-lxd-0 | https://10.85.165.153:8443 | database         | x86_64       | default        |             | ONLINE | Fully operational |
+--------------+----------------------------+------------------+--------------+----------------+-------------+--------+-------------------+
| ubuntu-lxd-1 | https://10.85.165.163:8443 | database         | x86_64       | default        |             | ONLINE | Fully operational |
+--------------+----------------------------+------------------+--------------+----------------+-------------+--------+-------------------+
| ubuntu-lxd-2 | https://10.85.165.156:8443 | database-standby | x86_64       | default        |             | ONLINE | Fully operational |
+--------------+----------------------------+------------------+--------------+----------------+-------------+--------+-------------------+
```

## コンテナの配置
指定したノード上でコンテナを起動。特に指定しなければ自動的に分散配置される。
```bash
lxc launch --target ubuntu-lxd-1 ubuntu:22.04 ubuntu-test
```

## コンテナの移動
```bash
lxc stop ubuntu-test
lxc move ubuntu-test --target ubuntu-lxd-2
```