redis with keepalived

keepalive issue:

http://www.cyberciti.biz/faq/linux-unix-verify-keepalived-working-or-not/

/sbin/ip addr del 10.194.0.200/32 dev eth1

tcpdump -vv -n -i eth3 host 10.194.3.200

# uuptable related

cat /var/logs/mess

/sbin/iptables -I INPUT -i eth1 -d 224.0.0.0/8 -j ACCEPT

/sbin/iptables -A INPUT -p 112 -i eth1 -j ACCEPT

/sbin/iptables -A OUTPUT -p 112 -o eth1 -j ACCEPT

/sbin/service iptables save

———————–

redis conf in java

<bean id=”jedisNewsPoolConfig”>

<property name=”maxActive” value=”20000″ />

<property name=”maxIdle” value=”500″ />

<property name=”maxWait” value=”500″ />

<property name=”testOnBorrow” value=”true” />

</bean>

<bean id=”jedisNewsConnectionFactory”

class=”org.springframework.data.redis.connection.jedis.JedisConnectionFactory”>

<property name=”hostName” value=”${redis.host}” />

<property name=”port” value=”${redis.port}” />

<property name=”password” value=”${redis.pass}” />

<property name=”poolConfig” ref=”jedisNewsPoolConfig” />

</bean>

<bean id=”redisNewsTemplate”>

<property name=”connectionFactory” ref=”jedisNewsConnectionFactory” />

</bean>

keepalived shell script :

!!!Master and Slave 分别安装 keepalived,需要OpenSSL lib

{{{

tar zxf keepalived-1.2.8.tar.gz

cd keepalived-1.2.8

./configure  –prefix=/usr/local/keepalived/ && make && make install

}}}

10.194.0.199

!1.编写Keepalived conf

!!!Master  (192.168.186.128)

*vi /etc/keepalived/keepalived.conf

{{{

vrrp_script chk_redis {

script “/etc/keepalived/scripts/redis_check.sh”   ###监控脚本

interval 2                                        ###监控时间

}

vrrp_instance VI_1 {

state MASTER                            ###设置为MASTER

interface eth0                          ###监控网卡

virtual_router_id 51

priority 101                            ###权重值,比Slave稍高

authentication {

auth_type PASS             ###加密

auth_pass redis            ###密码

}

track_script {

chk_redis                       ###执行上面定义的chk_redis

}

virtual_ipaddress {

10.194.3.200                         ###VIP

}

notify_master /etc/keepalived/scripts/redis_master.sh

notify_backup /etc/keepalived/scripts/redis_backup.sh

notify_fault  /etc/keepalived/scripts/redis_fault.sh

notify_stop   /etc/keepalived/scripts/redis_stop.sh

}

}}}

!!!Slave  (192.168.186.129)

*vi /etc/keepalived/keepalived.conf

{{{

vrrp_script chk_redis {

script “/etc/keepalived/scripts/redis_check.sh”   ###监控脚本

interval 2                                        ###监控时间

}

vrrp_instance VI_1 {

state BACKUP                                ###设置为BACKUP

interface eth0                              ###监控网卡

virtual_router_id 51

priority 100                                ###比MASTRE权重值低

authentication {

auth_type PASS

auth_pass redis                ###密码与MASTRE相同

}

track_script {

chk_redis                       ###执行上面定义的chk_redis

}

virtual_ipaddress {

10.194.3.200                         ###VIP

}

notify_master /etc/keepalived/scripts/redis_master.sh

notify_backup /etc/keepalived/scripts/redis_backup.sh

notify_fault  /etc/keepalived/scripts/redis_fault.sh

notify_stop   /etc/keepalived/scripts/redis_stop.sh

}

}}}

!2.编写Event Script:

!!!Master and Slave 各自建立 Redis check, fault , stop script

*vi /etc/keepalived/scripts/redis_check.sh

{{{

#!/bin/bash

ALIVE=`/usr/local/redis-stable/src/redis-cli PING`

if [ “$ALIVE” == “PONG” ]; then

echo $ALIVE

exit 0

else

echo $ALIVE

exit 1

fi

}}}

*vi /etc/keepalived/scripts/redis_fault.sh

{{{

#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo “[fault]” >> $LOGFILE

date >> $LOGFILE

}}}

*vi /etc/keepalived/scripts/redis_stop.sh

{{{

#!/bin/bash

LOGFILE=/var/log/keepalived-redis-state.log

echo “[stop]” >> $LOGFILE

date >> $LOGFILE

}}}

—-

!!!Master:

*vi /etc/keepalived/scripts/redis_master.sh

{{{

#!/bin/bash

REDISCLI=”/usr/local/redis-stable/src/redis-cli”

LOGFILE=”/var/log/keepalived-redis-state.log”

echo “[master]” >> $LOGFILE

date >> $LOGFILE

echo “Being master….” >> $LOGFILE 2>&1

echo “Run: SLAVEOF 10.194.3.194 6379” >> $LOGFILE

$REDISCLI SLAVEOF 10.194.3.194 6379 >> $LOGFILE  2>&1

echo “Run: sleep 10 (for waiting sync data)” >> $LOGFILE

sleep 10 #延迟10秒以后待数据同步完成后再取消同步状态

echo “Run: SLAVEOF NO ONE” >> $LOGFILE

$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

}}}

—-

*vi /etc/keepalived/scripts/redis_backup.sh

{{{

!/bin/bash

REDISCLI=”/usr/local/redis-stable/src/redis-cli”

LOGFILE=”/var/log/keepalived-redis-state.log”

echo “[backup]” >> $LOGFILE

date >> $LOGFILE

echo “Being slave….” >> $LOGFILE 2>&1

echo “Run: sleep 10 (wait for sync data by others)” $LOGFILE

sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色

echo “Run: SLAVEOF 10.194.3.194 6379″ >> $LOGFILE

$REDISCLI SLAVEOF 10.194.3.194 6379 >> $LOGFILE  2>&1

}}}

—-

!!!Slave:

*vi /etc/keepalived/scripts/redis_master.sh

{{{

#!/bin/bash

REDISCLI=”/usr/local/redis-stable/src/redis-cli”

LOGFILE=”/var/log/keepalived-redis-state.log”

echo “[master]” >> $LOGFILE

date >> $LOGFILE

echo “Being master….” >> $LOGFILE 2>&1

echo “Run: SLAVEOF NO ONE” >> $LOGFILE

$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

}}}

—-

*vi /etc/keepalived/scripts/redis_backup.sh

{{{

#!/bin/bash

REDISCLI=”/usr/local/redis-stable/src/redis-cli”

LOGFILE=”/var/log/keepalived-redis-state.log”

echo “[backup]” >> $LOGFILE

date >> $LOGFILE

echo “Being slave….” >> $LOGFILE 2>&1

echo “Run: sleep 10 (for waiting sync data for others)” >> $LOGFILE

sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色

echo “Run: SLAVEOF 10.194.3.194 6379” >> $LOGFILE

$REDISCLI SLAVEOF 10.194.3.194 6379 >> $LOGFILE  2>&1

}}}

!3.给脚本都加上可执行权限:

chmod +x /etc/keepalived/scripts/*.sh

!4.启动并测试

{{{

启动各自Redis Server:

/usr/local/redis-stable/src/redis-server

启动各自 Keepalived

/usr/local/keepalived/sbin/keepalived  -D -f /etc/keepalived/keepalived.conf

完成部署。

}}}

#################################################################################

redis shell script:

{{{

cd /etc/rc.d/init.d/

vi redis

}}}

{{{

#!/bin/bash

#chkconfig: 2345 55 25

#description: Starts,stops and restart the redis-server

#Ver:1.1

#Write by ND chengh(200808)

#usage: ./script_name -p [port] {start|stop|status|restart}

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check networking is up.

[ “$NETWORKING” = “no” ] && exit 0

RETVAL=0

REDIS_PORT=6379

PID=

if [ “$1” = “-p” ]; then

    REDIS_PORT=$2

    shift 2

fi

REDIS_DIR=”/usr/local/redis-stable”

REDIS=”${REDIS_DIR}/src/redis-server”

PROG=$(basename $REDIS)

CONF=”${REDIS_DIR}/etc/redis-${REDIS_PORT}.conf”

if [ ! -f $CONF ]; then

   if [ -f “${REDIS_DIR}/redis.conf” ];then

      CONF=”${REDIS_DIR}/redis.conf”

   else

      echo -n $”$CONF not exist.”;warning;echo

      exit 1

   fi

fi

PID_FILE=`grep “pidfile” ${CONF}|cut -d ‘ ‘ -f2`

PID_FILE=${PID_FILE:=/var/run/redis/redis.pid}

LOCKFILE=”/var/lock/subsys/redis/redis-${REDIS_PORT}”

if [ ! -x $REDIS ]; then

    echo -n $”$REDIS not exist.”;warning;echo

    exit 0

fi

start() {

    echo -n $”Starting $PROG: ”

    $REDIS $CONF

    RETVAL=$?

    if [ $RETVAL -eq 0 ]; then

        success;echo;touch $LOCKFILE

    else

        failure;echo

    fi

    return $RETVAL

}

stop() {

    echo -n $”Stopping $PROG: ”

    if [ -f $PID_FILE ] ;then

       read PID <  “$PID_FILE”

    else

       failure;echo;

       echo -n $”$PID_FILE not found.”;failure;echo

       return 1;

    fi

    if checkpid $PID; then

     kill -TERM $PID >/dev/null 2>&1

        RETVAL=$?

        if [ $RETVAL -eq 0 ] ;then

                success;echo

                echo -n “Waiting for Redis to shutdown ..”

         while checkpid $PID;do

                 echo -n “.”

                 sleep 1;

                done

                success;echo;rm -f $LOCKFILE

        else

                failure;echo

        fi

    else

        echo -n $”Redis is dead and $PID_FILE exists.”;failure;echo

        RETVAL=7

    fi

    return $RETVAL

}

restart() {

    stop

    start

}

rhstatus() {

    status -p ${PID_FILE} $PROG

}

hid_status() {

    rhstatus >/dev/null 2>&1

}

case “$1” in

    start)

        hid_status && exit 0

        start

        ;;

    stop)

        rhstatus || exit 0

        stop

        ;;

    restart)

        restart

        ;;

    status)

        rhstatus

        RETVAL=$?

        ;;

    *)

        echo $”Usage: $0 -p [port] {start|stop|status|restart}”

        RETVAL=1

esac

exit $RETVAL

}}}

{{{

chmod +x redis

sudo /sbin/chkconfig

sudo ln -s /sbin/chkconfig /usr/bin/

chkconfig –add redis

chkconfig –level 345 redis on

}}}

Advertisements

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