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 show
4daab0dc-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 show
75b40a94-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) started
Sending 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.42
PING 10.0.0.42 (10.0.0.42): 56 data bytes
64 bytes from 10.0.0.42: seq=0 ttl=64 time=1.359 ms
64 bytes from 10.0.0.42: seq=1 ttl=64 time=2.026 ms