lxc で使うコマンドをメモする
環境
host: Ubuntu Server 20.04
lxc: 4.0.9
Install lxd
sudo apt install snap sudo snap install lxd
初期設定
lxd init
英語でいろいろと聞かれるので、回答する
おすすめストレージバックエンドについての公式文書
sample
takashi@mini01:~$ lxd init # LXD クラスタリングする? Would you like to use LXD clustering? (yes/no) [default=no]: no # ストレージプール を新しく作る? Do you want to configure a new storage pool? (yes/no) [default=yes]: yes # ストレージプールの名前は? Name of the new storage pool [default=default]: default # ストレージプールのバックエンドは?(公式の推奨は zfs のようだ) Name of the storage backend to use (btrfs, dir, lvm, zfs, ceph) [default=zfs]: zfs # ZFS プールをつくる? Create a new ZFS pool? (yes/no) [default=yes]: yes # 既存の空のブロックデバイスを使う? Would you like to use an existing empty block device (e.g. a disk or partition)? (yes/no) [default=no]: no # 新しい プールデバイス の容量は? Size in GB of the new loop device (1GB minimum) [default=30GB]: 20GB # MAAS はつかう? Would you like to connect to a MAAS server? (yes/no) [default=no]: no # ネットワークブリッジを作る? Would you like to create a new local network bridge? (yes/no) [default=yes]: yes # 新しく作るネットワークブリッジの名前は? What should the new bridge be called? [default=lxdbr0]: lxdbr0 # IPv4 アドレスつかう? What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: auto # IPv6 アドレスつかう? What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: none # LXD をネットワーク越しでつかう? Would you like the LXD server to be available over the network? (yes/no) [default=no]: yes # どの LXD にアドレスを紐付ける? Address to bind LXD to (not including port) [default=all]: all # どのポートを LXD に紐付ける? 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] yes # ここまでの設定内容を YAML で表示する? Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]: yes config: core.https_address: '[::]:8443' core.trust_password: ******* networks: - config: ipv4.address: auto ipv6.address: none description: "" name: lxdbr0 type: "" storage_pools: - config: size: 20GB description: "" name: default driver: zfs profiles: - config: {} description: "" devices: eth0: name: eth0 network: lxdbr0 type: nic root: path: / pool: default type: disk name: default cluster: null takashi@mini01:~$
version 確認
lxc version
インスタンスリストを確認したい
lxc list
image のリストを確認したい
lxc image list
取り急ぎ全部デフォルト値で起動したい
lxc launch <image>:<version> <instance_name> # sample # for Ubuntu lxc launch ubuntu:20.04 container1 # for other distributions lxc launch images:centos/7 container1
instance の start, stop, restart, delete
lxc start <instance_name> lxc stop <instance_name> lxc restart <instance_name> # delete は stop してから lxc delete <instance_name>
インスタンスの中に入る
lxc shell <instance_name> # コンソールで入れる lxc console <instance_name> # 出る時は、 ctrl-a, q
インスタンスの外から、インスタンス内部のコマンドを実行する
lxc exec <instance_name> -- <command> lxc exec test -- ip -c a
host と guest (instance) 間のファイルのやり取り
# host to guest lxc file push <source_path> <instance_name>/<path> lxc file push /etc/hosts test-instance/etc/hosts # guest to host lxc file pull <instance_name>/<path> <source_path> lxc file pull test-instance/opt/pki/rootCA/root.crt /path/to/download/target
/
が区切りかと思い、/~/somefile
を試したところ、エラーになる
フルパスでやるべき
takashi@mini01:~$ lxc exec test -- touch somefile takashi@mini01:~$ lxc exec test -- ls snap somefile takashi@mini01:~$ lxc exec test -- pwd /root takashi@mini01:~$ lxc file pull test/~/somefile . Error: Not Found takashi@mini01:~$ lxc file pull test~/somefile . Error: Not Found takashi@mini01:~$ lxc file pull test/root/somefile . takashi@mini01:~$ ls somefile somefile takashi@mini01:~$
ストレージ
list, show, delete
lxc storage list lxc storage show <pool> lxc storage delete <pool>
create
lxc storage create <pool> <driver> [options key=value ...] lxc storage create test-pool zfs size=10GB
network
list, show, delete
lxc network list lxc network show <network> lxc network delete <network>
create
実はあんまり使わない
profile を定義して、launch してあげると勝手に出来るからである
任意のネットワークアドレスを持つブリッジを作る時は役に立つかも
lxc network create <network> lxc network create test lxc network create test \ ipv4.address="10.0.0.1/24" ipv6.address="none" ipv6.nat="false"
sample
takashi@mini01:~$ lxc network create test ipv4.address="10.0.0.1/24" ipv6.address="none" ipv6.nat="false" Network test created takashi@mini01:~$ takashi@mini01:~$ lxc network show test config: ipv4.address: 10.0.0.1/24 ipv6.address: none ipv6.nat: "false" description: "" name: test type: bridge used_by: [] managed: true status: Created locations: - none takashi@mini01:~$
profile
list, show, delete
lxc profile list lxc profile show <profile> lxc profile delete <profile>
profile create
lxc profile create <profile>
profile に device を追加する
syntax
lxc profile device add <instance_name> <device_name> <device_type> [options <key>=<value>...]
network
macvlan を作って起動するとき
lxc profile device add test-profile \ eth0 \ nic nictype=macvlan parent=eth0 name=eth0
既に作成された network を指定するとき
lxc profile device add test-profile \ eth0 \ nic network=<network> name=eth0
sample
takashi@mini01:~$ lxc profile device add test-profile eth0 nic network=test name=eth0 Device eth0 added to test-profile takashi@mini01:~$ takashi@mini01:~$ lxc profile show test-profile config: {} description: "" devices: eth0: name: eth0 network: test type: nic name: test-profile used_by: [] takashi@mini01:~$
storage
lxc profile device add test-profile \ root \ disk path=/ pool=test-pool
profile から device を削除する
lxc profile device remove <profile> <device_name>
sample
takashi@mini01:~$ lxc profile device remove test-profile eth0 Device eth0 removed from test-profile takashi@mini01:~$ takashi@mini01:~$ lxc profile show test-profile config: {} description: "" devices: {} name: test-profile used_by: [] takashi@mini01:~$
プロファイルをコピーして編集
default とか profile をコピーして編集したほうが速いときもある
lxc profile copy <profile from> <profile to> lxc profile copy default test-profile lxc profile edit test-profile
もし、エディタが任意のエディタではなかった場合
$EDITOR
を参照しているのでログイン時に環境変数に設定できるようにする
echo 'export EDITOR=vim' >> ~/.profile . ~/.profile
作成したプロファイルを設定した上で launch
lxc launch <image>:<name> <instance_name> --profile <基本となるプロファイル> --profile <部分プロファイル> lxc launch ubuntu:20.04 c1 --profile default --profile test-profile
port forward (proxy?)
追加
lxc config device add <instance_name> <device> <type> [options]
を利用する
lxc config device add <instance_name> <device_name> <type> \ listen=<tcp|udp>:<source_ip_address>:<source_port> \ connect=<tcp|udp>:<container_ip_address>:<container_port> # sample lxc config device add test-container http-proxy proxy listen=tcp:10.0.0.2:8080 \ connect=tcp:172.16.0.10:80
こうすると、ホストの、 http://10.0.0.2:8080/
にアクセスすると
ゲストの http://172.16.0.10/
にプロキシされる
削除
lxc config device remove <instance_name> <device_name> # sample lxc config device remove test-container http-proxy
起動したらすること(任意)
instance_name="name" lxc shell ${instance_name} timedatectl set-timezone Asia/Tokyo apt update && apt -y upgrade && apt -y autoremove apt install vim-nox update-alternatives --set editor /usr/bin/vim.nox exit lxc restart ${instance_name}
ホストOS とのファイルのやり取り
help
$ lxc file --help Description: Manage files in instances Usage: lxc file [flags] lxc file [command] Available Commands: delete Delete files in instances edit Edit files in instances pull Pull files from instances push Push files into instances Global Flags: --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 file [command] --help" for more information about a command. $
ホストからコンテナ
lxc file push /path/to/file INSTANCE_NAME/path/to/destination/ # HELP $ lxc file push --help Description: Push files into instances Usage: lxc file push <source path>... [<remote>:]<instance>/<path> [flags] Examples: lxc file push /etc/hosts foo/etc/hosts To push /etc/hosts into the instance "foo". Flags: -p, --create-dirs Create any directories necessary --gid Set the file's gid on push (default -1) --mode Set the file's perms on push -r, --recursive Recursively transfer files --uid Set the file's uid on push (default -1) Global Flags: --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 $
コンテナからホスト
lxc file pull INSTANCE_NAME/path/to/file /path/to/hostOS/destination/directory/ # INSTANCE_NAME が c1 なら lxc file pull c1/etc/hosts ~/ # HELP $ lxc file pull --help Description: Pull files from instances Usage: lxc file pull [<remote>:]<instance>/<path> [[<remote>:]<instance>/<path>...] <target path> [flags] Examples: lxc file pull foo/etc/hosts . To pull /etc/hosts from the instance and write it to the current directory. Flags: -p, --create-dirs Create any directories necessary -r, --recursive Recursively transfer files Global Flags: --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 $
今後
launch すると timezone とか設定されている!ってことが出来るようになるといいなぁ
参考
ここもみるといい
mickey-happygolucky.hatenablog.com
2022-02-27 更新
- port forward について追記
- profile に 定義済みの network を追加する方法を追記
2022-11-17 更新
lxc インスタンスとホストOS間でのファイルのやり取りについて追記