[root@master1 ~]# for i in `ps aux | grep qemu-kvm | grep -v grep | awk ‘{print $2}‘`;do kill $i;done
确保ovs(openVSwitch上,之前的虚拟机网口已经删除)
[root@master1 ~]# ovs-vsctl show
85eb6797-6dd5-4c5f-aaf8-b7b242b633ae
Bridge br-in
Port "s0"
trunks: [10]
Interface "s0"
Port br-in
Interface br-in
type: internal
Bridge br-test
Port br-test
Interface br-test
type: internal
Port "s1"
Interface "s1"
如果有删除失败,直接用命令删除,例:
[root@master1 ~]# ovs-vsctl del-port br-in vif1.0
移除之前的br-test和s0,s1
[root@master1 ~]# ovs-vsctl del-port br-test s1
[root@master1 ~]# ovs-vsctl del-port br-in s0
[root@master1 ~]# ovs-vsctl del-br br-test
[root@master1 ~]# ovs-vsctl show
85eb6797-6dd5-4c5f-aaf8-b7b242b633ae
Bridge br-in
Port br-in
Interface br-in
type: internal
[root@master1 ~]# cat /etc/if-up
#!/bin/bash
bridge=br-in
if [ -n "$1" ]; then
ip link set $1 up
sleep 1
ovs-vsctl add-port $bridge $1
[ $? -eq 0 ] && exit 0 || exit 1
else
echo "Error:no port specified."
exit 2
fi
[root@master1 ~]# cat /etc/if-down
#!/bin/bash
bridge=br-in
if [ -n "$1" ]; then
ip link set $1 down
sleep 1
ovs-vsctl del-port $bridge $1
[ $? -eq 0 ] && exit 0 || exit 1
else
echo "Error:no port specified."
exit 2
fi
复制到master2:
[root@master1 ~]# scp -p /etc/if-up /etc/if-down root@192.168.1.2:/etc/
查看:
[root@master2 ~]# ls /etc/if-up /etc/if-down
/etc/if-down /etc/if-up
[root@master2 ~]# yum install qemu-kvm
[root@master2 ~]# ln -sv /usr/libexec/qemu-kvm /usr/bin/
安装依赖包:
yum -y install make gcc openssl-devel autoconf automake rpm-build redhat-rpm-config
oepnVSwtich
下载包:
[root@master2 ~]# wget http://openvswitch.org/releases/openvswitch-2.5.0.tar.gz
准备构建rpm包
[root@master2 ~]# mkdir -p ~/rpmbuild/SOURCES
[root@master2 ~]# cp openvswitch-2.5.0.tar.gz ~/rpmbuild/SOURCES/
解压:
[root@master2 ~]# tar xf openvswitch-2.5.0.tar.gz
修改配置文件
[root@master1 ~]# sed ‘s/openvswitch-kmod, //g‘ openvswitch-2.5.0/rhel/openvswitch.spec > openvswitch-2.5.0/rhel/openvswitch_no_kmod.spec
构建RPM包:
[root@master2 ~]# rpmbuild -bb --nocheck ~/openvswitch-2.5.0/rhel/openvswitch_no_kmod.spec
安装
[root@master2 ~]# yum localinstall ~/rpmbuild/RPMS/x86_64/openvswitch-2.5.0-1.x86_64.rpm
启动服务
[root@master2 ~]# systemctl start openvswitch.service
[root@master2 ~]# ovs-vsctl add-br br-in
[root@master2 ~]# ovs-vsctl list-br
br-in
准备目录存放磁盘映像文件:
[root@master2 ~]# mkdir -pv /vm/images
[root@master2 images]# mv cirros-no_cloud-0.3.0-i386-disk.img c1.img
[root@master2 images]# mv cirros-no_cloud-0.3.0-x86_64-disk.img c2.img
[root@master2 images]# ls
c1.img c2.img
创建网络名称空间:
[root@master2 ~]# ip netns add r0
创建一对网卡,rif0连接名称空间,sif0连接到桥上:
[root@master2 ~]# ip link add sif0 type veth peer name rif0
[root@master2 ~]# ip link set sif0 up
rif0网卡添加到r0网络名称空间:
[root@master2 ~]# ip link set rif0 netns r0
[root@master2 ~]# ip netns exec r0 ip link set rif0 up
sif0网卡添加到br-in桥:
[root@master2 ~]# ovs-vsctl add-port br-in sif0
[root@master2 ~]# ovs-vsctl show
35f025aa-0395-4fa5-b111-553aa9c651e5
Bridge br-in
Port "sif0"
Interface "sif0"
Port br-in
Interface br-in
给rif0添加IP地址
[root@master2 ~]# ip netns exec r0 ip addr add 10.0.4.254/24 dev rif0
启动DHCP服务:
[root@master2 ~]# ip netns exec r0 dnsmasq -F 10.0.4.200,10.0.4.220,86400 --dhcp-option=option:router,10.0.4.254 -i rif0
查看DHCP服务是否有监听:
[root@master2 ~]# ip netns exec r0 ss -unlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
UNCONN 0 0 *:53 *:* users:(("dnsmasq",pid=3801,fd=6))
UNCONN 0 0 *:67 *:* users:(("dnsmasq",pid=3801,fd=4))
UNCONN 0 0 :::53 :::* users:(("dnsmasq",pid=3801,fd=8))
master1同上,或者不配置DHCP服务也行,自己手动配置IP。
[root@master2 ~]# qemu-kvm -name "c2" -m 128 -smp 1 -drive file=/vm/images/c2.img,media=disk,if=virtio -net nic,model=virtio,macaddr=52:54:00:00:00:04 -net tap,ifname=vif1.0,script=/etc/if-up,downscript=/etc/if-down -daemonize
VNC server running on `::1:5900‘
[root@master2 ~]# yum install tigervnc
[root@master2 ~]# vncviewer :5900
已经自动获取到IP
[root@master1 ~]# qemu-kvm -name "c1" -m 128 -smp 1 -drive file=/vm/images/cirros-no_cloud-0.3.0-i386-disk.img,media=disk,if=virtio -net nic,model=virtio,macaddr=52:54:00:00:00:01 -net tap,ifname=vif1.0,script=/etc/if-up,downscript=/etc/if-down -daemonize
VNC server running on `::1:5900‘
[root@master1 ~]# ip addr add 192.168.20.1/24 dev eth1
[root@master2 ~]# ip addr add 192.168.20.2/24 dev eth1
两台宿主机的eth1处于同一vmnet网络,可以ping通
添加port,名字为gre0
[root@master1 ~]# ovs-vsctl add-port br-in gre0
[root@master1 ~]# ifconfig gre0 up
查看
[root@master1 ~]# ovs-vsctl list port gre0 | grep name
name : "gre0"
[root@master1 ~]# ovs-vsctl show
85eb6797-6dd5-4c5f-aaf8-b7b242b633ae
Bridge br-in
Port "vif1.0"
Interface "vif1.0"
Port "gre0"
Interface "gre0"
设置端口为gre类型,并设置远端IP值
[root@master1 ~]# ovs-vsctl set interface gre0 type=gre options:remote_ip=192.168.20.2
查看:
[root@master1 ~]# ovs-vsctl list interface gre0 | egrep "type|options"
options : {remote_ip="192.168.20.2"}
type : gre
节点2配置:
[root@master2 ~]# ovs-vsctl add-port br-in gre0 -- set interface gre0 type=gre options:remote_ip=192.168.20.1
查看隧道建立状态,如果状态异常可以尝试重启oepnvswitch服务:
[root@master1 ~]# ovs-vsctl list interface gre0
link_state : up
mac_in_use : "b2:33:5e:d3:e6:5d"
mtu : []
name : "gre0"
ofport : 9
ofport_request : []
options : {remote_ip="192.168.20.2"}
other_config : {}
statistics : {collisions=0, rx_bytes=0, rx_crc_err=0, rx_dropped=0, rx_errors=0, rx_frame_err=0, rx_over_err=0, rx_packets=0, tx_bytes=0, tx_dropped=0, tx_errors=0, tx_packets=0}
status : {tunnel_egress_iface="eth1", tunnel_egress_iface_carrier=up}
type : gre
[root@master2 ~]# ovs-vsctl list interface gre0
[root@master1 ~]# killall qemu-kvm
[root@master1 ~]# qemu-kvm -name "c1" -m 128 -smp 1 -drive file=/vm/images/cirros-no_cloud-0.3.0-i386-disk.img,media=disk,if=virtio -net nic,model=virtio,macaddr=52:54:00:00:00:01 -net tap,ifname=vif1.0,script=/etc/if-up,downscript=/etc/if-down -daemonize
VNC server running on `::1:5900‘
可以获取到IP,并且ping通master2上的虚拟机
上图的ping持续,在master2抓包,下面是一个隧道通信过程。外面是eth1的IP之间通信,里面是虚拟机的IP间通信
[root@master1 ~]# qemu-kvm -name "c3" -m 128 -smp 1 -drive file=/vm/images/c3.img,media=disk,if=virtio -net nic,model=virtio,macaddr=52:54:00:00:00:03 -net tap,ifname=vif3.0,script=/etc/if-up,downscript=/etc/if-down -daemonize
VNC server running on `::1:5901‘
[root@master2 ~]# qemu-kvm -name "c4" -m 128 -smp 1 -drive file=/vm/images/c4.img,media=disk,if=virtio -net nic,model=virtio,macaddr=52:54:00:00:00:21 -net tap,ifname=vif4.0,script=/etc/if-up,downscript=/etc/if-down -daemonize
VNC server running on `::1:5901‘
[root@master1 ~]# ovs-vsctl set port vif1.0 tag=10
[root@master1 ~]# ovs-vsctl set port vif3.0 tag=20
[root@master2 ~]# ovs-vsctl set port vif1.0 tag=10
[root@master2 ~]# ovs-vsctl set port vif4.0 tag=20
现在的IP是
c1:10.0.4.203,宿主机:master1
c2:10.0.4.206,宿主机:master2
c3:10.0.4.205,宿主机:master1
c4:10.0.4.214,宿主机:master2
测试:
c1能够ping通另一台宿主机的c2,但是无法ping通同个宿主机的c3,和另一台宿主机的c4
测试:
c3能够ping通另一台宿主机的c4,但是无法ping通同个宿主机的c1,和另一台宿主机的c2
[root@master1 ~]# ovs-vsctl remove port vif1.0 tag 10
[root@master1 ~]# ovs-vsctl remove port vif3.0 tag 20
[root@master1 ~]# ovs-vsctl show
85eb6797-6dd5-4c5f-aaf8-b7b242b633ae
Bridge br-in
Port "gre0"
Interface "gre0"
type: gre
options: {remote_ip="192.168.20.2"}
Port "vif3.0"
Interface "vif3.0"
Port "vif1.0"
Interface "vif1.0"
[root@master2 ~]# ovs-vsctl remove port vif1.0 tag 10
[root@master2 ~]# ovs-vsctl remove port vif4.0 tag 20
[root@master2 ~]# ovs-vsctl show
35f025aa-0395-4fa5-b111-553aa9c651e5
Bridge br-in
Port "gre0"
Interface "gre0"
type: gre
options: {remote_ip="192.168.20.1"}
Port "vif1.0"
Interface "vif1.0"
Port "sif0"
Interface "sif0"
Port "vif4.0"
Interface "vif4.0"
继续关掉GRE,删除端口即可:
[root@master1 ~]# ovs-vsctl del-port br-in gre0
[root@master2 ~]# ovs-vsctl del-port br-in gre0
删除后,不同宿主机的虚拟机之间无法通信了
在br-in交换机添加端口:
[root@master1 ~]# ovs-vsctl add-port br-in vx0 -- set interface vx0 type=vxlan options:remote_ip=192.168.20.2
查看,如果状态异常可以尝试重启oepnvswitch服务:
[root@master1 ~]# ovs-vsctl list port vx0
[root@master1 ~]# ovs-vsctl list interface vx0
[root@master2 ~]# ovs-vsctl add-port br-in vx0 -- set interface vx0 type=vxlan options:remote_ip=192.168.20.1
[root@master2 ~]# ovs-vsctl list interface vx0
两端xvlan配置完毕后,宿主机间的虚拟机又可以通信了:
抓包
[root@master1 x86_64]# scp openvswitch-2.5.0-1.x86_64.rpm root@10.201.106.134:/root/
安装
[root@master4 ~]# yum localinstall openvswitch-2.5.0-1.x86_64.rpm
启动服务
[root@master4 ~]# systemctl start openvswitch.service
配置IP如下:
能够ping通,另外两台宿主机的eth1的IP即可
[root@master1 ~]# ovs-vsctl del-port br-in vx0
[root@master2 ~]# ovs-vsctl del-port br-in vx0
设置外网网卡桥接到br-ex桥
[root@master4 network-scripts]# vim ifcfg-eth2
TYPE="Ethernet"
NAME="eth2"
DEVICE="eth2"
ONBOOT="yes"
NM_CONTOLLED="no"
BRIDGE=br-ex
设置br-ex桥的IP:
[root@master4 network-scripts]# vim ifcfg-br-ex
TYPE="Bridge"
BOOTPROTO="static"
NAME="br-ex"
DEVICE="br-ex"
ONBOOT="yes"
IPADDR=10.201.106.134
NETMASK=255.255.255.0
GATEWAY=10.201.106.2
DNS1=10.201.106.2
NM_CONTOLLED="no"
[root@master4 ~]# systemctl restart network
添加内部桥:
[root@master4 ~]# ovs-vsctl add-br br-in
跟master2建立GRE隧道:
master1配置:
[root@master2 ~]# ovs-vsctl add-port br-in gre0
[root@master2 ~]# ovs-vsctl set interface gre0 type=gre options:remote_ip=192.168.20.254
查看状态:
[root@master2 ~]# ovs-vsctl list interfaces gre0
如果配置GRE时,使用ovs-vsctl show查看有配置未找到报错,可以考虑重装openvswitch或者换一个临近版本
master4(网络节点)配置:
[root@master4 ~]# ovs-vsctl add-port br-in gre0
[root@master4 ~]# ovs-vsctl set interface gre0 type=gre options:remote_ip=192.168.20.2
创建网络名称空间r0
[root@master4 ~]# ip netns add r0
创建两对内部网卡:
内部网卡:
[root@master4 ~]# ip link add sin0 type veth peer name rin0
外部网卡:
[root@master4 ~]# ip link add sex0 type veth peer name rex0
激活关联到桥上的网卡,并关联到桥:
[root@master4 ~]# ip link set sin0 up
[root@master4 ~]# ip link set sex0 up
将内部网卡添加到内部桥:
[root@master4 ~]# ovs-vsctl add-port br-in sin0
将外部网卡添加到外部桥:
[root@master4 ~]# brctl addif br-ex sex0
ps:关联到名称空间的,需要先加入名称空间后,再在名称空间里面激活网卡
将要关联到名称空间的网卡,加入到名称空间里面:
[root@master4 ~]# ip link set rin0 netns r0
[root@master4 ~]# ip link set rex0 netns r0
配置rin0地址(内部业务地址):
[root@master4 ~]# ip netns exec r0 ifconfig rin0 10.0.4.253/24 up
测试在名称空间ping虚拟机地址
抓包:
配置rex0地址(外网地址):
[root@master4 ~]# ip netns exec r0 ifconfig rex0 10.201.106.88/24 up
master4(网络节点IP示意图如下)
网络名称空间已经ping到外网网关:
[root@master4 ~]# ip netns exec r0 sysctl -w net.ipv4.ip_forward=1
net.ipv4.ip_forward = 1
网络名称空间里面专门新建一个网卡IP,作为映射地址:
[root@master4 ~]# ip netns exec r0 ifconfig rex0:0 10.201.106.66/24 up
将来自内网10.0.4.0的地址转换为10.201.106.66
[root@master4 ~]# ip netns exec r0 iptables -t nat -A POSTROUTING -s 10.0.4.0/24 -j SNAT --to-source 10.201.106.66
将外部访问10.201.106.66的数据,转换为虚拟机IP地址
[root@master4 ~]# ip netns exec r0 iptables -t nat -A PREROUTING -d 10.201.106.66 -j DNAT --to-destination 10.0.4.206
还需要在名称空间添加去往外网的默认路由:
[root@master4 ~]# ip netns exec r0 route add default gw 10.201.106.2
测试:外部ping 10.201.106.66,能够转换为10.0.4.206
抓网络名称空间的包:
在业务网口抓包:
如果虚拟机想访问互联网,还需要在网络名称空间添加默认路由:
[root@master4 ~]# ip netns exec r0 route add default gw 10.201.106.2
原文地址:http://blog.51cto.com/zhongle21/2096877