LXC 与 Open vSwitch

Posted

技术标签:

【中文标题】LXC 与 Open vSwitch【英文标题】:LXC with Open vSwitch 【发布时间】:2016-03-05 09:57:45 【问题描述】:

我想尝试 OVS(软件 Linux 交换机)http://openvswitch.org/ 与我的 LXC 容器,Ubuntu 作为主机和来宾。所以我已经安装了它:

# apt-get install openvswitch-switch

根据此文档配置https://infologs.wordpress.com/2015/06/19/how-to-attach-lxc-container-to-ovs-openvswitch/

    创建的测试容器:

    # lxc-create -t ubuntu -n veth03-ovs -- -r trusty
    

    创建 ovs 网桥并为其分配 IP:

    # ovs-vsctl add-br switch0
    # ip add add 192.168.100.1/24 dev switch0
    

让它成为新的网络 192.168.100.0/24 和 switch0(根据我的理解)将是那里的第一个地址(网关)。

看起来不错:

# ip a
...
4: ovs-system: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default 
    link/ether 52:9d:e1:60:1d:56 brd ff:ff:ff:ff:ff:ff
5: switch0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default 
    link/ether 16:63:eb:47:13:41 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.1/24 scope global switch0
       valid_lft forever preferred_lft forever

网桥接口状态:

# ovs-vsctl show
1b236728-4637-42a5-8b81-53d4c93a6803
    Bridge "switch0"
        Port "switch0"
            Interface "switch0"
                type: internal
    ovs_version: "2.3.2"

    编辑 veth03-ovs 容器的 LXC 配置以使用此桥:

    # vim /var/lib/lxc/veth03-ovs/config
    ...
    lxc.network.type = veth
    lxc.network.flags = up
    
    lxc.network.script.up = /etc/lxc/ifup
    lxc.network.script.down = /etc/lxc/ifdown
    # lxc.network.veth.pair = lxc0
    lxc.network.hwaddr = 00:16:3e:15:b3:62
    lxc.network.ipv4 = 192.168.100.10
    

所以我想将 192.168.100.10 指向容器的内部接口。

/etc/lxc/ifup/etc/lxc/ifdown 在容器启动/停止后添加/删除此网桥的端口。

# cat /etc/lxc/ifup
#!/bin/bash

BRIDGE=”switch0″
ovs-vsctl –may-exist add-br $BRIDGE
ovs-vsctl –if-exists del-port $BRIDGE $5
ovs-vsctl –may-exist add-port $BRIDGE $5

# cat /etc/lxc/ifdown 
#!/bin/bash
ovsBr=’switch0′
ovs-vsctl –if-exists del-port $ovsBr $5

所以现在我想启动容器但得到:

# lxc-start -n veth03-ovs --logfile /tmp/log
lxc-start: lxc_start.c: main: 344 The container failed to start.
lxc-start: lxc_start.c: main: 346 To get more details, run the container in foreground mode.
lxc-start: lxc_start.c: main: 348 Additional information can be obtained by setting the --logfile and --logpriority options.

# cat /tmp/log
  lxc-start 1448974395.199 ERROR    lxc_conf - conf.c:run_buffer:342 - Script exited with status 1
  lxc-start 1448974395.225 ERROR    lxc_conf - conf.c:lxc_create_network:3079 - failed to create netdev
  lxc-start 1448974395.225 ERROR    lxc_start - start.c:lxc_spawn:950 - failed to create the network
  lxc-start 1448974395.225 ERROR    lxc_start - start.c:__lxc_start:1213 - failed to spawn 'veth03-ovs'
  lxc-start 1448974400.730 ERROR    lxc_start_ui - lxc_start.c:main:344 - The container failed to start.
  lxc-start 1448974400.730 ERROR    lxc_start_ui - lxc_start.c:main:346 - To get more details, run the container in foreground mode.
  lxc-start 1448974400.730 ERROR    lxc_start_ui - lxc_start.c:main:348 - Additional information can be obtained by setting the --logfile and --logpriority options.

我想 lxc.network.script.up / lxc.network.script.down 脚本中的错误并且它没有得到 $5 参数,应该是LXC传递给OVS的中间接口。但我不确定。

LXC 是否支持 Open vSwitch? https://github.com/lxc/lxc/issues/256

【问题讨论】:

【参考方案1】:

这是我的错。我刚刚从 Wordpress 博客中复制了 ifup/ifdown 脚本。但是有错别字:

ovs-vsctl –may-exist add-br $BRIDGE

但应该是:

ovs-vsctl --may-exist add-br $BRIDGE

--,而不仅仅是- 之前的may。在ifup/ifdown 脚​​本中到处都是一样的。所以它们应该如下所示:

# cat /etc/lxc/ifup
#!/bin/bash

BRIDGE=switch0
ovs-vsctl --may-exist add-br $BRIDGE
ovs-vsctl --if-exists del-port $BRIDGE $5
ovs-vsctl --may-exist add-port $BRIDGE $5

# cat /etc/lxc/ifdown
#!/bin/bash
ovsBr=switch0
ovs-vsctl --if-exists del-port $ovsBr $5

【讨论】:

以上是关于LXC 与 Open vSwitch的主要内容,如果未能解决你的问题,请参考以下文章

Docker 与 VM 和 LXC 的关系

“lxd”与 lxc/docker 有何不同?

LXC简单介绍与使用

lxc 与 nextcloud 和 nginx 代理:未知:POST Content-Length

如何理解LXC与Docker之间的主要区别

3.Docker与LXC虚拟化技术的区别——虚拟化技术本质上是在模拟硬件,Docker底层是LXC,本质都是cgroups是在直接操作硬件