Open vSwitch实践 -- 通过VXLUN技术让不同物理机上的虚拟机互通

来源:互联网 发布:夜色钢琴淘宝 编辑:程序博客网 时间:2024/05/29 04:45

OpenStack里提供了隧道技术,便于不同计算节点上的虚机之间进行通信。隧道技术的主要代表时GRE和VXLAN(Virtual Extensible LAN)。VXLAN是由VMware最早提出的一种IP Overlay技术,使用MAC in UDP的方式进行封装,如下所示:



下面是两个物理机上的虚拟机通过VXLAN通信的示意图:



两个主机上的VM都通过位于主机1上的tap-qdhcp设备获得IP地址。如果VM1需要和VM3通信,那从VM1出发的数据包的流向应该如下:

VM1 --> tap1 --> br-int (主机1)--> br-tun(主机1) -->eth0(主机1) --> eth0(主机2) --> br-tun(主机2) --> br-int(主机2) --> tap3 --> VM3

具体的实现过程如下:

1. 根据之前的文章"Open vSwitch实践 -- 利用dnsmasq为虚机分配IP地址"搭建主机1上的虚拟机、br-int以及dnsmasq环境。文章链接如下:

http://blog.csdn.net/zhangli_perdue/article/details/50420362


2. 在主机1上创建隧道网桥br-tun,并patch到br-int,同时建立到主机2的VXLAN隧道连接。

# ovs-vsctl add-br br-tun# ovs-vsctl add-port br-int patch-tun -- set Interface patch-tun type=patch options:peer=patch-int# ovs-vsctl add-port br-tun patch-int -- set Interface patch-int type=patch options:peer=patch-tun# ovs-vsctl add-port br-tun vxlan0 -- set Interface vxlan0 type=vxlan options:remote_ip=192.168.100.33# ovs-vsctl show4daab0dc-86dc-4b09-b7c2-e93ba990166a    Bridge br-tun        Port br-tun            Interface br-tun                type: internal        Port patch-int            Interface patch-int                type: patch                options: {peer=patch-tun}        Port "vxlan0"            Interface "vxlan0"                type: vxlan                options: {remote_ip="192.168.100.33"}    Bridge br-int        Port br-int            Interface br-int                type: internal        Port patch-tun            Interface patch-tun                type: patch                options: {peer=patch-int}        Port tap-qdhcp            Interface tap-qdhcp                type: internal        Port "tap1"            Interface "tap1"    ovs_version: "2.4.0"

3. 在主机2上安装并启动Open vSwitch

# yum -y install openvswitch# systemctl start openvswitch# systemctl enable openvswitch

4. 在主机2上创建集成网桥br-int。

# ovs-vsctl add-br br-int


5. 在主机2上创建隧道网桥br-tun,并patch到br-int,同时建立到主机1的隧道连接。

# ovs-vsctl add-br br-tun# ovs-vsctl add-port br-int patch-tun -- set Interface patch-tun type=patch options:peer=patch-int# ovs-vsctl add-port br-tun patch-int -- set Interface patch-int type=patch options:peer=patch-tun# ovs-vsctl add-port br-tun vxlan0 -- set Interface vxlan0 type=vxlan options:remote_ip=192.168.100.32# ovs-vsctl show75b40a94-413a-418e-a32d-1d4605b1dc78    Bridge br-tun        Port patch-int            Interface patch-int                type: patch                options: {peer=patch-tun}        Port "vxlan0"            Interface "vxlan0"                type: vxlan                options: {remote_ip="192.168.100.32"}        Port br-tun            Interface br-tun                type: internal    Bridge br-int        Port patch-tun            Interface patch-tun                type: patch                options: {peer=patch-int}        Port br-int            Interface br-int                type: internal        Port tap-qdhcp            Interface tap-qdhcp                type: internal    ovs_version: "2.4.0"

6. 在主机2上安装libvirt, virt-install, qemu-kvm,并启动libvirtd

# yum -y install libvirt virt-install qemu-kvm# systemctl start libvirtd# systemctl enable libvirtd


7. 在主机2上用virt-install创建两个虚机,分别命名为vm3和vm4。为了简单起见,用cirros作为disk image。需要提前将image文件cirros-0.3.1-x86_64-disk.img上传到/tmp/vm1/和/tmp/vm2/下面。这里用到的network就是安装libvirt时自动创建的网络default。

# virt-install --connect=qemu:///system --name=vm3 --ram=50 --vcpus=1 --virt-type qemu --disk \path=/tmp/vm3/cirros-0.3.1-x86_64-disk.img,format=qcow2 --import --network network:default# virt-install --connect=qemu:///system --name=vm4 --ram=50 --vcpus=1 --virt-type qemu --disk \path=/tmp/vm4/cirros-0.3.1-x86_64-disk.img,format=qcow2 --import --network network:default


8. 在主机2上,为了让新创建的vm1和vm2使用br-int,需要对两个虚机的配置文件进行重新编辑:

首先停止两个虚机:

# virsh destroy vm3# virsh destroy vm4

然后编辑/etc/libvirt/qemu/vm3.xml,原始的需要替换的部分如下:

    <interface type='network'>      <mac address='52:54:00:99:81:00'/>      <source network='default'/>      <model type='rtl8139'/>      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>    </interface>

最终需要将其替换为如下内容:

     <interface type='bridge'>      <source bridge='br-int'/>      <virtualport type='openvswitch'/>      <target dev='tap3'/>      <model type='virtio'/>    </interface>

然后对/etc/libvirt/qemu/vm4.xml做同样修改,修改后的内容片段如下:

    <interface type='bridge'>      <source bridge='br-int'/>      <virtualport type='openvswitch'/>      <target dev='tap4'/>      <model type='virtio'/>    </interface>


9. 在主机2上使用修改后的虚机配置文件来重新定义虚机

首先将vm3.xml和vm4.xml拷贝到/tmp/下面:

# cp /etc/libvirt/qemu/vm3.xml /tmp/# cp /etc/libvirt/qemu/vm4.xml /tmp/

删除之前创建的旧的虚机的定义:

# virsh undefine vm3# virsh undefine vm4

将/tmp/vm3.xml和/tmp/vm4.xml拷贝回/etc/libvirt/qemu:

# cp /tmp/vm*.xml /etc/libvirt/qemu/

使用新的配置文件重新定义虚机:

# virsh define /etc/libvirt/qemu/vm3.xml# virsh define /etc/libvirt/qemu/vm4.xml


10. 在主机2上启动虚拟机VM3,可以在console上看到在启动过程中得到了dhcp分配的IP地址,如下:

......Starting network...udhcpc (v1.20.1) startedSending discover...Sending select for 10.0.0.137...Lease of 10.0.0.137 obtained, lease time 3600

11. 登陆VM3,ping位于主机1上的VM1,可以ping通:

# ping 10.0.0.42PING 10.0.0.42 (10.0.0.42): 56 data bytes64 bytes from 10.0.0.42: seq=0 ttl=64 time=1.359 ms64 bytes from 10.0.0.42: seq=1 ttl=64 time=2.026 ms

0 0