CentOS7 安装 OpenVSwitch
[root@ci-cd ~]# yum install -y openvswitch
[root@ci-cd ~]# ovs-vsctl -V
ovs-vsctl (Open vSwitch) 2.0.0
Compiled Apr 19 2018 17:57:34
启动服务:
[root@ci-cd ~]# systemctl start openvswitch.service
[root@ci-cd ~]# systemctl status openvswitch.service
● openvswitch.service - Open vSwitch
Loaded: loaded (/usr/lib/systemd/system/openvswitch.service; disabled; vendor preset: disabled)
Active: active (exited) since Fri 2019-08-16 16:43:40 CST; 3s ago
Process: 9224 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 9224 (code=exited, status=0/SUCCESS)
Aug 16 16:43:40 ci-cd systemd[1]: Starting Open vSwitch...
Aug 16 16:43:40 ci-cd systemd[1]: Started Open vSwitch.
[root@ci-cd ~]# systemctl enable openvswitch.service
创建ovs网桥:
[root@ci-cd ~]# ovs-vsctl add-br ovs-br0
[root@ci-cd ~]# ifconfig
eth0: flags=4163 mtu 1500
inet 192.168.247.129 netmask 255.255.255.0 broadcast 192.168.247.255
inet6 fe80::20c:29ff:fe5e:aa08 prefixlen 64 scopeid 0x20
ether 00:0c:29:5e:aa:08 txqueuelen 1000 (Ethernet)
RX packets 13166 bytes 18407974 (17.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6203 bytes 379950 (371.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4163 mtu 1500
inet 192.168.81.129 netmask 255.255.255.0 broadcast 192.168.81.255
inet6 fe80::20c:29ff:fe5e:aa12 prefixlen 64 scopeid 0x20
ether 00:0c:29:5e:aa:12 txqueuelen 1000 (Ethernet)
RX packets 648 bytes 65345 (63.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 397 bytes 111670 (109.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth2: flags=4163 mtu 1500
inet 10.116.0.19 netmask 255.255.254.0 broadcast 10.116.1.255
inet6 fe80::767e:62e2:d940:8aa2 prefixlen 64 scopeid 0x20
ether 00:0c:29:5e:aa:1c txqueuelen 1000 (Ethernet)
RX packets 944 bytes 125307 (122.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 166 bytes 59075 (57.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 2851 bytes 2195567 (2.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2851 bytes 2195567 (2.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ovs-br0: flags=67 mtu 1500
inet6 fe80::8c46:78ff:fe59:ffca prefixlen 64 scopeid 0x20
ether 76:59:64:3c:bf:4d txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5 bytes 426 (426.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
清空一个网口(如本次实验中eth1网卡)的IP地址,并把此IP添加到刚才创建的网桥中:
[root@ci-cd ~]# ip addr flush dev eth1
[root@ci-cd ~]# ip addr add 192.168.81.129/24 dev ovs-br0
[root@ci-cd ~]# ifconfig ovs-br0
ovs-br0: flags=67 mtu 1500
inet 192.168.81.129 netmask 255.255.255.0 broadcast 0.0.0.0
inet6 fe80::8c46:78ff:fe59:ffca prefixlen 64 scopeid 0x20
ether 76:59:64:3c:bf:4d txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 8 bytes 656 (656.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# 把eth1加入ovs-br0网桥中
[root@ci-cd ~]# ovs-vsctl add-port ovs-br0 eth1
[root@ci-cd ~]# ip link set dev ovs-br0 up
上述的操作都是临时生效的配置,要想后续生效,需要修改配置文件:
[root@ci-cd ~]# cd /etc/sysconfig/network-scripts/
[root@ci-cd network-scripts]# cp ifcfg-eth1 ifcfg-ovs-br0
[root@ci-cd network-scripts]# cat ifcfg-eth1
DEVICE=eth1
TYPE=OVSPort
DEVICETYPE=ovs
NAME=eth1
OVS_BRIDGE=ovs-br0
ONBOOT=yes
[root@ci-cd network-scripts]# cat ifcfg-ovs-br0
DEVICE=ovs-br0
DEVICETYPE=ovs
TYPE=OVSBridge
BOOTPROTO=static
IPADDR=192.168.81.129
NETMASK=255.255.255.0
GATEWAY=192.168.81.1
ONBOOT=yes
[root@ci-cd network-scripts]# systemctl restart network
验证刚才的操作(ovs桥接及其端口):
[root@ci-cd ~]# ovs-vsctl show
a2336910-6be8-4008-a7e7-7df048b34a72
Bridge "ovs-br0"
Port "eth1"
Interface "eth1"
Port "ovs-br0"
Interface "ovs-br0"
type: internal
ovs_version: "2.0.0"
定义virsh网络:
[root@ci-cd ~]# cat << EOF > /tmp/ovs-network.xml
ovs-network
EOF
在KVM中定义一个ovs-network:
[root@ci-cd ~]# virsh net-define /tmp/ovs-network.xml
[root@ci-cd ~]# virsh net-start ovs-network
[root@ci-cd ~]# virsh net-autostart ovs-network
查看刚才的网络:
[root@ci-cd ~]# virsh net-list
Name State Autostart Persistent
----------------------------------------------------------
default active yes yes
ovs-network active yes yes
创建一个虚拟机:
[root@ci-cd ~]# virt-install \
-n testvm1 \
--description "Test VM1 for OVS" \
--os-type=Linux \
--os-variant=rhel7 \
--ram=1096 \
--vcpus=1 \
--disk path=/var/lib/libvirt/images/testvm1.img,bus=virtio,size=10 \
--network network:ovs-network \
--graphics none \
--location /root/CentOS-7-x86_64-DVD-1511.iso \
--extra-args console=ttyS0
查看网桥信息:
[root@ci-cd ~]# ovs-vsctl show
8dc5f8e7-0e54-4d9d-ba7a-cd6b9b94f470
Bridge "ovs-br0"
Port "ovs-br0"
Interface "ovs-br0"
type: internal
Port "eth1"
Interface "eth1"
Port "vnet0"
Interface "vnet0"
Port "vnet1"
Interface "vnet1"
ovs_version: "2.9.2"
[root@ci-cd ~]# ovs-vsctl list-ports ovs-br0
eth1
vnet0
vnet1