CentOS下手工创建Redis集群(3.0.4) 2016-04-20 22:30

说明

Redis3.0自带了一个Ruby的工具,用于创建Redis Cluster。除了这个工具,还有一种选择,是直接手工通过相关Redis命令直接创建Redis Cluster。本文介绍手工创建的方法。

安装

基于redis-6380.conf批量创建配置文件(在data01,data02,data03三个节点分别进行如下操作),各个实例的监听端口为22400~22409:

cd /opt/redis/conf/
for i in 224{00..09}; do sed "s/6380/$i/g" redis-6380.conf > redis-$i.conf; done

在data01,data02,data03三个节点分别启动10个redis实例:

for i in 224{00..09}; do /opt/redis/bin/redis-server /opt/redis/conf/redis-$i.conf; done

将Redis实例加入到集群中(data01节点执行即可):

for i in 224{01..09}; do /opt/redis/bin/redis-cli -h data01 -p 22400 CLUSTER MEET 192.168.1.11 $i; done
for i in 224{00..09}; do /opt/redis/bin/redis-cli -h data01 -p 22400 CLUSTER MEET 192.168.1.12 $i; done
for i in 224{00..09}; do /opt/redis/bin/redis-cli -h data01 -p 22400 CLUSTER MEET 192.168.1.13 $i; done

分配slot:

for i in {0..1091}; do /opt/redis/bin/redis-cli -h data01 -p 22400 CLUSTER ADDSLOTS $i; done
for i in {1092..2183}; do /opt/redis/bin/redis-cli -h data01 -p 22401 CLUSTER ADDSLOTS $i; done
for i in {2184..3275}; do /opt/redis/bin/redis-cli -h data01 -p 22402 CLUSTER ADDSLOTS $i; done
for i in {3276..4367}; do /opt/redis/bin/redis-cli -h data01 -p 22403 CLUSTER ADDSLOTS $i; done
for i in {4368..5459}; do /opt/redis/bin/redis-cli -h data01 -p 22404 CLUSTER ADDSLOTS $i; done
for i in {5460..6551}; do /opt/redis/bin/redis-cli -h data02 -p 22400 CLUSTER ADDSLOTS $i; done
for i in {6552..7643}; do /opt/redis/bin/redis-cli -h data02 -p 22401 CLUSTER ADDSLOTS $i; done
for i in {7644..8735}; do /opt/redis/bin/redis-cli -h data02 -p 22402 CLUSTER ADDSLOTS $i; done
for i in {8736..9827}; do /opt/redis/bin/redis-cli -h data02 -p 22403 CLUSTER ADDSLOTS $i; done
for i in {9828..10919}; do /opt/redis/bin/redis-cli -h data02 -p 22404 CLUSTER ADDSLOTS $i; done
for i in {10920..12011}; do /opt/redis/bin/redis-cli -h data03 -p 22400 CLUSTER ADDSLOTS $i; done
for i in {12012..13103}; do /opt/redis/bin/redis-cli -h data03 -p 22401 CLUSTER ADDSLOTS $i; done
for i in {13104..14195}; do /opt/redis/bin/redis-cli -h data03 -p 22402 CLUSTER ADDSLOTS $i; done
for i in {14196..15287}; do /opt/redis/bin/redis-cli -h data03 -p 22403 CLUSTER ADDSLOTS $i; done
for i in {15288..16383}; do /opt/redis/bin/redis-cli -h data03 -p 22404 CLUSTER ADDSLOTS $i; done
  • 配置备节点

登录备节点,使用CLUSTER RELICATE <主节点ID>进行配置。

比如15主,15备。则分别登录15个备节点,执行CLUSTER REPLICATE <主节点>进行配置。

常用维护命令

CLUSTER NODES
CLUSTER INFO
CLUSTER ADDSLOTS
CLUSTER DELSLOTS
Tags: #Redis    Post on Linux