转载请注明出处哈:http://carlosfu.iteye.com/blog/2240426
一、目的
Redis-Cluster是Redis的分布式解决方案,Redis Cluster提供了在线扩容(添加分片)功能,有效扩展存储能力和读写能力。
水平扩容思路如下:
1. 启动新的Redis实例。
2. 集群中的机器meet上述Redis实例,将其加入集群。
3. 由于新的实例没有指派槽,所以要将部分其他实例上面的槽迁移至新的实例。
二、具体实现:(已建立好的集群参考http://carlosfu.iteye.com/blog/2242578)
准备数据,使用hashtag生成数据到指定slot上
for num in `seq 100 1000` do redis-cli -c -p 8002 set {aa}${num} ${num} done
1. 启动新的Redis实例,端口是8006:
sed 's/8000/8006/g' redis-8000.conf > redis-8006.conf redis-server /opt/soft/redis/conf/redis-8006.conf
2. 将8006加入集群:
redis-cli -c -p 8000 cluster meet 127.0.0.1 8006
3. 迁移slot, 这个实现起来比较麻烦,具体步骤如下:将(8002上的slot-15495 slot-15496分配给8006)
对源节点发送`CLUSTER SETSLOT <slot> MIGRATING <target_node_id>`命令,表示源节点将向目标节点迁移slot;
对源节点发送`CLUSTER GETKEYSINSLOT <slot> <count>`命令,表示从slot中取出count个key/value对的key;执行如下:
对源节点发送`MIGRATE <target_ip> <target_port> <key_name> 0 <timeout>`命令,表示将key迁移到目标
重复执行步骤3和4,直到该slot中所有的key都被迁移完毕;
向集群中的迁移双方节点发送`CLUSTER SETSLOT <slot> NODE <target_node_id>`,表示告诉集群,将该slot分配给目标节点
等待集群的状态变为OK;(cluster info中的cluster_state:ok
(1). 对目标节点发送`CLUSTER SETSLOT <slot> IMPORTING <source_node_id>`命令,表示目标节点将从源节点迁移slot
sh target-import.sh 8002 8006 15495 15496
target-import.sh
source=$1 target=$2 startSlot=$3 endSlot=$4 for slot in `seq ${startSlot} ${endSlot}` do redis-cli -c -p ${target} cluster setslot ${slot} IMPORTING `redis-cli -c -p ${target} cluster nodes | grep ${source} | awk '{print $1}'` done
(2). 对源节点发送`CLUSTER SETSLOT <slot> MIGRATING <target_node_id>`命令,表示源节点将向目标节点迁移slot;
sh source-migrate.sh 8002 8006 15495 15496
source-migrate.sh
source=$1 target=$2 startSlot=$3 endSlot=$4 for slot in `seq ${startSlot} ${endSlot}` do redis-cli -c -p ${source} cluster setslot ${slot} MIGRATING `redis-cli -c -p ${source} cluster nodes | grep ${target} | awk '{print $1}'` done
(3). 对源节点发送`CLUSTER GETKEYSINSLOT <slot> <count>`命令,表示从slot中取出count个key/value对的key;执行如下:
对源节点发送`MIGRATE <target_ip> <target_port> <key_name> 0 <timeout>`命令,表示将key迁移到目标
(4). 重复执行步骤3和4,直到该slot中所有的key都被迁移完毕
(5). 向集群中的迁移双方节点发送`CLUSTER SETSLOT <slot> NODE <target_node_id>`,表示告诉集群,将该slot分配给目标节点
sh migrate-data.sh 8002 8006 15495 15496
migrate-data.sh
source=$1 target=$2 startSlot=$3 endSlot=$4 for slot in `seq ${startSlot} ${endSlot}` do while [ 1 -eq 1 ] do allkeys=`redis-cli -c -p ${source} cluster getkeysinslot ${slot} 20` if [ !-z ${allkeys} ] then redis-cli -c -p ${source} cluster setslot ${slot} NODE `redis-cli -c -p ${source} cluster nodes | grep ${target} | awk '{print $1}'` redis-cli -c -p ${target} cluster setslot ${slot} NODE `redis-cli -c -p ${source} cluster nodes | grep ${target} | awk '{print $1}'` break else for key in ${allkeys} do echo "slot ${slot} key ${key}" redis-cli -c -p ${source} MIGRATE 127.0.0.1 ${target} ${key} 0 5000 done fi done done
(6). 等待集群的状态变为OK;(cluster info中的cluster_state:ok)
127.0.0.1:8000> cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:7 cluster_size:5 cluster_current_epoch:7 cluster_my_epoch:1 cluster_stats_messages_sent:193766 cluster_stats_messages_received:193765