RedisCluster管理脚本

安装好redis后在脚本中配置好IP和redis-trib.rb的路径,然后执行./create-cluster start就可以创建6个redis实例,然后在执行./create-cluster create 就可以创建一个3主3从的redis集群。如果报错NOT Empty,则执行./create-cluster fix进行修复即可。脚本名称:create-cluster,内容如下:

#!/bin/bash

# Settings
PORT=6999
TIMEOUT=2000
NODES=6
REPLICAS=1
IP=172.16.20.17
EXEC=/usr/local/bin/redis-server
REDISCLI=/usr/local/bin/redis-cli
REDISTRIB=/opt/rediscluster/redis-3.2.6/src/redis-trib.rb
# You may want to put the above config parameters into config.sh in order to
# override the defaults without modifying this script.

if [ -a config.sh ]
then
    source "config.sh"
fi

# Computed vars
ENDPORT=$((PORT+NODES))

if [ "$1" == "start" ]
then
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        echo "Starting $PORT"
        $EXEC --bind 0.0.0.0 --port $PORT --cluster-enabled yes --cluster-config-file nodes-${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly-${PORT}.aof --dbfilename dump-${PORT}.rdb --logfile ${PORT}.log --daemonize yes
    done
    exit 0
fi

if [ "$1" == "create" ]
then
    HOSTS=""
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        HOSTS="$HOSTS $IP:$PORT"
    done
        echo $HOSTS
    echo "$REDISTRIB create --replicas $REPLICAS $HOSTS"
    $REDISTRIB create --replicas $REPLICAS $HOSTS
    exit 0
fi

if [ "$1" == "stop" ]
then
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        echo "Stopping $PORT"
        $REDISCLI -p $PORT shutdown nosave
    done
    exit 0
fi

if [ "$1" == "watch" ]
then
    PORT=$((PORT+1))
    while [ 1 ]; do
        clear
        date
        $REDISCLI -p $PORT cluster nodes | head -30
        sleep 1
    done
    exit 0
fi

if [ "$1" == "fix" ]
then
   $REDISTRIB fix $IP:$((PORT+1))
   exit 0
fi

  
if [ "$1" == "tail" ]
then
    INSTANCE=$2
    PORT=$((PORT+INSTANCE))
    tail -f ${PORT}.log
    exit 0
fi

if [ "$1" == "call" ]
then
    while [ $((PORT < ENDPORT)) != "0" ]; do
        PORT=$((PORT+1))
        $REDISCLI -p $PORT $2 $3 $4 $5 $6 $7 $8 $9
    done
    exit 0
fi

if [ "$1" == "clean" ]
then
    rm -rf *.log
    rm -rf appendonly*.aof
    rm -rf dump*.rdb
    rm -rf nodes*.conf
    exit 0
fi

echo "Usage: $0 [start|create|stop|watch|tail|clean]"
echo "start       -- Launch Redis Cluster instances."
echo "create      -- Create a cluster using redis-trib create."
echo "fix        -- Fix the cluster when it throw the exception of not empty"
echo "stop        -- Stop Redis Cluster instances."
echo "watch       -- Show CLUSTER NODES output (first 30 lines) of first node."
echo "tail <id>   -- Run tail -f of instance at base port + ID."
echo "clean       -- Remove all instances data, logs, configs."

阅读更多
文章标签: Redis
个人分类: Shell
上一篇python 装饰器
下一篇Docker1.13搭建私有仓库
想对作者说点什么? 我来说一句

RedisClusterDemo

2017年10月26日 42.12MB 下载

没有更多推荐了,返回首页

关闭
关闭