Load Balancer menggunakan ipvsadm & keepalived di Centos 6

Skema yang saya gunakan (dalam 1 network):

Linux Director 1 (lv1) = 192.168.89.216
Linux Director 2 (lv2) = 192.168.89.215
Real Server 1 = 192.168.89.217
Real Server 2 = 192.168.89.218
Database Server = 192.168.89.209
VIP (Virtual IP Address) = 192.168.89.210

Untuk test ini, saya menggunakan LVS Direct Routing schema (http://www.ultramonkey.org/papers/lvs_tutorial/html/).



Instalasi dan konfigurasi LVS di Linux Director.

[root@lv1 ‾]# modprobe ip_vs
[root@lv1 ‾]# cat /proc/net/ip_vs
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port Forward Weight ActiveConn InActConn

Install ipvsadm di Linux Director

[root@lv1 ‾]# yum install ipvsadm
[root@lv1 ‾]# ipvsadm
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn

Konfigurasi Linux Director

[root@lv1 ‾]# ipvsadm -C
[root@lv1 ‾]# ipvsadm -A -t 192.168.89.210:80 -s rr
[root@lv1 ‾]# ipvsadm -a -t 192.168.89.210:80 -r 192.168.89.217:80 -g -w 1
[root@lv1 ‾]# ipvsadm -a -t 192.168.89.210:80 -r 192.168.89.218:80 -g -w 1
[root@lv1 ‾]# ipvsadm -S
[root@lv1 ‾]# 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.89.210:80 rr
  -> 192.168.89.217:80            Route   1      0          0         
  -> 192.168.89.218:80            Route   1      0          0

Aktifkan IP forwarding. Tambahkan kode berikut di file /etc/sysctl.conf dan kemudian jalankan “sysctl -p”.

net.ipv4.ip_forward = 1

Install keepalived di Linux Director (tambahkan  yum repository epel, jika belum ada)

[root@lv1 ‾]# wget http://ftp.riken.jp/Linux/fedora/epel/RPM-GPG-KEY-EPEL-6
[root@lv1 ‾]# rpm --import RPM-GPG-KEY-EPEL-6
[root@lv1 ‾]# rm -f RPM-GPG-KEY-EPEL-6
[root@lv1 ‾]# vim /etc/yum.repos.d/epel.repo
# create new
[epel]
name=EPEL RPM Repository for Red Hat Enterprise Linux
baseurl=http://ftp.riken.jp/Linux/fedora/epel/6/$basearch/
gpgcheck=1
enabled=0

[root@lv1 ‾]# yum --enablerepo=epel install keepalived
[root@lv1 ‾]# vim /etc/keepalived/keepalived.conf
#add/edit the following value
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.89.210
    }
}
[root@lv1 ‾]# /etc/rc.d/init.d/keepalived start
[root@lv1 ‾]# tail -f /var/log/messages 
[root@lv1 ‾]# chkconfig keepalived on

catatan:
Konfigurasi keepalived lv2 sama persis dengan lv1, kecuali perubahan  nilai state di VI_1 dari MASTER diubah menjadi BACKUP.


Configuration of the Real Server
tambahkan command berikut di /etc/rc.local :

  ifconfig lo:0 192.168.89.210 netmask 255.255.255.255 broadcast 192.168.89.210 up
  route add -host 192.168.89.210 dev lo:0

tambahkan konfigurasi berikut di /etc/sysctl.conf, kemudian jalankan “sysctl -p”.

  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

catatan:
konfigurasi di realserver lainnya sama.


Test
– Buka web site http://192.168.89.210 lewat web browser.

– Check  LVS Director.

[root@lv1 ‾]# 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.89.210:80 rr
  -> 192.168.89.217:80            Route   1      3          4         
  -> 192.168.89.218:80            Route   1      3          4

– Check  Keepalived di lv1

[root@lv1 ‾]# tail -f /var/log/messages
May 22 13:08:59 lv1 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
May 22 13:09:00 lv1 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
May 22 13:09:00 lv1 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
May 22 13:09:00 lv1 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.89.210
May 22 13:09:00 lv1 Keepalived_healthcheckers: Netlink reflector reports IP 192.168.89.210 added

– Check Keepalived di lv2

[root@lv2 ‾]# tail -f /var/log/messages
May 22 13:58:25 lv2 Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.
May 22 13:58:25 lv2 Keepalived_vrrp: Configuration is using : 61281 Bytes
May 22 13:58:25 lv2 Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
May 22 13:58:25 lv2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE

– Shutdown lv1, kemudian cek status  lv2

[root@lv2 ‾]# tail -f /var/log/messages
May 22 13:59:35 lv2 Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
May 22 13:59:36 lv2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
May 22 13:59:36 lv2 Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
May 22 13:59:36 lv2 Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.89.210
May 22 13:59:36 lv2 Keepalived_healthcheckers: Netlink reflector reports IP 192.168.89.210 added