4
2

More than 1 year has passed since last update.

LXDに入門する

Last updated at Posted at 2023-09-03

目的

VM的にコンテナを扱おうとするとDockerではどうしても出来ない事が多い。今回はLXDでVM的にコンテナを扱ってみようと思う。

LXDとは?

Linux上で動作するシステムコンテナ。Dockerに比べVMに近い様な動きをする。コンテナの中でsystemd等が実行できる。

インストール

sudo snap install lxd

pathの追加

export PATH=$PATH:/snap/bin/

初期設定

$ lxd init
root@shoma:/home/shoma# lxd init

Would you like to use LXD clustering? (yes/no) [default=no]: no #LXDを別LXDホストとクラスタリングするか
Do you want to configure a new storage pool? (yes/no) [default=yes]: yes #新しいストレージプールの作成
Name of the new storage pool [default=default]: default #ストレージプールの名前
Name of the storage backend to use (dir, lvm, btrfs, ceph) [default=btrfs]: #ストレージプールのバックエンド
Create a new BTRFS pool? (yes/no) [default=yes]: #新しいBTRFSを作成
Would you like to use an existing empty block device (e.g. a disk or partition)? (yes/no) [default=no]: #既存んのブロックデバイスを使用するか
Size in GiB of the new loop device (1GiB minimum) [default=30GiB]: #ストレージプールのサイズ
Would you like to connect to a MAAS server? (yes/no) [default=no]: #MAASサーバーに接続するか
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]: #コンテナが使用するアドレス(IPv4)
What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: #コンテナが使用するアドレス(IPv6)
Would you like the LXD server to be available over the network? (yes/no) [default=no]: #LXDサーバーをネットワーク越しに使用するか
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]: #lxd initの結果をYAML表示するか

LXDでubuntuを起動してみる

インストール出来るイメージの調査

lxc image list images:ubuntu

起動

$ lxc launch images:ubuntu/23.10 ubuntu-test

起動確認

root@shoma:/home/shoma# lxc list
+-------------+---------+---------------------+-----------------------------------------------+-----------+-----------+
|    NAME     |  STATE  |        IPV4         |                     IPV6                      |   TYPE    | SNAPSHOTS |
+-------------+---------+---------------------+-----------------------------------------------+-----------+-----------+
| ubuntu-test | RUNNING | 10.107.73.50 (eth0) | fd42:d70a:2761:a81b:216:3eff:fe2a:52a7 (eth0) | CONTAINER | 0         |
+-------------+---------+---------------------+-----------------------------------------------+-----------+-----------+

コンテナの操作

コンテナ停止

lxc stop ubuntu-test

コンテナ一時停止

lxc pause ubuntu-test

コンテナの再開

lxc start ubuntu-test

コンテナ削除

lxc delete ubuntu-test

コンテナのコピー

lxc copy ubuntu-test ubuntu-test-copy

コンテナログイン

shellに入る

lxc shell ubuntu-test

コマンド実行

lxc exec ubuntu-test hostname
lxc exec ubuntu-test -- ls -la

エクスポート&インポート

エクスポート

スナップショットの作成

lxc snapshot ubuntu-test

確認

lxc info ubuntu-test --verbose

snapshot名:snap0が完成されている

root@shoma:/home/shoma# lxc info ubuntu-test --verbose
Name: ubuntu-test
Status: RUNNING
Type: container
Architecture: aarch64
PID: 1642
Created: 2023/09/03 00:47 JST
Last Used: 2023/09/03 01:09 JST

Resources:
  Processes: 110
  CPU usage:
    CPU usage (in seconds): 114
  Network usage:
    eth0:
      Type: broadcast
      State: UP
      Host interface: vethca0e90ff
      MTU: 1500
      Bytes received: 35.30MB
      Bytes sent: 675.75kB
      Packets received: 23579
      Packets sent: 9819
      IP addresses:
        inet:  10.107.73.50/24 (global)
        inet6: fd42:d70a:2761:a81b:216:3eff:fe2a:52a7/64 (global)
        inet6: fe80::216:3eff:fe2a:52a7/64 (link)
    lo:
      Type: loopback
      State: UP
      MTU: 65536
      Bytes received: 18.75kB
      Bytes sent: 18.75kB
      Packets received: 80
      Packets sent: 80
      IP addresses:
        inet:  127.0.0.1/8 (local)
        inet6: ::1/128 (local)

Snapshots:
+-------+----------------------+------------+----------+
| NAME  |       TAKEN AT       | EXPIRES AT | STATEFUL |
+-------+----------------------+------------+----------+
| snap0 | 2023/09/03 01:36 JST |            | NO       |
+-------+----------------------+------------+----------+

スナップショットをイメージに変換

lxc publish CONTAINER_NAME/SNAPSHOT_NAME --alias my-export

## 例
root@shoma:/home/shoma# lxc publish ubuntu-test/snap0 --alias ubuntu-test

イメージのエクスポート

lxc image export my-export .

## 例
lxc image export ubuntu-test .

インポート

tarからイメージを作成

lxc image import TARBALL --alias my-export

## 例
lxc image import c253327c12cd04b03a164540d5171585df00e02cb2e72e6867ba859566b3a491.tar.gz --alias ubuntu-test

イメージからコンテナを作成

lxc init my-export NEW-CONTAINER

## 例
lxc init ubuntu-test ubuntu-new

検証

LXDコンテナ内にdockerをインストールし、ubuntuコンテナを実行してみる

apt -y install docker docker.io docker-compose

docker-compose.yml
imageのshomaigu/ubuntuはarmで動くようにリビルドしたもの

version: '3'

services:
  ubuntu1:
    image: shomaigu/ubuntu:latest
    tty: true

  ubuntu2:
    image: shomaigu/ubuntu:latest
    tty: true

起動確認

docker-compose up
docker-compose ps
CONTAINER ID   IMAGE                    COMMAND       CREATED              STATUS              PORTS     NAMES
e8837c69b4cb   shomaigu/ubuntu:latest   "/bin/bash"   About a minute ago   Up About a minute             root_ubuntu1_1
b4d535fecb9f   shomaigu/ubuntu:latest   "/bin/bash"   About a minute ago   Up About a minute             root_ubuntu2_1

これはすごい!

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2