Implement floating IP technology on your own under the Liunx dual machine

Source: Internet
Author: User
Tags bind

Original: http://www.360doc.com/content/11/0823/18/5907545_142730623.shtml

There are two Linux servers, one host (ip:139.24.214.22) provides some network services externally, and the other slave (ip:139.24.214.24) can provide the same service, but the IP address is not exposed externally.

The client connection is 139.24.214.22 this IP address, if the host fails, it will cause the network service temporarily interrupted, the longer the time, the greater the loss,

Because there is no dual-machine hot backup technology, consider their own Linux script to implement a simple floating IP technology, when the host failure from the machine to obtain 139.24.214.22 this IP, temporarily replace the host to provide services, when the host recovery, the slave automatically release this IP.

Ideas:
Technology to bind multiple IP addresses with a single NIC and crontab automatic execution technology
For the host's network card more binding a static IP, such as 139.24.214.82, this address is easy to determine from the machine,
Bind a dynamic ip,127.0.0.1 to the slave's Nic, which will be modified by the script to 139.24.214.22 when the host fails
Add a script on the slave machine/root/ autofloatip.sh, using crontab technology to make this script to execute every minute, the role of this script is to determine the host address 82 can ping, once it is not normal will be the extra IP address of their network card to 139.24.214.22, if the host recovery, the address is changed back to 127 .0.0.1

Steps
1. Add a static IP139.24.214.82 to the host, because this is a static IP, you can use the graphical interface to set up this IP and save the method, or in/etc/sysconfig/ A file named Ifcfg-eth0:1 is created in the Network-scripts directory with the following contents:
Device=eth0:1
ipaddr=139.24.214.82
netmask= 255.255.255.0
onboot= Yes

2. On the slave, create a script under/root autofloatip.sh
to allow it to execute with chmod +x autofloatip.sh, with the contents of the script
c1=$ (ping 139.24.214.82-c 1|grep UNREACHABLE|WC-L)
If [$c 1-gt 0]; then
c2=$ (ping 139.24.214.82-c 10|grep unreachable|wc-l)
If [$c 2-gt 9]; Then
    c3=$ (ping 139.24.214.22-c 10|grep unreachable|wc-l)
      if [$c 3-GT 9]; Then
        /sbin/ifconfig eth0:1 139.24.214.22 netmask 255.255.254.0
echo "Float IP to $"
      fi
Fi
echo "Can not connect"
Else
c4=$ (/sbin /ifconfig|grep 139.24.214.22|wc-l)
If [$c 4-gt 0]; then
   /sbin/ifconfig eth0:2 127.0.0.1 Net Mask 255.255.254.0
    echo "Reset IP"
Fi
Echo "Connection is OK"
fi

One of the key commands is
/sbin/ifconfig eth0:1 139.24.214.22 netmask 255.255.254.0
/sbin/ifconfig eth0:2 127.0.0.1 netmask 255.255.254.0
Using this method to dynamically modify IP, the dynamic IP will disappear when the computer restarts

3 building crontab from the machine
With the CRONTAB-E command.
Let's add such a line and save
* * * * * */root/autofloatip.sh >/dev/null 2>&1

Little Tips
In the test, you can first change this sentence to * * * * * */root/autofloatip.sh
So you can get a lot of mail with mutt, the user keeps track of script execution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.