目录
我这里用的Redis
版本:Redis X64 3.2
在Redis3.0以后,Redis集群模式是一种分布式的架构,作者在这上面用了4年的时间实现集群之间的分片管理,主从复制,故障迁移等特点,这中间相当于把replication(主从同步)和sentinal(哨兵)的功能给集成过来了。
关于Redis集群的日志说明可以查看:http://antirez.com/news/79
1.Redis集群信息描述
redis-cli.exe -c -h ip地址 -p 端口号 例如:redis-cli.exe -c -h 127.0.0.1 -p 6379
-c:启动集群模式 千万不要漏了
进入客户端查看集群信息
cluster info
cluster_state:ok
cluster_slots_assigned:16384 #已分配的槽
cluster_slots_ok:16384 #槽的状态是ok的数目
cluster_slots_pfail:0 #可能失效的槽的数目
cluster_slots_fail:0 #已经失效的槽的数目
cluster_known_nodes:6 #集群中节点个数
cluster_size:3 #集群中设置的分片个数
cluster_current_epoch:6 #集群中的currentEpoch总是一致的,currentEpoch越高, 代表节点的配置或者操作越新,集群中最大的那个node epoch
cluster_my_epoch:1 #当前节点的config epoch,每个主节点都不同,一直递增, 其表示某节点最后一次变成主节点或获取新slot所有权的逻辑时间.
cluster_stats_messages_sent:3796
cluster_stats_messages_received:3765
查看各集群节点信息
cluster nodes
2.redis-trib.rb支持的操作
# redis-trib.rb help
Usage: redis-trib <command> <options> <arguments ...>
create host1:port1 ... hostN:portN
--replicas <arg>
check host:port
info host:port
fix host:port
--timeout <arg>
reshard host:port
--from <arg>
--to <arg>
--slots <arg>
--yes
--timeout <arg>
--pipeline <arg>
rebalance host:port
--weight <arg>
--auto-weights
--use-empty-masters
--timeout <arg>
--simulate
--pipeline <arg>
--threshold <arg>
add-node new_host:new_port existing_host:existing_port
--slave
--master-id <arg>
del-node host:port node_id
set-timeout host:port milliseconds
call host:port command arg arg .. arg
import host:port
--from <arg>
--copy
--replace
help (show this help)
For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
支持的操作如下:
1. create:创建集群
2. check:检查集群
3. info:查看集群信息
4. fix:修复集群
5. reshard:在线迁移slot
6. rebalance:平衡集群节点slot数量
7. add-node:添加新节点
8. del-node:删除节点
9. set-timeout:设置节点的超时时间
10. call:在集群所有节点上执行命令
11. import:将外部redis数据导入集群
3.Redis(主/从)节点新增
add-node new_host(新节点):new_port existing_host(现有节点):existing_port
redis-trib.rb add-node 127.0.0.1:6385(新主机) 127.0.0.1:6379(这个随便搞个现有主机)
check检查一下状态:
redis-trib.rb check 127.0.0.1:6385
可以看出来新增的是个主节点,没有分配slot的主节点,但是遵循一主一/多从的原则。
现在再新增一个从节点(从节点指令):
add-node --slave --master-id 主节点ID 从节点 主节点
redis-trib.rb add-node --slave --master-id a575fbf94d24523c7f033a9ecb434092602b7309 127.0.0.1:6386 127.0.0.1:6385
check一下:
4主4从
可以看到新加入的节点是没有分配Hash槽的,给主节点6386分配hash槽
redis-trib.rb reshard 主节点
1.redis-trib.rb reshard 127.0.0.1:6385
2.How many slots do you want to move (from 1 to 16384)? 2000 //分配的数量
3.What is the receiving node ID? 2b7bb3be16460f2e0848c69cef3acc68f655a041 //新主节点id
4.Source node #1:all //all表示从所有主节点中转移2000个哈希槽
5.Do you want to proceed with the proposed reshard plan (yes/no)? yes //确认分配
成功分配
并且也进行了主从复制复制了
4.Redis宕机程度对数据存储的影响
对于数据存储有影响前提是一组主库和备用库全部宕机,例如:6379和6382是一主一从,因为6379上面分配slot,如果6379和6382都挂了,那么数据的set和get就会有问题,如果挂其中一个对于数据的存储没有影响的。
5.Redis Cluster集群分片模式
1.三个Slave和三个Master代表三个主库和三个从库(3主3从),这个主从库的定义和redis的连接先后有关,必须满足一个主库有一个或者多个对应的备用库。
2.Redis集群有16384个Hash槽,平均分成三等份分配给三个主库,哈希槽对应的存储就是根据key的hash大小来分配存储位置。
6.Redis故障迁移
我们首先把6379这个主库停掉
可以看到6379这个曾经的主库fail掉了,但是可以惊奇的发现以前的从库6382变成了现在的主库,hash槽重新分配, 这就是哨兵模式的特点。
再把6379启动,全体恢复正常,不过6379就变成了从库。
因为满足一个主库对应了一个备用库,如果主库宕机了,那么备用库升级为主库,并且全部节点等待这个宕机的重启,重启后由于备用库成了主库,那么这个曾经的主库就变成了备用库。
7.Redis主从复制
我们可以用桌面程序Redis Desktop Manager来感受一下:
下载地址和使用方法:https://www.jb51.net/softs/669908.html
这代表我们有六台Redis虚拟机
我们的三个主库的Hash槽分配:
6382:0-5460 6380:2461-10922 6381:10923-16383
先看看我要set key的slot大小:
可以看到当set a时 slot大小为15495 从6382跳到了6381 因为15495这个slot在6381范围中
set test时大小为6918 所以又从6381跳到了6380
同时可以看到我虽然set了俩个虚拟机,但是由于主从复制,我所有的Redis数据都是一致的。