一、bonding 的定義
bonding是通過將同一設備的多個物理網卡綁定到一個虛擬網卡上,再對外提供連接。對于外端來說,多個物理網卡共享虛擬網卡的IP和mac地址,也就表現為一個網卡設備。通過bonding技術可以實現高可用或者負載均衡。
bonding有7種工作模式:
Mode 0 (balance- rr)輪轉(Round- robin)策略:從頭到尾順序的在每一個slave接口上面發送數據包。本模式提供負載均衡和容錯的能力
Mode 1 (active-backup)活動-備份(主備)策略:只有一個slave被激活,當且僅當活動的slave接口失敗時才會激活其他slave。 為了避免交換機發生混亂此時綁定的MAC地址只有一個外部端口上可見
Mode 3 (broadcast)廣播策略:在所有的slave接口上傳送所有的報文,提供容錯能力
Mode 2 (balance-xor)XOR policy(平衡策略); Mode 4 Dynamic link aggregation(IEEE802.3ad 動態鏈接聚合);Mode 5 (balance-tlb)Adaptive transmit load balancing(適配器傳輸負載均衡);Mode 6 (balance-alb)Adaptive load balancing(適配器適應性負載均衡)具體請參照內核源碼包文件:/usr/share/doc/kernel-doc-version(ex 3.10.0)/Documentation/networking/bonding.txt (需要安裝kernel-doc包)
二、Centos6 bonding的配置
1. 創建bonding設備bond0的配置文件
#vi /etc/sysconfig/network-scripts/ifcfg-bond0 DEVICE=band0 BONDING_OPTS="mode=1 miimon=100" <-- 工作模式為1;參考1) BOOTPROTO=none IPADDR=192.168.192.100 <-- 指定bond0的IP PREFIX=24 GATEWAY=172.17.0.1
1)miimon 是用來進行鏈路監測的。如果miimon=100,那么系統每100ms 監測一次鏈路連接狀態,如果有一條線路不通就轉入另一條線路
2.修改物理網卡配置文件
#vi ifcfg-eth0 DEVICE=eth0 MASTER=bond0 SLAVE=yes #vi ifcfg-eth1 DEVICE=eth1 MASTER=bond0 SLAVE=yes
3.重啟網絡服務
#service network restart <-- 注意在centos6中要先關閉NetworkManager服務
4.查看bond0狀態
[root@centos6 ~]#cat /proc/net/bonding/bond0 Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011) Bonding Mode: fault-tolerance (active-backup) <-- mode Primary Slave: None Currently Active Slave: eth0 <-- 當前工作的物理網卡 MII Status: up MII Polling Interval (ms): 100 Up Delay (ms): 0 Down Delay (ms): 0 Slave Interface: eth0 MII Status: up Speed: 1000 Mbps Duplex: full Link Failure Count: 0 Permanent HW addr: 00:0c:29:e2:f9:b2 Slave queue ID: 0 Slave Interface: eth1 MII Status: up Speed: 1000 Mbps Duplex: full Link Failure Count: 0 Permanent HW addr: 00:50:56:28:0b:0c Slave queue ID: 0
[root@centos6 ~]#ifconfig bond0 Link encap:Ethernet HWaddr 00:0C:29:E2:F9:B2 <--mac地址一致 inet addr:192.168.196.100 Bcast:192.168.196.255 Mask:255.255.255.0 <--IP唯一 inet6 addr: fe80::20c:29ff:fee2:f9b2/64 Scope:Link UP BROADCAST RUNNING MASTER MULTICAST MTU:1500 Metric:1 RX packets:111 errors:0 dropped:0 overruns:0 frame:0 TX packets:85 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:12290 (12.0 KiB) TX bytes:12131 (11.8 KiB) eth0 Link encap:Ethernet HWaddr 00:0C:29:E2:F9:B2 UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1 RX packets:1375 errors:0 dropped:0 overruns:0 frame:0 TX packets:819 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:139581 (136.3 KiB) TX bytes:142252 (138.9 KiB) eth1 Link encap:Ethernet HWaddr 00:0C:29:E2:F9:B2 UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1 RX packets:353 errors:0 dropped:0 overruns:0 frame:0 TX packets:70 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:28577 (27.9 KiB) TX bytes:8274 (8.0 KiB)
[root@centos6 ~]#ping 192.162.196.100 <--另一個主機可以ping通 PING 192.168.196.100 (192.168.196.100) 56(84) bytes of data. 64 bytes from 192.168.196.100: icmp_seq=1 ttl=64 time=0.842 ms 64 bytes from 192.168.196.100: icmp_seq=2 ttl=64 time=1.82 ms 64 bytes from 192.168.196.100: icmp_seq=3 ttl=64 time=0.423 ms
5. 更改bond0工作模式
直接修改bond0配置文件中的mode,重啟網絡服務即可
6.如何刪除bond0
[root@centos6 ~]#ifconfig bond0 down [root@centos6 ~]#rm -f ifcfg-bond0 刪除第2步物理網卡配置文件中的MASTER&SLAVE條目 [root@centos6 ~]#rmmod bonding
三、Centos7 bonding的配置
1) 使用nmcli管理工具
1.創建邏輯網卡bond0
[root@centos7 network-scripts]#nmcli con add type bond con-name bond0 ifname bond0 mode active-backup Connection 'bond0' (c505387c-6f85-4af5-bf50-7958d8cf39ef) successfully added. #add 相當與自動生成配置文件 [root@centos7 network-scripts]#nmcli con modify bond0 ipv4.addresses 192.168.196.100/24 ipv4.method manual <--設定bond0 ip
2. 添加從屬網絡接口
[root@centos7 network-scripts]#nmcli con ad type bond-slave ifname eth0 master bond0 <--綁定物理網卡 Connection 'bond-slave-eth0' (92332d4d-5d26-430f-843a-2403326e1861) successfully added. [root@centos7 network-scripts]#nmcli con ad type bond-slave ifname eth1 master bond0 <--綁定物理網卡 Connection 'bond-slave-eth1' (5f66a399-098d-4fde-9828-5f4461ea2196) successfully added.
3. 關聯物理網卡
[root@centos7 network-scripts]#nmcli connection show <--查看當前鏈接狀態 NAME UUID TYPE DEVICE bond0 c505387c-6f85-4af5-bf50-7958d8cf39ef bond bond0 eth0 c01fcf0b-866d-3df4-84be-67e72dc6821f 802-3-ethernet eth0 eth1 fcaec399-151c-3b67-8cd1-498a17e3fab9 802-3-ethernet eth1 virbr0 761fc9c3-2c1a-43fc-9181-978bfe2e9cb1 bridge virbr0 bond-slave-eth0 92332d4d-5d26-430f-843a-2403326e1861 802-3-ethernet -- <--配置要關聯物理網卡 bond-slave-eth1 5f66a399-098d-4fde-9828-5f4461ea2196 802-3-ethernet -- <--配置要關聯物理網卡 [root@centos7 network-scripts]#nmcli con up bond-slave-eth0 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7) [root@centos7 network-scripts]#nmcli con up bond-slave-eth1 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/8) [root@centos7 network-scripts]#nmcli connection show NAME UUID TYPE DEVICE bond-slave-eth0 92332d4d-5d26-430f-843a-2403326e1861 802-3-ethernet eth0 bond-slave-eth1 5f66a399-098d-4fde-9828-5f4461ea2196 802-3-ethernet eth1 bond0 c505387c-6f85-4af5-bf50-7958d8cf39ef bond bond0 virbr0 761fc9c3-2c1a-43fc-9181-978bfe2e9cb1 bridge virbr0 eth0 c01fcf0b-866d-3df4-84be-67e72dc6821f 802-3-ethernet -- eth1 fcaec399-151c-3b67-8cd1-498a17e3fab9 802-3-ethernet -- [root@centos7 network-scripts]#nmcli con up bond0 Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/9)
4.查看bond0狀態 參考上文Centos6中步驟4
5.更改bond0模式
[root@centos7 network-scripts]#nmcli con modify bond0 mode broadcast [root@centos7 network-scripts]#nmcli con up bond0 Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/12)
6.刪除bond0
方法1
[root@centos7 network-scripts]#nmcli con down bond0 <--相當于刪除配置文件并生效 Connection 'bond0' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/12) [root@centos7 network-scripts]#nmcli con del bond-slave-eth0 Connection 'bond-slave-eth0' (92332d4d-5d26-430f-843a-2403326e1861) successfully deleted. [root@centos7 network-scripts]#nmcli con del bond-slave-eth1 Connection 'bond-slave-eth1' (5f66a399-098d-4fde-9828-5f4461ea2196) successfully deleted.
方法2
[root@centos7 network-scripts]rm -f ifcfg-bond* <--直接刪除配置文件 [root@centos7 network-scripts]nmcli con reload
2)創建網絡組Networking Teaming實現類似功能
網絡組:是將多個網卡聚合在一起方法,從而實現冗錯和提高吞吐量。但網絡組不同于舊版中bonding技術,提供更好的性能和擴展性。網絡組由內核驅動和teamd守護進程實現
同樣利用nmcli工具,其步驟與創建bonding設備類似
1.創建網絡組team0
[root@centos7 network-scripts]#nmcli con add type team con-name team0 ifname team0 config '{"runner":{"name":"activebackup"}}' ipv4.method manual ipv4.addresses 192.168.196.100/24 Connection 'team0' (83620f8e-aad2-4de9-b793-2f11faf9ca7a) successfully added.
2. 添加從屬網絡接口
[root@centos7 network-scripts]#nmcli con add type team-slave ifname eth0 master team0 Connection 'team-slave-eth0' (370f85d6-a68c-4d5c-b994-202effada86a) successfully added. [root@centos7 network-scripts]#nmcli con add type team-slave ifname eth1 master team0
3. 關聯物理網卡
[root@centos7 network-scripts]#nmcli con up team-slave-eth1 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/19) [root@centos7 network-scripts]#nmcli con up team-slave-eth0 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/18)
4.查看team0狀態
[root@centos7 network-scripts]#teamdctl team0 stat setup: runner: activebackup ports: eth0 link watches: link summary: up instance[link_watch_0]: name: ethtool link: up down count: 0 eth1 link watches: link summary: up instance[link_watch_0]: name: ethtool link: up down count: 0 runner: active port: eth0
5.更改team0模式
[root@centos7 network-scripts]#nmcli con mod team0 config '{"runner":{"name":"broadcast"}}' [root@centos7 network-scripts]#nmcli con up team0 Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/35)
6.刪除team0 –> 參考上文Centos7中第6步
網絡組還有幾個特點:
啟動網絡組接口不會自動啟動網絡組中的port接口
啟動網絡組接口中的port接口總會自動啟動網絡組接口
禁用網絡組接口會自動禁用網絡組中的port接口
沒有port接口的網絡組接口可以啟動靜態IP連接 –>在沒有關聯網卡的情況下
啟用DHCP連接時,沒有port接口的網絡組會等待port接口的加入
原創文章,作者:ffu,如若轉載,請注明出處:http://www.www58058.com/79092