Linux中高階網路配置(鏈路聚合BOND&TEAM、橋接)
阿新 • • 發佈:2018-11-21
BOND&TEAM
讓Linux核心支援網絡卡繫結驅動。常見的網絡卡繫結驅動有三種模式——mode0、mode1、mode6。
mode0(平衡負載模式):平時兩塊網絡卡均工作,且自動備援,但需要在與伺服器本地網絡卡相連的交換機裝置上進行埠聚合來支援繫結技術。
mode1(平衡備援模式):平時只有一塊網絡卡工作,在它故障後自動替換為另外的網絡卡。
mode6(平衡負載模式):平時兩塊網絡卡工作,且自動備援,無需交換機裝置提供輔助支援。
BOND:
網絡卡bond是通過把多張網絡卡繫結為一個邏輯網絡卡,實現本地網絡卡的冗餘,頻寬擴容和負載均衡。在應用部署中是一種常用的技術。bond不支援負載均衡。
以命令方式進行配置
命令後面沒有解釋的會在圖片中詳細解釋其引數代表的含義
[[email protected] ~]# ifconfig 檢視也沒有以及配置的網絡卡有的話刪除
[[email protected] ~]# nm-connection-editor 以圖形介面刪除,或者/etc/sysconfig/network-scripts/刪除其檔案
[[email protected] ~]# nmcli connection add con-name bond0 ifname bond0 type bond mode active-backup ip4 172.25.254.160/24
[ [email protected] ~]# watch -n 1 cat /proc/net/bonding/bond0 監控命令
[[email protected] ~]# nmcli connection add con-name eth1 ifname eth1 type bond-slave master bond0
[[email protected] ~]# nmcli connection add con-name eth1 ifname eth1 type bond-slave master bond0
[[email protected] ~]# ifconfig eth0 down 後面這三條命令為測試命令
[ [email protected] ~]# ifconfig eth1 down 控制變數法測試
[[email protected] ~]# ifconfig eth1 up
這裡介紹以下nmcli 以及nmcli控制 NetworkManager
Red Hat Enterprise Linux 7 與 CentOS 7 中預設的網路服務由 NetworkManager 提供,這是動態控制及配置網路的守護程序,它用於保持當前網路裝置及連線處於工作狀態,同時也支援傳統的 ifcfg 型別的配置檔案。
NetworkManager 可以用於以下型別的連線:Ethernet,VLANS,Bridges,Bonds,Teams,Wi-Fi,mobile boradband(如移動3G)以及 IP-over-InfiniBand。針對與這些網路型別,NetworkManager 可以配置他們的網路別名,IP 地址,靜態路由,DNS,VPN連線以及很多其它的特殊引數。
可以用命令列工具 nmcli 來控制 NetworkManager。
在CentOS / RHEL 7中網路管理命令列工具,也叫nmcli。經常使用ifconfig的使用者應該在CentOS 7中避免使用ifconfig了。nmcli的功能要強大、複雜的多。
地址配置工具:nmcli
nmcli – command-line tool for controlling NetworkManager
命令語法:
nmcli[ OPTIONS ] OBJECT { COMMAND | help }
OBJECT和COMMAND可以用全稱也可以用簡稱,最少可以只用一個字母,建議用頭三個字母。OBJECT裡面我們平時用的最多的就是connection和device,這裡需要簡單區分一下connection和device。
device叫網路介面,是物理裝置
device -show and manage network interfaces
nmcli device help
connection是連線,偏重於邏輯設定
connection -start, stop, and manage network connections
nmcli connection help
多個connection可以應用到同一個device,但同一時間只能啟用其中一個connection。這樣的好處是針對一個網路介面,我們可以設定多個網路連線,比如靜態IP和動態IP,再根據需要up相應connection
con add – 新增新的連線
con-name – 連線名
type – 裝置型別
ifname – 介面名
autoconnect no – 禁止開機自動啟動
以編輯配置檔案方式進行配置
[[email protected] ~]# nm-connection-editor 刪除,不要直接刪除配置檔案
[[email protected] network-scripts]# vim ifcfg-bond0
[[email protected] network-scripts]# vim ifcfg-eth0
[[email protected] network-scripts]# vim ifcfg-eth1
[[email protected] network-scripts]# systemctl restart network 記得重啟網路
[[email protected] network-scripts]# nmcli connection show
[[email protected] network-scripts]# cat ifcfg-bond0
DEVICE=bond0 名稱為bond0
ONBOOT=yes
BOOTRPOTO=none
IPADDR=172.25.254.160
NETMASK=255.255.255.0
TYPE=Bond 型別為Bond,這裡B大寫
BONDING_OPTS=mode=active-backup 輪循模式
[[email protected] network-scripts]# cat ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTRPOTO=none
MASTER=bond0 指向bond0下
[[email protected] network-scripts]# cat ifcfg-eth1
DEVICE=eth1
ONBOOT=yes
BOOTRPOTO=none 指向bond0下
MASTER=bond0
### 然後用另一臺主機進行測試就行了
TEAM
在 linux 中, centos7之前都是使用bond機制來實現多網路繫結同一個IP 地址,來對網路提供訪問,並按不同的模式來負載均衡或者輪迴接替管理處理資料。而到了contos7之後,提供了一種強大的工具,nmcli工具命令,使用此工具,將會根據命令引數的配置來重新生成特定的配置檔案來供網路介面使用,方便而又靈活。在linux不再使用bond機制定義,而是使用網路組 team 機制,把 team 組當作一個裝置。
team :也是鏈路聚合 最多支援8塊網絡卡,Team 不需要手動載入相應核心模組
支援模式:
broadcast 廣播容錯
roundrobin 輪詢
activebackup 主備
loadbalance 負載均衡
以命令方式進行配置
思路和做法都和bond設定沒有多少區別,下面把設定方式簡單說一邊,以下圖片中包含個人在實驗中存在的錯誤,以及糾正方法,比較亂,慎看!!!
[[email protected] ~]# nm-connection-editor 刪除上次實驗的配置
[[email protected] ~]# nmcli connection show
[[email protected] ~]# watch -n 1 teamdctl team0 stat 監控命令,這裡的監控的時程式而不是檔案
[[email protected] ~]# nmcli connection add con-name team0 ifname team0 type team config '{"runner":{"name":"activebackup"}}' ip4 172.25.254.160/24 這裡需要理解Linux中轉義字元的作用,意會!
[[email protected] ~]# nmcli connection add con-name eth0 ifname eth0 type team-slave master team0
[[email protected] ~]# nmcli connection add con-name eth1 ifname eth1 type team-slave master team0
[[email protected] ~]# nmcli connection show
[[email protected] ~]# ping 172.25.254.60
[[email protected] ~]# ifconfig eth0 down 控制變數法測試
[[email protected] ~]# ifconfig eth1 down
[[email protected] ~]# ifconfig eth1 up
[[email protected] ~]# ifconfig eth1 down
[[email protected] ~]# ifconfig eth0 up
按照這個方法再做一邊沒出現以下的錯誤
反思,刪除的時候nmcli connection delete 命令,而不是直接刪除配置檔案,誰刪誰傻逼
正確姿勢:
[[email protected] ~]# nmcli connection delete team0
[[email protected] ~]# nmcli connection delete eth0
[[email protected] ~]# nmcli connection delete eth1
以編輯配置檔案方式進行配置
[[email protected] ~]# systemctl restart network
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-team0
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth1
[[email protected] ~]#
[[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-team0
DEVICE=team0
TEAM_CONFIG='{"runner":{"name":"activebackup"}}'
DEVICETYPE=Team
BOOTPROTO=none
IPADDR=172.25.254.160
PREFIX=24
NAME=team0
ONBOOT=yes
[[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
BOOTPROTO=none
DEVICE=eth0
ONBOOT=yes
TEAM_MASTER=team0
DEVICETYPE=TeamPort
[[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
BOOTPROTO=none
DEVICE=eth1
ONBOOT=yes
TEAM_MASTER=team0
DEVICETYPE=TeamPort
橋接
實驗真機裡面
關掉wifi
刪除
[[email protected] Desktop]# cd /etc/sysconfig/network-scripts/
[[email protected] network-scripts]# ls
[[email protected] network-scripts]# nm-connection-editor
[[email protected] network-scripts]# ls
[[email protected] network-scripts]# vim ifcfg-enp6s0
[[email protected] network-scripts]# vim ifcfg-br0
[[email protected] network-scripts]# systemctl restart network
[[email protected] network-scripts]# brctl show
[[email protected] network-scripts]# cat ifcfg-enp6s0
DEVICE=enp6s0
ONBOOT=yes
BOOTPROTO=none
BRIDGE=br0
[[email protected] network-scripts]# cat ifcfg-br0
DEVICE=br0
ONBOOT=yes
BOOTPROTO=none
IPADDR=172.25.254.60
NETMASK=255.255.255.0
TYPE=Bridge