redis的cluster集群模式shell一键启动/停止/重启/清缓存脚本

1 redis集群配置模板
vi /opt/cachecloud/conf/redis-cluster-template.conf

daemonize yes
tcp-backlog 511
timeout 0
tcp-keepalive 60
loglevel notice
databases 16
dir /opt/cachecloud/data
stop-writes-on-bgsave-error no
repl-timeout 60
repl-ping-slave-period 10
repl-disable-tcp-nodelay no
repl-backlog-size 10Mb
repl-backlog-ttl 7200
slave-serve-stale-data yes
slave-read-only yes
slave-priority 100
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 512mb 128mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
port {:port}
maxmemory {:maxmemory}mb
maxmemory-policy allkeys-lru
appendonly no
appendfsync everysec
appendfilename appendonly-{:port}.aof
dbfilename dump-{:port}.rdb
aof-rewrite-incremental-fsync yes
no-appendfsync-on-rewrite yes
auto-aof-rewrite-min-size 4000000kb
auto-aof-rewrite-percentage 88
rdbcompression yes
rdbchecksum yes
repl-diskless-sync no
repl-diskless-sync-delay 5
maxclients 10000
hll-sparse-max-bytes 3000
min-slaves-to-write 0
min-slaves-max-lag 10
aof-load-truncated yes
notify-keyspace-events ""
protected-mode no
cluster-enabled yes
cluster-node-timeout 15000
cluster-slave-validity-factor 10
cluster-migration-barrier 1
cluster-config-file nodes-{:port}.conf
cluster-require-full-coverage no

2 一键配置脚本
vi redisCluster.sh


CONF_DIR="/opt/cachecloud/conf/";
LOG_DIR="/opt/cachecloud/logs/";
REDIS_SRC_DIR="/opt/cachecloud/redis/src/";
REDIS_CLUSTER_TEMPLATE="/opt/cachecloud/conf/redis-cluster-template.conf"
SLOT_SIZE=16384

PORTS=(6961 6962 6963 6964 6965 6966)
MAX_MEMORY=100

function _createConfigFile()
{
    local isAllExists=1
    local i=0
    local port=0
    local configFile=''
    for(( i=0;i<${#PORTS[@]};i++)) 
    do
        port=${PORTS[i]} 
        configFile=$CONF_DIR"redis-cluster-"$port".conf" 
        if [ ! -f $configFile ]; then
            isAllExists=0
            break
        fi
    done

    if [ $isAllExists -eq 1 ];then
        return 0
    fi

    for(( i=0;i<${#PORTS[@]};i++)) 
    do
        port=${PORTS[i]} 
        configFile=$CONF_DIR"redis-cluster-"$port".conf" 
        cp $REDIS_CLUSTER_TEMPLATE $configFile -f
        sed -i 's/{:port}/'$port'/g' $configFile
        sed -i 's/{:maxmemory}/'$MAX_MEMORY'/g' $configFile
    done
}

function _runRedisServer()
{
    local port=0
    local configFile=''
    local redisServerCmd=''
    local logFile=''
    local curDateTime=''
    local res=''
    for(( i=0;i<${#PORTS[@]};i++)) 
    do
        port=${PORTS[i]} 
        configFile=$CONF_DIR"redis-cluster-"$port".conf" 
        redisServerCmd=$REDIS_SRC_DIR"redis-server" 
        curDateTime=`date +%Y%m%d%H%M`
        logFile=$LOG_DIR"redis-"$port"-"$curDateTime".log"
        cmd=$redisServerCmd" "$configFile 
        res=`$cmd > $logFile 2>&1 &`
    done
}

function _meet()
{

    local firstPort=${PORTS[0]}
    local port=0
    local res=''
    local i=0
    for(( i=0;i<${#PORTS[@]};i++)) 
    do
        port=${PORTS[i]} 
        cmd=$REDIS_SRC_DIR"redis-cli -p "$firstPort" --raw cluster meet 127.0.0.1 "$port
        res=`$cmd > /dev/null 2>&1`
    done
}

function _slotadd()
{
    local count=${#PORTS[@]}
    local masterCount=`expr $count / 2`
    local pageSize=`expr $SLOT_SIZE / $masterCount`

    local index=0 
    local tmpStart=0
    local tmpEnd=0
    local slot=0
    local slotList=()
    local i=0
    local endSlot=`expr $SLOT_SIZE - 1`
    local page=0
    local key=''
    for(( i=0;i<$SLOT_SIZE;i++)) 
    do
        let index=index+1;
        if [[ $index -gt $pageSize || $i -eq endSlot ]];then
            tmpEnd=$i
            key=$tmpStart":"$tmpEnd
            slotList[$page]=$key
            let tmpStart=tmpEnd+1
            let page=page+1
            index=0
            
        fi
    done

    local slotIndex=0
    local remainder=0
    local res=''
    for(( i=0;i<${#PORTS[@]};i++)) 
    do
        remainder=$(( $i % 2 ))
        if [ $remainder -eq 1 ] ; then
            continue
        fi

        start=${slotList[$slotIndex]%:*}
        end=${slotList[$slotIndex]#*:}
        port=${PORTS[i]} 
        # redis-cli -p 6961 cluster addslots {0..5461}
        # redis-cli -p 6963 cluster addslots {5462..10923}
        # redis-cli -p 6965 cluster addslots {10924..16383}
        cmd=$REDIS_SRC_DIR"redis-cli -p "$port" cluster addslots {"$start".."$end"}" 
        python -c "import os;os.system('$cmd')" > /dev/null 
        let slotIndex=slotIndex+1
    done
}



function _slave()
{
    local cmd=$REDIS_SRC_DIR"redis-cli -p "${PORTS[0]}" cluster nodes"
    local nodesMap=`$cmd | awk '{start =index($2,":"); end = index($2,"@"); port = substr($2,start + 1,end - start -1);key = (port":"$1);print key}'`
    local portArr=()
    local nodeIdArr=()
    local index=0
    local item=''
    for item in $nodesMap
    do 
        portArr[$index]=${item%:*}
        nodeIdArr[$index]=${item#*:}
        let index=index+1;
    done;

    local nodeIdArrOrder=()
    local i=0
    local j=0
    for(( i=0;i<${#PORTS[@]};i++)) 
    do
        for(( j=0;j<${#portArr[@]};j++)) 
        do
            if [ ${PORTS[$i]} -eq ${portArr[$j]} ] ;then
                nodeIdArrOrder[$i]=${nodeIdArr[$j]}
            fi
        done
    done

    local k=0
    local res=''
    for(( k=0;k<${#PORTS[@]};k++)) 
    do
        remainder=$(( $k % 2 ))
        if [ $remainder -eq 0 ] ; then
            continue
        fi
        tmpPort=${PORTS[$k]}
        nodeIdIndex=`expr $k - 1`
        tmpMasterNodeId=${nodeIdArrOrder[$nodeIdIndex]}
        cmd=$REDIS_SRC_DIR"redis-cli -p "$tmpPort" cluster replicate "$tmpMasterNodeId

        res=`$cmd`
        # try agin
        if [[ $res != "OK" ]];then
            sleep 2
            res=`$cmd`
        fi
    done
}

function start() 
{
    _createConfigFile
    _runRedisServer
    _meet
    _slotadd
    sleep 1
    _slave
}

function stop()
{
    local k=0
    local res=''
    for(( k=0;k<${#PORTS[@]};k++)) 
    do
        cmd=$REDIS_SRC_DIR"redis-cli -p "${PORTS[$k]}" shutdown nosave"
        res=`$cmd`
    done

}

function flushAll()
{
    local k=0
    local res=''
    for(( k=0;k<${#PORTS[@]};k++)) 
    do
        remainder=$(( $k % 2 ))
        if [ $remainder -eq 1 ] ; then
            continue
        fi
        cmd=$REDIS_SRC_DIR"redis-cli -p "${PORTS[$k]}" --raw flushall"
        res=`$cmd`
    done
 
}

function run()
{
    if [ "redis" != $(whoami) ];then
        echo "ERROR,you must use redis user to run this script" 
       return
    fi

    local action=$1;
    case $action in
         start)
            start
            echo "OK"
             ;;
         stop)
            stop
            echo "OK"
             ;;
         restart)
            stop
            start
            echo "OK"
             ;;
         flushAll)
            flushAll
            echo "OK"
             ;;
          *)
             echo "ERROR cmd invalid";
             echo "useage:"          
             echo "sh redisCluster.sh start|stop|restart|flushAll"
             ;;
    esac
    
}
run $1

你可能感兴趣的:(redis的cluster集群模式shell一键启动/停止/重启/清缓存脚本)