目录
openvswitch创建linux bridge03-01-01
00-查看是否开启硬件虚拟化支持
如未有值,需要在主机BIOS启动
egrep --color '(vmx|svm )' /proc/cpuinfo
或者
egrep -c '(vmx|svm)' /proc/cpuinfo
执行# lsmod |grep kvm查看kvm模块是否已经加载,如果没有加载,且CPU是Intel,则使用# modprobe kvm-intel 命令加载;AMD CPU执行# modprobe kvm-amd。
01-安装QEMU
yum -y install qemu-kvm
02-安装libvirt
yum -y install libvirt
systemctl start libvirtd
systemctl enable libvirtd
03-实例网络配置
我们可以通过Linux自带bridge进行网络配置,也可以通过openvswitch,二选一即可。
03-01安装openvswitch(二选一)
yum -y install centos-release-openstack-train
yum clean all
yum makecache
yum -y install openvswitch
systemctl enable openvswitch
systemctl start openvswitch
openvswitch创建linux bridge03-01-01
ovs-vsctl add-br br-ext
ip link set br-ext up
ip addr add 192.168.x.x/24 dev br-ext
ip route add default via 192.168.x.x dev br-ext
ip addr flush dev enp3s0f0
ovs-vsctl add-port br-ext enp3s0f0
通过脚本配置03-01-02
如果将管理IP所在interface绑定到网桥,需要将该interface上的IP地址配置到网桥上。否则,会断网。
## 配置网桥
# cat switch2bridge.sh
#!/bin/bash
if [ -f /etc/profile ]; then
. /etc/profile
fi
if [ -f ~/.bash_profile ]; then
. ~/.bash_profile
fi
script_abs=$(readlink -f "$0")
script_dir=$(dirname $script_abs)
if [ $(ip add show br-ext|grep 'global br-ext'|grep 192|wc -l) -eq 0 ]; then
ip addr flush dev enp3s0f0
ip link set br-ext up
ip addr add 192.168.x.x/24 dev br-ext
ip route add default via 192.168.x.x dev br-ext
fi
if [ $(ovs-vsctl list-ports br-ext |grep enp3s0f0 |wc -l) -eq 0 ]; then
ovs-vsctl add-port br-ext enp3s0f0
fi
sleep 5s
$script_dir/recover.sh
## 恢复原有局域网设置
# cat recover.sh
#!/bin/bash
if [ -f /etc/profile ]; then
. /etc/profile
fi
if [ -f ~/.bash_profile ]; then
. ~/.bash_profile
fi
script_abs=$(readlink -f "$0")
script_dir=$(dirname $script_abs)
isConnected=$(ping www.baidu.com -c 2 |grep '2 packets transmitted' |wc -l)
if [ $isConnected -eq 0 ]; then
nmcli c reload && nmcli c up enp3s0f0
if [ $(ovs-vsctl list-ports br-ext |grep enp3s0f0 |wc -l) -eq 1 ]; then
ovs-vsctl del-port br-ext enp3s0f0
fi
if [ $(ip add show br-ext|grep 'global br-ext'|grep 192|wc -l) -eq 1 ]; then
ip route del default via 192.168.x.x dev br-ext
ip addr del 192.168.x.x/23 dev br-ext
ip link set br-ext down
fi
fi
## 注册服务
# cat /usr/lib/systemd/system/bridge-network.service
[Unit]
Description=Bridge Network Service
After=network.target NetworkManager.service
Wants=NetworkManager.service
ConditionFileIsExecutable=/root/switch2bridge.sh
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/root/switch2bridge.sh
TimeoutSec=0
[Install]
WantedBy=multi-user.target
ConditionFileIsExecutable= 检测指定的路径是否存在并且是一个可执行文件,必须使用绝对路径。可以在路径前面加上感叹号(!)前缀,表示逻辑反转。
03-02Linux自带(二选一)
centos6
方法一:/etc/sysconfig/network-scripts/下不会创建文件
创建网桥并添加port
brctl addbr br0
brctl addif br0 eth0
设置流量镜像
brctl setageing br1 0
brctl setfd br1 0
查看网桥及其properties
brctl showstp br0
方法二:/etc/sysconfig/network-scripts/下会创建文件,通过brctl delbr删除网桥之后,需要手动去该目录下删除相应的网络配置文件。
创建网桥并添加port
virsh iface-bridge eth0 br0 [--no-stp] [--delay <number>] [--no-start]
--no-stp do not enable STP for this bridge
--delay <number> number of seconds to squelch traffic on newly connected ports
--no-start don't start the bridge immediately
centos8
创建网桥:/etc/sysconfig/network-scripts/下面会创建文件
nmcli c add type bridge con-name br0 ifname br0 autoconnect yes
添加port到网桥
nmcli c add type bridge-slave ifname ens33 master br0
设置流量镜像
nmcli c modify br0 bridge.forward-delay 2 bridge.ageing-time 0
查看网桥及其properties
nmcli c show br0
重启网卡,令配置生效
nmcli c down ens33
nmcli c up br0
centos7
兼容centos8和centos6的所有方法