redis-集群创建脚本
之前建好了redis集群, 但没有找到集群重启的机制, 停电2次, 重新创建太麻烦, 于是写了个脚本辅助启动
redis的创建过程可以看:
http://www.cnblogs.com/wenbronk/p/6864390.html http://www.cnblogs.com/wenbronk/p/6865914.html
然后, 直接放在父目录下就可以了
[wenbronk@localhost redis1]$ cat redis-cluster.sh #!/bin/bash # create by wenbronk at 2017/7/17 # redis集群管理脚本 PORTNUM=6 PASS_WD=bd86d2ab-08e5-4ae0-87ce-06cc021336d0 REDIS_HOME=/opt/wenbronk_soft/redis/redis1 HOST1=192.168.50.202 PORT1=7000 PORT2=7001 PORT3=7002 PORT4=7003 PORT5=7004 PORT6=7005 HOSTS=($HOST1 $HOST1 $HOST1 $HOST1 $HOST1 $HOST1) PORTS=($PORT1 $PORT2 $PORT3 $PORT4 $PORT5 $PORT6) BASE_DIR=(bin bin1 bin2 bin3 bin4 bin5) declare -A HOST_GROUP # clean if [ "$1" == "clean" ] then for direct in $REDIS_HOME/* do if [ -d "$direct" ] then echo $direct rm -rf $direct/dump* rm -rf $direct/nodes* rm -rf $direct/appendonly* sed -i '1{:a;N;'$n'!b a};$d;N;P;D' $direct/redis.conf fi done exit 0 fi #create if [ "$1" == "create" ] then for direct in $REDIS_HOME/* do if [ -d "$direct" ] then $direct/redis-server $direct/redis.conf PORTNUM=$((PORTNUM-1)) HOST_GROUP[$PORTNUM]=${HOSTS[$PORTNUM]}:${PORTS[$PORTNUM]} fi done $REDIS_HOME/bin/redis-trib.rb create --replicas 1 ${HOST_GROUP[*]} exit 0 fi #auth if [ "$1" == "auth" ] then for direct in $REDIS_HOME/* do if [ -d "$direct" ];then PORTNUM=$PORTNUM-1 $direct/redis-cli -c -h ${HOSTS[$PORTNUM]} -p ${PORTS[$PORTNUM]} config set masterauth $PASS_WD $direct/redis-cli -c -a $PASS_WD -h ${HOSTS[$PORTNUM]} -p ${PORTS[$PORTNUM]} config set requirepass $PASS_WD $direct/redis-cli -c -a $PASS_WD -h ${HOSTS[$PORTNUM]} -p ${PORTS[$PORTNUM]} auth $PASS_WD $direct/redis-cli -c -a $PASS_WD -h ${HOSTS[$PORTNUM]} -p ${PORTS[$PORTNUM]} config rewrite # $direct/redis-cli -c -h ${HOSTS[$PORTNUM]} -p ${PORTS[$PORTNUM]} exit fi done exit 0 fi #stop if [ "$1" == "stop" ] then pkill -9 redis fi exit 0 #help if [ "$1" == "help" ] then echo "stop|clean|create|auth" fi exit 0
由于没有找到和shell交互的方式, 创建的时候需要输入一个yes, 所以吧集群创建和密码设置分开了
执行时, stop->clean->create->auth 即可实现
然后集群的统一启动脚本
开始想用spawn, 但找了半天没找到怎么循环使用, 所以需要在每个机器侠有个start-this.sh用于本机的redis管理
#!/bin/bash # create by wenbronk at 2017/7/17 # redis 集群管理脚本 PORTNUM=6 PASS_WD=bd86d2ab-08e5-4ae0-87ce-06cc021336d0 REDIS_HOME=/opt/redis HOST1=redis-130 HOST2=redis-131 HOST3=redis-132 PORT1=20327 PORT2=20329 HOSTS=($HOST1 $HOST1 $HOST2 $HOST2 $HOST3 $HOST3) PORTS=($PORT1 $PORT2 $PORT1 $PORT2 $PORT1 $PORT2) BASEDIR=(bin-20327 bin-20329) declare -A HOSTGROUP case "$1" in #clean clean) for (( i=0; i<${#HOSTS[@]}; i=i+2)) do ssh root@${HOSTS[$i]} "sh $REDIS_HOME/start-this.sh clean" done exit 0 ;; #create create) for (( i=0; i<${#HOSTS[@]}; i=i+1)) do ssh root@${HOSTS[$i]} "sh $REDIS_HOME/start-this.sh create" HOSTGROUP[$i]=${HOSTS[$i]}:${PORTS[$i]} i=$((i+1)) HOSTGROUP[$i]=${HOSTS[$i]}:${PORTS[$i]} done $REDIS_HOME/bin/redis-trib.rb create --replicas 1 ${HOSTGROUP[*]} ;; #auth auth) for (( i=0; i<${#HOSTS[@]}; i=i+1)) do echo connect to ${HOST[$i]} at ${PORTS[$i]} and change passwd $direct/redis-cli -c -h ${HOSTS[$i]} -p ${PORTS[$i]} config set masterauth $PASS_WD $direct/redis-cli -c -a $PASS_WD -h ${HOSTS[$i]} -p ${PORTS[$i]} config set requirepass $PASS_WD $direct/redis-cli -c -a $PASS_WD -h ${HOSTS[$i]} -p ${PORTS[$i]} auth $PASS_WD $direct/redis-cli -c -a $PASS_WD -h ${HOSTS[$i]} -p ${PORTS[$i]} config rewrite done exit 0 ;; #other *) echo "unsupport params!! please input stop|clean|create|auth as params_one" exit 1 esac
start-this.sh
#!/bin/bash # create by wenbronk at 2017/7/19 # redis cluster clean & create REDIS_HOME=/opt/redis HOSTIP=redis-130 case "$1" in clean) echo "connect to $HOSTIP" for direct in $REDIS_HOME/* do if [ -d "$direct" ];then rm -rf $direct/dump* echo "rm $direct/dump*" rm -rf $direct/nodes* echo "rm $direct/nodes*" rm -rf $direct/appendonly* echo "rm $direct/appendonly*" sed -i '1{:a;N;'$n'!b a};$d;N;P;D' $direct/redis.conf echo "sed last 2 line at $direct/redis.conf" fi done echo "logout from $HOSTIP" exit 0 ;; start) echo "connect to $HOSTSIP" for direct in $REDIS_HOME/* do if [ -d "$direct" ];then echo "start redis $HOSTIP" $REDIS_HOME/redis-server.sh $REDIS_HOME/redis.conf fi done echo "logout from $HOSTSIP" exit 0 ;; esac
ps: 本脚本只用于redis集群的统一启动, 不用于搭建, 需要有预先搭建好的环境, 关于redis集群的搭建, 可以参考另一个博客:
http://www.cnblogs.com/wenbronk/p/6864390.html