1、关于redis-cli --cluster help说明
2、准备8台机器
192.168.0.109 | 192.168.0.110 | 192.168.0.111 | 192.168.0.112
192.168.0.113 | 192.168.0.114 | 192.168.0.115 | 192.168.0.116
109~114做cluster,115与116做集群扩容与缩容用
3、分别在这8台机器中安装redis,并修改redis.conf
redis.conf配置文件需要修改的地方:
#bind 127.0.0.1
masterauth 123456
requirepass 123456
daemonize yes #后台启动
protected-mode no ; ## 允许外部访问
cluster-enabled yes #开启cluster,去掉注释
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000 #节点通信时间
logfile /usr/redis/logs/redis.log
4、创建cluster集群
# ./redis-cli --cluster create 192.168.0.109:6379 192.168.0.110:6379 192.168.0.111:6379 192.168.0.112:6379 192.168.0.113:6379 192.168.0.114:6379 --cluster-replicas 1 -a 123456
查看集群节点信息:
验证集群:
# ./redis-cli -c -a 123456
5、扩容节点
添加一个主节点192.168.0.115
./redis-cli --cluster add-node 192.168.0.115:6379 192.168.0.109:6379 -a 123456
查看集群信息:
可以看到新加入的115节点id: 6854ffdba361c3efb17a2243c665bade25afaaf1
6、添加从节点192.168.0.116
#./redis-cli --cluster add-node 192.168.0.116:6379 192.168.0.109:6379 --cluster-slave --cluster-master-id 6854ffdba361c3efb17a2243c665bade25afaaf1 -a 123456
查看集群信息:
7、为新节点115分配slots
# ./redis-cli --cluster reshard 192.168.0.109:6379 -a 123456
运行命令后出现的选项说明:
How many slots do you want to move (from 1 to 16384) ? 4096 -->分配多少slots给新节点
What is the receiving node ID? 6854ffdba361c3efb17a2243c665bade25afaaf1 -->给哪个节点分配slots,6854ffdba361c3efb17a2243c665bade25afaaf1为新加入主节点ID
Please enter all the source node IDs
Type ‘all’ to use all the nodes as source nodes for the hash slots.
Type ‘done’ once you entered all the source nodes IDs.
Source node #1: all
all方式为从所有主节点中分配slots给新加入的主节点
Done方式为从输入的节点中分配slots给新加入的主节点
查看集群信息,可以看到115已经分配了slots:
8、缩容节点
# ./redis-cli --cluster reshard 192.168.0.109:6379 --cluster-from 6854ffdba361c3efb17a2243c665bade25afaaf1 --cluster-to 86cb92cdceca5963e0e92bbb04dea49490d6e8ec --cluster-slots 4096 -a 123456
把所有115上的slots迁移给了109,这时候集群信息:
平衡集群中各节点slots数量:
./redis-cli -a 123456 --cluster rebalance 192.168.0.109:6379