目录
1.CentOS 7 安装 OpenVSwitch(构建RPM安装包)
(1)关闭Selinux或设置为Permissive
①查看配置文件的命令 cat /etc/selinux/config 来查看状态
cat /etc/selinux/config
发现SELinux共有3个状态enforcing (执行中)、permissive (不执行但产生警告)、disabled(关闭)。
②也可以用这个命令检查:getenforce
getenforce
③也可以用下述命令;如果SELinux?status参数为enabled即为开启状态
/usr/sbin/sestatus -v
④临时关闭(重启机器后失效)
#setenforce 0设置为permissive模式;setenforce 1 设置为enforcing模式;
setenforce 0
⑤永久关闭(需要重启机器)
#重启机器即可
sed -i s#SELINUX=enforcing#SELINUX=disabled# /etc/selinux/config
(2)安装依赖包
yum -y install wget openssl-devel gcc make python-devel openssl-devel kernel-devel graphviz kernel-debug-devel autoconf automake rpm-build redhat-rpm-config libtool python-twisted-core python-zope-interface PyQt4 desktop-file-utils libcap-ng-devel groff checkpolicy selinux-policy-devel
(3)下载OpenVSwitch软件包到本地
官网下载:
wget https://www.openvswitch.org/releases/openvswitch-2.5.10.tar.gz
(4)预处理
mkdir -p ~/rpmbuild/SOURCES
cp openvswitch-2.5.10.tar.gz ~/rpmbuild/SOURCES/
cd ~/rpmbuild/SOURCES
tar -zxvf openvswitch-2.5.10.tar.gz
(6)构建RPM包
cd ~/rpmbuild/SOURCES
rpmbuild -bb --nocheck openvswitch-2.5.10/rhel/openvswitch-fedora.spec
(7)安装
yum -y localinstall ~/rpmbuild/RPMS/x86_64/openvswitch-2.5.10-1.el7.centos.x86_64.rpm
(8)查看OVS工具版本状态
ovs-vsctl -V
(9)手动创建目录
mkdir /var/run/openvswitch
(10)开启Open VSwitch服务并设置开机自启动
systemctl enable openvswitch
systemctl start openvswitch
(11)检查服务是否启动
systemctl is-active openvswitch
(1)查看KVM存储磁盘文件
# cd /var/lib/libvirt/images/
# ls
如果没有需要创建:
qemu-img create -f qcow2 /var/lib/libvirt/images/vm01.qcow2 10G
qemu-img create -f qcow2 /var/lib/libvirt/images/vm02.qcow2 10G
(2)查看KVM网络配置文件
# cd /etc/libvirt/qemu/networks/
# ls
(3)启动管理,查看当前虚拟机状况
# virt-manager
(4)强制关闭vm01,查看当前虚拟机状况
# virsh destroy vm01
(5)删除vm01,查看当前虚拟机状况
# virsh undefine vm01
(6)强制关闭vm02,查看当前虚拟机状况
# virsh destroy vm02
(7)删除vm02,查看当前虚拟机状况
# virsh undefine vm01
(8) 声明网络OVS端口组配置文件
echo "
<network>
<name>ovs01_network</name>
<forward mode='bridge'/>
<bridge name='ovs01'/>
<virtualport type='openvswitch' />
<portgroup name='default_vlan' default='yes'>
<vlan>
<tag id='0'/>
</vlan>
</portgroup>
<portgroup name='vlan10'>
<vlan>
<tag id='10'/>
</vlan>
</portgroup>
<portgroup name='vlan20'>
<vlan>
<tag id='20'/>
</vlan>
</portgroup>
</network>
" >> /etc/libvirt/qemu/networks/ovs01_network.xml
(5)查看配置文件
(6) 定义并启动网络
# virsh net-define /etc/libvirt/qemu/networks/ovs01_network.xml
# virsh net-start ovs01_network
# virsh net-autostart ovs01_network
(7)列出当前虚拟化中定义的网络
# virsh net-list
(8)上传镜像:TinyCore-11.0.iso?
(9)创建
(9)创建OVS?虚拟交换机
# ovs-vsctl add-br ovs01
# ovs-vsctl show
(10)创建vm01
#创建VM01
virt-install \
--name=vm01 \
--cdrom=/var/lib/libvirt/images/TinyCore-11.0.iso \
--os-type=linux \
--os-variant=fedora18 \
--cpu host \
--vcpus 2,maxvcpus=4 \
--memory 512 \
--disk=/var/lib/libvirt/images/vm01.qcow2 \
--network network=ovs01_network,target=if01,mac=02:01:01:01:01:01
(11)查看OVS?虚拟交换机
# ovs-vsctl show
(12)创建vm02
#创建VM02
virt-install \
--name=vm02 \
--cdrom=/var/lib/libvirt/images/TinyCore-11.0.iso \
--os-type=linux \
--os-variant=fedora18 \
--cpu host \
--vcpus 2,maxvcpus=4 \
--memory 512 \
--disk=/var/lib/libvirt/images/vm02.qcow2 \
--network network=ovs01_network,target=if02,mac=02:01:01:01:01:02
(13)查看OVS?虚拟交换机
# ovs-vsctl show
(14)查看虚拟机vm01
# virsh edit vm01
……
<interface type='network'>
<mac address='02:01:01:01:01:01'/>
<source network='ovs01_network'/>
<target dev='if01'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
……
(15)?查看虚拟机vm02
# virsh edit vm01
……
<interface type='network'>
<mac address='02:01:01:01:01:02'/>
<source network='ovs01_network'/>
<target dev='if02'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
……
(16)手动给端口if01添加10网段并查看
# ovs-vsctl set port if01 tag=10
# ovs-vsctl show
(17) 修改vm02添加20段网络
# virsh edit vm02
……
<interface type='network'>
<mac address='02:01:01:01:01:02'/>
<source network='ovs01_network' portgroup='vlan20'/>
<target dev='if02'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
……
(18)强制关机vm02并查看
# virsh destroy vm02
# ovs-vsctl show
(19)启动虚拟机vm02并查看,网段20自动添加
# virsh start vm02
# ovs-vsctl show
(20)强制关机vm01并查看
# virsh destroy vm01
# ovs-vsctl show
(21)启动虚拟机vm01并查看,网段10没有自动添加,恢复为0网段,说明手动给端口添加网段在关机重启后会失效
# virsh start vm01
# ovs-vsctl show
(22)修改vm01添加10段网络
# virsh edit vm01
……
<interface type='network'>
<mac address='02:01:01:01:01:01'/>
<source network='ovs01_network' portgroup='vlan10'/>
<target dev='if01'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
……
(23)强制关机vm01并查看
# virsh destroy vm01
# ovs-vsctl show
(24)启动虚拟机vm01并查看,网段10自动添加
# virsh start vm01
# ovs-vsctl show
(1)报错
In file included from lib/netlink-conntrack.c:26:0:
/usr/include/linux/netfilter/nf_conntrack_sctp.h:25:2: error: unknown type name 'u8'
u8 last_dir;
^
/usr/include/linux/netfilter/nf_conntrack_sctp.h:26:2: error: unknown type name 'u8'
u8 flags;
^
(2)原因分析
CentOS7内核升级到kernel-3.10.0-1160.2.2.el7.x86_64后,OVS2.5.10构建RPM安装包时可能会出错。
linux内核版本kernel-3.10.0-1127.19.1.el7.x86_64 成功构建
linux内核版本kernel-3.10.0-1160.2.2.el7.x86_64 构建失败 报错
(3)解决方法
安装较新的OVS版本 如2.12.0。
安装依赖包:
rpmbuild -bb --without check ~/rpmbuild/SOURCES/openvswitch-2.12.0/rhel/openvswitch_no_kmod.spec
安装:
yum -y localinstall ~/rpmbuild/RPMS/x86_64/openvswitch-2.12.0-1.x86_64.rpm
systemctl enable openvswitch
systemctl start openvswitch
检查服务是否启动:
systemctl is-active openvswitch
在已经安装低版本OVS后,如2.5.10。
可以直接安装高版本如2.12.0
安装完成需重启OpenVSwitch后 OVS服务替换为2.12.0 版本
systemctl restart openvswitch
原数据都会得到保留。
OVS 卸载:
卸载前保证 OVS上设置的网桥全部删除。
可直接使用命令 卸载。使用ssh连接,最好保证卸载OVS后ssh 还能连上,不然网络会中途断掉,连接失败,卸载也会终止。
yum -y remove openvswitch
(1)报错
(2)原因分析
软件未安装
(3)解决方法
安装软件。
yum install libguestfs-tools
yum install virt-install.noarch
(1)取消定义
# virsh net-undefine ovs01
(2)删除
# virsh net-destroy ovs01
(1)自定义网络
echo "
<network>
<name>ovs01</name>
<uuid></uuid>
<forward mode='bridge' />
<bridge name='ovs01' />
<virtualport type='openvswitch' />
</network>
" > /etc/libvirt/qemu/networks/ovs01.xml
(2)开启网络自动启动
# virsh net-define /etc/libvirt/qemu/networks/ovs01.xml
# virsh net-start /etc/libvirt/qemu/networks/ovs01.xml
# virsh net-list
# cd /autostart/
# virsh net-autostart ovs01