linux系統配置網卡綁定

linux系統配置網卡綁定

一、centos6下配置網卡綁定(bonding)

1、bonding概述

(1)作用:就是將多塊網卡綁定同一IP地址對外提供服務,可以實現高可用或者負載均衡。當然,直接給兩塊網卡設置同一IP地址是不可能的。通過bonding,虛擬一塊網卡對外提供連接,物理網卡的被修改爲相同的MAC地址。

(2)Bonding的工作模式

   1)Mode 0 (balance-rr)輪轉(Round-robin)策略:從頭到尾順序的在每一個slave接口上面發送數據包。本模式提供負載均衡和容錯的能力

   2)Mode 1 (active-backup)活動-備份(主備)策略:在綁定中,只有一個slave被激活。當且僅當活動的slave接口失敗時纔會激活其他slave。爲了避免交換機發生混亂此時綁定的MAC地址只有一個外部端口上可見

   3)Mode 3 (broadcast)廣播策略:在所有的slave接口上傳送所有的報文。本模式提供容錯能力。

2、bonding配置(詳細配置文檔:/usr/share/doc/kerneldocversion/Documentation/networking

/bonding.txt)

(1)準備工作:需要關閉NetworkManager

[root@dayi123-6 ~]# service NetworkManager stop

(2)創建bonding配置文件(miimon是用來進行鏈路監測的。如果miimon=100,那麼系統每100ms 監測一次鏈路連接狀態,如果有一條線路不通就轉入另一條線路,mode用來指定bonding的工作模式)

[root@dayi123-6 ~]# cd /etc/sysconfig/network-scripts/

[root@dayi123-6 network-scripts]#cat ifcfg-bond0   #bonding的配置

DEVICE=bond0       #設備名稱

BOOTPROTO=dhcp    #IP地址獲取方式

BONDING_OPTS="miimon=100mode=1"

(3)配置需要bonding的網卡

[root@dayi123-6 network-scripts]#cat ifcfg-eth1 ifcfg-eth0  #物理網卡的配置

DEVICE=eth1   

BOOTPROTO=dhcp

MASTER=bond0    #指定主設備爲bond0

SLAVE=yes        #將當前設備設置爲從設備

USERCTL=no

 

DEVICE=eth0

BOOTPROTO=dhcp

MASTER=bond0

SLAVE=yes

USERCTL=no

(4)使配置生效(配置完成後需要重啓網卡時配置生效,生效後查看網卡信息時,只有bonding設備有IP地址,物理網卡沒有IP地址)

[root@dayi123-6 network-scripts]#service network restart  #重啓網卡使配置的bonding生效

[root@dayi123-6 network-scripts]#ifconfig  

bond0     Link encap:Ethernet  HWaddr 00:0C:29:D3:7B:9B 

          inet addr:172.16.254.250  Bcast:172.16.255.255  Mask:255.255.0.0

          inet6 addr:fe80::20c:29ff:fed3:7b9b/64 Scope:Link

          UP BROADCAST RUNNING MASTERMULTICAST  MTU:1500  Metric:1

          RX packets:862 errors:0 dropped:0overruns:0 frame:0

          TX packets:23 errors:0 dropped:0overruns:0 carrier:0

          collisions:0 txqueuelen:0

          RX bytes:92932 (90.7 KiB)  TX bytes:3326 (3.2 KiB)

 

eth0      Link encap:Ethernet  HWaddr 00:0C:29:D3:7B:9B 

          UP BROADCAST RUNNING SLAVEMULTICAST  MTU:1500  Metric:1

          RX packets:298656 errors:0 dropped:0overruns:0 frame:0

          TX packets:9015 errors:0 dropped:0overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:33161613 (31.6 MiB)  TX bytes:1608752 (1.5 MiB)

 

eth1      Link encap:Ethernet  HWaddr 00:0C:29:D3:7B:9B 

          UP BROADCAST RUNNING SLAVEMULTICAST  MTU:1500  Metric:1

          RX packets:425 errors:0 dropped:0overruns:0 frame:0

          TX packets:0 errors:0 dropped:0overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:45922 (44.8 KiB)  TX bytes:0 (0.0 b)

(5)查看bonding的信息(通過查看/proc/net/bonding/bond0能夠查看網卡的的狀態,在主備模式下那塊網卡處於激活狀態)

[root@dayi123-6 network-scripts]#cat /proc/net/bonding/bond0

Ethernet Channel Bonding Driver:v3.7.1 (April 27, 2011)

 

Bonding Mode: fault-tolerance(active-backup)

Primary Slave: None

Currently Active Slave: eth0

MII Status: up

MII Polling Interval (ms): 100

Up Delay (ms): 0

Down Delay (ms): 0

3、刪除bonding

[root@dayi123-6 network-scripts]#ifconfig bond0 down   #停掉bonding

[root@dayi123-6 network-scripts]#rm ifcfg-bond0    #刪除bonding配置文件

二、centos7下配置網卡綁定(bonding)

1、網卡命名

(1)網卡命名機制,systemd對網絡設備的命名方式

   1) 如果Firmware或BIOS爲主板上集成的設備提供的索引信息可用,且可預測則根據此索引進行命名,例如eno1

   2) 如果Firmware或BIOS爲PCI-E擴展槽所提供的索引信息可用,且可預測,則根據此索引進行命名,例如ens1

   3) 如果硬件接口的物理位置信息可用,則根據此信息進行命名,例如enp2s0

   4) 如果用戶顯式啓動,也可根據MAC地址進行命名,enx2387a1dc56

   5) 上述均不可用時,則使用傳統命名機制

(2) 名稱組成格式

    en: Ethernet 有線局域網

    wl: wlan無線局域網

    ww: wwan無線廣域網

   名稱類型:

    o<index>: 集成設備的設備索引號

    s<slot>: 擴展槽的索引號

    x<MAC>: 基於MAC地址的命名

    p<bus>s<slot>:enp2s1

(3)網卡設備的命名過程

   第一步:udev, 輔助工具程序/lib/udev/rename_device/usr/lib/udev/rules.d/60-net.rules

   第二步:biosdevname會根據/usr/lib/udev/rules.d/71-biosdevname.rules

(4)更改centos7網卡名稱

   CentOS從7.0版本以後,網卡的命名自動基於固件及拓撲結構和位置信息來固定。不會自動生成eth0等網卡名稱,但是,我們可以將網卡名稱修改爲eth0。

方法一:在安裝系統的過程中修改網卡名稱爲eth0(在安裝系統的選擇界面,選擇“install centOS 7” 按下”Tab”鍵在最下面加入”net.ifnames=0 biosdevname=0”如下圖所示)

wKiom1kN3UXxVcM0AABJKQgOpr0150.jpg-wh_50

方法二:編輯/etc/grub2.cfg配置文件:(加入net.ifnames=0)

[root@dayi123 ~]# cat -n /etc/grub2.cfg | grep biosdevname

    99         linux16 /vmlinuz-3.10.0-327.el7.x86_64root=UUID=90b71a77-dd23-4528-8338-d7e466927721 ro crashkernel=autobiosdevname=0 net.ifnames=0 rhgbquiet.UTF-8

或修改/etc/default/grub配置文件:(在下述行加入net.ifnames=0)

[root@dayi123 ~]#cat /etc/default/grub

……

GRUB_CMDLINE_LINUX="crashkernel=autobiosdevname=0 net.ifnames=0 rhgbquiet"

方法三:爲grub2生成其配置文件

[root@dayi123 ~]#grub2-mkconfig-o /etc/grub2.cfg

注:通過方法二和方法三修改後需要重啓系統是配置生效,同時也需要修改網卡配置文件中的網卡名稱

2、nmcli命令的使用

(1)作用:地址配置工具

(2)用法:nmcli[ OPTIONS ] OBJECT{ COMMAND | help }(具體用法可以在使用時按Tab鍵補全)

   選項:device

       connection

[root@dayi123 network-scripts]#nmcli device show eth1   #查看網卡eth1的詳細信息

GENERAL.DEVICE:                         eth1

GENERAL.TYPE:                           ethernet

GENERAL.HWADDR:                         00:0C:29:ED:0F:1B

GENERAL.MTU:                            1500

GENERAL.STATE:                          100 (connected)

GENERAL.CONNECTION:                     Wired connection 1

GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/6

WIRED-PROPERTIES.CARRIER:               on

IP4.ADDRESS[1]:                         10.0.0.3/24

IP4.GATEWAY:                            10.0.0.2

IP4.DNS[1]:                             10.0.0.2

IP4.DOMAIN[1]:                          localdomain

(3)通過nmcli修改網卡配置文件名稱

[root@dayi123 network-scripts]#nmcli connection show #查看當前系統有多少套配置文件

NAME          UUID                           TYPE            DEVICE

virbr0      084f00d4-652e-44c8-83b7-70b469f89f31 bridge          virbr0

eth0        d50e8c71-6416-4ab6-97ca-ad2002cd3353  802-3-ethernet  eth0  

Wired connection 1  370c3d11-868f-4efc-95bc-630169b62ef1  802-3-ethernet  eth1

[root@dayi123 network-scripts]#nmcli connection modify Wired\ connection\ 1 connection.id eth1                            #修改網卡配置文件名稱

[root@dayi123 network-scripts]#nmcli connection show   #查看修改後的名稱

NAME       UUID                                  TYPE            DEVICE

virbr0     084f00d4-652e-44c8-83b7-70b469f89f31  bridge          virbr0

eth0      d50e8c71-6416-4ab6-97ca-ad2002cd3353 802-3-ethernet  eth0  

eth1      370c3d11-868f-4efc-95bc-630169b62ef1 802-3-ethernet  eth1 

(4)通過nmcli命令爲網卡在不同的環境創建不同的配置文件

   1)創建網卡配置文件

[root@dayi123 network-scripts]#nmcli connection add ifname eth1 type ethernet con-name eth1-home ip4192.168.1.2/24 gw4 192.168.1.1     #通過命令創建一份網卡配置文件

Connection 'eth1-home'(4e73fce1-800e-420d-902f-48b19c8dfc07) successfully added.

[root@dayi123 network-scripts]#nmcli connection show

……

eth1-home  4e73fce1-800e-420d-902f-48b19c8dfc07  802-3-ethernet  --    

[root@dayi123 network-scripts]#cp ifcfg-eth1-home ifcfg-eth1-office  #通過複製創建網卡配置

[root@dayi123 network-scripts]#nmcli connection reload     #加載複製的網卡配置

[root@dayi123 network-scripts]#nmcli connection show      #查看生效後的配置

……

eth1-office  1bbd1be4-e93e-4b29-0ced-eaffbef36326  802-3-ethernet  --    

eth1-home    4e73fce1-800e-420d-902f-48b19c8dfc07  802-3-ethernet  --     

   2)使創建的網卡配置文件生效

[root@dayi123 network-scripts]#nmcli connection up eth1-home #使配置eth1-home生效

Connection successfully activated(D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/9)

[root@dayi123 network-scripts]#nmcli connection show   #查看當前生效的配置

NAME         UUID                                  TYPE            DEVICE

…….

eth1-home    4e73fce1-800e-420d-902f-48b19c8dfc07  802-3-ethernet  eth1  

[root@dayi123 network-scripts]#ifconfig eth1  #查看eth1IP地址

eth1:flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 192.168.1.2  netmask 255.255.255.0  broadcast 192.168.1.255

[root@dayi123 network-scripts]#nmcli device status   #查看設備的狀態

DEVICE      TYPE     STATE      CONNECTION

virbr0      bridge   connected  virbr0    

eth0        ethernet  connected eth0      

eth1        ethernet  connected eth1-home 

lo          loopback  unmanaged --        

virbr0-nic  tap      unmanaged  --

   3)刪除網卡配置文件

[root@dayi123 network-scripts]#nmcli connection delete eth1-home

Connection 'eth1-home'(4e73fce1-800e-420d-902f-48b19c8dfc07) successfully deleted.

[root@dayi123 network-scripts]#nmcli connection show  #查看刪除後可用的配置文件

(4)通過nmcli網絡設置

   1)用法:nmcliconnection modify IFACE[+|-]setting.propertyvalue

   2)常用參數

Nmcli  修改參數

對應網卡配置文件選項

ipv4.method  manual

BOOTPROTO=none

ipv4.method  auto

BOOTPROTO=dhcp

ipv4.addresses  “192.0.2.1/24 192.0.2.254”

IPADDR0=192.0.2.1

PREFIX0=24

GATEWAY0=192.0.2.254

ipv4.dns  8.8.8.8

DNS0=8.8.8.8

ipv4.dns-search  example.com

DOMAIN=example.com

ipv4.ignore-auto-dns  true

PEERDNS=no

connection.autoconnectyes

ONBOOT=yes

connection.id  eth0

NAME=eth0

connection.interface-name  eth0

DEVICE=eth0

802-3-ethernet.mac-address  . . .

HWADDR=  . . .

#將網卡配置文件中的ONBOOT設置爲no

[root@dayi123 network-scripts]# nmcli connection modify eth1-home connection.autoconnect no

#設置dns

[root@dayi123 network-scripts]#nmcli connection modify eth1-home ipv4.dns 114.114.114.114

#爲網卡再添加一個IP地址

[root@dayi123 network-scripts]#nmcli connection modify eth1-home +ipv4.addresses 192.168.1.100/24

[root@dayi123 network-scripts]#cat ifcfg-eth1-home | grep -E "IPADDR|DNS"

DNS1=114.114.114.114

IPADDR=192.168.1.2

IPADDR1=192.168.1.100

[root@dayi123 network-scripts]#nmcli connection modify eth1-home -ipv4.addresses 192.168.1.100/24    #刪除設置的一個IP地址

3、通過nmcli命令實現bonding

(1)添加bonding接口

[root@dayi123 network-scripts]#nmcli connection add type bond con-name bond0 ifname bond0 mode active-backup   #創建bonding接口,模式爲主從模式

Connection 'bond0'(da41db3d-b705-4eca-8e0a-06e7c59a95eb) successfully added.

[root@dayi123 network-scripts]#nmcli connection show

……

bond0   da41db3d-b705-4eca-8e0a-06e7c59a95eb  bond            bond0 

(2)爲bonding接口添加從屬接口

[root@dayi123 network-scripts]#nmcli connection add type bond-slave ifname eth0 master bond0 #eth0接口添加到bond0的從屬接口,添加後會生成新的配置文件bond-slave-eth0

Connection 'bond-slave-eth0'(681e4abb-f538-4756-b0bd-cd5a51eb0cf6) successfully added.

[root@dayi123 network-scripts]#nmcli connection add type bond-slave ifname eth1 master bond0 #eth1接口添加到bond0的從屬接口,添加後會生成新的配置文件bond-slave-eth1

Connection 'bond-slave-eth1'(0a1708cc-e9fc-48d6-b6c2-6349b14d06e0) successfully added.

(3)啓動綁定

[root@dayi123 network-scripts]#nmcli connection up bond-slave-eth0 #啓動綁定從屬接口0

Connection successfully activated(D-Bus active path: /org/freedesktop/NetworkManager/Active

Connection/13)

[root@dayi123 network-scripts]#nmcli connection up bond-slave-eth1 #啓動綁定從屬接口1

Connection successfully activated(D-Bus active path: /org/freedesktop/NetworkManager/Active

Connection/14)

[root@dayi123 network-scripts]#nmcli connection up bond0  #啓動綁定

Connection successfully activated(master waiting for slaves) (D-Bus active path: /org/freedesktop/

NetworkManager/ActiveConnection/15)

(4)查看bonding狀態及信息

[root@dayi123 network-scripts]#cat /proc/net/bonding/bond0

Ethernet Channel Bonding Driver:v3.7.1 (April 27, 2011)

Bonding Mode: fault-tolerance(active-backup)

Primary Slave: None

Currently Active Slave: eth0

MII Status: up

MII Polling Interval (ms): 100

Up Delay (ms): 0

Down Delay (ms): 0

……

(5)修改bongding的工作模式

[root@dayi123 network-scripts]#cat ifcfg-bond0

…….

BONDING_OPTS=mode=broadcast     #將主從模式修改廣播模式

……

[root@dayi123 network-scripts]#nmcli connection up bond0   #使修改後的配置生效

Connection successfully activated(master waiting for slaves) (D-Bus active path:/org/freedesktop/NetworkManager/ActiveConnection/18)

[root@dayi123 network-scripts]#cat /proc/net/bonding/bond0  #查看狀態

Ethernet Channel Bonding Driver:v3.7.1 (April 27, 2011)

Bonding Mode: fault-tolerance(broadcast)

MII Status: up

MII Polling Interval (ms): 100

Up Delay (ms): 0

Down Delay (ms): 0

(6)刪除bonding

[root@dayi123 network-scripts]#nmcli connection delete bond0  #刪除bonding配置文件

[root@dayi123 ~]# nmcliconnection delete bond-slave-eth0  #刪除bonding從屬接口配置文件

[root@dayi123 ~]# nmcliconnection delete bond-slave-eth1

三、centos7下通過網絡組Network Teaming綁定

1、網絡組概述

(1)網絡組作用:是將多個網卡聚合在一起方法,從而實現冗錯和提高吞吐量,提供更好的性能和擴展性

(2)網絡組由內核驅動和teamd守護進程實現

(3)支持的方式:broadcast、roundrobin、activebackup、loadbalance、lacp(implements the 802.3ad LinkAggregation Control Protocol)

2、創建網絡組(即創建banding)

(1)創建網絡組接口

   1)創建命令:nmclicon add type team con-nameCNAMEifnameINAME[configJSON]

   2)參數說明:CNAME連接名,INAME接口名

            JSON指定runner方式

           格式:'{"runner": {"name":"METHOD"}}'

            METHOD可以是broadcast,roundrobin,activebackup,loadbalance,lacp

[root@dayi123 ~]# nmcli connection add type team con-name team0 ifname team0 config'{"runner":{"name":"loadbalance"}}'  #創建網絡組接口,創建後會自動生成網絡組配置文件

[root@dayi123 network-scripts]#ls ifcfg-team0   #生成的網絡組配置文件

ifcfg-team0

#爲網絡組接口配置IP地址

[root@dayi123 network-scripts]#nmcli connection modify team0 ipv4.addresses 172.16.1.252/16

#指定網絡接口組IP地址類型爲手工指定

[root@dayi123 network-scripts]#nmcli connection modify team0 ipv4.method manual

#爲網絡組接口設置DNS和網關

[root@dayi123 network-scripts]#nmcli connection modify team0 ipv4.gateway 172.16.0.1

[root@dayi123 network-scripts]#nmcli connection modify team0 ipv4.dns 172.16.0.1  

(2)創建port接口(即爲網絡組添加物理網卡)

   1)用法:nmclicon add type team-slave con-nameCNAMEifnameINAMEmaster TEAM

   2)參數:CNAME連接名

         INAME網絡接口名

         TEAM網絡組接口名

[root@dayi123 network-scripts]#nmcli connection add con-name team0-eth0 type team-slave ifname eth0  master team0 #eth0添加到網絡組,會生成新的配置文件team0-eth0

Connection 'team0-eth0' (26f78262-827e-4576-a6dd-ffa633710cad)successfully added.

[root@dayi123 network-scripts]#nmcli connection add con-name team0-eth1 type team-slave ifname eth1  master team0  #eth1添加到網絡組,會生成新的配置文件team0-eth1

Connection 'team0-eth1' (520a97b9-22ce-4e26-9c7f-ff2470260b11)successfully added.

[root@dayi123 network-scripts]#nmcli connection show   #查看網絡配置文件

……

team0      f6ad9858-fa1c-447c-b05c-d64026722648 team            team0 

team0-eth1  520a97b9-22ce-4e26-9c7f-ff2470260b11  802-3-ethernet  --    

team0-eth0  26f78262-827e-4576-a6dd-ffa633710cad  802-3-ethernet  --    

(3)啓動網絡組

   1)啓動網絡組接口不會自動啓動網絡組中的port接口

   2)啓動網絡組接口中的port接口總會自動啓動網絡組接口

   3)禁用網絡組接口會自動禁用網絡組中的port接口

   4)沒有port接口的網絡組接口可以啓動靜態IP連接

   5)啓用DHCP連接時,沒有port接口的網絡組會等待port接口的加入

[root@dayi123 network-scripts]#nmcli connection up team0    #啓動網絡組

Connection successfully activated(master waiting for slaves) (D-Bus active path:/org/freedesktop/NetworkManager/ActiveConnection/24)

#沒有啓動網絡組接口靜態IP可以生效,並可以ping通(前提示和物理網卡同一網段)

[root@dayi123 network-scripts]#ifconfig team0

team0:flags=4099<UP,BROADCAST,MULTICAST> mtu 1500

        inet 172.16.1.252  netmask 255.255.0.0  broadcast 172.16.255.255

        ether 4a:12:68:43:5b:74  txqueuelen 0 (Ethernet)

[root@dayi123 network-scripts]#nmcli connection show #啓動網絡組並不會啓動網絡組中接口

NAME        UUID                                  TYPE            DEVICE

team0      f6ad9858-fa1c-447c-b05c-d64026722648 team            team0 

…… 

team0-eth1  520a97b9-22ce-4e26-9c7f-ff2470260b11  802-3-ethernet  --     

team0-eth0  26f78262-827e-4576-a6dd-ffa633710cad  802-3-ethernet  --   

#啓動網絡組中接口,啓動網絡組接口後,網絡組接口ip地址纔會失效

[root@dayi123 network-scripts]#nmcli connection up team0-eth0

[root@dayi123 network-scripts]#nmcli connection up team0-eth1

(4)查看網絡組狀態信息

[root@dayi123 ~]# teamdctl team0state         

setup:

 runner: loadbalance

ports:

……

(5)刪除網絡組

[root@dayi123 ~]# nmcliconnection down team0  #停掉網絡組,停掉後物理網絡會自動開啓

#刪除網絡組port配置信息

[root@dayi123 ~]# nmcliconnection delete team0-eth0

Connection 'team0-eth0'(26f78262-827e-4576-a6dd-ffa633710cad) successfully deleted.

[root@dayi123 ~]# nmcliconnection delete team0-eth1

Connection 'team0-eth1'(520a97b9-22ce-4e26-9c7f-ff2470260b11) successfully deleted.

[root@dayi123 ~]# nmcliconnection delete team0  #刪除網絡組

Connection 'team0'(f6ad9858-fa1c-447c-b05c-d64026722648) successfully deleted.

[root@dayi123 ~]# nmcliconnection show   #查看配置信息時,沒有了網絡組配置信息

NAME    UUID                                  TYPE            DEVICE

virbr0  084f00d4-652e-44c8-83b7-70b469f89f31  bridge          virbr0

eth0    d50e8c71-6416-4ab6-97ca-ad2002cd3353  802-3-ethernet  eth0  

eth1    370c3d11-868f-4efc-95bc-630169b62ef1  802-3-ethernet  eth1     


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章