DNS load balancing is the first implementation of load balancing technology. Configuring the same name for multiple addresses in a DNS configuration file is configured with multiple A records pointing to a different IP, and the client will randomly obtain one of the addresses when querying this a record. It is not difficult to find that DNS load balancing has the characteristics of simple configuration, excellent performance, and no cost of modifying the architecture. Therefore, it is often used in intranet.
said the advantages, but also to talk about shortcomings. DNS load equalization is a simple round robin load algorithm, can not distinguish the difference between servers, can not be based on the running state of the back-end server dynamic adjustment, that is, health checks. Due to the randomness of the algorithm, it is not possible to allocate more requests for the server with better performance, which often occurs when the request is concentrated on a server.
If your load-balancing requirements are high, it's easier to use other load-balancing techniques, such as Lvs,nginx or Haproxy. Modify the algorithm, not only to see understand yangyang scattered tens of thousands of lines of source code, but also to the perfect integration of their codes, the cost varies from person to person, but certainly not overnight. But if it's just a back-end server's health-detection problem, you can do it with a shell script.
Thinking: The DNS server through some mechanism of the back-end RS host running state to judge, if the back-end host failure, then DNS to do is to modify the configuration file, the problem host from the configuration file and restart the service. Further, when the RS host is restored, the DNS host also restores it to the configuration file.
The first is the health detection mechanism. If the RS host is a Web service, then there are at least three optional icmp,telnet,curl, which work on the third, fourth, and seventh layers respectively. We use ICMP here. Modifying the configuration file can be done by overwriting the original file with the prepared profile, and, of course, sed-i, where we use Sed-i. Finally, use the while and if elif to properly nest these elements.
Web1 192.168.1.1 WEB2 192.168.1.2
DNS configuration file:
www in A 192.168.1.1
www in A 192.168.1.2
#! /bin/bash
While True;do #定义无限循环, let the script work uninterrupted.
Ping-c1 192.168.1.1 &>/dev/null #由于linux下的ping命令会无限进行下去, so we want to use the-c parameter to specify the number of packets, and the standard error and standard output are all redirected to/dev/ Null in this dirty directory.
if! [$-eq 0];then #如果 $] The return value is not zero, that is, the ping is not pass.
Sed-i '/192.168.1.1/s/^/;/' DNS #修改配置文件 that replaces the beginning of line with 192.168.1.1 with a number. (The comment for the DNS configuration file is; number)
/etc/init.d/bind restart
Else
Sed-i '/192.168.1.1/s/;//' DNS #如果可以ping通, then removed;
/etc/init.d/bind restart
Fi
PING-C1 192.168.1.2 &>/dev/null
if! [$-eq 0];then
Sed-i '/192.168.1.2/s/^/;/' DNS
/etc/init.d/bind restart
Else
Sed-i '/192.168.1.2/s/;//' DNS
/etc/init.d/bind restart
Fi
Sleep 5 #休息5秒 and continue to work.
Done