网络管理 - 网络进阶管理

1. 管理聚合链路和桥接网络概述

通过网口绑定技术Bonding,实现网络冗余, 负载均衡, 从而提升网络传输能力,避免网络链路单点故障, 达到高可用高可靠的目的。

1.1 链路聚合的优势

  1. 网络冗余: 避免单点故障
  2. 负载均衡: 提升网络传输能力
  3. 高可用: 自动故障切换
  4. 带宽聚合: 多个网卡带宽叠加

1.2 Bonding工作模式

Bonding的两种绑定工作模式:实际上有7种,其他不常用。

模式 名称 说明 特点
0 balance-rr 负载轮询 2网卡单独都是100MB,聚合为1个网络传输带宽200MB
1 active-backup 高可用 其中一条线若断线,其他线路将会自动备援
2 balance-xor 负载均衡 基于XOR哈希算法
3 broadcast 广播容错 所有数据包在所有接口上传输
4 802.3ad LACP动态聚合 需要交换机支持LACP协议
5 balance-tlb 适配器传输负载均衡 根据每个slave的负载情况分配流量
6 balance-alb 适配器适应性负载均衡 包括balance-tlb和接收负载均衡

1.1 Linux7配置bond聚合链路

1.1.1 配置bond0 (balance-rr模式)

步骤1: 查看网卡状态

1
2
3
4
5
[root@linux-node1 ~]# nmcli device
DEVICE TYPE STATE CONNECTION
ens32 ethernet connected ens32
ens36 ethernet disconnected –
ens37 ethernet disconnected –

步骤2: 创建bond0

1
2
3
4
5
6
7
# 创建bond0, 模式为balance-rr
[root@linux-node1 ~]# 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

步骤3: 添加物理网卡连接至bond0

1
2
3
4
5
6
# 添加物理网卡连接至bond0
[root@linux-node1 ~]# nmcli connection add type bond-slave \
con-name bond-slave36 ifname ens36 master bond0

[root@linux-node1 ~]# nmcli connection add type bond-slave \
con-name bond-slave37 ifname ens37 master bond0

步骤4: 激活连接

1
2
3
4
# 激活bond0和slave连接
[root@linux-node1 ~]# nmcli connection up bond0
[root@linux-node1 ~]# nmcli connection up bond-slave36
[root@linux-node1 ~]# nmcli connection up bond-slave37

步骤5: 查看bond配置信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 查看bond配置信息
[root@linux-node1 ~]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

Bonding Mode: load balancing (round-robin)
MII Status: up
MII Polling Interval (ms): 0
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: ens36
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0

Slave Interface: ens37
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0

步骤6: 测试bond0

1
2
3
4
5
# 关闭ens36网卡, 测试bond0是否正常
[root@linux-node1 ~]# nmcli device disconnect ens36

# 查看bond0状态
cat /proc/net/bonding/bond0

1.1.2 配置bond1 (active-backup模式)

步骤1: 查看网卡状态

1
2
3
4
5
[root@linux-node1 ~]# nmcli device
DEVICE TYPE STATE CONNECTION
ens32 ethernet connected ens32
ens36 ethernet disconnected –
ens37 ethernet disconnected –

步骤2: 创建bond1

1
2
3
4
5
6
7
# 创建bond1相关设备
[root@linux-node1 ~]# 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'

步骤3: 添加连接至bond1

1
2
3
4
5
6
# 添加连接至bond1
[root@linux-node1 ~]# nmcli connection add type bond-slave \
con-name bond-slave36 ifname ens36 master bond1

[root@linux-node1 ~]# nmcli connection add type bond-slave \
con-name bond-slave37 ifname ens37 master bond1

步骤4: 启用相关连接

1
2
3
4
# 启用相关连接
[root@linux-node1 ~]# nmcli connection up bond1
[root@linux-node1 ~]# nmcli connection up bond-slave36
[root@linux-node1 ~]# nmcli connection up bond-slave37

步骤5: 验证bond1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 验证
[root@linux-node1 ~]# 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

步骤6: 测试故障切换

1
2
3
4
5
6
# 停止ens36物理网卡设备
[root@linux-node1 ~]# nmcli device disconnect ens36

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

1.2 Linux7配置team聚合链路

centos7/rhce7使用teaming实现聚合链路,能够提供网卡绑定之后的网络吞吐性能,并且提供网卡的故障切换处理能力。

1.2.1 Team概述

Team是基于一个小型内核驱动实现聚合链路,在用户层提供teamd命令实现链路管理。

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

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

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

1.2.2 配置team0 (activebackup模式)

步骤1: 创建team0

1
2
3
4
5
[root@linux-node1 ~]# 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

步骤2: 添加team-slave

1
2
3
4
5
[root@linux-node1 ~]# nmcli connection add type team-slave \
con-name team0-port1 ifname eth1 master team0

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

步骤3: 激活连接

1
2
3
[root@linux-node1 ~]# nmcli connection up team0
[root@linux-node1 ~]# nmcli connection up team0-port1
[root@linux-node1 ~]# nmcli connection up team0-port2

步骤4: 检查team0状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 检查team0状态
[root@linux-node1 ~]# ping -I team0 192.168.56.1
[root@linux-node1 ~]# teamdctl team0 state
{
"runner": {
"active_port": "eth1",
"hwaddr": "00:0c:29:34:92:06",
"ports": {
"eth1": {
"link": {
"up": true
}
},
"eth2": {
"link": {
"up": true
}
}
}
}
}

步骤5: 测试故障切换

1
2
3
4
5
6
7
8
9
# 断掉后检测
[root@linux-node1 ~]# nmcli dev disconnect eth1
[root@linux-node1 ~]# teamdctl team0 state
{
"runner": {
"active_port": "eth2", # 已切换到eth2
...
}
}

1.2.3 动态修改team模式

步骤1: 导出配置

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

步骤2: 修改配置

1
2
3
4
5
6
# 修改为roundrobin模式
{
"runner": {
"name": "roundrobin"
}
}

步骤3: 应用配置

1
2
3
4
5
6
7
8
# 以最新修改的配置选项修改team0属性
[root@linux-node1 ~]# nmcli con mod team0 team.config /tmp/team.conf

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

1.3 Linux7配置bridge桥接网络

1.3.1 Bridge概述

桥接网络用于连接不同的网络段,常用于虚拟化环境中,让虚拟机能够访问物理网络。

1.3.2 创建桥接网络br1

步骤1: 创建桥接接口

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

步骤2: 桥接至eth1

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

步骤3: 激活连接

1
2
[root@linux-node1 ~]# nmcli connection up br1
[root@linux-node1 ~]# nmcli connection up br1-port1

步骤4: 验证桥接

1
2
3
4
5
6
7
# 测试桥接网络
[root@linux-node1 ~]# ping -I br1 192.168.56.1

# 查看桥接状态
[root@linux-node1 ~]# brctl show
bridge name bridge id STP enabled interfaces
br1 8000.000c29349206 no eth1

1.3.3 Bridge管理命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 安装bridge-utils(如果未安装)
yum install -y bridge-utils

# 查看桥接信息
brctl show

# 查看桥接详细信息
brctl showstp br1

# 添加接口到桥接
brctl addif br1 eth2

# 从桥接移除接口
brctl delif br1 eth2

1.4 Linux6配置bond链路聚合

适用于RedHat6以及CentOS6。

1.4.1 创建绑定网卡配置文件

1
2
3
4
5
6
7
8
9
10
11
12
# 创建bond0配置文件
[root@liyanzhao ~]# cat /etc/sysconfig/network-scripts/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即可

1.4.2 修改eth0和eth1网卡配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 修改eth0网卡配置文件
[root@liyanzhao ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
USERCTL=no
BOOTPROTO=none
MASTER=bond0
SLAVE=yes

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

1.4.3 添加驱动支持bond0

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

1.4.4 重启网络服务

1
2
3
4
5
# 重启网络服务
[root@liyanzhao ~]# service network restart

# 或
[root@liyanzhao ~]# /etc/init.d/network restart

1.4.5 验证bond配置

1
2
3
4
5
# 查看bond状态
cat /proc/net/bonding/bond0

# 查看网络接口
ifconfig bond0

2. CentOS 8双网卡绑定

2.1 NAT网络配置(所有服务器)

安装必要工具

1
# yum install bash-completion

2.2 bond0配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# cd /etc/sysconfig/network-scripts/

# bond0配置
# vim ifcfg-bond0
BOOTPROTO=static
NAME=bond0
DEVICE=bond0
ONBOOT=yes
BONDING_MASTER=yes
BONDING_OPTS="mode=1 miimon=100" # 指定绑定类型为1及链路状态监测间隔时间
IPADDR=192.168.10.21
NETMASK=255.255.255.0
GATEWAY=192.168.10.2
DNS1=202.106.0.20

2.3 eth0配置

1
2
3
4
5
6
7
8
# cat ifcfg-eth0
BOOTPROTO=static
NAME=eth0
DEVICE=eth0
ONBOOT=yes
MASTER=bond0
USERCTL=no
SLAVE=yes

2.4 eth1配置

1
2
3
4
5
6
7
8
# vim ifcfg-eth1
BOOTPROTO=static
NAME=eth1
DEVICE=eth1
ONBOOT=yes
MASTER=bond0
USERCTL=no
SLAVE=yes

2.5 仅主机网络配置

bond1配置

1
2
3
4
5
6
7
8
9
10
11
12
# cd /etc/sysconfig/network-scripts/

# Bond1配置
# vim ifcfg-bond1
BOOTPROTO=static
NAME=bond1
DEVICE=bond1
ONBOOT=yes
BONDING_MASTER=yes
BONDING_OPTS="mode=1 miimon=100" # 指定绑定类型为1及链路状态监测间隔时间
IPADDR=192.168.20.21
NETMASK=255.255.255.0

eth2配置

1
2
3
4
5
6
7
8
# vim ifcfg-eth2
BOOTPROTO=static
NAME=eth2
DEVICE=eth2
ONBOOT=yes
MASTER=bond1
USERCTL=no
SLAVE=yes

eth3配置

1
2
3
4
5
6
7
8
# vim ifcfg-eth3
BOOTPROTO=static
NAME=eth3
DEVICE=eth3
ONBOOT=yes
MASTER=bond1
USERCTL=no
SLAVE=yes

2.6 查看网卡状态

1
2
3
4
5
6
7
8
9
10
11
# 重新加载配置
# nmcli c reload

# 激活网卡
# nmcli c up eth0
# nmcli c up eth1
# nmcli c up eth2
# nmcli c up eth3

# 查看IP地址
# ip a

输出示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever

2: eth0: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc fq_codel master bond0 state UP group default qlen 1000
link/ether 00:0c:29:9d:a6:a6 brd ff:ff:ff:ff:ff:ff

3: eth1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc fq_codel master bond0 state UP group default qlen 1000
link/ether 00:0c:29:9d:a6:a6 brd ff:ff:ff:ff:ff:ff

4: eth2: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc fq_codel master bond1 state UP group default qlen 1000
link/ether 00:0c:29:9d:a6:ba brd ff:ff:ff:ff:ff:ff

5: eth3: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc fq_codel master bond1 state UP group default qlen 1000
link/ether 00:0c:29:9d:a6:ba brd ff:ff:ff:ff:ff:ff

6: bond1: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 00:0c:29:9d:a6:ba brd ff:ff:ff:ff:ff:ff
inet 192.168.20.50/24 brd 192.168.20.255 scope global noprefixroute bond1
valid_lft forever preferred_lft forever

7: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 00:0c:29:9d:a6:a6 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.50/24 brd 192.168.10.255 scope global noprefixroute bond0
valid_lft forever preferred_lft forever

2.7 内外网通信测试

1
2
3
4
5
# 测试外网连通性
# ping www.baidu.com

# 测试内网连通性
# ping 192.168.20.2

2.8 更新yum源

1
2
3
4
5
6
7
# yum install wget –y
# rm -rf /etc/yum.repos.d/
# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# yum install –y centos-release-openstack-ocata.noarch
# yum install -y https://rdoproject.org/repos/rdo-release.rpm
# yum clean all
# yum makecache

: 更新阿里云openstack-ocata源,需更改rdo.repo, openstack-ocata.repo中的源为:

  • http://mirrors.aliyun.com/centos/7/cloud/x86_64/openstack-ocata/
  • http://mirrors.aliyun.com/centos/7/cloud/x86_64/openstack-queens/

2.9 查看yum源版本

1
2
# 查看yum源版本
yum list centos-release-openstack*

2.10 安装常用命令

1
2
# 安装常用命令
# yum install -y net-tools vim lrzsz tree screen lsof ntpdate telnet wget

3. Bond与Team对比

3.1 对比说明

特性 Bond Team
内核支持 内核驱动 内核驱动+用户空间
配置方式 配置文件/nmcli nmcli/teamd
灵活性 较低 较高
性能
适用版本 CentOS6/7/8 CentOS7/8

3.2 选择建议

  • CentOS6: 使用Bond(配置文件方式)
  • CentOS7: 推荐使用Team,也支持Bond
  • CentOS8: 推荐使用Bond(nmcli方式)

4. 链路聚合最佳实践

4.1 模式选择建议

场景 推荐模式 说明
负载均衡 balance-rr (0) 需要提升带宽
高可用 active-backup (1) 需要故障切换
LACP支持 802.3ad (4) 交换机支持LACP

4.2 配置建议

  1. 网卡选择: 使用相同型号和速度的网卡
  2. 交换机配置: 某些模式需要交换机支持
  3. 监控: 定期检查bond/team状态
  4. 测试: 配置后测试故障切换功能

4.3 故障排查

1
2
3
4
5
6
7
8
9
10
11
# 查看bond状态
cat /proc/net/bonding/bond0

# 查看team状态
teamdctl team0 state

# 查看网络接口
ip link show

# 查看网络统计
ip -s link show bond0

5. 命令总结

5.1 Bond管理命令

命令 功能 示例
nmcli con add type bond 创建bond nmcli con add type bond ...
nmcli con add type bond-slave 添加slave nmcli con add type bond-slave ...
cat /proc/net/bonding/bond0 查看bond状态 cat /proc/net/bonding/bond0

5.2 Team管理命令

命令 功能 示例
nmcli con add type team 创建team nmcli con add type team ...
nmcli con add type team-slave 添加slave nmcli con add type team-slave ...
teamdctl team0 state 查看team状态 teamdctl team0 state
teamdctl team0 config dump 导出配置 teamdctl team0 config dump

5.3 Bridge管理命令

命令 功能 示例
nmcli con add type bridge 创建bridge nmcli con add type bridge ...
nmcli con add type bridge-slave 添加slave nmcli con add type bridge-slave ...
brctl show 查看bridge brctl show

6. 实战案例

6.1 案例1: 生产环境双网卡绑定(高可用)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/bash
# 生产环境双网卡绑定脚本(active-backup模式)

# 1. 创建bond0
nmcli connection add type bond \
con-name bond0 ifname bond0 mode active-backup \
ipv4.method manual \
ipv4.addresses 192.168.1.100/24 \
ipv4.gateway 192.168.1.1 \
ipv4.dns 8.8.8.8

# 2. 添加slave
nmcli connection add type bond-slave \
con-name bond0-eth0 ifname eth0 master bond0

nmcli connection add type bond-slave \
con-name bond0-eth1 ifname eth1 master bond0

# 3. 激活连接
nmcli connection up bond0
nmcli connection up bond0-eth0
nmcli connection up bond0-eth1

# 4. 验证
cat /proc/net/bonding/bond0

6.2 案例2: 虚拟化环境桥接网络

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
# 虚拟化环境桥接网络配置

# 1. 创建桥接
nmcli connection add type bridge \
con-name br0 ifname br0 \
ipv4.method manual \
ipv4.addresses 192.168.1.200/24 \
ipv4.gateway 192.168.1.1

# 2. 添加物理网卡到桥接
nmcli connection add type bridge-slave \
con-name br0-eth0 ifname eth0 master br0

# 3. 激活
nmcli connection up br0
nmcli connection up br0-eth0

# 4. 验证
brctl show

实战优化