Linux

在 linux 容器 (LXC) 介面和主機之間配置橋接

  • March 22, 2013

我想在一個主機下執行 2 個 LXC 容器,並且我希望每個容器在網路上都有自己的 IP,假設主機有 10.10.1.10,容器有 10.10.1.11 和 10.10.1.12。

我的設置是否要求我在 hosts /etc/network/interfaces 文件中配置 2 個橋接介面,如下所示:

auto br0
iface br0 inet static
  address 10.10.1.11 #ip of our host
  netmask 255.255.255.0 #subnet of our host
  network 10.10.1.0 #network of our host
  broadcast 10.10.1.255 #our broadcast address
  gateway 10.10.1.1 #default gw for our host to access the internet
  bridge_ports eth0
  bridge_fd 9
  bridge_hello 2
  bridge_maxage 12
  bridge_stp off

auto br1
iface br0 inet static
  address 10.10.1.12 #ip of our host
  netmask 255.255.255.0 #subnet of our host
  network 10.10.1.0 #network of our host
  broadcast 10.10.1.255 #our broadcast address
  gateway 10.0.1.1 #default gw for our host to access the internet
  bridge_ports eth0
  bridge_fd 9
  bridge_hello 2
  bridge_maxage 12
  bridge_stp off

或者是否有另一種方式來配置主機,更像是一個開關,並允許各個容器配置自己的 IP (這會更安全,所以我們並不總是在移動容器時更改介面文件)。

在主機上使用一個網橋。

LXC 配置文件規定來賓/容器 IP 定址將是什麼。

# ifconfig 
br0       Link encap:Ethernet  HWaddr 22:C7:47:62:9C:02  
         inet addr:10.2.0.6  Bcast:10.2.127.255  Mask:255.255.128.0
         inet6 addr: fe80::e611:5bff:febe:b62a/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:1756184372 errors:0 dropped:0 overruns:0 frame:0
         TX packets:470262588 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:0 
         RX bytes:112684582109 (104.9 GiB)  TX bytes:657886062154 (612.7 GiB)

還有一個範例 LXC 配置文件…

# cat /etc/lxc/Analytics.conf 
lxc.utsname = Analytics
lxc.tty = 4
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = br0
lxc.network.name = eth0
lxc.network.mtu = 1500
lxc.network.ipv4 = 10.2.0.80/17
lxc.network.hwaddr = 00:30:6E:08:EC:80
lxc.rootfs = /srv/lxc/Analytics
lxc.mount = /etc/lxc/Analytics.fstab
lxc.cgroup.devices.deny = a
# /dev/null and zero
lxc.cgroup.devices.allow = c 1:3 rwm
lxc.cgroup.devices.allow = c 1:5 rwm
# consoles
lxc.cgroup.devices.allow = c 5:1 rwm
lxc.cgroup.devices.allow = c 5:0 rwm
lxc.cgroup.devices.allow = c 4:0 rwm
lxc.cgroup.devices.allow = c 4:1 rwm
# /dev/{,u}random
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 1:8 rwm
# /dev/pts/*
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
# rtc
lxc.cgroup.devices.allow = c 254:0 rwm

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