kvm+ovs自定义网络并开启虚拟机

定义一个网络,该网络桥接到ovs网桥上:

[root@cnc mnt]# cat test.xml 
<network>
  <name>test</name>
  <forward mode="bridge"/>
  <bridge name="test-br"/>
  <virtualport type="openvswitch"/>
</network>

上面是定义一个网络所需的xml文件,通过文件,我们可以定义该网络的名字、转发的方式、桥的名字以及接口的类型。

步骤如下:

1.创建ovs网桥

[root@cnc mnt]# ovs-vsctl add-br test-br
[root@cnc mnt]# ovs-vsctl show
e0738e00-be28-4ac0-8f0b-856bbb9f072c
    Bridge test-br
        Port test-br
            Interface test-br
                type: internal

    ovs_version: "2.0.0"

2.利用如上xml文件定义网络

[root@cnc mnt]# virsh net-define test.xml 
Network test defined from test.xml


[root@cnc mnt]# virsh net-list --all
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 test                 inactive   no            yes

[root@cnc mnt]# virsh net-start test
Network test started

[root@cnc mnt]# virsh net-list --all
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 test                 active     no            yes

[root@cnc mnt]# virsh net-autostart test
Network test marked as autostarted

3.创建虚拟机

将镜像转换成raw格式:

qemu-img convert -f qcow2 -O raw cirros-0.3.4-x86_64-disk.img cirros-test.raw

创建虚拟机

[root@cnc mnt]# virt-install --virt-type kvm --name cirros-test-vm --ram 128 --boot hd  --disk path=cirros-test.raw --network network=test,mac=52:54:00:aa:69:cc --graphics vnc,listen=0.0.0.0 --noautoconsole
WARNING  No operating system detected, VM performance may suffer. Specify an OS with --os-variant for optimal results.

Starting install...
Domain creation completed.
[root@cnc mnt]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     cirros-test-vm                 running

我们可以发现虚拟机网卡以及桥接到test-vm网桥上:

[root@cnc mnt]# ovs-vsctl show
e0738e00-be28-4ac0-8f0b-856bbb9f072c
    Bridge test-br
        Port "vnet0"
            Interface "vnet0"
        Port test-br
            Interface test-br
                type: internal

    ovs_version: "2.0.0"

我们再创建一个虚拟机:

[root@cnc mnt]# qemu-img convert -f qcow2 -O raw cirros-0.3.4-x86_64-disk.img cirros-test2.raw
[root@cnc mnt]# virt-install --virt-type kvm --name cirros-test-vm2 --ram 128 --boot hd  --disk path=cirros-test2.raw --network network=test,mac=52:54:00:aa:69:dd --graphics vnc,listen=0.0.0.0 --noautoconsole
WARNING  No operating system detected, VM performance may suffer. Specify an OS with --os-variant for optimal results.

Starting install...
Domain creation completed.
[root@cnc mnt]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 1     cirros-test-vm                 running
 2    cirros-test-vm2                running

[root@cnc mnt]# ovs-vsctl show
e0738e00-be28-4ac0-8f0b-856bbb9f072c
    Bridge test-br
        Port "vnet1"
            Interface "vnet1"

        Port "vnet0"
            Interface "vnet0"
        Port test-br
            Interface test-br
                type: internal

    ovs_version: "2.0.0"

如此,我们就通过桥接的方式,自定义网络并创建了两个虚拟机桥接到ovs网桥上。

下面,我们在ovs网桥上添加一个端口并设置ip

[root@cnc mnt]# ovs-vsctl add-port test-vm dhcp_tap

[root@cnc mnt]# ovs-vsctl set Interface dhcp_tap type=internal

[root@cnc mnt]# ip link set dhcp_tap address 12:23:34:45:56:67

[root@cnc mnt]# ifconfig dhcp_tap 10.10.10.1/24 up

[root@cnc mnt]# ifconfig dhcp_tap
dhcp_tap: flags=67<UP,BROADCAST,RUNNING>  mtu 1500
        inet 10.10.10.1  netmask 255.255.255.0  broadcast 10.10.10.255
        inet6 fe80::5c1c:37ff:feaa:9701  prefixlen 64  scopeid 0x20<link>
        ether 12:23:34:45:56:67  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 648 (648.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

网络拓扑如下:

这里的虚拟机目前无法自动获取ip,后面文章会利用这个拓扑网络讲解如何模仿openstack中dhcp功能虚机自动获取ip。

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值