悠悠楠杉
Linux网络接口绑定主备模式配置指南
一、什么是主备模式绑定?
主备模式(Active-Backup)是Linux网络接口绑定(Bonding)的一种常见策略,通过将多块物理网卡虚拟为单一逻辑接口,实现网络冗余。当主网卡(Active)发生故障时,系统会自动切换到备用网卡(Backup),确保网络持续可用。
典型应用场景包括:
- 服务器高可用性要求较高的环境
- 物理网卡易损或线路不稳定的场景
二、配置前的准备工作
硬件要求
- 至少两块物理网卡(如eth0、eth1)
- 确认网卡驱动支持bonding(
lsmod | grep bonding
)
内核模块加载
bash sudo modprobe bonding mode=active-backup miimon=100
miimon=100
表示每100毫秒检测一次链路状态。网络工具安装
bash sudo apt install ifenslave # Debian/Ubuntu sudo yum install bonding # RHEL/CentOS
三、详细配置步骤
步骤1:创建Bonding接口
编辑网络配置文件(以Ubuntu 20.04为例):bash
sudo nano /etc/netplan/01-netcfg.yaml
添加以下内容:yaml
network:
version: 2
renderer: networkd
bonds:
bond0:
interfaces: [eth0, eth1]
parameters:
mode: active-backup
primary: eth0 # 指定主网卡
ethernets:
eth0: {}
eth1: {}
步骤2:应用配置
bash
sudo netplan apply
步骤3:验证绑定状态
bash
cat /proc/net/bonding/bond0
输出应包含:plaintext
Bonding Mode: fault-tolerance (active-backup)
Primary Slave: eth0
Currently Active Slave: eth0
步骤4:测试故障切换
- 手动断开主网卡:
bash sudo ifconfig eth0 down
- 观察备用网卡是否接管:
bash ip link show # 检查eth1状态 ping 8.8.8.8 # 测试网络连通性
四、常见问题排查
绑定接口未生效
- 检查内核模块:
lsmod | grep bonding
- 查看日志:
journalctl -xe
- 检查内核模块:
主备切换延迟
- 调整
miimon
参数为更短间隔(如50毫秒)
- 调整
网络性能下降
- 确认交换机端口配置为相同的速率和双工模式
五、进阶优化建议
结合LACP(动态聚合)
若交换机支持,可配置为mode=4
(802.3ad)提升吞吐量。多网卡负载均衡
使用mode=balance-rr
(轮询)实现流量分发。持久化配置
在/etc/modules-load.d/bonding.conf
中添加:plaintext bonding
通过以上步骤,Linux服务器的网络可靠性将显著提升,满足关键业务对连续性的需求。