Netplan 配置的 LXD 主机在重启时不会恢复网络配置

Netplan 配置的 LXD 主机在重启时不会恢复网络配置

我有一台 17.10 服务器,通过 snap(2 个容器)安装了 LXD。

我已将主机配置为具有静态 IP,并允许从 LAN 访问容器。

我的 01-netcfg.yaml 在 /etc/netplan 中:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp1s0:
      dhcp4: no
      dhcp6: no
  bridges:
    lxdbr0:
      dhcp4: no
      interfaces:
        - enp1s0
      addresses: [192.168.0.10/24]
      gateway4: 192.168.0.1
      nameservers:
        addresses: [192.168.0.1,8.8.8.8,8.8.4.4]
      parameters:
        stp: false
        forward-delay: 0

运行 sudo netplan apply 后一切正常,我可以从主机访问互联网。

ifconfig 给出以下内容:

enp1s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        ether 2c:4d:54:4f:33:30  txqueuelen 1000  (Ethernet)
        RX packets 5412  bytes 5196030 (5.1 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4483  bytes 624400 (624.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 222  bytes 19478 (19.4 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 222  bytes 19478 (19.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lxdbr0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.10  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::60c5:c9ff:fee3:a28f  prefixlen 64  scopeid 0x20<link>
        ether 62:c5:c9:e3:a2:8f  txqueuelen 1000  (Ethernet)
        RX packets 2646  bytes 1973322 (1.9 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2640  bytes 332354 (332.3 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethAUK196: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::fca4:95ff:fed5:a908  prefixlen 64  scopeid 0x20<link>
        ether fe:a4:95:d5:a9:08  txqueuelen 1000  (Ethernet)
        RX packets 1787  bytes 291994 (291.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 2916  bytes 3204729 (3.2 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethBY974H: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::fccc:7dff:fe51:3ae6  prefixlen 64  scopeid 0x20<link>
        ether fe:cc:7d:51:3a:e6  txqueuelen 1000  (Ethernet)
        RX packets 23  bytes 1958 (1.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 326  bytes 26711 (26.7 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

vethP62W14: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet6 fe80::fce2:c7ff:fe4d:d7da  prefixlen 64  scopeid 0x20<link>
        ether fe:e2:c7:4d:d7:da  txqueuelen 1000  (Ethernet)
        RX packets 46  bytes 3508 (3.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 317  bytes 26407 (26.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

重启后,主机中没有互联网(但是,LAN 子网内 ping 可以,容器中的互联网可以正常使用)。

ifconfig 显示的内容完全相同,只是 lxdbr0 广播 0.0.0.0 而不是原始值广播 192.168.0.255。

重启后运行 sudo netplan apply 后它会再次开始工作,并且 lxdbr0 的广播再次填充工作值。

有任何想法吗?

更新:安装了 NetworkManager 并尝试将其用作渲染器 - 没有效果,一切都像使用 networkd 一样工作 :(

答案1

复制于解决方案作为答案,因为我第一次看到这个(在评论中)时错过了它,感谢@user4124,将其标记为社区维基。

安装 LXD(仅限主机)

sudo apt remove --purge lxd lxd-client && apt install bridge-utils
sudo snap install lxd
sudo reboot
sudo lxd init

安装时,不要创建网桥。

设置主机网络

sudo nano /etc/netplan/01-netcfg.yaml

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    enp1s0:
      dhcp4: no
      dhcp6: no
  bridges:
    br0:
      dhcp4: no
      dhcp6: no
      interfaces:
        - enp1s0
      addresses: [ 192.168.0.2/24 ]
      gateway4: 192.168.0.1
      nameservers:
          addresses:
              - 192.168.0.1
              - 8.8.8.8
              - 8.8.4.4
      parameters:
          stp: false
          forward-delay: 0

sudo netplan --debug apply

重新启动并确认配置正确:

sudo reboot
ifconfig -a
ping google.com

编辑默认配置文件以确保配置中仅存在桥接器,没有额外的网卡

sudo lxc profile show default // add '> out.yaml' to output to file
sudo lxc profile edit default // add '< out.yaml' to read from file

示例 out.yaml:

### This is a yaml representation of the profile.
### Any line starting with a '# will be ignored.
###
### A profile consists of a set of configuration items followed by a set of
### devices.
###
### An example would look like:
### name: onenic
### config:
###   raw.lxc: lxc.aa_profile=unconfined
### devices:
###   eth0:
###     nictype: bridged
###     parent: lxdbr0
###     type: nic
###
### Note that the name is shown but cannot be changed

config: {}
description: Default LXD profile
devices:
  br0:
    nictype: bridged
    parent: br0
    type: nic
  root:
    path: /
    pool: default
    type: disk
name: default
used_by:
- /1.0/containers/apache

为容器设置网络

跑步:

sudo lxc exec <container> bash
nano /etc/netplan/50-cloud-init.yaml

插入:

network:
  version: 2
  ethernets:
    eth0:
      dhcp4: no
      dhcp6: no
      addresses:
        - 192.168.0.5/24
      gateway4: 192.168.0.1
      nameservers:
        addresses:
          - 192.168.0.1
          - 8.8.8.8

    netplan --debug apply

相关内容