検索
Canonical LXD
記事の項目

最近の投稿

コンテナ LXD

LXD (発音lex-dee )は、軽量コンテナーハイパーバイザーです。

この記事の項目

コンテナ-LXDとは

LXD(Linuxコンテナデーモン)は、Linuxベースの仮想化技術を提供するコンテナ管理ツールおよびハイパーバイザーです。LXDはCanonicalによって開発およびサポートされており、Ubuntuの一部として提供されていますが、他のLinuxディストリビューションでも使用できます。LXDは、システムコンテナまたはマシンコンテナを作成、管理、実行するためのプロダクトです。

以下はLXDの主な特徴と用途です:

  1. コンテナ仮想化:LXDはLinuxコンテナ仮想化を提供し、アプリケーションやサービスを軽量なコンテナ内で実行することができます。これにより、アプリケーション間のリソース分離が容易に行え、環境の隔離が実現できます。

  2. システムコンテナ:LXDはシステムコンテナとしても使用でき、フルシステムを仮想化することが可能です。これにより、仮想マシンに近い隔離とリソース制御が提供されます。

  3. ライブマイグレーション:LXDは仮想マシンやコンテナのライブマイグレーションをサポートし、アプリケーションやサービスを中断せずに仮想マシンを別のホストに移動できます。

  4. ストレージ管理:LXDは柔軟なストレージ管理を提供し、ストレージバックエンドとしてZFS、btrfs、LVMなどをサポートしています。

  5. リモート管理:LXDはリモートでコンテナや仮想マシンを管理するためのCLIおよびRESTful APIを提供し、遠隔地からリソースの管理や監視が可能です。

  6. クラウド対応:LXDはクラウドインフラストラクチャと統合でき、大規模なコンテナオーケストレーション環境で使用することもできます。

LXDは、特に開発、テスト、デプロイメント、サーバー仮想化、コンテナオーケストレーションなどのさまざまなシナリオで使用されます。LXDを使用することで、効率的なリソース利用、アプリケーションの隔離、セキュリティの向上などの利点を得ることができます。

システム全体をコンテナ化

LXDのインストール

Ubuntu 22.04 Server の標準インストールしたなら、最初から LXD がインストールされています。

もしも、インストールされていないのであれば、

				
					$ sudo snap install lxd

[sudo] password for user: 
lxd 5.17-e5ead86 from Canonical✓ installed
				
			

LXDの初期化

LXDをデフォルトで初期化するには、

				
					$ sudo lxd init --auto
				
			

LXDを個別に設定を変更することも可能だ。

今回は
Would you like the LXD server to be available over the network? (yes/no) [default=no]: yes
とした。

				
					$ 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 (cephobject, dir, lvm, zfs, btrfs, ceph) [default=zfs]: 
Create a new ZFS pool? (yes/no) [default=yes]: 
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=19GiB]: 
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]: 
Would you like the LXD server to be available over the network? (yes/no) [default=no]:  yes
Address to bind LXD to (not including port) [default=all]: 
Port to bind LXD to [default=8443]: 
Trust password for new clients: 
Again: 
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]:
				
			

Would you like the LXD server to be available over the network? (yes/no) [default=no]: yes <— ここ
Address to bind LXD to (not including port) [default=all]:
Port to bind LXD to [default=8443]:
Trust password for new clients:
Again:

WEBブラウザで [ IP アドレス : 8443 ] を入力して、ここで設定したパスワードで表示させることができる。

LXDで使えるイメージを探す

CPU アーキテクチャ amd64 で絞ってもたくさんのイメージが表示されます

※ コンテナの操作は「lxc」なので注意

				
					$ lxc image list images: amd64
				
			

CPU アーキテクチャ amd64 の中の ubuntuのイメージを検索してみます。

				
					$ lxc image list ubuntu: amd64

+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
|   ALIAS    | FINGERPRINT  | PUBLIC |                 DESCRIPTION                 | ARCHITECTURE |      TYPE       |   SIZE   |          UPLOAD DATE          |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| a (5 more) | 2d53824fdf89 | yes    | ubuntu 17.10 amd64 (release) (20180706)     | x86_64       | CONTAINER       | 169.51MB | Jul 6, 2018 at 12:00am (UTC)  |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| a (5 more) | 34bae4293007 | yes    | ubuntu 17.10 amd64 (release) (20180706)     | x86_64       | VIRTUAL-MACHINE | 307.06MB | Jul 6, 2018 at 12:00am (UTC)  |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| b (5 more) | 8d21d7ed8198 | yes    | ubuntu 18.04 LTS amd64 (release) (20230607) | x86_64       | VIRTUAL-MACHINE | 386.88MB | Jun 7, 2023 at 12:00am (UTC)  |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| b (5 more) | c533845b5db1 | yes    | ubuntu 18.04 LTS amd64 (release) (20230607) | x86_64       | CONTAINER       | 215.55MB | Jun 7, 2023 at 12:00am (UTC)  |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| c (5 more) | 49a822c3585c | yes    | ubuntu 18.10 amd64 (release) (20190628)     | x86_64       | VIRTUAL-MACHINE | 458.06MB | Jun 28, 2019 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| c (5 more) | 091b1efac72f | yes    | ubuntu 18.10 amd64 (release) (20190628)     | x86_64       | CONTAINER       | 310.07MB | Jun 28, 2019 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| d (5 more) | 1acbeb25ce62 | yes    | ubuntu 19.04 amd64 (release) (20200109)     | x86_64       | VIRTUAL-MACHINE | 476.81MB | Jan 9, 2020 at 12:00am (UTC)  |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| d (5 more) | acb8f742bfff | yes    | ubuntu 19.04 amd64 (release) (20200109)     | x86_64       | CONTAINER       | 322.61MB | Jan 9, 2020 at 12:00am (UTC)  |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| e (5 more) | 56258f5eb5b7 | yes    | ubuntu 19.10 amd64 (release) (20200717)     | x86_64       | VIRTUAL-MACHINE | 498.94MB | Jul 17, 2020 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| e (5 more) | e1f280d082da | yes    | ubuntu 19.10 amd64 (release) (20200717)     | x86_64       | CONTAINER       | 353.03MB | Jul 17, 2020 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| f (5 more) | 91c6a5919ebf | yes    | ubuntu 20.04 LTS amd64 (release) (20230811) | x86_64       | CONTAINER       | 437.45MB | Aug 11, 2023 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| f (5 more) | b3eb8a78c93f | yes    | ubuntu 20.04 LTS amd64 (release) (20230811) | x86_64       | VIRTUAL-MACHINE | 603.06MB | Aug 11, 2023 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| g (5 more) | 96234c60d652 | yes    | ubuntu 20.10 amd64 (release) (20210720)     | x86_64       | CONTAINER       | 373.50MB | Jul 20, 2021 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| g (5 more) | f73dd8e169a0 | yes    | ubuntu 20.10 amd64 (release) (20210720)     | x86_64       | VIRTUAL-MACHINE | 529.56MB | Jul 20, 2021 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| h (5 more) | 1b4e4d326603 | yes    | ubuntu 21.04 amd64 (release) (20220118)     | x86_64       | CONTAINER       | 384.55MB | Jan 18, 2022 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| h (5 more) | 740498eb4f97 | yes    | ubuntu 21.04 amd64 (release) (20220118)     | x86_64       | VIRTUAL-MACHINE | 547.69MB | Jan 18, 2022 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| i (5 more) | a2ea1199cec1 | yes    | ubuntu 21.10 amd64 (release) (20220708)     | x86_64       | CONTAINER       | 415.56MB | Jul 8, 2022 at 12:00am (UTC)  |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| i (5 more) | cfe9ee7e0201 | yes    | ubuntu 21.10 amd64 (release) (20220708)     | x86_64       | VIRTUAL-MACHINE | 574.88MB | Jul 8, 2022 at 12:00am (UTC)  |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| j (9 more) | ba714c2aca06 | yes    | ubuntu 22.04 LTS amd64 (release) (20230828) | x86_64       | VIRTUAL-MACHINE | 611.13MB | Aug 28, 2023 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| j (9 more) | c2f7dd935a86 | yes    | ubuntu 22.04 LTS amd64 (release) (20230828) | x86_64       | CONTAINER       | 434.09MB | Aug 28, 2023 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| k (5 more) | 98e520bf51c2 | yes    | ubuntu 22.10 amd64 (release) (20230716)     | x86_64       | CONTAINER       | 519.32MB | Jul 16, 2023 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| k (5 more) | d20bc44888cc | yes    | ubuntu 22.10 amd64 (release) (20230716)     | x86_64       | VIRTUAL-MACHINE | 706.25MB | Jul 16, 2023 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| l (5 more) | 6c9d6abf2cc9 | yes    | ubuntu 23.04 amd64 (release) (20230829)     | x86_64       | VIRTUAL-MACHINE | 709.13MB | Aug 29, 2023 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| l (5 more) | 09557a3d5244 | yes    | ubuntu 23.04 amd64 (release) (20230829)     | x86_64       | CONTAINER       | 520.18MB | Aug 29, 2023 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| p (5 more) | be4aa8e56eab | yes    | ubuntu 12.04 LTS amd64 (release) (20170502) | x86_64       | CONTAINER       | 152.61MB | May 2, 2017 at 12:00am (UTC)  |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| p (5 more) | c49f7d7f9305 | yes    | ubuntu 12.04 LTS amd64 (release) (20170502) | x86_64       | VIRTUAL-MACHINE | 248.63MB | May 2, 2017 at 12:00am (UTC)  |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| t (5 more) | 8a96b6b12c5b | yes    | ubuntu 14.04 LTS amd64 (release) (20191107) | x86_64       | VIRTUAL-MACHINE | 260.31MB | Nov 7, 2019 at 12:00am (UTC)  |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| t (5 more) | 97ca81741254 | yes    | ubuntu 14.04 LTS amd64 (release) (20191107) | x86_64       | CONTAINER       | 122.87MB | Nov 7, 2019 at 12:00am (UTC)  |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| u (5 more) | 31b05f404241 | yes    | ubuntu 14.10 amd64 (release) (20150723)     | x86_64       | CONTAINER       | 123.58MB | Jul 23, 2015 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| u (5 more) | d19a3c146864 | yes    | ubuntu 14.10 amd64 (release) (20150723)     | x86_64       | VIRTUAL-MACHINE | 264.00MB | Jul 23, 2015 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| v (5 more) | 778094462409 | yes    | ubuntu 15.04 amd64 (release) (20160203)     | x86_64       | CONTAINER       | 132.90MB | Feb 3, 2016 at 12:00am (UTC)  |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| v (5 more) | cfe5b5f07cb8 | yes    | ubuntu 15.04 amd64 (release) (20160203)     | x86_64       | VIRTUAL-MACHINE | 281.44MB | Feb 3, 2016 at 12:00am (UTC)  |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| w (5 more) | 4ea51b888084 | yes    | ubuntu 15.10 amd64 (release) (20160715)     | x86_64       | VIRTUAL-MACHINE | 317.50MB | Jul 15, 2016 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| w (5 more) | 9b9de680184b | yes    | ubuntu 15.10 amd64 (release) (20160715)     | x86_64       | CONTAINER       | 154.62MB | Jul 15, 2016 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| x (5 more) | 712a58368655 | yes    | ubuntu 16.04 LTS amd64 (release) (20211001) | x86_64       | CONTAINER       | 171.55MB | Oct 1, 2021 at 12:00am (UTC)  |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| x (5 more) | fdfe665ab748 | yes    | ubuntu 16.04 LTS amd64 (release) (20211001) | x86_64       | VIRTUAL-MACHINE | 310.50MB | Oct 1, 2021 at 12:00am (UTC)  |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| y (5 more) | 38431cacc797 | yes    | ubuntu 16.10 amd64 (release) (20170719)     | x86_64       | CONTAINER       | 157.68MB | Jul 19, 2017 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| y (5 more) | c373cdc01d84 | yes    | ubuntu 16.10 amd64 (release) (20170719)     | x86_64       | VIRTUAL-MACHINE | 330.38MB | Jul 19, 2017 at 12:00am (UTC) |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| z (5 more) | 04d36575235a | yes    | ubuntu 17.04 amd64 (release) (20171208)     | x86_64       | CONTAINER       | 161.72MB | Dec 8, 2017 at 12:00am (UTC)  |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+
| z (5 more) | 99f34e8501d8 | yes    | ubuntu 17.04 amd64 (release) (20171208)     | x86_64       | VIRTUAL-MACHINE | 335.69MB | Dec 8, 2017 at 12:00am (UTC)  |
+------------+--------------+--------+---------------------------------------------+--------------+-----------------+----------+-------------------------------+

				
			

Ubuntu22.04を使ってみましょう

イメージを取得してコンテナーインスタンスを起動してみます

ubuntu server 22.04 LTS のイメージを取得し、u2204 という名前のインスタンスを作成、起動実行します。

				
					 $ lxc launch ubuntu:22.04 u2204
 
Creating u2204
Starting u2204 
				
			
				
					 $ lxc list            
+-------+---------+----------------------+----------------------------------------------+-----------+-----------+
| NAME  |  STATE  |         IPV4         |                     IPV6                     |   TYPE    | SNAPSHOTS |
+-------+---------+----------------------+----------------------------------------------+-----------+-----------+
| focal | RUNNING | 10.43.211.178 (eth0) | fd42:e13:ca55:f008:216:3eff:fe9b:ac58 (eth0) | CONTAINER | 0         |
+-------+---------+----------------------+----------------------------------------------+-----------+-----------+
				
			

STATUS は RUNNING になっています。

起動した focal で shell を動かしてみます。

				
					$ lxc shell focal

root@focal:~# 

				
			

コンテナ内で何が動作しているのかみてみましょう。

				
					# ps -ef

UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 01:14 ?        00:00:00 /sbin/init
root          43       1  0 01:14 ?        00:00:00 /lib/systemd/systemd-journald
root          78       1  0 01:14 ?        00:00:00 /lib/systemd/systemd-udevd
systemd+      91       1  0 01:14 ?        00:00:00 /lib/systemd/systemd-networkd
root         107       1  0 01:14 ?        00:00:00 /usr/sbin/cron -f -P
message+     108       1  0 01:14 ?        00:00:00 @dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation --syslog-only
root         111       1  0 01:14 ?        00:00:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
syslog       112       1  0 01:14 ?        00:00:00 /usr/sbin/rsyslogd -n -iNONE
root         113       1  0 01:14 ?        00:00:00 /lib/systemd/systemd-logind
systemd+     114       1  0 01:14 ?        00:00:00 /lib/systemd/systemd-resolved
root         123       1  0 01:14 pts/0    00:00:00 /sbin/agetty -o -p -- \u --noclear --keep-baud console 115200,38400,9600 linux
root         230       0  0 01:17 pts/0    00:00:00 bash
root         241     230  0 01:18 pts/0    00:00:00 ps -ef

				
			

コンテナのシェルを止めるのは、普通に exit で logout される。

				
					root@focal:~# exit
				
			

さらに動作中のコンテナ focal を停止させるには

				
					$ lxc stop focal
				
			

2回目以降、コンテナを動作させるのは、start を使用する。

ホストOS を再起動すると LXDのコンテナは自動起動するようです。

				
					$ lxc start focal
				
			

lxc の help を見てみましょう。

				
					$ lxc -h

Description:
  Command line client for LXD

  All of LXD's features can be driven through the various commands below.
  For help with any of those, simply call them with --help.

Usage:
  lxc [command]

Available Commands:
  alias       Manage command aliases
  cluster     Manage cluster members
  config      Manage instance and server configuration options
  console     Attach to instance consoles
  copy        Copy instances within or in between LXD servers
  delete      Delete instances and snapshots
  exec        Execute commands in instances
  export      Export instance backups
  file        Manage files in instances
  help        Help about any command
  image       Manage images
  import      Import instance backups
  info        Show instance or server information
  launch      Create and start instances from images
  list        List instances
  move        Move instances within or in between LXD servers
  network     Manage and attach instances to networks
  operation   List, show and delete background operations
  profile     Manage profiles
  project     Manage projects
  publish     Publish instances as images
  remote      Manage the list of remote servers
  rename      Rename instances and snapshots
  restart     Restart instances
  restore     Restore instances from snapshots
  snapshot    Create instance snapshots
  start       Start instances
  stop        Stop instances
  storage     Manage storage pools and volumes
  version     Show local and remote versions
  warning     Manage warnings

Flags:
      --all            Show less common commands
      --debug          Show all debug messages
      --force-local    Force using the local unix socket
  -h, --help           Print help
      --project        Override the source project
  -q, --quiet          Don't show progress information
      --sub-commands   Use with help or --help to view sub-commands
  -v, --verbose        Show all information messages
      --version        Print version number

Use "lxc [command] --help" for more information about a command.
				
			

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

関連記事

Redis

$ sudo apt update $ sudo