And then there is the other problem: when your web server dies, your website dies with it. Of course you keep backups of everything, but still it takes a lot of time to get a new hardware, install it and restore the backups. Even if you keep a hot stand-by server, you have to be around to redirect the website traffic to the new server.
What you need is to spread your website load to more servers and for that, you need a load balancer in front of your web server farm. You can buy a ready-made box for this purpose, but they tend to be costly. In this article, I'll explain how to build a simple load balancer using a great piece of open source software called Pound.
How does it work?
Pound keeps a pool of your web servers. If a request arrives at pound, it chooses a web server randomly and forwards the request to this server. You can define weights of servers, enabling you to use servers with different performance. Pound waits for the response from the web server and forwards the response back to the requesting client.
If a web server dies or is unresponsive, it is removed from the pool of active servers and pound stops forwarding requests to it. It re-tries the server repeatedly and when it becomes available, it returns it back to the active pool.
Installing pound
Pound runs under any UNIX-type system. I have personal experience with FreeBSD and Linux. Like I always say, the best Linux/Unix-type system is the one you know, so pick any distribution you are familiar with. Chances are, that packages exist for your distribution. I will not go into much detail, I leave it to the kind reader to install pound as such.
My favorite Linux distribution is CentOS, where installation is as simple as downloading the pound packages from the famous dag repository and installing with:
yum -y localinstall pound-2.4-1.el5.rf.i386.rpm
After that, you end up with a vanilla load balancer, that is not just ready to serve your visitors. The important file we'll inspect and edit now is the pound.conf, usually located in /etc directory.
Prepare your landscape
I assume you already have your web servers ready. In some future article I may delve deeper into the actual web server configuration, but basically what you want to do is make an exact copy or copies of your web server, changing only the IP adress, hostname and possibily deleting old logfiles. You will need the IP addresses of your server. Under normal circumstances, I am all for using hostnames, instead of IP addresses, but this is one of the few exceptions, where usage of IP adresses is justifiable and even preferred.
For the purpose of this article, I'll assume you have just two web servers with following IP addresses:
- Server: 192.168.1.10
- Server: 192.168.1.20
/etc/pound.conf
Following is a simple config file, with comments. The comments are delimited by a hash sign (#) and you may remove them if you want to use this file.
# The Control directive contains a path and filename,
# where pound will create a handle, which can later
# be used for controlling pound via the
# poundctl utility
Control "/tmp/poundctl"
# In this directive we tell pound to listen on port 80
# on any IP address the server has.
ListenHTTP
Address 0.0.0.0
Port 80
End
# For the purpose of this article, we'll have
# just one simple service, which will serve
# any and all requests arriving
# at the balancer's port 80.
Service
# Specify Server 1.
# The Port is the port on the web server, where http
# server listens on.
# The TimeOut directive tells pound how long to wait
# for a response from the web server. If the web
# server does not respond in this amount of seconds,
# it is considered dead and pound removes it
# from the pool of usable server.
BackEnd
Address 192.168.1.10
Port 80
TimeOut 30
End
# Server 2
BackEnd
Address 192.168.1.20
Port 80
TimeOut 30
End
End
Starting pound
If you installed pound from RPM packages, you probably have all you need to launch. Just issue the standard:
service pound startTo enable automatic startup at the server start, issue:
chkconfig pound on...and you're done
You have just created your first load balancer. It is a very simple one, but it does the job. I will try to describe some more advanced features and techniques in some future article.
After you reach a certain amount of traffic you will want to do some tuning as well, but for many cases, the default settings of pound and Linux kernel are OK.
1 komentářů: