在ubuntu上运行LXC虚拟机

背景:由于工作测试需要,在不同的主机上测试软件功能,但是又没有单独的虚拟机可以使用,想到可以使用LXC进行轻量级的虚拟化。LXC虚拟机选择在主机上虚拟macvlan网卡供虚拟机使用。主机eth0网络地址时192.168.85.153,主机的网关是192.168.85.1,虚拟机的网络使分两种情况,一种是使用单独的网络,192.168.100.0/24。另外一种是自动获取和主机一样的IP网络。

主机准备:

1 主机打开转发功能,在/etc/sysctl.conf添加net.ipv4.ip_forward = 1。 然后执行sysctl -p

2 主机网卡设置。

  #ip link add link eth0 macvlan0 type macvlan mode bridge

        #ip link set macvlan0 up

Case 1:

此时虚拟机的配置文件如下:

    lxc.network.type = macvlan

    lxc.network.macvlan.mode = bridge

    lxc.network.flags = up

    lxc.network.link = macvlan0

    lxc.network.hwaddr = fe:e9:51:8f:88:08

    lxc.network.ipv4 = 192.168.100.2/24    

    lxc.network.ipv4.gateway = 192.168.85.153

    lxc.rootfs = /home/lxc/centos-test1/rootfs

    lxc.include = /usr/share/lxc/config/centos.common.conf

    lxc.arch = x86_64

    lxc.utsname = centos-test1.synnex.org

     lxc.autodev = 1

    lxc.mount.auto = proc sys cgroup

    在启动虚拟机之前应该修改虚拟机的网卡配置文件,

      # vim rootfs/etc/sysconfig/network-scripts/ifcfg-eth0

        更改为使用静态IP :BOOTPROTO=none

    主机路由设置:

  #route add -net 192.168.100.0/24 dev macvlan0 

    此时虚拟机路由:

        route -n 

        Kernel IP routing table

        Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

        0.0.0.0         192.168.85.153  0.0.0.0         UG    0      0        0 eth0

        169.254.0.0     0.0.0.0         255.255.0.0     U     1006   0        0 eth0

        192.168.85.153  0.0.0.0         255.255.255.255 UH    0      0        0 eth0

        192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0

此时主机和虚拟机之间是连通的。

Case 2: 

     此时LXC虚拟机的配置文件如下

        lxc.network.type = macvlan

        lxc.network.macvlan.mode = bridge

        lxc.network.flags = up

        lxc.network.link = macvlan0

        lxc.network.hwaddr = fe:e4:24:fc:55:da

        lxc.rootfs = /home/lxc/centos-test2/rootfs

        lxc.include = /usr/share/lxc/config/centos.common.conf

        lxc.arch = x86_64

        lxc.utsname = centos-test2.lmy.org

        lxc.autodev = 1

        lxc.mount.auto = proc sys cgroup

    获取IP:192.168.85.228 

     主机路由设置:

  #route add -host 192.168.85.228 gw 192.168.85.1 

  说明:虽然使用了macvlan0作为虚拟机网卡,但是它的网关也是192.168.85.1,所以在主机上设置主机访问该虚拟机的路由也应该设置为同样的路由

       此时虚拟机路由,

        [root@centos-test2 ~]# route -n 

        Kernel IP routing table

        Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

        0.0.0.0         192.168.85.1    0.0.0.0         UG    0      0        0 eth0

        169.254.0.0     0.0.0.0         255.255.0.0     U     1022   0        0 eth0

        192.168.85.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0

    可以看出多了一条路由设置,将他删除

        #route del -net 192.168.85.0/24 dev eth0

    此时的虚拟机路由:

        # route -n 

        Kernel IP routing table

        Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

        0.0.0.0         192.168.85.1    0.0.0.0         UG    0      0        0 eth0

        169.254.0.0     0.0.0.0         255.255.0.0     U     1022   0        0 eth0

    此时虚拟机和主机之间网络是互通的,但是虚拟机与外网还未通。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。