开始学习LXD

为了达到某个预定的目标、结果或利益。

尝试在VM中处理容器时,Docker很难实现许多操作。这次我想尝试使用LXD来处理容器,类似于VM的方式。

LXD 是什么?

在Linux上运行的系统容器。与Docker相比,其行为类似于虚拟机。可以在容器中运行systemd等操作系统服务。

安装

sudo snap install lxd

添加路径

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

これはすごい!

广告
将在 10 秒后关闭
bannerAds