Posted by: kezhong | February 28, 2010

Implementing Load Balance on CentOS 5.4

The Linux Virtual Server (LVS) allows load balancing of networked services such as web and mail servers. It is extremely fast and allows such services to be scaled to service 10s or 100s of thousands of simultaneous connections.

I installed CentOS 5.4 on three machines which one as director and two as real servers. The IP address of the Linux director was 192.168.26.201. The IP addresses of the two real servers were 192.168.26.211 and 192.168.26.212 respectively. The VIP was 192.168.26.200.

Installation of the LVS on the Linux Director
In CentOS 5.4, LVS is a loadable kernel module. Add the LVS module to the Linux kernel on the Linux Director.

# modprobe ip_vs
# cat /proc/net/ip_vs
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weghit ActiveConn InActConn

Install ipvsadm on the Linux Director
# yum install ipvsadm
# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weghit ActiveConn InActConn

Configuration of the Director
Config VIP
# ifconfig eth0:0 192.168.26.200 netmask 255.255.255.255 broadcast 192.168.26.200 up

Enable packet forwarding
# echo 1 > /proc/sys/net/ipv4/ip_forward

Add a virtual service and two real servers
# ipvsadm –A –t 192.168.26.200:80 –s rr
# ipvsadm –a –t 192.168.26.200:80 –r 192.168.26.211 –g
# ipvsadm –a –t 192.168.26.200:80 –r 192.168.26.212 –g

Append the above five command lines to the /etc/rc.local file so that you will not need to run them again after the next boot up.

Close the firewall
# iptables -F

Configuration of the Real Server 1
Append the four lines as below to the /etc/sysctl.conf file to disable arp request.
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

Load in sysctl settings from the file /etc/sysctl.conf.
# sysctl –p

Config VIP and route
# ifconfig lo:0 192.168.26.200 netmask 255.255.255.255 broadcast 192.168.26.200 up
# route add -host 192.168.26.200 dev lo:0

Append the above two command lines to the /etc/rc.local file.

Modify the main page in this server
# echo ‘ the site is 192.168.26.211’ > /var/www/html/index.html

Enable the apache webserver to automatically start
# chkconfig httpd on

Startup the apache webserver
# service httpd start

Close the firewall
# iptables -F

Configuration of the Real Server 2
Append the four lines as below to the /etc/sysctl.conf file to disable arp request.
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2

Load in sysctl settings from the file /etc/sysctl.conf
# sysctl –p

Config VIP and route
# ifconfig lo:0 192.168.26.200 netmask 255.255.255.255 broadcast 192.168.26.200 up
# route add -host 192.168.26.200 dev lo:0

Append the above two command lines to the /etc/rc.local file.

Modify the main page in this server
# echo ‘ the site is 192.168.26.212’ > /var/www/html/index.html

Enable the apache webserver to automatically start
# chkconfig httpd on

Startup the apache webserver
# service httpd start

Close the firewall
# iptables -F

Test
Launch the web site http://192.168.26.200 through the web browser at another machine. Refresh the browser again and again, the different pages appeared. It means that it succeeded.

Check at the LVS Director.
# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.26.200:80 rr
-> 192.168.26.211:80             Route   1    0          3
-> 192.168.26.212:80             Route   1    0          3

InActConn was both 3, it means that the two real servers had received the forwards.

References
Linux Virtual Server Tutorial
Linux Virtual Server Administration


Responses

  1. […] a Linux loadbalancer with LVS and ldirectord I had built a basic LVS as my last post Implementing Load Balance on CentOS 5.4, but it was not practical. Because once a real server goes down, the director will not be able to […]

  2. awsome..ur articles is cool.
    thx gan

  3. thank you very much, helps me a lot…

  4. Hi,

    I am facing problem while configuring real server. When I run following command

    ifconfig lo:0 192.168.26.200 netmask 255.255.255.255 broadcast 192.168.26.200 up

    Network crashes. I am using centos.

    Can you help me in this?

    Thanks & Regards,
    Atul Atri.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: