Keepalive

lightweight vs heavyweight
Keepalive        Corosync+pacemaker

lightweight Difference with heavyweight:
1. System load
2. Performance
3. Configuration Complex

Technology core is VRRP  (Virtual Route Redundancy Protocol)

Keepalive IPAddress
ENV:
Server 1 192.168.199.187
Server 2 192.168.199.179
VIP: 192.168.199.188

Server 1
1. time sync & close firewall selinux
2. install keepalive: #yum install keepalived
Check package information: #yum info keepalived
3. Check help information
#man keepalived
#man keepalived.conf
4. Change Config
#cd /etc/keepalived
#cp keepalived.conf keepalived.conf.bk
#vim /etc/keepalived/keepalived.conf
A.Add # from virtual_server 192.168.200.100 433 { to end
:.,$s/^/#
B. Change from
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass apache
    }
    virtual_ipaddress {
    192.168.200.16
    192.168.200.17
    192.168.200.18
    }
}
to
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass apache
    }
    track_interface {
        eth0
    }
    virtual_ipaddress {
    192.168.199.188/24 dev eth0 label eth0:0
    }
}
5. start keepalived service: service keepalived start

Server 2
1. time sync & close firewall selinux
2. install keepalive: #yum install keepalived
Check package information: #yum info keepalived
3. Check help information
#man keepalived
#man keepalived.conf
4. Change Config
#cd /etc/keepalived
#cp keepalived.conf keepalived.conf.bk
#vim /etc/keepalived/keepalived.conf
A.Add # from virtual_server 192.168.200.100 433 { to end
:.,$s/^/#
B. Change from
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass apache
    }
    virtual_ipaddress {
    192.168.200.16
    192.168.200.17
    192.168.200.18
    }
}
to
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass apache
    }
    track_interface {
        eth0
    }
    virtual_ipaddress {
        192.168.199.188/24 dev eth0 label eth0:0
    }
}
5. start keepalived service: #service keepalived start
6. check server1/server2 system log: #more /var/log/messages
7. using client ping the 192.168.199.188
8. check arp list: arp -a
9. stop the server1 service: #service keepalived stop
10. check again 
11. stop the server2 network: #service network stop
12. check again
13. Add Check script instead of stop service on server 1 / 2
check the service down file exist or not.
If exits, means the service is down, reduce 5 of the priority 
#vim /etc/keepalived/keepalived.conf
vrrp_script chk_maintenance_down {
    script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
    interval 1
    weight -5
}

14. Add trigger script on server 1/2
#vim /etc/keepalived/keepalived.conf
from
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass apache
    }
    track_interface {
        eth0
    }
    virtual_ipaddress {
        192.168.199.188/24 dev eth0 label eth0:0
    }
}
to
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 98
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass apache
    }
    track_interface {
        eth0
    }
    virtual_ipaddress {
        192.168.199.188/24 dev eth0 label eth0:0
    }
    track_script {
        chk_maintenance_down
    }
}

15. restart the keepalived service on server 1/2
16. server 1 add file /etc/keepalived/down, ipaddress go to server 2
17. server 1 remove file /etc/keepalived/down, ipaddress come back to server 1

Keepalive Apache depending on above test
1. install httpd service
2. create test page
3. change httpd configuration to keep listen the VIP address
4. before change, the keepalived working on level 4 (network level)
5. change the keepalived configuration to make keepalived work on level 7 (application level)
using curl or killall -0 <service> to test service running or not

#vim /etc/keepalived/keepalived.conf
A. Add
chk_httpd{
    script "killall -0 httpd && exit 0||exit 1"
    interval 1
    weight -5
}

B. Change from 
chk_maintenance_down
to
chk_httpd

6. Add the start and stop script for master and backup on server 1/2
#vim /etc/keepalived/notify.sh
#!/bin/bash
#Author: lianshu <linuxedu@foxmail.com>
# description: An example of notify script
#  

vip=192.168.199.188
contact='root@localhost'

notify() {
    mailsubject="`hostname` to be $1: $vip floating"
    mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"
    echo $mailbody | mail -s "$mailsubject" $contact
}

case "$1" in
     master)
        notify master
        /etc/rc.d/init.d/httpd start
        exit 0
    ;;
    backup)
        notify backup
        /etc/rc.d/init.d/httpd stop
        exit 0
    ;;
    fault)
        notify fault
        /etc/rc.d/init.d/httpd stop
        exit 0
    ;;
    *)
        echo 'Usage: `basename $0` {master|backup|fault}'
        exit 1
    ;;
esac

7. Change the keepalived configuration file on server 1/2
#vim /etc/keepalived/keepalived.conf
Add following in vrrp_instance_VI_1{
}
notify_master "/etc/keepalived/notify.sh master"  
notify_backup "/etc/keepalived/notify.sh backup"  
notify_fault "/etc/keepalived/notify.sh fault"  

8. stop httpd all server
9. restart keepalived service

Test result
Server 1 stop httpd/network, service switch to server 2;
server 1 start httpd/network, service still work in server 2;
server 2 stop httpd/network, service switch to server 1;
both server stop httpd/network, service will stop working;

Comments

Popular posts from this blog

Nginx Proxy & Load Balance & LNMP

Snort+barnyard2+Snorby CentOS 6.5_64 Installation

ORACLE Error