Configuring floating IP for linux

Posted by amylou on Sat, 19 Feb 2022 07:16:11 +0100

In the High Availability cluster environment, it is generally necessary to use floating IP to achieve high web availability.

For the concept of floating IP and why floating IP is needed, please refer to: Floating IP (FLOAT IP)

This article mainly talks about the actual operation steps:

It can be dual computer or multi computer. The main server is 172.24.8.55 and the floating IP is 172.24.8.80.

1, Configure floating IP:

Using the technology that a single network card can bind multiple IP addresses.

1. Primary server configuration floating IP

Copy eth0 bit eth0:1 on the master server and make modifications.

cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0:1

Edit file ifcfg-eth0:1:

vim /etc/sysconfig/network-scripts/ifcfg-eth0:1

Modify DEVICE to eth0:1, NM_ Set the control led to no, change the IPADDR to the floating IP address 172.24.8.80, remove the gateway information, and modify it as follows:

DEVICE="eth0:1"
BOOTPROTO="static"
ONBOOT="yes"
NM_CONTROLLED="no"
TYPE="Ethernet"
IPADDR="172.24.8.80"
NETMASK="255.255.255.0"

Then start the network card and verify whether it takes effect:

ifup eth0:1

Check whether there is a floating IP address:

ip addr

Then access the floating IP address 172.24.8.80 and the access is successful, that is, the same as that accessed by the main server 172.24.8.55, indicating that the configuration is successful.

2. Configure floating IP from server

The configuration steps are exactly the same as those of the primary server. The IP address of eth0:1 is also set to floating ip172 24.8.80 before starting the network card test, it is necessary to turn off the floating IP on the main server:

ifdown eth0:1

Then start eth0:1 from the server

ifup eth0:1

The test steps are the same as the main server.

3. If there are other slave servers, the configuration steps are as follows.

2, Write a script to complete the automatic opening and closing of floating IP

Using crontab automatic execution technology, regularly check the status of IP address, and start eth0:1 or turn off eth0:1.

1. From the server script slavefloatip sh:

MASTER_IP="172.24.8.55"
FLOAT_IP="172.24.8.80"

c1=$(/usr/bin/ping $MASTER_IP -c 1|grep Unreachable|wc -l)
c2=$(/usr/bin/ping $FLOAT_IP -c 1|grep Unreachable|wc -l)
c3=$(/usr/sbin/ip addr|grep eth0:1)
if [ $c1 -gt 0 -a $c2 -gt 0 ]
then
    /usr/sbin/ifup eth0:1
elif [ $c1 -eq 0 -a $c2 -eq 0 -a "$c3" ]
then
    /usr/sbin/ifdown eth0:1
fi

 

ping the IP address of the primary server. If the ping fails, it means that the primary server is down, and then ping the floating IP address. If the ping fails, it means that the floating IP is not started, then start the floating IP.

When the master server goes online again, it can ping the master server and the floating IP address at the same time, and then judge whether the slave server has started the floating IP. If so, it needs to be shut down and let the master server start the floating IP.

If there are other slave servers, just copy the script.

2. Master server script: masterfloatip sh:

FLOAT_IP="172.24.8.80"

c=$(/usr/bin/ping $FLOAT_IP -c 1|grep Unreachable|wc -l)
if [ $c -gt 0 ]
then
    /usr/sbin/ifup eth0:1
fi

If the floating IP address cannot be ping ed, start eth0:1

3. Use crontab to automatically execute scripts every 5 seconds.

crontab -e

Enter the file editing interface, and the main server configuration is as follows:

* * * * * /root/masterFloatIP.sh
* * * * * sleep 5; /root/masterFloatIP.sh
* * * * * sleep 10; /root/masterFloatIP.sh
* * * * * sleep 15; /root/masterFloatIP.sh
* * * * * sleep 20; /root/masterFloatIP.sh
* * * * * sleep 25; /root/masterFloatIP.sh
* * * * * sleep 30; /root/masterFloatIP.sh
* * * * * sleep 35; /root/masterFloatIP.sh
* * * * * sleep 40; /root/masterFloatIP.sh
* * * * * sleep 45; /root/masterFloatIP.sh
* * * * * sleep 50; /root/masterFloatIP.sh
* * * * * sleep 55; /root/masterFloatIP.sh

The configuration from the server is the same, and the script path is changed to / root / slavefloatip sh

For the introduction and use of crontab, please refer to: Introduction to crontab

 

This completes the floating IP configuration and the master-slave server automatically turns on the floating IP.

After completion, it can be accessed through the floating IP address. As long as one server in the server cluster is running normally, the web can be accessed, realizing high web availability.

 

Transferred from: https://www.cnblogs.com/victorwu/p/7061168.html

Topics: Linux Operation & Maintenance