(1)集群中所有master参与投票,如果半数以上master节点与其中一个master节点通信超过(cluster-node-timeout),认为该master节点挂掉.
(2):什么时候整个集群不可用(cluster_state:fail)?
Ø 如果集群任意master挂掉,且当前master没有slave,则集群进入fail状态。也可以理解成集群的[0-16383]slot映射不完全时进入fail状态。
Ø 如果集群超过半数以上master挂掉,无论是否有slave,集群进入fail状态。
redis集群管理工具redis-trib.rb依赖ruby环境,首先需要安装ruby环境。
Ø 安装ruby
[root@redis01 bin]# yum install ruby [root@redis01 bin]# yum install rubygems |
Ø 使用sftp工具上传redis-3.0.0.gem至/usr/local下
sftp> put -r "E:\03-teach\03-讲课\0707\04-redis\res\ruby和redis接口\redis-3.0.0.gem" |
Ø 安装ruby和redis的接口程序
[root@linux02 local]# gem install /usr/local/redis-3.0.0.gem |
Ø 将Redis集群搭建脚本文件复制到/usr/local/redis0707目录下
[root@redis01 /]# cd /root/redis-3.0.0/src/ [root@redis01 src]# ll *.rb -rwxrwxr-x. 1 root root 48141 4月 1 2015 redis-trib.rb [root@redis01 src]# cp redis-trib.rb /usr/local/redis0707/ -r |
搭建集群最少也得需要3台主机,如果每台主机再配置一台从机的话,则最少需要6台机器。
第一步:创建6个redis实例,需要端口号7001~7006
第四步:创建集群
[root@localhost-0723 redis]# ./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 >>> Creating cluster Connecting to node 127.0.0.1:7001: OK Connecting to node 127.0.0.1:7002: OK Connecting to node 127.0.0.1:7003: OK Connecting to node 127.0.0.1:7004: OK Connecting to node 127.0.0.1:7005: OK Connecting to node 127.0.0.1:7006: OK >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 Adding replica 127.0.0.1:7004 to 127.0.0.1:7001 Adding replica 127.0.0.1:7005 to 127.0.0.1:7002 Adding replica 127.0.0.1:7006 to 127.0.0.1:7003 M: d8f6a0e3192c905f0aad411946f3ef9305350420 127.0.0.1:7001 slots:0-5460 (5461 slots) master M: 7a12bc730ddc939c84a156f276c446c28acf798c 127.0.0.1:7002 slots:5461-10922 (5462 slots) master M: 93f73d2424a796657948c660928b71edd3db881f 127.0.0.1:7003 slots:10923-16383 (5461 slots) master S: f79802d3da6b58ef6f9f30c903db7b2f79664e61 127.0.0.1:7004 replicates d8f6a0e3192c905f0aad411946f3ef9305350420 S: 0bc78702413eb88eb6d7982833a6e040c6af05be 127.0.0.1:7005 replicates 7a12bc730ddc939c84a156f276c446c28acf798c S: 4170a68ba6b7757e914056e2857bb84c5e10950e 127.0.0.1:7006 replicates 93f73d2424a796657948c660928b71edd3db881f Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join.... >>> Performing Cluster Check (using node 127.0.0.1:7001) M: d8f6a0e3192c905f0aad411946f3ef9305350420 127.0.0.1:7001 slots:0-5460 (5461 slots) master M: 7a12bc730ddc939c84a156f276c446c28acf798c 127.0.0.1:7002 slots:5461-10922 (5462 slots) master M: 93f73d2424a796657948c660928b71edd3db881f 127.0.0.1:7003 slots:10923-16383 (5461 slots) master M: f79802d3da6b58ef6f9f30c903db7b2f79664e61 127.0.0.1:7004 slots: (0 slots) master replicates d8f6a0e3192c905f0aad411946f3ef9305350420 M: 0bc78702413eb88eb6d7982833a6e040c6af05be 127.0.0.1:7005 slots: (0 slots) master replicates 7a12bc730ddc939c84a156f276c446c28acf798c M: 4170a68ba6b7757e914056e2857bb84c5e10950e 127.0.0.1:7006 slots: (0 slots) master replicates 93f73d2424a796657948c660928b71edd3db881f [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. [root@localhost-0723 redis]# |
命令:./redis-cli –h 127.0.0.1–p 7001 -c
-c:指定是集群连接
[root@localhost-0723 redis]# ./redis-cli -p 7006 -c 127.0.0.1:7006> set key1 123 -> Redirected to slot [9189] located at 127.0.0.1:7002 OK 127.0.0.1:7002> |
Ø 查看集群状态
127.0.0.1:7003> cluster info cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:3 cluster_stats_messages_sent:926 cluster_stats_messages_received:926 |
Ø 查看集群中的节点:
127.0.0.1:7003> cluster nodes 7a12bc730ddc939c84a156f276c446c28acf798c 127.0.0.1:7002 master - 0 1443601739754 2 connected 5461-10922 93f73d2424a796657948c660928b71edd3db881f 127.0.0.1:7003 myself,master - 0 0 3 connected 10923-16383 d8f6a0e3192c905f0aad411946f3ef9305350420 127.0.0.1:7001 master - 0 1443601741267 1 connected 0-5460 4170a68ba6b7757e914056e2857bb84c5e10950e 127.0.0.1:7006 slave 93f73d2424a796657948c660928b71edd3db881f 0 1443601739250 6 connected f79802d3da6b58ef6f9f30c903db7b2f79664e61 127.0.0.1:7004 slave d8f6a0e3192c905f0aad411946f3ef9305350420 0 1443601742277 4 connected 0bc78702413eb88eb6d7982833a6e040c6af05be 127.0.0.1:7005 slave 7a12bc730ddc939c84a156f276c446c28acf798c 0 1443601740259 5 connected 127.0.0.1:7003> |
集群创建成功后可以向集群中添加节点,下面是添加一个master主节点
Ø 添加7007结点作为新节点
执行命令:./redis-trib.rb add-node127.0.0.1:7007 127.0.0.1:7001
Ø 查看集群结点发现7007已添加到集群中
添加完主节点需要对主节点进行hash槽分配,这样该主节才可以存储数据。
Ø 查看集群中槽占用情况
redis集群有16384个槽,集群中的每个结点分配自已槽,通过查看集群结点可以看到槽占用情况。
Ø 给刚添加的7007结点分配槽
第一步:连接上集群(连接集群中任意一个可用结点都行)
[root@redis01 redis0707]# ./redis-trib.rb reshard 192.168.101.3:7001 |
第二步:输入要分配的槽数量
输入:500,表示要分配500个槽
第三步:输入接收槽的结点id
输入:15b809eadae88955e36bcdbb8144f61bbbaf38fb
PS:这里准备给7007分配槽,通过cluster nodes查看7007结点id为:
15b809eadae88955e36bcdbb8144f61bbbaf38fb
第四步:输入源结点id
输入:all
第五步:输入yes开始移动槽到目标结点id
输入:yes
集群创建成功后可以向集群中添加节点,下面是添加一个slave从节点。
Ø 添加7008从结点,将7008作为7007的从结点
命令:./redis-trib.rb add-node --slave --master-id 主节点id 新节点的ip和端口 旧节点ip和端口
执行如下命令:
./redis-trib.rb add-node --slave --master-id cad9f7413ec6842c971dbcc2c48b4ca959eb5db4 192.168.101.3:7008 192.168.101.3:7001 |
cad9f7413ec6842c971dbcc2c48b4ca959eb5db4 是7007结点的id,可通过cluster nodes查看。
注意:如果原来该结点在集群中的配置信息已经生成到cluster-config-file指定的配置文件中(如果cluster-config-file没有指定则默认为nodes.conf),这时可能会报错:
[ERR] Node XXXXXX is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0 |
解决方法是删除生成的配置文件nodes.conf,删除后再执行./redis-trib.rbadd-node指令
Ø 查看集群中的结点,刚添加的7008为7007的从节点:
命令:./redis-trib.rbdel-node 127.0.0.1:7005 4b45eb75c8b428fbd77ab979b85080146a9bc017
删除已经占有hash槽的结点会失败,报错如下:
[ERR] Node 127.0.0.1:7005 is not empty!Reshard data away and try again.
需要将该结点占用的hash槽分配出去(参考hash槽重新分配章节)。