Linux uses bond to implement dual network cards to bind a single IP sample code

Linux uses bond to implement dual network cards to bind a single IP sample code

In order to provide high availability of the network, we may need to bind multiple network cards into a virtual network card to provide external services, so that even if one of the physical network cards fails, it will not cause the connection to be interrupted.
Bond is called bonding in Linux, etherchanel in IBM, and team in Broadcom, but no matter how the name changes, the effect is to use two or more network cards as one, which can increase bandwidth and improve redundancy.

There are two ways to implement dual network card bonding: bond and team
Here we first write down the bond binding method

Bond supported modes

A total of seven bond modes are supported [0-6], and three of them are commonly used, as follows:

  • mode=0: Default, load balancing mode, with automatic backup, but requires switch configuration.
  • mode=1: Active/standby mode. If one line is disconnected, the other lines will automatically take over. No switch configuration is required.
  • mode=6: load balancing mode with automatic backup, no switch configuration required.

As for the other modes, the explanations are as follows:

mode=2: Select the serial number of the network card = (source MAC address XOR target MAC address) % the number of slave network cards. Other transmission strategies can be specified through the xmit_hash_policy configuration item.
mode=3: Use broadcast strategy, the data packet will be broadcast to all slave network cards for transmission
mode=4: Use dynamic link aggregation strategy. When starting, an aggregation group will be created. All slave network cards share the same speed and duplex settings. However, mode4 has two necessary conditions.

1. Support using ethtool to obtain the speed and duplex settings of each slave network card;
2. The switch needs to support IEEE 802.3ad Dynamic link aggregation mode
mode=5: Select the transmission network card based on the rate of each slave network card. Necessary condition: Support using ethtool to obtain the rate of each slave network card.

Configuring Bond

Network Card bond1 IP Bond Mode
ens33, ens36 192.168.171.111 mode 1

Note: The IP address is configured on bond1, and the physical network card does not need to be configured with an IP address

#Load the bonding module and confirm that it has been loaded [root@web01 ~]# modprobe --first-time bonding
[root@web01 ~]# lsmod | grep bonding
bonding 141566 0 
#Edit bond1 configuration file [root@web01 ~]# cat > /etc/sysconfig/network-scripts/ifcfg-bond1 << EOF
> DEVICE=bond1
> TYPE=Bond
>IPADDR=192.168.171.111
> NETMASK=255.255.255.0
> GATEWAY==192.168.171.2
> DNS1=114.114.114.114
> DNS2=8.8.8.8
> USERCTL=no
> BOOTPROTO=none
>ONBOOT=yes
> EOF
#Modify ens33 configuration file [root@web01 ~]# cat > /etc/sysconfig/network-scripts/ifcfg-ens33 << EOF
> DEVICE=ens33
> TYPE=Ethernet
>ONBOOT=yes
> BOOTPROTO=none
> DEFROUTE=yes
> IPV4_FAILURE_FATAL=no
> NMAE=ens33
> MASTER=bond1 # Need to be consistent with the DEVICE value in the ifcfg-bond0 configuration file above > SLAVE=yes
> EOF
#Modify ens36 configuration file [root@web01 ~]# cat > /etc/sysconfig/network-scripts/ifcfg-ens33 << EOF
> DEVICE=ens36
> TYPE=Ethernet
>ONBOOT=yes
> BOOTPROTO=none
> DEFROUTE=yes
> IPV4_FAILURE_FATAL=no
> NAME=ens36
> MASTER=bood1
> SLAVE=yes
> EOF

# Configuring bonding
[root@web01 ~]# cat >> /etc/modules-load.d/bonding.conf << EOF
> alias bond1 bonding
> options bonding mode=1 miimon=200 # Load the bonding module, and the external virtual network interface device is bond1
> EOF

#Restart the network card to make the configuration take effect[root@web01 ~]# systemctl restart network

Note: If the network card service fails to start after the configuration is complete and no errors are found in the log, you can try closing NetworkManager and restarting the network card again.

After restarting the network, check the information of each network card

[root@web01 ~]# ip a show ens33
2: ens33: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond1 state UP group default qlen 1000
  link/ether 00:0c:29:9f:33:9f brd ff:ff:ff:ff:ff:ff
[root@web01 ~]# ip a show ens36
3: ens36: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master bond1 state UP group default qlen 1000
  link/ether 00:0c:29:9f:33:9f brd ff:ff:ff:ff:ff:ff
[root@web01 ~]# ip a show bond1
7: bond1: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
  link/ether 00:0c:29:9f:33:9f brd ff:ff:ff:ff:ff:ff
  inet 192.168.171.111/24 brd 192.168.171.255 scope global noprefixroute bond1
    valid_lft forever preferred_lft forever
  inet6 fe80::20c:29ff:fe9f:339f/64 scope link 
    valid_lft forever preferred_lft forever

View bond1 related information

# View the interface status of bond1 [root@web01 ~]# cat /proc/net/bonding/bond1        
Ethernet Channel Bonding Driver: v3.7.1 (April 27, 2011)

Bonding Mode: load balancing (round-robin) # Bonding mode MII Status: up # Interface status MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: ens33 # Backup interface: ens33
MII Status: up # Interface status Speed: 1000 Mbps # Port rate Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:9f:33:9f # Interface permanent MAC address Slave queue ID: 0

Slave Interface: ens36 # Backup interface: ens36
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 00:0c:29:9f:33:a9
Slave queue ID: 0

When this step is achieved, if any network card in ens33 or ens36 is down, it will not affect communication

Note: If you are using a vmware workstaction virtual machine for testing, please do not directly execute the command ifdown ens33 or ifdown ens36 for testing. In this way, the test will not work because of the virtual machine. You can cancel the connection in the network adapter.

This is the end of this article about the sample code of using bond in linux to bind dual network cards to a single IP. For more information about how to bind dual network cards to a single IP in linux, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Linux system dual network card binding configuration implementation
  • Linux dual network card binding script method example
  • Linux uses dual network card bond and screwdriver interface
  • Configuration process of dual network card firewalld under Linux (recommended)
  • Detailed explanation of Linux dual network card binding to achieve load balancing
  • How to set up dual network cards in Linux
  • Summary of the practice of dual network card configuration under Linux system

<<:  Implementation of clicking through the transparent area of ​​​​irregular forms in Electron

>>:  In-depth analysis of why MySQL does not recommend using uuid or snowflake id as primary key

Recommend

How to use axios request in Vue project

Table of contents 1. Installation 2. There is no ...

Tutorial on installing MySQL 8.0.11 under Linux

1. Go to the official website to download the ins...

This article helps you understand PReact10.5.13 source code

Table of contents render.js part create-context.j...

XHTML Getting Started Tutorial: Using the Frame Tag

<br />The frame structure allows several web...

How to execute PHP scheduled tasks in CentOS7

Preface This article mainly introduces the releva...

How to insert 10 million records into a MySQL database table in 88 seconds

The database I use is MySQL database version 5.7 ...

Vue+Element realizes paging effect

This article example shares the specific code of ...

How to recover files accidentally deleted by rm in Linux environment

Table of contents Preface Is there any hope after...

5 commonly used objects in JavaScript

Table of contents 1. JavaScript Objects 1).Array ...

Why the explain command may modify MySQL data

If someone asked you whether running EXPLAIN on a...

Analysis of the difference between Mysql InnoDB and MyISAM

MySQL supports many types of tables (i.e. storage...

Centos8 (minimum installation) tutorial on how to install Python3.8+pip

After minimizing the installation of Python8, I i...

Common writing examples for MySQL and Oracle batch insert SQL

Table of contents For example: General writing: S...