Monday, January 16, 2012

DNS BIND load-balance setup

I one of my previous blogposts I stated that a method to loadbalance a cluster of (for example) webserver nodes would be using bind DNS and round robin. By using such a method your DNS server would provide you a different IP address evert time you request the IP address of the server based upon the name.

As an example I have created a domain to play with in my own home setup. I have created the domain johanexample.com and I do run a website on it which can be accessed via the www.johanexample.com. Meaning that if I try to access www.johanexample.com my laptop will request the IP address of this webserver by asking it to my DNS bind server for it. The DNS server will tell the ip address is 192.0.2.44. This is a situation which you can see in the example diagram below. 


Now I do think that a lot of people like to see this page and for this reason I have build a cluster of webservers all ready to provide the content of the website www.johanexample.com to the users who want to see it. I have in total 4 webservers up and running and I named them node0, node1, node2 and node3 and the have the IP addresses 192.0.2.20, 192.0.2.21, 192.0.2.22 and 291.0.2.23

So what I would like is that the first user who wants to visit my website would be directed to node0 the second to node1, the third to node2, the fourth to node3 and fifth to node0 etc etc etc. To do this I have to setup my bind server in such a way this will start happening. So I have taken the following steps:

1) Create a zone file for the johanexample.com domain. I have created /etc/bind/zones/johanexample.com

2) Make sure the file johanexample.com is refereed to in the named.conf.local file so it will be picked up in the main configuration of bind.In my case I added:

zone "johanexample.com" {
    type master;
    file "/etc/bind/zones/johanexample.com";
};


3) make sure you create a complete zone file. My zone file looks like the one below. I do make more use of johanexample.com in my home network for testing so not all things in here are needed in this example:
; johanexample.com
$TTL    604800
@       IN      SOA     ns1.johanexample.com. root.johanexample.com. (
                     2012011501 ; Serial
                         604800 ; Refresh
                          86400 ; Retry
                        2419200 ; Expire
                         604800); Negative Cache TTL
;
@       IN      NS      ns1
        IN      MX      10 mail
        IN      A       192.168.1.109 
ns1     IN      A       192.168.1.109 
mail    IN      A       192.0.2.128
www     IN      A       192.0.2.20
 IN A 192.0.2.21
 IN A 192.0.2.22
 IN A 192.0.2.23
node0 IN A 192.0.2.20
node1 IN A 192.0.2.21
node2 IN A 192.0.2.22
node3 IN A 192.0.2.23

What specifically is needed for the load balance part is the records which are associated with www. We use www in combination with johanexample.com and where you see in a "normal" setup only one IP address per server you now see 4 different ones:

www     IN      A       192.0.2.20
 IN A 192.0.2.21
 IN A 192.0.2.22
 IN A 192.0.2.23

I have also created records in the zone file for all the specific nodes so I can acces them by making use of node0.johanexample.com, node1.johanexample.com, etc etc. This is however not needed. You can do without however it can be very handy from time to time. When you do play arround with your zone files and do try some things it is good to know that next to the more known utility named-checkconf you also have a utility to check your zone file: named-checkzone. In this example the way to use it would be:

root@debian-bind:/# named-checkzone johanexample.com /etc/bind/zones/johanexample.com 
zone johanexample.com/IN: loaded serial 2012011501
OK
root@debian-bind:/# 

When you zone file is correct configured and you have configured multiple IP's to one name you should be able to see the loadbalancing happening when you for example do a nslookup command like in the example below:
root@debian-bind:/# nslookup www.johanexample.com
Server:  192.168.1.109
Address: 192.168.1.109#53

Name: www.johanexample.com
Address: 192.0.2.21
Name: www.johanexample.com
Address: 192.0.2.22
Name: www.johanexample.com
Address: 192.0.2.23
Name: www.johanexample.com
Address: 192.0.2.20

root@debian-bind:/# 

A second check could be to do multiple ping commands and you will see the target IP change:
root@debian-bind:/# ping www.johanexample.com
PING www.johanexample.com (192.0.2.23) 56(84) bytes of data.

root@debian-bind:/# ping www.johanexample.com
PING www.johanexample.com (192.0.2.22) 56(84) bytes of data.

root@debian-bind:/# ping www.johanexample.com
PING www.johanexample.com (192.0.2.21) 56(84) bytes of data.

root@debian-bind:/# ping www.johanexample.com
PING www.johanexample.com (192.0.2.20) 56(84) bytes of data.

root@debian-bind:/# ping www.johanexample.com
PING www.johanexample.com (192.0.2.23) 56(84) bytes of data.

No comments: