同样拥有两块网卡,一块连接内部网络,一块连接外网nat转换。
打开路由转发功能:
[root@master3 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
立即生效:
[root@master3 ~]# sysctl -p
net.ipv4.ip_forward = 1
做nat转换
[root@master3 ~]# iptables -t nat -A POSTROUTING -s 192.168.1.0/24 ! -d 192.168.1.0/24 -j SNAT --to-source 10.201.106.133
测试:master1,2已经可以联通外网
安装依赖包:
yum -y install make gcc openssl-devel autoconf automake rpm-build redhat-rpm-config
1.3.1 下载包:
[root@master1 ~]# wget http://openvswitch.org/releases/openvswitch-2.5.0.tar.gz
1.3.2 准备构建rpm包
[root@master1 ~]# mkdir -p ~/rpmbuild/SOURCES
[root@master1 ~]# cp openvswitch-2.5.0.tar.gz ~/rpmbuild/SOURCES/
解压:
[root@master1 ~]# 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
1.3.3 构建RPM包:
[root@master1 ~]#rpmbuild -bb --nocheck ~/openvswitch-2.5.0/rhel/openvswitch_no_kmod.spec
1.3.4 安装
yum localinstall ~/rpmbuild/RPMS/x86_64/openvswitch-2.5.0-1.x86_64.rpm
1.3.5 启动服务
[root@master1 ~]# systemctl start openvswitch.service
master2节点同上配置即可。
查看安装了哪些文件:
[root@master1 ~]# rpm -ql openvswitch
查看当前ovs版本:
[root@master1 ~]# ovs-vsctl show
85eb6797-6dd5-4c5f-aaf8-b7b242b633ae
ovs_version: "2.5.0"
查看ovs使用帮助
[root@master1 ~]# ovs-vsctl --help
[root@master1 ~]# ovs-vsctl add-br br-in
[root@master1 ~]# ovs-vsctl show
85eb6797-6dd5-4c5f-aaf8-b7b242b633ae
Bridge br-in
Port br-in
Interface br-in
type: internal
打开内部网桥:
[root@master1 ~]# ifconfig br-in up
上
显示所有桥:
[root@master1 ~]# ovs-vsctl list-br
br-in
将eth1网卡添加进内部网桥br-in
[root@master1 ~]# ovs-vsctl add-port br-in eth1
[root@master1 ~]# ovs-vsctl list-ports br-in
eth1
查看接口:
[root@master1 ~]# ovs-vsctl list-ifaces br-in
eth1
查看interface表信息:
[root@master1 ~]# ovs-vsctl list Interface
只查看interface表中eth1的信息:
[root@master1 ~]# ovs-vsctl list Interface eth1
显示端口表信息:
[root@master1 ~]# ovs-vsctl list Port
通过条件查找,表里面的信息:
[root@master1 ~]# ovs-vsctl find Port name=‘eth1‘
由于暂时不需要两台宿主机(计算节点)通信,先从内部桥移除eth1
[root@master1 ~]# ovs-vsctl del-port br-in eth1
安装qemu-kvm
[root@master1 ~]# yum install qemu-kvm
创建存放磁盘映像的目录:
[root@master1 ~]# mkdir -pv /vm/images
mkdir: 已创建目录 "/vm"
mkdir: 已创建目录 "/vm/images"
[root@master1 ~]# cd /vm/images/
[root@master1 images]# ls
cirros-no_cloud-0.3.0-i386-disk.img cirros-no_cloud-0.3.0-x86_64-disk.img
启动网卡脚本:
# vim /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 ~]# chmod +x /etc/if-up
移除网卡脚本:
[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
[root@master1 ~]# chmod +x /etc/if-down
创建第一台虚拟机(放到后台运行):
[root@master1 images]# 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‘
安装vnc客户端
[root@master1 ~]# yum install tigervnc
vnc连接查看:
[root@master1 ~]# vncviewer :5900
启动第二个虚拟机:
[root@master1 ~]# qemu-kvm -name "c2" -m 128 -smp 1 -drive file=/vm/images/cirros-no_cloud-0.3.0-x86_64-disk.img,media=disk,if=virtio -net nic,model=virtio,macaddr=52:54:00:00:00:02 -net tap,ifname=vif2.0,script=/etc/if-up,downscript=/etc/if-down -daemonize
VNC server running on `::1:5901‘
查看内部网桥:
[root@master1 ~]# ovs-vsctl show
85eb6797-6dd5-4c5f-aaf8-b7b242b633ae
Bridge br-in
Port "vif2.0"
Interface "vif2.0"
Port "vif1.0"
Interface "vif1.0"
Port br-in
Interface br-in
type: internal
修改tag(vlan ID号)
[root@master1 ~]# ovs-vsctl set port vif1.0 tag=10
[root@master1 ~]# ovs-vsctl list port vif1.0 | grep tag
tag : 10
已经无法ping通:
把vif2.0网口也设置vlan10后又可以ping通了
[root@master1 ~]# ovs-vsctl set port vif2.0 tag=10
修改脚本
[root@master1 ~]# cp -a /etc/if-up /etc/if-up2
[root@master1 ~]# cp -a /etc/if-down /etc/if-down2
[root@master1 ~]# vim /etc/if-up2
#!/bin/bash
bridge="br-test"
[root@master1 ~]# vim /etc/if-down2
#!/bin/bash
bridge="br-test"
创建第二个交换机(桥)
[root@master1 ~]# ovs-vsctl add-br br-test
[root@master1 ~]# ovs-vsctl list-br
br-in
br-test
拷贝磁盘映像文件:
[root@master1 ~]# cp /vm/images/cirros-no_cloud-0.3.0-i386-disk.img /vm/images/c3.img
[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-up2,downscript=/etc/if-down2 -daemonize
VNC server running on `::1:5902‘
[root@master1 ~]# ovs-vsctl show
85eb6797-6dd5-4c5f-aaf8-b7b242b633ae
Bridge br-in
Port "vif2.0"
tag: 10
Interface "vif2.0"
Port "vif1.0"
tag: 10
Interface "vif1.0"
Port br-in
Interface br-in
type: internal
Bridge br-test
Port br-test
Interface br-test
type: internal
Port "vif3.0"
Interface "vif3.0"
设置vif3.0网口的vlan号为10
[root@master1 ~]# ovs-vsctl set port vif3.0 tag=10
创建一对网卡名为s0和s1,s0在br-in,s1在br-test,即可实现互联功能
[root@master1 ~]# ip link add s0 type veth peer name s1
[root@master1 ~]# ip link show | egrep "s0|s1"
17: s1@s0: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
18: s0@s1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
激活网卡:
[root@master1 ~]# ip link set s0 up
[root@master1 ~]# ip link set s1 up
将网卡添加至对应交换机(网桥):
[root@master1 ~]# ovs-vsctl add-port br-in s0
[root@master1 ~]# ovs-vsctl add-port br-test s1
[root@master1 ~]# ovs-vsctl show
85eb6797-6dd5-4c5f-aaf8-b7b242b633ae
Bridge br-in
Port "vif2.0"
tag: 10
Interface "vif2.0"
Port "s0"
Interface "s0"
Port "vif1.0"
tag: 10
Interface "vif1.0"
Port br-in
Interface br-in
type: internal
Bridge br-test
Port br-test
Interface br-test
type: internal
Port "s1"
Interface "s1"
Port "vif3.0"
tag: 10
Interface "vif3.0"
从C3测试网络又通了:
PS:移除vlan tag:
[root@master1 ~]# ovs-vsctl remove port vif3.0 tag 10
重新添加:
[root@master1 ~]# ovs-vsctl set port vif3.0 tag=10
####刚才默认应该是trunk模式,所以两个交换机的虚拟机只要处于同个vlan号就能互相通信
[root@master1 ~]# ovs-vsctl set port s0 trunks=10
[root@master1 ~]# ovs-vsctl set port s1 trunks=10
原文地址:http://blog.51cto.com/zhongle21/2096561