一、集群搭建
docker run -d --name redis-node-1 --net host -v /data/redis/share/redis-node-1:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6381
docker run -d --name redis-node-2 --net host -v /data/redis/share/redis-node-2:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6382
docker run -d --name redis-node-3 --net host -v /data/redis/share/redis-node-3:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6383
docker run -d --name redis-node-4 --net host -v /data/redis/share/redis-node-4:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6384
docker run -d --name redis-node-5 --net host -v /data/redis/share/redis-node-5:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6385
docker run -d --name redis-node-6 --net host -v /data/redis/share/redis-node-6:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6386
docker run -d --name redis-node-7 --net host -v /data/redis/share/redis-node-7:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6387
docker run -d --name redis-node-8 --net host -v /data/redis/share/redis-node-8:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6388
进入容器
docker exec -it redis-node-1 /bin/bash
redis-cli --cluster create 192.168.3.66:6381 192.168.3.66:6382 192.168.3.66:6383 192.168.3.66:6384 192.168.3.66:6385 192.168.3.66:6386 192.168.3.66:6387 192.168.3.66:6388 --cluster-replicas 1
redis-cli --cluster create 192.168.31.221:6381 192.168.31.221:6382 192.168.31.221:6383 192.168.31.221:6384 192.168.31.221:6385 192.168.31.221:6386 --cluster-replicas 1
redis-cli -p 6381
查看集群信息
cluster info
cluster nodes
二、集群扩容
docker run -d --name redis-node-7 --net host -v /data/redis/share/redis-node-7:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6387
docker run -d --name redis-node-8 --net host -v /data/redis/share/redis-node-8:/data redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6388
进入容器
docker exec -it redis-node-7 /bin/bash
进入6387节点
redis-cli --cluster add-node 192.168.3.66:6387 192.168.3.66:6381
检查集群状态
redis-cli --cluster check 192.168.3.66:6381
分配hash槽位,不是从0开始重新分配是每家均分
redis-cli --cluster reshard 192.168.3.66:6381
选择每个节点槽位数量16384/node
How many slots do you want to move (from 1 to 16384)?
分配给谁
What is the receiving node 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.
all
redis-cli --cluster check 192.168.3.66:6381
扩容机器槽位
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
为主节点6387分配从节点6388
redis-cli --cluster add-node 从节点 主节点 --cluster-slave --cluster-master-id 主节点id
redis-cli --cluster add-node 192.168.3.66:6388 192.168.3.66:6387 --cluster-slave --cluster-master-id d1170103babca0f701a7f11309d47b3851843c5a
三、集群缩容
删除从节点
redis-cli --cluster del-node 从节点ip:port 从节点nodeId
redis-cli --cluster del-node 192.168.3.66:6388 ad706504eba03d26536fac1c8c28da4568a61302
hash槽位重新分配
redis-cli --cluster reshard 192.168.3.66:6381
How many slots do you want to move (from 1 to 16384)? 4096
谁来接手槽位
What is the receiving node 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:
Source node #1: d1170103babca0f701a7f11309d47b3851843c5a
Source node #2: done
192.168.3.66:6381 (162fc2a5...) -> 0 keys | 8192 slots | 1 slaves.
删除6387
redis-cli --cluster del-node 192.168.3.66:6387 ad706504eba03d26536fac1c8c28da4568a61302