redis cluster 集群拓展
准备工作
举例:添加2个节点(1主7006节点,1从7007节点)
在/home/redis-cluster下生成conf和data目标,并生成配置信息
for port in `seq 7006 7007`; do \ mkdir -p ./${port}/conf \ && PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \ && mkdir -p ./${port}/data; \ done
共生成2个文件夹,从7006到7007,每个文件夹下包含data和conf文件夹,同时conf里面有redis.conf配置文件
创建redis容器
for port in `seq 7006 7007`; do \ docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} \ -v /home/redis-cluster/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \ -v /home/redis-cluster/${port}/data:/data \ --restart always --name redis-${port} --net redis-net \ --sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf; \ done
新生成2个redis容器
添加节点
启动ruby容器
docker run -tdi --rm --net redis-net ruby
进入容器
docker exec -it a3e0f3502f78(容器ID) /bin/bash
执行命令
gem install redis wget http://download.redis.io/redis-stable/src/redis-trib.rb
将节点添加到集群
添加主节点
ruby redis-trib.rb add-node 192.168.1.199:7006 192.168.1.199:7005
注释:192.168.1.199:7006 新增的节点
192.168.1.199:7005 集群任一个旧节点
添加从节点
ruby redis-trib.rb add-node --slave --master-id ad07845aed1ef6d983846909ada8b42282e3748c 192.168.1.199:7007 192.168.1.199:7005
注释:- -slave 表示添加的是从节点
- -master-id ad07845aed1ef6d983846909ada8b42282e3748c 主节点的node id 在这里是前面新添加的7006的node id
192.168.1.199:7007 新节点
192.168.1.199:7005 集群任一个旧节点
查看7006节点的id
ruby redis-trib.rb check 192.168.1.199:7005
现在添加的主节点是没有slots的,必须分配slots
分配slots
ruby redis-trib.rb reshard 192.168.1.199:7006 How many slots do you want to move (from 1 to 16384)? 2000 //设置slot数2000 What is the receiving node ID? ad07845aed1ef6d983846909ada8b42282e3748c //新节点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:all //表示全部节点重新洗牌 Do you want to proceed with the proposed reshard plan (yes/no)? yes //确认重新分
注:主节点如果没有slots的话,存取数据就都不会被选中
查看一下,集群情况
ruby redis-trib.rb check 192.168.1.199:7005
至此,新添加的两个节点可以再集群内正常使用
删除节点
(该操作是在上面启动好的ruby容器内做的)
1,删除从节点
redis-trib.rb del-node 192.168.11.199:7007 '9c240333476469e8e2c8e80b089c48f389827265'
2,删除主节点
如果主节点有从节点,将从节点转移到其他主节点
如果主节点有slot,去掉分配的slot,然后在删除主节点
ruby redis-trib.rb reshard 192.168.11.199:7006 //取消分配的slot,下面是主要过程 How many slots do you want to move (from 1 to 16384)? 2000 //被删除master的所有slot数量 What is the receiving node ID? 5d8ef5a7fbd72ac586bef04fa6de8a88c0671052 //接收7006节点slot的master 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:ad07845aed1ef6d983846909ada8b42282e3748c //被删除master的node-id Source node #2:done Do you want to proceed with the proposed reshard plan (yes/no)? yes //取消slot后,reshard
新增master节点后,也进行了这一步操作,当时是分配,现在去掉。反着的。
redis-trib.rb del-node 192.168.1.199:6378 'ad07845aed1ef6d983846909ada8b42282e3748c'
新的master节点被删除了