02.网路进阶管理

徐亮伟, 江湖人称标杆徐。多年互联网运维工作经验,曾负责过大规模集群架构自动化运维管理工作。擅长Web集群架构与自动化运维,曾负责国内某大型电商运维工作。
个人博客"徐亮伟架构师之路"累计受益数万人。
笔者Q:552408925、572891887
架构师群:471443208

管理聚合链路和桥接网络

通过网口绑定技术Bonding,实现网络冗余, 负载均衡, 从而提升网络传输能力,避免网络链路单点故障, 达到高可用高可靠的目的。
Bonding的两种绑定工作模式:实际上有7种,其他不常用

模式0 balance-rr负载轮询(2网卡单独都是100MB,聚合为1个网络传输带宽200MB)
模式1 active-backup高可用(其中一条线若断线,其他线路将会自动备援)

                            --> eth0  ----\
    app  --发送数据到--> bond0          <---> switch 
                            --> eth1  ----/

1.1 Linux7配置bond聚合链路

centos7系统配置链路聚合bond

完成bond0, balance-rr

[root@xuliangwei ~]# nmcli device
DEVICE      TYPE      STATE                      CONNECTION
ens32       ethernet  connected                     ens32
ens36       ethernet  disconnected                  --
ens37       ethernet  disconnected                  --


//创建bond0, 模式为balance-rr
[root@xuliangwei ~]# nmcli connection add type bond \
mode balance-rr con-name bond0 ifname bond0 \
ipv4.method manual \
ipv4.addresses 192.168.69.223/24 \
ipv4.gateway 192.168.69.1 \
ipv4.dns 8.8.8.8 

//添加物理网卡连接至bond0
[root@xuliangwei ~]# nmcli connection add type bond-slave \
con-name bond-slave36 ifname ens36 master bond0
[root@xuliangwei ~]# nmcli connection add type bond-slave \
con-name bond-slave37 ifname ens37 master bond0

//查看bond配置信息
[root@xuliangwei ~]# cat /proc/net/bonding/bond0

//关闭ens36网卡, 测试bond0是否正常
[root@xuliangwei ~]# nmcli device disconnect ens36

完成bond1, active-backup

[root@xuliangwei ~]# nmcli device
DEVICE      TYPE      STATE                      CONNECTION
ens32       ethernet  connected                     ens32
ens36       ethernet  disconnected                  --
ens37       ethernet  disconnected                  --

//创建bond1相关设备
[root@xuliangwei ~]# nmcli connection add type bond \
con-name bond1 ifname bond1 mode active-backup \
ipv4.method manual ipv4.addresses '192.168.69.222/24' \
ipv4.gateway='192.168.69.2' ipv4.dns='192.168.69.2' 

//添加连接至bond1
[root@xuliangwei ~]# nmcli connection add type bond-slave \
con-name bond-slave36 ifname ens36 master bond1
[root@xuliangwei ~]# nmcli connection add type bond-slave \
con-name bond-slave37 ifname ens37 master bond1

//启用相关连接
[root@xuliangwei ~]# nmcli connection up bond1
[root@xuliangwei ~]# nmcli connection up bond-slave36
[root@xuliangwei ~]# nmcli connection up bond-slave37



//验证
[root@xuliangwei ~]# cat /proc/net/bonding/bond1
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

Bonding Mode: fault-tolerance (active-backup)
Primary Slave: None
Currently Active Slave: ens36   //目前是ens36网卡提供支撑
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: ens36
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:34:92:06
Slave queue ID: 0

Slave Interface: ens37
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:34:92:10
Slave queue ID: 0

//停止ens36物理网卡设备
[root@xuliangwei ~]# nmcli device disconnect ens36

//ens37物理网卡设备会进行自动切换
[root@xuliangwei ~]# grep "Currently Active Slave" /proc/net/bonding/bond1
Currently Active Slave: ens37

1.2 LInux7配置team聚合链路

centos7/rhce7使用teaming实现聚合链路,能够提供网卡绑定之后的网络吞吐性能,并且提供网卡的故障切换处理能力。
Team是基于一个小型内核驱动实现聚合链路,在用户层提供teamd命令实现链路管理。

teamd可以实现以下模式的聚合链路

broadcast 广播容错
roundrobin 负载轮询
activebackup 主备(必考)
loadbalance 负载均衡
lacp 需要交换机支持lacp协议

//考试建议使用命令行配置,图形界面配置不稳定

[root@xuliangwei ~]# nmcli connection add type team con-name team0 ifname team0 \
config '{"runner":{"name":"activebackup"}}' \
ipv4.addresses 192.168.56.111/24 \
ipv4.gateway 192.168.56.2 \
ipv4.dns 192.168.56.2 ipv4.method manual

[root@xuliangwei ~]# nmcli connection add type team-slave \
con-name team0-port1 ifname eth1 master team0
[root@xuliangwei ~]# nmcli connection add type team-slave \
con-name team0-port2 ifname eth2 master team0


//检查team0状态
[root@xuliangwei ~]# ping -I team0 192.168.56.1
[root@xuliangwei ~]# teamdctl team0 state

//断掉后检测
[root@xuliangwei ~]# nmcli dev disconnect eth1
[root@xuliangwei ~]# teamdctl team0 state

动态修改team模式

// 导出配置进行修改 (man teamd.conf)
[root@xuliangwei ~]# teamdctl team0 config dump > /tmp/team.conf
[root@xuliangwei ~]# vim /tmp/team.conf

//以最新修改的配置选项修改team0属性
[root@xuliangwei ~]# nmcli con mod team0 team.config /tmp/team.conf

//修改之后需要重启team0
[root@xuliangwei ~]# nmcli connection down team0;nmcli connection up team0 
[root@xuliangwei ~]# nmcli connection up team0-port1
[root@xuliangwei ~]# nmcli connection up team0-port2

1.3 Linux7配置brige桥接网络

建立桥接接口

+------eth0-----+   
|   |   |
|    [ br0 ]    |
|   |   |   
|   +--eth0--+  |
|   |   vm   |  |
|   +--------+  |
+---------------+

创建桥接网络br1
[root@xuliangwei ~]# nmcli connection add type bridge \
con-name br1 ifname br1 \
ipv4.addresses 192.168.56.222/24 ipv4.method manual

桥接至eth1
[root@xuliangwei ~]# nmcli connection add type bridge-slave \
con-name br1-port1 ifname eth1 master br1

[root@xuliangwei ~]# ping -I br1 192.168.56.1
[root@xuliangwei ~]# brctl show

1.4 Linux6配置bond链路聚合

适用于RedHat6以及CentOS6

系统 网卡 bond地址 bond模式 bond功能
Centos6.9 eth0: 192.168.56.11 eth1:192.168.56.12 192.168.56.100 模式0 负载均衡
//1.创建绑定网卡配置文件
[root@xuliangwei ~]# cat ifcfg-bond0
DEVICE=bond0
TYPE=Ethernet
ONBOOT=yes
USERCTL=no
BOOTPROTO=static
IPADDR=192.168.56.200
NETMASK=255.255.255.0
GATEWAY=192.168.56.2
DNS1=192.168.56.2
BONDING_OPTS="mode=0 miimon=50" #如果使用模式1将mode修改为1即可

//2.修改eth0和eth1网卡配置文件
[root@xuliangwei ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
USERCTL=no
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
[root@xuliangwei ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
USERCTL=no
BOOTPROTO=none
MASTER=bond0
SLAVE=yes

//3.添加驱动支持bond0
[root@xuliangwei ~]# vim /etc/modprobe.d/bonding.conf
alias bond0 bonding