目的
lxc で cloud-init の network-config version 2 を使って IP アドレスを設定する方法を備忘録として残す
環境
ホストOS: Ubuntu Server 22.04 LTS
LXC 5.0.0
ゲストOS: Ubuntu 22.04
参考資料
まずはデフォルトの挙動を再確認する
lxc launch ubuntu:22.04 c1-default
確認
[~]$lxc launch ubuntu:22.04 c1-default Creating c1-default Starting c1-default [~]$ [~]$lxc exec c1-default -- ip -c a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 14: eth0@if15: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether 00:16:3e:8d:c5:a5 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 10.21.161.188/24 metric 100 brd 10.21.161.255 scope global dynamic eth0 valid_lft 3571sec preferred_lft 3571sec inet6 fe80::216:3eff:fe8d:c5a5/64 scope link valid_lft forever preferred_lft forever [~]$ [~]$lxc exec c1-default -- resolvectl dns Global: Link 14 (eth0): 10.21.161.1 [~]$ [~]$lxc exec c1-default -- cat /etc/netplan/50-cloud-init.yaml # This file is generated from information provided by the datasource. Changes # to it will not persist across an instance reboot. To disable cloud-init's # network configuration capabilities, write a file # /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following: # network: {config: disabled} network: version: 2 ethernets: eth0: dhcp4: true [~]$
cloud-init を使う
設定情報を作る
dhcp4 だけ有効にし、 IPv6 を利用せず、DNS を Google に向ける設定
Ubuntu の場合 netplan
を使用していることでしょう
network-config
の version 2
は netplan
との互換性があります
そのため netplan
の書式で書けばよいので楽です
config: user.network-config: | version: 2 ethernets: eth0: dhcp4: true dhcp6: false link-local: [] nameservers: addresses: - 8.8.8.8 - 8.8.4.4 search: []
設定情報をテキストファイルにする
cat << 'EOL' > c1.custom.yaml config: user.network-config: | version: 2 ethernets: eth0: dhcp4: true dhcp6: false link-local: [] nameservers: addresses: - 8.8.8.8 - 8.8.4.4 search: [] EOL
新規プロファイルを作る
lxc profile create c1.custom
一応中身を見る
[~]$lxc profile show c1.custom config: {} description: "" devices: {} name: c1.custom used_by: [] [~]$
c1.custom
プロファイルにネットワーク設定を入れる
cat c1.custom.yaml | lxc profile edit c1.custom
確認
[~]$lxc profile show c1.custom config: user.network-config: | version: 2 ethernets: eth0: dhcp4: true dhcp6: false link-local: [] nameservers: addresses: - 8.8.8.8 - 8.8.4.4 search: [] description: "" devices: {} name: c1.custom used_by: [] [~]$
うまいこと config
セクションに入っている。パースして、入っていっているのだろう
launch
lxc launch ubuntu:22.04 c1-custom --profile default --profile c1.custom
cloud-init の status は cloud-init status
で確認できる
cloud-init はインスタンス内で実行されるので、 exec
コマンドで見るのがいいだろう
lxc exec c1-custom -- cloud-init status
log
[~]$lxc launch ubuntu:22.04 c1-custom --profile default --profile c1.custom Creating c1-custom Starting c1-custom [~]$lxc exec c1-custom -- cloud-init status status: running [~]$lxc exec c1-custom -- cloud-init status status: done [~]$
確認しよう
lxc exec c1-custom -- ip -c a lxc exec c1-custom -- resolvectl dns lxc exec c1-custom -- cat /etc/netplan/50-cloud-init.yaml
log
[~]$lxc exec c1-custom -- ip -c a; resolvectl dns 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 16: eth0@if17: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000 link/ether 00:16:3e:ca:4f:93 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 10.21.161.116/24 metric 100 brd 10.21.161.255 scope global dynamic eth0 valid_lft 3493sec preferred_lft 3493sec [~]$ [~]$lxc exec c1-custom -- resolvectl dns Global: Link 16 (eth0): 8.8.8.8 8.8.4.4 10.21.161.1 [~]$ [~]$lxc exec c1-custom -- cat /etc/netplan/50-cloud-init.yaml # This file is generated from information provided by the datasource. Changes # to it will not persist across an instance reboot. To disable cloud-init's # network configuration capabilities, write a file # /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following: # network: {config: disabled} network: ethernets: eth0: dhcp4: true dhcp6: false link-local: [] nameservers: addresses: - 8.8.8.8 - 8.8.4.4 search: [] version: 2 [~]$
ちゃんと想定通りの設定が入っています。
これで インスタンス のネットワーク設定も cloud-init に内包できますね。
以上です。