Linux高級網絡設置——將多個網卡設置成一個網卡


Linux下可以設置網卡模式

模式0:負載均衡

模式1:主備模式,不提高網絡帶寬

模式3:多網卡同時發送相同的數據

准備實驗環境:

  Redhat 6.4 企業版64位,最小化安裝。

給虛擬機添加網卡

此時我虛擬機有2塊網卡,但是真正啟用工作的只有一塊,使用ifconfig也只能看到一塊網卡。ifconfig -a可以看到我們加入的另一塊網卡,此時這塊網卡還沒有啟用。

[root@51cto network-scripts]# ifconfig -a
eth0      Link encap:Ethernet  HWaddr 00:0C:29:82:AA:8B  
          inet addr:192.168.80.222  Bcast:192.168.80.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe82:aa8b/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:114 errors:0 dropped:0 overruns:0 frame:0
          TX packets:91 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:11152 (10.8 KiB)  TX bytes:12126 (11.8 KiB)

eth1      Link encap:Ethernet  HWaddr 00:0C:29:82:AA:95  
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

可見原來的那塊網卡IP沒變,新加的網卡沒有IP。現在將兩塊網卡綁定成一塊網卡,並設置新地址為192.168.80.240。這就要求這兩個網卡各自不設置地址,綁定后設置公共地址。

具體流程如下:

①創建綁定網卡的配置文件

[root@51cto network-scripts]# vi /etc/sysconfig/network-scripts/ifcfg-bond0
[root@51cto network-scripts]# cat ifcfg-bond0 
DEVICE=bond0
IPADDR=192.168.80.240
PREFIX=24
GATEWAY=192.168.80.1
ONBOOT=yes
BOOTPROTO=none
USERCTL=no
BONDING_OPTS="mode=0 miimon=50" 
miimon= — Specifies (in milliseconds) how often MII link monitoring occurs. This is useful if high availability is required because MII is used to verify that the NIC is active. To verify that the driver for a particular NIC supports the MII tool, type the following command as root:
ethtool <interface-name> | grep "Link detected:"
In this command, replace <interface-name> with the name of the device interface, such as eth0, not the bond interface. If MII is supported, the command returns:
Link detected: yes
If using a bonded interface for high availability, the module for each NIC must support MII.
Setting the value to 0 (the default), turns this feature off. When configuring this setting, a good starting point for this parameter is 100.
更多參考:Link monitoring
網卡配置文件各個字段含義參考:Interface Configuration 
②更改網卡配置文件
[root@51cto network-scripts]# cat ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
USERCTL=no
[root@51cto network-scripts]# cat ifcfg-eth1
DEVICE=eth1
ONBOOT=yes
BOOTPROTO=none
MASTER=bond0
SLAVE=yes
USERCTL=no

③添加驅動支持

[root@51cto modprobe.d]# pwd
/etc/modprobe.d
[root@51cto modprobe.d]# ls
anaconda.conf  blacklist.conf  dist-alsa.conf  dist.conf  dist-oss.conf  openfwwf.conf
[root@51cto modprobe.d]#  vi /etc/modprobe.d/bonding.conf
[root@51cto modprobe.d]# cat bonding.conf 
alias bond0 bonding

④重啟網絡服務

在xshell中測試,192.168.80.222這個綁定前的IP不通了,綁定后的公共IP通

[c:\~]$ ping 192.168.80.222

正在 Ping 192.168.80.222 具有 32 字節的數據:
請求超時。

192.168.80.222 的 Ping 統計信息:
    數據包: 已發送 = 1,已接收 = 0,丟失 = 1 (100% 丟失),
^C
[c:\~]$ ping 192.168.80.240

正在 Ping 192.168.80.240 具有 32 字節的數據:
來自 192.168.80.240 的回復: 字節=32 時間<1ms TTL=64
來自 192.168.80.240 的回復: 字節=32 時間<1ms TTL=64

192.168.80.240 的 Ping 統計信息:
    數據包: 已發送 = 2,已接收 = 2,丟失 = 0 (0% 丟失),
往返行程的估計時間(以毫秒為單位):
    最短 = 0ms,最長 = 0ms,平均 = 0ms

查看綁定的配置,顯示網卡工作模式為負載均衡

[root@51cto ~]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.6.0 (September 26, 2009)
Bonding Mode: load balancing (round-robin)
MII Status: up
MII Polling Interval (ms): 50
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:82:aa:8b
Slave queue ID: 0
Slave Interface: eth1
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:82:aa:95
Slave queue ID: 0

驗證網卡工作狀態是否是負載均衡

物理機上保持ping 192.168.80.240,在虛擬機系統里面每隔幾秒鍾執行ifconfig查看發送接收數據包變化。

 

 但是這種驗證方法太笨

借助watch命令,可以設置間隔多少時間執行一次ifconfig

watch -n 5 ifconfig

 上面驗證的是負載均衡模式,如果是主備模式至於需要在ifcfg-bond0配置文件里面吧模式改為0即可

如果需要拔掉虛擬機的網卡,如下設置

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM