Background: Because of job testing needs, testing software features on different hosts, but no separate virtual machines can be used, think of using LXC for lightweight virtualization. The LXC virtual machine chooses the virtual Macvlan NIC on the host for use by the virtual machine. Host Eth0 Network address when the 192.168.85.153, the host's gateway is 192.168.85.1, the network of the virtual machine is divided into two situations, one is to use a separate network, 192.168.100.0/24. Another is the automatic acquisition of the same IP network as the host.
Host Preparation:
1 host turn on forwarding function, add Net.ipv4.ip_forward = 1 in/etc/sysctl.conf. Then execute sysctl-p
2 Host network card settings.
#ip link Add link eth0 macvlan0 type Macvlan mode bridge
#ip Link Set macvlan0 up
Case 1:
The configuration file for this virtual machine is as follows:
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
You should modify the network card configuration file for the virtual machine before starting the virtual machine.
# Vim Rootfs/etc/sysconfig/network-scripts/ifcfg-eth0
Change to use static Ip:bootproto=none
Host Routing settings:
#route add-net 192.168.100.0/24 Dev macvlan0
At this point, the virtual machine routes:
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
At this point the host and the virtual machine are connected.
Case 2:
The configuration file for the LXC virtual machine is as follows
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
Get ip:192.168.85.228
Host Routing settings:
#route add-host 192.168.85.228 GW 192.168.85.1
Note: Although Macvlan0 is used as a virtual machine NIC, but its gateway is also 192.168.85.1, setting the host to access the virtual machine's route should also be set to the same route
At this point, the virtual machine routes
[Email protected] ~]# 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
You can see that one more route setting will remove him
#route del-net 192.168.85.0/24 Dev eth0
The virtual machine route at this time:
# 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
At this point the network between the virtual machine and the host is interoperable, but the virtual machine and the external network are not yet through.
Running the LXC virtual machine on Ubuntu