redis-cluster迁移slots脚本
redis-cluster添加节点需要重新平均slots,但是平均是根据哈希算法平均分配的。需求是顺序分配slots,所以写了下面这个脚本。redis-cluster添加节点需要重新平均slots,但是平均是根据哈希算法平均分配的。需求是顺序分配slots,所以写了下面这个脚本。
开发需求是需要做通过算法将数据存入指定的某个节点slots中来缓解单台redis节点的压力
#!/bin/bash
master=`./redis-cli -p 7001 -c cluster nodes |grep master > ./node.txt`
targetnode1_id=`grep ${1} ./node.txt |awk '{print $1}'`
targetnode_prot=`echo ${1} |awk -F":" '{print $2}'`
targetnode=`echo ${1} |awk -F":" '{print $1}'`
sourcenode=`echo ${2} |awk -F":" '{print $1}'`
source_prot=`echo ${2} |awk -F":" '{print $2}'`
start_slot=$3
end_slot=$4
for i in $(seq $start_slot $end_slot)
do
#通知目标节点准备迁移插槽
./redis-cli -h ${targetnode} -p ${targetnode_prot} -c CLUSTER SETSLOT $i IMPORTING ${targetnode1_id}
#让源节点准备迁出插槽数据
./redis-cli -h $sourcenode -p $source_prot -c CLUSTER SETSLOT $i MIGRATING $targetnode1_id
while true
do
./redis-cli -h ${sourcenode} -p ${source_prot} -c CLUSTER GETKEYSINSLOT $i 1000 > ./key.txt
if [ `cat key.txt |wc -l` -eq 1 ];then
./redis-cli -h ${sourcenode} -p ${source_prot} -c cluster setslot $i node ${targetnode1_id}
sleep 1
echo "yes" |./redis-cli --cluster fix ${2}
break
else
for slot_id in `cat key.txt`
do
./redis-cli -h ${sourcenode} -p ${source_prot} -c MIGRATE ${targetnode} ${targetnode_prot} ${slot_id} 0 100
done
fi
done
sleep 3
#echo $targetnode1_id $targetnode_prot $targetnode $sourcenode $source_prot
done
运行的时候是
sh redis_slots.sh 目标节点 源数据节点 迁移从哪些slots开始 到哪些slots结尾