如何在 kvm 上创建多个 VNIC?

如何在 kvm 上创建多个 VNIC?

我已经在 ubuntu 上安装了 kvm,并创建了一个虚拟机。我想在虚拟机上创建 VNIC。我该怎么做?

答案1

openvswitch 安装:

# apt-get install \
    openvswitch-controller \
    openvswitch-switch \
    openvswitch-datapath-source

创建桥梁:

# ovs-vsctl add-br br0
# ovs-vsctl add-br br1

# ovs-vsctl show
fb28566b-aada-4cf6-8260-ca5677d8c014
    Bridge "br0"
        Port "br0"
            Interface "br0"
                type: internal
    Bridge "br1"
        Port "br1"
            Interface "br1"
                type: internal
    ovs_version: "2.0.2"

配置/etc/network/interfaces:

# cat /etc/network/interfaces
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

auto br0
iface br0 inet static
address 192.168.0.1
network 192.168.0.0
netmask 255.255.255.0
broadcast 192.168.0.255

auto br1
iface br1 inet static
address 192.168.1.1
network 192.168.1.0
netmask 255.255.255.0
broadcast 192.168.1.255

上行接口:

# ifup br0
# ifup br1

# ip -4 a show br0
4: br0: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default 
    inet 192.168.0.1/24 brd 192.168.0.255 scope global br0
       valid_lft forever preferred_lft forever

# ip -4 a show br1
5: br1: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default 
    inet 192.168.1.1/24 brd 192.168.1.255 scope global br1
       valid_lft forever preferred_lft forever

创建 libvirt 网络:

# cat br0.xml 
<network>
  <name>br0</name>
  <forward mode='bridge'/>
  <bridge name='br0'/>
  <virtualport type='openvswitch'/>
</network>

# cat br1.xml 
<network>
  <name>br1</name>
  <forward mode='bridge'/>
  <bridge name='br1'/>
  <virtualport type='openvswitch'/>
</network>

定义并启动网络:

# virsh net-define br0.xml 
# virsh net-start  br0
# virsh net-autostart  br0

# virsh net-define br1.xml 
# virsh net-start  br1
# virsh net-autostart  br1

# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 br0                  active     yes           yes
 br1                  active     yes           yes

创建虚拟机:

# qemu-img create -f raw /opt/test.img 3G

# virt-install \
  --ram 1024 \
  --accelerate \
  --disk path=/opt/test.img \
  --name test \
  --network network:br0 \
  --network network:br1 \
  --vcpus 1 \
  --graphics vnc,listen=0.0.0.0 \
  --cdrom ubuntu-14.04.3-server-amd64.iso

完成安装,登录到新的虚拟机并配置网络:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.0.2
netmask 255.255.255.0

auto eth1
iface eth1 inet static
address 192.168.1.2
netmask 255.255.255.0

启动网络接口:

# ifup eth0
# ifup eth1

检查主机的 ping 情况:

# ping -c 1 192.168.0.2
PING 192.168.0.2 (192.168.0.2) 56(84) bytes of data.
64 bytes from 192.168.0.2: icmp_seq=1 ttl=64 time=1.11 ms

--- 192.168.0.2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 1.118/1.118/1.118/0.000 ms

# ping -c 1 192.168.1.2
PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.
64 bytes from 192.168.1.2: icmp_seq=1 ttl=64 time=2.89 ms

--- 192.168.1.2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 2.897/2.897/2.897/0.000 ms

相关内容