Linux

lxc-net 不喜歡橋接容器的靜態 ip

  • February 10, 2017

我剛剛開始使用 LXC 容器,我發現即使我在 lxc 容器配置文件上設置了一個特定的 IP 地址,lxc-ls --fancy也總是顯示另一個 IP,它是可 ping 的,並且/etc/network/interfaces設置不會覆蓋lxc-net設置。

我正在使用lxc2.0.7。

容器配置:

lxc.network.type = veth
lxc.network.hwaddr = 00:1a:b2:ff:62:32
lxc.network.link = lxcbr0
lxc.network.ipv4 = 10.0.3.2/24
lxc.network.ipv4.gateway = 10.0.3.1
lxc.network.flags = up

lxc.rootfs = /var/lib/lxc/debcontainer/rootfs
lxc.rootfs.backend = dir

# Common configuration
lxc.include = /usr/share/lxc/config/debian.common.conf

# Container specific configuration
lxc.tty = 4
lxc.utsname = debcontainer
lxc.arch = amd64

介面配置:

auto eth0

iface eth0 inet static
address 10.0.3.3
netmask 255.255.255.0
gateway 10.0.3.1

主機介面:

eth0      Link encap:Ethernet  HWaddr XX:XX:XX:XX:XX:XX
         ...

lo        Link encap:Local Loopback
         ...

lxcbr0    Link encap:Ethernet  HWaddr 00:16:3e:00:00:00  
         inet addr:10.0.3.1  Bcast:0.0.0.0  Mask:255.255.255.0
         inet6 addr: fe80::216:3eff:fe00:0/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:137 errors:0 dropped:0 overruns:0 frame:0
         TX packets:131 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000 
         RX bytes:16641 (16.2 KiB)  TX bytes:14826 (14.4 KiB)

vethE2RLT9 Link encap:Ethernet  HWaddr fe:66:28:6b:1a:f1  
         inet6 addr: fe80::fc66:28ff:fe6b:1af1/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:137 errors:0 dropped:0 overruns:0 frame:0
         TX packets:161 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000 
         RX bytes:18559 (18.1 KiB)  TX bytes:19428 (18.9 KiB)

容器配置:

eth0      Link encap:Ethernet  HWaddr 00:1a:b2:ff:62:32  
         inet addr:10.0.3.2  Bcast:255.255.255.255  Mask:255.255.255.255
         inet6 addr: fe80::216:abff:fec4:63ff/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:170 errors:0 dropped:0 overruns:0 frame:0
         TX packets:147 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000 
         RX bytes:20061 (19.5 KiB)  TX bytes:20259 (19.7 KiB)

lo        Link encap:Local Loopback
         ...

lxc-fs輸出:

user@debian-8-test:~$ sudo lxc-ls --fancy
NAME         STATE   AUTOSTART GROUPS IPV4                 IPV6 
debcontainer RUNNING 0         -      10.0.3.2, 10.0.3.211 -

解決方案

編輯 /etc/default/lxc-net 並註釋或刪除 LXC 網橋名稱(如果指定)和 dhcp 選項:

USE_LXC_BRIDGE="true"
#LXC_BRIDGE="lxcbr0"
LXC_ADDR="10.0.3.1"
LXC_NETMASK="255.255.255.0"
LXC_NETWORK="10.0.3.0/24"
#LXC_DHCP_RANGE="10.0.3.2,10.0.3.254"
#LXC_DHCP_MAX="253"
#LXC_DHCP_CONFILE=""
LXC_DOMAIN=""

編輯/etc/network/interfaces和設置靜態配置:

auto eth0
iface eth0 inet static
   address 10.0.3.3
   gateway 10.0.3.1
   netmask 255.255.255.0

或者

設置網路配置<container>/config

lxc.network.type = veth
lxc.network.hwaddr = 00:1a:b2:ff:62:32
lxc.network.link = lxcbr0
lxc.network.ipv4 = 10.0.3.2/24
lxc.network.ipv4.gateway = 10.0.3.1
lxc.network.flags = up

並設置手動界面配置/etc/network/interfaces

auto eth0
iface eth0 inet manual

引用自:https://serverfault.com/questions/830776