GRE: Generic Routing Encapsulation,通用路由封装;是一种隧道技术;
跨交换机利用GRE技术:
安装openvswitch:
# systemctl disable firewalld
# setenforce 0
# yum -y install vim
# yum -y install net-tools
# yum -y install epel-release
# yum -y install centos-release-openstack-ocata.noarch
# yum -y install openvswitch
# systemctl start openvswitch
# yum -y install qemu-kvm
# ln -sv /usr/libexec/qemu-kvm /usr/sbin/
# modprobe kvm
# modprobe kvm_intel
kvm1:
# ovs-vsctl add-br vm_net
# ovs-vsctl add-port vm_net gre0 — set Interface gre0 type=gre options:remote_ip=10.0.0.151
# ovs-vsctl set interface gre0 option:local_ip=10.0.0.150
kvm2:
# ovs-vsctl add-br vm_net
# ovs-vsctl add-port vm_net gre0 — set Interface gre0 type=gre options:remote_ip=10.0.0.150
# ovs-vsctl set interface gre0 option:local_ip=10.0.0.151
启动虚拟机脚本:
# vim /etc/qemu-kvm/if-up
#!/bin/bash
#
bridgename=vm_net
if [ -n “$1” ] ; then
ip link set $1 up
sleep 1
ovs-vsctl add-port $bridgename $1
[ $? -eq 0 ] && exit 0 || exit 1
else
echo “Error: no port specified.”
fi
# vim /etc/qemu-kvm/if-down
#!/bin/bash
#
bridgename=vm_net
if [ -n “$1” ] ; then
ovs-vsctl del-port $bridgename $1
sleep 1
ip link set $1 down
[ $? -eq 0 ] && exit 0 || exit 1
else
echo “Error: no port specified.”
fi
# chmod u+x /etc/qemu-kvm/if-*
# chmod +x /etc/qemu-kvm/if-*
# bash -n /etc/qemu-kvm/if-up
# bash -n /etc/qemu-kvm/if-down
# scp /etc/qemu-kvm/if-up /etc/qemu-kvm/if-down [email protected]:/etc/qemu-kvm/
准备测试虚拟机镜像文件:
# mkdir -pv /images/cirros/ ssh
# ssh [email protected] “mkdir -pv /images/cirros/ ssh”
# cp cirros-0.3.5-x86_64-disk.img /images/cirros/ -av
# mv -v cirros-0.3.5-x86_64-disk.img /images/cirros/cirros-0.3.5-x86_64-disk2.img
# scp /images/cirros/* [email protected]:/images/cirros/
kvm1上启动两台虚拟机:
# qemu-kvm -name vm1 -m 128 -smp 1 -vnc :0 -usbdevice tablet -daemonize -balloon virtio \
-drive file=/images/cirros/cirros-0.3.5-x86_64-disk.img,media=disk,cache=writeback,if=virtio,format=qcow2 \
-net nic,macaddr=52:54:00:00:00:01,model=virtio -net tap,vhost=on,vnet_hdr=on,script=/etc/qemu-kvm/if-up,downscript=/etc/qemu-kvm/if-down
# qemu-kvm -name vm2 -m 128 -smp 1 -vnc :1 -usbdevice tablet -daemonize -balloon virtio \
-drive file=/images/cirros/cirros-0.3.5-x86_64-disk2.img,media=disk,cache=writeback,if=virtio,format=qcow2 \
-net nic,macaddr=52:54:00:00:00:02,model=virtio -net tap,vhost=on,vnet_hdr=on,script=/etc/qemu-kvm/if-up,downscript=/etc/qemu-kvm/if-down
kvm2上启动两台虚拟机:
# qemu-kvm -name vm3 -m 128 -smp 1 -vnc :0 -usbdevice tablet -daemonize -balloon virtio \
-drive file=/images/cirros/cirros-0.3.5-x86_64-disk.img,media=disk,cache=writeback,if=virtio,format=qcow2 \
-net nic,macaddr=52:54:00:00:00:03,model=virtio -net tap,vhost=on,vnet_hdr=on,script=/etc/qemu-kvm/if-up,downscript=/etc/qemu-kvm/if-down
# qemu-kvm -name vm4 -m 128 -smp 1 -vnc :1 -usbdevice tablet -daemonize -balloon virtio \
-drive file=/images/cirros/cirros-0.3.5-x86_64-disk2.img,media=disk,cache=writeback,if=virtio,format=qcow2 \
-net nic,macaddr=52:54:00:00:00:04,model=virtio -net tap,vhost=on,vnet_hdr=on,script=/etc/qemu-kvm/if-up,downscript=/etc/qemu-kvm/if-down
vm1:
ifconfig eth0 172.16.0.1/24 broadcast 172.16.0.255 up
vm2:
ifconfig eth0 172.16.0.2/24 broadcast 172.16.0.255 up
vm3:
ifconfig eth0 172.16.0.3/24 broadcast 172.16.0.255 up
vm4:
ifconfig eth0 172.16.0.4/24 broadcast 172.16.0.255 up
kvm1 vm2加入vlan10:
# ovs-vsctl set port tap1 tag=10
kvm2 vm3加入vlan10:
# ovs-vsctl set port tap0 tag=10