DNS is used to realize a simple load balancing method on Linux

  • 2020-10-31 22:06:02
  • OfStack

DNS polling maps multiple servers to the same host name and doesn't do much for the magic shown here.

If your back-end server is made up of multiple servers, such as clustered or mirrored Web or file servers, a single 1 entry point is provided through the load balancer. Large, busy e-commerce companies spend a lot of money on high-end load balancers that perform a variety of tasks: agents, caching, health checking, SSL processing, configurable prioritization, traffic shaping, and more.

But you don't need a load balancer to do that much work. What you need is a simple way to distribute load across servers that provides failover and doesn't care if it's efficient or perfect. DNS polling and subdomain delegates using polling are two simple ways to achieve this goal.

DNS polling maps multiple servers to the same hostname when a user accesses it foo.example.com When multiple servers can be used to process their requests, this is the approach used.

It is useful to use polled subdomain delegates when you have multiple subdomains or when your servers are geographically dispersed. You have a primary DNS server and subdomains have their own DNS servers. Your primary DNS server directs all requests to subdomains to their own DNS servers. This will improve response time because the DNS agreement will automatically find the fastest link.

DNS polling

It has nothing to do with tourist thrush robins. According to the librarian I know, it started as a French phrase. ruban rond、 or round ribbon . Long ago, French government officials signed petitions in unclassified circles, wavy lines, or straight lines to cover the original sponsors.

DNS polling is also non-hierarchical, simply configuring a list of servers and then routing requests to each server. It doesn't really do load balancing, because it doesn't measure load at all, and it doesn't have health checks, so if one server goes down, requests will still be sent to that server that's down. It has the advantage of simplicity. If you have a small file or a cluster of Web servers and want a simple way to spread the load between them, DNS polling is for you.

All you have to do is create multiple A or AAAA records, mapping multiple servers to a single host name. This BIND example USES both the IPv4 and IPv6 private address classes:


fileserv.example.com. IN A 172.16.10.10
fileserv.example.com. IN A 172.16.10.11
fileserv.example.com. IN A 172.16.10.12
fileserv.example.com. IN AAAA fd02:faea:f561:8fa0:1::10
fileserv.example.com. IN AAAA fd02:faea:f561:8fa0:1::11
fileserv.example.com. IN AAAA fd02:faea:f561:8fa0:1::12

Dnsmasq in /etc/hosts Save THE A and AAAA records in the file:


172.16.1.10 fileserv fileserv.example.com
172.16.1.11 fileserv fileserv.example.com
172.16.1.12 fileserv fileserv.example.com
fd02:faea:f561:8fa0:1::10 fileserv fileserv.example.com
fd02:faea:f561:8fa0:1::11 fileserv fileserv.example.com
fd02:faea:f561:8fa0:1::12 fileserv fileserv.example.com

Note that these examples are very simplified and there are many ways to parse fully qualified domain names, so learn how to configure DNS yourself.

use dig Command to check that your configuration is working as expected. will ns.example.com Replace it with your domain name server:


$ dig @ns.example.com fileserv A fileserv AAA

It displays the polling record for both IPv4 and IPv6.

Subdomain delegation and polling

Subdomain delegation combined with polling requires more configuration, but it has one advantage. Use it when you have multiple subdomains or geographically dispersed servers. It has a faster response time, and the down server doesn't respond, so the client doesn't get stuck waiting for a reply. A short TTL, say 60 seconds, will do it for you.

This approach requires multiple domain name servers. In the simplest scenario, you would need a primary domain server and two subdomains, each with its own domain server. Configure your polling records on the subdomain server, and then configure the delegate on your primary domain server.

In BIND on the primary domain server, you need at least two additional configurations, a zone declaration, and the A/AAAA record in the zone data file. The delegate in the primary domain server should look like this:


ns1.sub.example.com. IN A 172.16.1.20
ns1.sub.example.com. IN AAAA fd02:faea:f561:8fa0:1::20
ns2.sub.example.com. IN A 172.16.1.21
ns2.sub.example.com. IN AAA fd02:faea:f561:8fa0:1::21
sub.example.com. IN NS ns1.sub.example.com.
sub.example.com. IN NS ns2.sub.example.com.

Each of the next subdomain servers has its own section file. Here its key point is that each server goes back to its own IP address. in named.conf States that all services are the same:


zone "sub.example.com" {
  type master;
  file "db.sub.example.com";
};

Then the data file is the same, except that the A/AAAA record USES each server's own IP address. SOA records all point to the primary domain name server:


; first subdomain name server
$ORIGIN sub.example.com.
$TTL 60
sub.example.com IN SOA ns1.example.com. admin.example.com. (
    2018123456   ; serial
    3H       ; refresh
    15       ; retry
    3600000     ; expire
)
sub.example.com. IN NS ns1.sub.example.com.
sub.example.com. IN A 172.16.1.20
ns1.sub.example.com. IN AAAA fd02:faea:f561:8fa0:1::20
; second subdomain name server
$ORIGIN sub.example.com.
$TTL 60
sub.example.com IN SOA ns1.example.com. admin.example.com. (
    2018234567   ; serial
    3H       ; refresh
    15       ; retry
    3600000     ; expire
)
sub.example.com. IN NS ns1.sub.example.com.
sub.example.com. IN A 172.16.1.21
ns2.sub.example.com. IN AAAA fd02:faea:f561:8fa0:1::21

Next, generate the polling records on the subdomain server, using the same methods as before. You now have multiple domain name servers to handle requests to your subdomain. Again, BIND is complicated, and there are many ways to do the same thing, so the homework assignment for you is to find the best configuration that works for you.

It is easy to do subdomain delegation in Dnsmasq. On your primary domain name server dnsmasq.conf Add the following line to the file to point to the domain name server for the subdomain:


server=/sub.example.com/172.16.1.20
server=/sub.example.com/172.16.1.21
server=/sub.example.com/fd02:faea:f561:8fa0:1::20
server=/sub.example.com/fd02:faea:f561:8fa0:1::21

Then on the domain name server of the subdomain /etc/hosts Configure polling.

For details and help on configuration methods, refer to these resources:

Dnsmasq DNS and BIND, 5th Edition

Related articles: