一、OVS使用
1、OVS编译安装
下载ovs2.17版本源码
http://www.openvswitch.org//download/
./boot.sh
./configure
make && make install
2、启动OVS服务
(1)创建文件/etc/systemd/system/openvswitch.service
[root@localhost qemu]# systemctl cat openvswitch
# /etc/systemd/system/openvswitch.service
[Unit]
Description=Open vSwitch server daemon
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/local/share/openvswitch/scripts/ovs-ctl start
ExecStop=/usr/local/share/openvswitch/scripts/ovs-ctl stop
[Install]
WantedBy=network-online.target
(2)启动ovs
systemctl daemon-reload
systemctl restart openvswitch
3、创建OVS网桥
sudo ovs-vsctl add-br OVS0
sudo ovs-vsctl add-br OVS1
二、KVM配置
1、使用virsh配置域XML文件
<interface type='network'>
<mac address='52:54:00:8e:00:21'/>
<source network='OVS1'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<interface type='network'>
<mac address='52:54:00:8e:00:22'/>
<source network='OVS0'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</interface>
2、进入/etc/libvirt/qemu/networks目录
(1)创建OVS0.xml文件
<network>
<name>OVS0</name>
<uuid>38d78811-6c60-4fc3-932a-fedd9400cdf4</uuid>
<forward mode='bridge'/>
<bridge name='OVS0'/>
<virtualport type='openvswitch'/>
</network>
(2)创建OVS1.xml文件
<network>
<name>OVS1</name>
<uuid>38d78832-6c60-4fc3-932a-fedd9400cdf4</uuid>
<forward mode='bridge'/>
<bridge name='OVS1'/>
<virtualport type='openvswitch'/>
</network>
(3)使用以下命令启动网桥
# OVS0管理网桥
virsh net-define OVS0
virsh net-start OVS0
virsh net-autostart OVS0
# OVS1内部通信网桥
virsh net-define OVS1
virsh net-start OVS1
virsh net-autostart OVS1
3、配置网桥ip地址
(1)修改物理网卡网络配置文件
[root@localhost network-scripts]# cat ifcfg-eno1
TYPE=OVSPort
PROXY_METHOD=none
BROWSER_ONLY=no
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=eno1
UUID=511ff6a1-e78d-475a-bfe7-74a46557b9b5
DEVICE=eno1
ONBOOT=yes
DEVICETYPE=ovs
OVS_BRIDGE=OVS0
(2)添加OVS网桥网络配置文件
[root@localhost network-scripts]# cat ifcfg-OVS0
DEVICE=OVS0
STP=yes
BOOTPROTO=none
ONBOOT=yes
TYPE=OVSBridge
DEVICETYPE=ovs
USERCTL=yes
PEERDNS=yes
IPV6INIT=no
NAME=OVS0
IPADDR=172.18.3.25
NETMASK=255.255.0.0
GATEWAY=172.18.0.1
DNS1=8.8.8.8
[root@localhost network-scripts]# cat ifcfg-OVS1
DEVICE=OVS1
STP=yes
BOOTPROTO=none
ONBOOT=yes
TYPE=OVSBridge
DEVICETYPE=ovs
USERCTL=yes
PEERDNS=yes
IPV6INIT=no
NAME=OVS1
IPADDR=192.168.100.1
NETMASK=255.255.255.0
(3)将物理网卡加入网桥OVS0
ovs-vsctl add-port OVS0 eno1
(4)查看kvm网桥信息
[root@localhost network-scripts]# virsh net-list
Name State Autostart Persistent
----------------------------------------------------------
br0 active yes yes
default active yes yes
OVS0 active yes yes
OVS1 active yes yes
4、创建KVM虚拟机
qemu-img create -f qcow2 /opt/vm/test.qcow2 10G
virt-install \
--ram 1024 \
--accelerate \
--disk path=/opt/vm/test.qcow2 \
--name test \
--network network:OVS0\
--vcpus 1 \
--cpu core2duo \
--cdrom /opt/iso/CentOS-7.0-1406-x86_64-DVD.iso
(1)虚拟机网桥xml文件
<interface type='network'>
<mac address='52:54:00:8e:00:21'/>
<source network='OVS1'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
(2)查看KVM虚拟机状态
[root@localhost network-scripts]# virsh list
Id Name State
----------------------------------------------------
1 Namenode01 running
2 Hdfs_client running
3 Datanode02 running
4 Datanode03 running
5 Namenode03 running
7 Namenode02 running
8 Distribution running
9 Datanode01 running
10 Namenode04 running
15 manager running
(3)查看ovs网桥信息
[root@localhost network-scripts]# ovs-vsctl show
2ffbf926-68c9-4dba-8c49-ddca5ed8ec76
Bridge OVS1
Port OVS1
Interface OVS1
type: internal
Port vnet7
Interface vnet7
Port vnet4
Interface vnet4
Port vnet8
Interface vnet8
Port vnet3
Interface vnet3
Port vnet1
Interface vnet1
Port vnet0
Interface vnet0
Port vnet6
Interface vnet6
Port vnet5
Interface vnet5
Port vnet2
Interface vnet2
Bridge OVS0
Port OVS0
Interface OVS0
type: internal
Port vnet9
Interface vnet9
Port eno1
Interface eno1
ovs_version: "2.17.5"
(4)重启网络服务
systemctl restart network
(5)查看宿主机ip地址
[root@localhost qemu]# ip a|grep OVS
5: OVS1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000
inet 192.168.100.1/24 brd 192.168.100.255 scope global OVS1
6: OVS0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000
inet 172.18.3.25/16 brd 172.18.255.255 scope global OVS0
5、进入虚拟机配置虚拟机IP地址
(1)进入虚拟机图形界面
virt-manager
(2)配置虚拟机IP地址
(3)宿主机ping虚拟机可ping通