且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

linux下绑定双网卡

更新时间:2022-05-16 08:46:12

linux 下绑定双网卡


系统环境:

操作系统: RedHat EL55

linux下绑定双网卡


     双网卡绑定单个IP 地址为了提供网络的高可用性,我们可能需要将多块网卡绑定成一块虚拟网卡对外提供服务,这样即使其中的一块物理网卡出现故障,也不会导致连接中断。

    为了提供网络的高可用性,我们可能需要将多块网卡绑定成一块虚拟网卡对外提供服务,这样即使其中的一块物理网卡出现故障,也不会导致连接中断。在Linux下叫bondingIBM称为etherchanelbroadcomteam

1、查看内核是否支持网卡绑定


[root@bjsrv ~]# modinfo bonding

filename:       /lib/modules/2.6.18-194.el5xen/kernel/drivers/net/bonding/bonding.ko

author:         Thomas Davis, tadavis@lbl.gov and many others

description:    Ethernet Channel Bonding Driver, v3.4.0

version:        3.4.0

license:        GPL

srcversion:     A2FB5CCCDB38BE9A099CC9C

depends:        ipv6

vermagic:       2.6.18-194.el5xen SMP mod_unload 686 REGPARM 4KSTACKS gcc-4.1

parm:           max_bonds:Max number of bonded devices (int)

parm:           num_grat_arp:Number of gratuitous ARP packets to send on failover event (int)

parm:           num_unsol_na:Number of unsolicited IPv6 Neighbor Advertisements packets to send on failover event (int)

parm:           miimon:Link check interval in milliseconds (int)

parm:           updelay:Delay before considering link up, in milliseconds (int)

parm:           downdelay:Delay before considering link down, in milliseconds (int)

parm:           use_carrier:Use netif_carrier_ok (vs MII ioctls) in miimon; 0 for off, 1 for on (default) (int)

parm:           mode:Mode of operation : 0 for balance-rr, 1 for active-backup, 2 for balance-xor, 3 for broadcast, 4 for 802.3ad, 5 for balance-tlb, 6 for balance-alb (charp)

parm:           primary:Primary network device to use (charp)

parm:           primary_reselect:Reselect primary slave once it comes up; 0 for always (default), 1 for only if speed of primary is better, 2 for only on active slave failure (charp)

parm:           lacp_rate:LACPDU tx rate to request from 802.3ad partner (slow/fast) (charp)

parm:           xmit_hash_policy:XOR hashing method: 0 for layer 2 (default), 1 for layer 3+4 (charp)

parm:           arp_interval:arp interval in milliseconds (int)

parm:           arp_ip_target:arp targets in n.n.n.n form (array of charp)

parm:           arp_validate:validate src/dst of ARP probes: none (default), active, backup or all (charp)

parm:           fail_over_mac:For active-backup, do not set all slaves to the same MAC.  none (default), active or follow (charp)

parm:           debug:Print debug messages; 0 for off (default), 1 for on (int)

module_sig:     883f3504ba03af81e1fa4939f6a629311282410a0c9263f8f3007023d923512d2ecbb7e81cefc609f41f79cd9bf13e480d0b17e94899d3c4408c6a6

[root@bjsrv ~]#


2、配置虚拟网卡


[root@bjsrv ~]# cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-bond0

[root@bjsrv ~]#


[root@bjsrv ~]# cat /etc/sysconfig/network-scripts/ifcfg-bond0

# Intel Corporation 82540EM Gigabit Ethernet Controller

DEVICE=bond0

BOOTPROTO=static

IPADDR=192.168.10.254

NETMASK=255.255.255.0

GATEWAY=192.168.10.1

ONBOOT=yes

[root@bjsrv ~]#


[root@bjsrv ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1

# Intel Corporation 82540EM Gigabit Ethernet Controller

DEVICE=eth1

BOOTPROTO=none

ONBOOT=yes

MASTER=bond0

SLAVE=yes

[root@bjsrv ~]#


[root@bjsrv ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth2

# Intel Corporation 82540EM Gigabit Ethernet Controller

DEVICE=eth2

BOOTPROTO=none

ONBOOT=yes

MASTER=bond0

SLAVE=none

[root@bjsrv ~]#


[root@bjsrv ~]# grep  bond /etc/modprobe.d/modprobe.conf.dist

alias bond0 bonding

options bond0 miimon=100 mode=1

[root@bjsrv ~]# grep  bond /etc/modprobe.conf

alias bond0 bonding

options bond0 miimon=100 mode=1


三、参数说明


options bond0 miimon=100 mode=1

如果达到负载均衡的目的,应该是0,1是热备状态。

miimon是毫秒数,每100毫秒触发检测线路稳定性的事件。

mode= 是ifenslave的工作状态。

一共有7种方式:

=0: (balance-rr) Round-robin policy: (平衡抡循环策略):传输数据包顺序是依次传输,直到最后一个传输完毕,

     此模式提供负载平衡和容错能力,这需要设置交换机的配合才能完成。

=1: (active-backup) Active-backup policy:(主-备份策略):只有一个设备处于活动状态。

     一个宕掉另一个马上由备份转换为主设备。mac地址是外部可见得。 此模式提供了容错能力。

=2:(balance-xor) XOR policy:(广播策略):将所有数据包传输给所有接口。 此模式提供了容错能力。

=3:(balance-xor) XOR policy:   (平衡 策略): 传输根据原地址布尔值选择传输设备。

    此模式提供负载平衡和容错能力。

=4:(802.3ad) IEEE 802.3ad Dynamic link aggregation.  

    IEEE 802.3ad 动态链接聚合:创建共享相同的速度和双工设置的聚合组。

=5:(balance-tlb) Adaptive transmit load balancing(适配器传输负载均衡)

   :没有特殊策略,第一个设备传不通就用另一个设备接管第一个设备正在处理的mac地址,帮助上一个传。

=6:(balance-alb) Adaptive load balancing: (适配器传输负载均衡):大致意思是包括mode5,bonding驱动程序截获 ARP 在本地系统发送出的请求,用其中之一的硬件地址覆盖从属设备的原地址。就像是在服务器上不同的人使用不同的硬件地址一样。


四、重新启动网络服务


[root@bjsrv ~]#service network restart

Shutting down interface bond0:  [  OK  ]

Shutting down interface eth0:  [  OK  ]

Shutting down loopback interface:  [  OK  ]

Bringing up loopback interface:  [  OK  ]

Bringing up interface bond0:  [  OK  ]

Bringing up interface eth0:  [  OK  ]


五、查看虚拟网卡信息


[root@bjsrv ~]# cat /proc/net/bonding/bond0

Ethernet Channel Bonding Driver: v3.4.0 (October 7, 2008)


Bonding Mode: fault-tolerance (active-backup)

Primary Slave: None

Currently Active Slave: eth1

MII Status: up

MII Polling Interval (ms): 100

Up Delay (ms): 0

Down Delay (ms): 0


Slave Interface: eth1

MII Status: up

Link Failure Count: 0

Permanent HW addr: 08:00:27:99:ce:df


Slave Interface: eth2

MII Status: up

Link Failure Count: 0

Permanent HW addr: 08:00:27:ce:7a:4d

[root@bjsrv ~]#


[root@bjsrv ~]# netstat -nr

Kernel IP routing table

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface

192.168.10.0    0.0.0.0         255.255.255.0   U         0 0          0 bond0

192.168.122.0   0.0.0.0         255.255.255.0   U         0 0          0 virbr0

192.168.8.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0

169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 eth0

0.0.0.0         192.168.10.1    0.0.0.0         UG        0 0          0 bond0

[root@bjsrv ~]#


六、down 掉主网卡测试

linux下绑定双网卡

  在测试中,主网卡down 后,会有连接超时,但会立刻联通备份网卡!

查看虚拟网卡状态:

[root@bjsrv ~]# cat /proc/net/bonding/bond0

Ethernet Channel Bonding Driver: v3.4.0 (October 7, 2008)


Bonding Mode: fault-tolerance (active-backup)

Primary Slave: None

Currently Active Slave: eth2

MII Status: up

MII Polling Interval (ms): 100

Up Delay (ms): 0

Down Delay (ms): 0


Slave Interface: eth1

MII Status: down

Link Failure Count: 1

Permanent HW addr: 08:00:27:99:ce:df


Slave Interface: eth2

MII Status: up

Link Failure Count: 0

Permanent HW addr: 08:00:27:ce:7a:4d

[root@bjsrv ~]#










本文转自 客居天涯 51CTO博客,原文链接:http://blog.51cto.com/tiany/1388453,如需转载请自行联系原作者