此内容在之前文章的基础之上。
前置内容:
master:192.168.126.128:6379 slave:192.168.126.131:6389
master:192.168.126.128:6380 slave:192.168.126.131:690
master:192.168.126.128:6381 slave:192.168.126.131:6391
1.cd /usr/local/redis
2.mkdir cluster cluster/conf cluster/data
3.cp redis.conf cluster/conf/redis.conf
4.vim cluster/conf/redis.conf
5.配置,以6379为例:
bind 192.168.126.128 port 6379 cluster-enabled yes cluster-node-timeout 15000 cluster-config-file /usr/local/redis/cluster/data/nodes-6379.conf
6.下载 ruby-2.3.1.tar.gz 和 redis-3.3.0.gem(见git工程)
7.tar -zxvf ruby-2.3.1.tar.gz
8. cd ruby-2.3.1
9. ./configure -prefix=/usr/local/ruby
10. make && make install //过程会有点慢,大概5-10分钟
11. 然后gem install -l redis-3.3.0.gem //若没有gem需要安装yum install gem -y
12, 依次启动6个节点:./redis-server cluster/conf/redis.conf
13.如果之前redis有数据存在,flushall清空;(坑:不需要cluster meet …)
14.执行./redis-trib.rb create –replicas
./redis-trib.rb create --replicas 1 192.168.126.128:6379 192.168.126.129:6380 192.168.126.130:6381 192.168.126.131:6389 192.168.126.132:6390 192.168.126.133:6391
表示成功。
15.登录其中一个:
./redis-cli -h 192.168.126.128 -p 6379 -c
注意:记得加-c
CLUSTER nodes
测试:
1.set,get值测试
登录6381:
./redis-cli -h 192.168.126.130 -p 6381 -c
说明:键name通过hash发现槽是在6380中,然后6379就redirect到了6380并把键保存,然后我再登录6381尝试get name发现也是重定向到6380并拿到值。
2.增加集群测试:为了方便不增加虚拟机了,直接在192.168.126.128上再起俩个redis
前置说明:master:192.168.126.128:6382 slave:192.168.126.128:6392
cd usr/local/redis/cluster/conf
cp redis.conf redis6382.conf cp redis.conf redis6392.conf
修改conf中的port和nodes-6382.conf。
启动:
./redis-server cluster/conf/redis6382.conf ./redis-server cluster/conf/redis6392.conf
./redis-trib.rb add-node 192.168.126.128:6382 192.168.126.128:6379
6382为新的主节点,6379为原来的主节点。
cat /usr/local/redis/cluster/data/nodes-6382.conf
发现主节点增加成功。
./redis-trib.rb add-node --slave --master-id b378feb2eb7e43e1b391fa08778d8ec269ea81b2 192.168.126.128:6392 192.168.126.128:6379
–slave,表示添加的是从节点
–master-id 03ccad2ba5dd1e062464bc7590400441fafb63f2,主节点的node id,在这里是前面新添加的6378的node id
192.168.126.128:6392,新从节点
192.168.126.128:6379集群原存在的旧节点
重新分配solt
redis-trib.rb reshard 192.168.126.128:6382
日志中需要自己设置的内容:
How many slots do you want to move (from 1 to 16384)? 1000 //设置slot数1000
What is the receiving node ID? 464bc7590400441fafb63f2 //新节点node id
Source node #1:all //表示全部节点重新洗牌
cat /usr/local/redis/cluster/data/nodes-6382.conf
表示成功。
设置值,redirected也可以。
3.宕机测试:
./redis-cli -h 192.168.126.128 -p 6382 shutdown
说明:6382挂掉后conf没有更新,但6392的conf可以看出成为了master,6382的槽也对应的移过来了,6382是fail状态。
再起6382:
./redis-server cluster/conf/redis6382.conf &
可以看出:6382成了slave
删除集群节点:
1.删除从节点:
./redis-trib.rb del-node 192.168.126.128:6382 b378feb2eb7e43e1b391fa08778d8ec269ea81b2
2.删除主节点:
第一步:移除持有的slots
./redis-trib.rb reshard 192.168.126.128:6392
报错:
说明:在节点:192.168.126.128:6379中slots:741和5798是open的
解决:
./redis-cli -h 192.168.126.128 -p 6379 -c
cluster setslot 741 stable cluster setslot5798 stable
./redis-trib.rb check 192.168.126.128:6379
成功解决。
./redis-trib.rb reshard 192.168.126.128:6392
How many slots do you want to move (from 1 to 16384)? 2780 //设置多少slots要移动,数量要跟节点持有slots一致,相当于要清空
What is the receiving node ID? 464bc7590400441fafb63f2 //接收slots的node id
Source node #1:8db9b485900d5a92d82e9e28da5e9b77cfb4c974 //需要移除slots的node id
查看6392的配置文件发现没有slot了。
第二步:
删除节点
./redis-trib.rb del-node 192.168.126.128:6392 8db9b485900d5a92d82e9e28da5e9b77cfb4c974
原文链接:https://blog.csdn.net/shidebin/article/details/83418336
原创文章,作者:优速盾-小U,如若转载,请注明出处:https://www.cdnb.net/bbs/archives/16231