Keepalived is a lightweight and high availability framework solution in Linux, It is seem as HeartBeat, RoseHA, has similar function that implement the service and the high availability of the network, but there are difference that HeartBeat is very professional and functional, but the deployment is very complex.
So Compare with HeartBeat, Keepalived is mainly through the virtual routing redundancy protocol(VRRP) to achive high availability, and the installation and configuraion is very simple.
Keepalived has three modules, core, check and vrrp
Install Keepalived
you can download the latest verion in the offical site: http://www.keepalived.org, Currently, the latest version is 1.3.2. the other way, just install by yum or apt-get
# Handle Incoming HTTP Connection Requests on the virtual IP address controlled by Keepalived listen http-incoming mode http bind 192.168.193.202:80 # Use each server in turn, according to its weight value balance roundrobin # Verify that service is available option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www # Insert X-Forwarded-For header option forwardfor # Define the back-end servers, which can handle up to 512 concurrent connections each server websvr1 192.168.193.225:80 weight 1 maxconn 512 check server websvr2 192.168.193.226:80 weight 1 maxconn 512 check
And the keepalived configuration is same as the previous profile.
Keepalived + Redis
Currently, use the Redis Sentinel completely solved the master failover incident, but also use the Keepalived to solve the switching master-slave.
First, we should write some scripts to monitor redis status:
Redis scripts
check redis server is alive
1 2 3 4 5 6 7 8 9 10
#!/bin/bash ###/etc/keepalived/scripts/redis_check.sh ALIVE=`/usr/bin/redis-cli PING` if [ "$ALIVE" == "PONG" ]; then echo$ALIVE exit 0 else echo$ALIVE exit 1 fi
echo"`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver]" >> $LOGFILE echo"`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver] Run 'SLAVEOF 10.20.112.27 6379'" >> $LOGFILE $REDISCLI SLAVEOF 10.20.112.27 6379 >> $LOGFILE 2>&1 echo"`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver] wait 10 sec for data sync from old master" >> $LOGFILE sleep 10 echo"`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[slaver] data rsync from old mater ok..." >> $LOGFILE echo"`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] Run slaveof no one,close master/slave" >> $LOGFILE $REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1 echo"`date +'%Y-%m-%d:%H:%M:%S'`|$pid|state:[master] wait other slave connect...." >> $LOGFILE