redis cluster集群配置和运维(4)
时间:2017-06-09 17:17 来源:潇湘夜雨 作者:华嵩阳 点击:次
#关闭主节点
not connected>
127.0.0.1:6379> get master #在其它节点读取主节点创建的键值
-> Redirected to slot [5565] located at 192.168.198.128:6383 #自动重定向到刚才的从节点
"2"
192.168.198.128:6383> info replication
# Replication
role:master #现在从节点已经提升为主节点了
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
[root@localhost redis]# redis-server redis4.conf #启动原来的主节点
127.0.0.1:6382> info replication
# Replication
role:slave #恢复后变成了从节点
master_host:192.168.198.128
4、手动制定从节点的主节点
127.0.0.1:5379> CLUSTER REPLICATE 976c18cecb71b74d7b9b90d3d61ef6ea9bfd4e89
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.198.128,port=6382,state=online,offset=7911,lag=1
slave1:ip=192.168.198.130,port=5379,state=online,offset=7911,lag=1 #主节点中已经添加了从节点
注意:这里是在线指定从节点的主节点,一般不能直接指定非空主节点为从节点。
部分停机能实现故障自动转移,需要时间20秒以内。(测试环境中):在3.0release中这个速度提高了一倍。
过多机器停机或者实例挂掉可能会造成集群不完整。
cluster-require-full-coverage=yes不允许集群部分失败, cluster-require-full-coverage=no允许集群部分失败。
5、 redis-cluster选举:容错
领着选举过程是集群中所有master参与,如果半数以上master节点与master节点通信超过(cluster-node-timeout),认为当前master节点挂掉.
什么时候整个集群不可用(cluster_state:fail)?
如果集群任意master挂掉,且当前master没有slave.集群进入fail状态,也可以理解成集群的slot映射[0-16383]不完成时进入fail状态. ps : redis-3.0.0.rc1加入cluster-require-full-coverage参数,默认关闭,打开集群兼容部分失败.
如果集群超过半数以上master挂掉,无论是否有slave集群进入fail状态.
ps:当集群不可用时,所有对集群的操作做都不可用,收到((error) CLUSTERDOWN The cluster is down)错误
6、集群状态中键值的存放
CLUSTER KEYSLOT <key> 计算键 key 应该被放置在哪个槽上。
192.168.198.128:6382> CLUSTER KEYSLOT m4
(integer) 2977
192.168.198.128:6382> CLUSTER NODES
976c18cecb71b74d7b9b90d3d61ef6ea9bfd4e89 192.168.198.128:6383 master,fail - 1496987811084 1496987809065 13 disconnected
14cc31f74c23fa6b237334b11d984989c6d8cc4e 192.168.198.130:5380 slave 399cbff5edff7258081fd73f98a1cb2857cb9824 0 1496990111032 15 connected
659b9e99cfe7cfe198d10231bc4526a34c316cdc 192.168.198.128:6379 master - 0 1496990114063 16 connected 5628-10922
0ab74446f7052b1c681a2cce11f46298c5fa93bb 192.168.198.128:6382 myself,master - 0 0 17 connected 0-165 5461-5627 10923-11088
e97d65ce427e00b4ff89014dcdb05cfdad9f8152 192.168.198.128:6380 master - 0 1496990113048 14 connected 166-5460 #在该节点上
f4c656b100cc40650cbcbb266cb5523020fb4b38 192.168.198.130:5379 slave 0ab74446f7052b1c681a2cce11f46298c5fa93bb 0 1496990112042 17 connected
399cbff5edff7258081fd73f98a1cb2857cb9824 192.168.198.128:6381 master - 0 1496990112042 15 connected 11089-16383
8e2ad457f09186549b714c2260742773de1db658 192.168.198.130:5381 slave 659b9e99cfe7cfe198d10231bc4526a34c316cdc 0 1496990110018 16 connected
192.168.198.128:6382> set m4 df
-> Redirected to slot [2977] located at 192.168.198.128:6380 #创建时自动跳转到应放置的节点
OK
192.168.198.128:6380> KEYS *
1) "a803"
2) "dfkdj"
3) "ui"
4) "df"
5) "a4"
6) "m4"
7、集群节点的删除
(1) 删除主节点
[root@localhost redis]# redis-trib.rb reshard 192.168.198.128:6380 #先迁移主节点的slots
>>> Performing Cluster Check (using node 192.168.198.128:6380)
M: e97d65ce427e00b4ff89014dcdb05cfdad9f8152 192.168.198.128:6380
slots:1931-5460 (3530 slots) master
0 additional replica(s)
S: f4c656b100cc40650cbcbb266cb5523020fb4b38 192.168.198.130:5379
slots: (0 slots) slave
replicates 0ab74446f7052b1c681a2cce11f46298c5fa93bb
M: 0ab74446f7052b1c681a2cce11f46298c5fa93bb 192.168.198.128:6382
slots:0-1930,5461-7392,10923-12853 (5794 slots) master
1 additional replica(s)
M: 399cbff5edff7258081fd73f98a1cb2857cb9824 192.168.198.128:6381
slots:12854-16383 (3530 slots) master
1 additional replica(s)
M: 659b9e99cfe7cfe198d10231bc4526a34c316cdc 192.168.198.128:6379
slots:7393-10922 (3530 slots) master
1 additional replica(s)
S: 14cc31f74c23fa6b237334b11d984989c6d8cc4e 192.168.198.130:5380
slots: (0 slots) slave
replicates 399cbff5edff7258081fd73f98a1cb2857cb9824
S: 8e2ad457f09186549b714c2260742773de1db658 192.168.198.130:5381
slots: (0 slots) slave
replicates 659b9e99cfe7cfe198d10231bc4526a34c316cdc
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 3530 #主节点的slots总数
What is the receiving node ID? 399cbff5edff7258081fd73f98a1cb2857cb9824 #迁移的目标节点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:e97d65ce427e00b4ff89014dcdb05cfdad9f8152 #要删除的节点id
Source node #2:done
Do you want to proceed with the proposed reshard plan (yes/no)? yes
......开始移动slots
Moving slot 5458 from 192.168.198.128:6380 to 192.168.198.128:6381:
Moving slot 5459 from 192.168.198.128:6380 to 192.168.198.128:6381:
Moving slot 5460 from 192.168.198.128:6380 to 192.168.198.128:6381:
[root@localhost redis]# redis-trib.rb check 192.168.198.128:6380 #再次检查集群状态
>>> Performing Cluster Check (using node 192.168.198.128:6380)
M: e97d65ce427e00b4ff89014dcdb05cfdad9f8152 192.168.198.128:6380
slots: (0 slots) master #已经没有slots
0 additional replica(s)
S: f4c656b100cc40650cbcbb266cb5523020fb4b38 192.168.198.130:5379
slots: (0 slots) slave
replicates 0ab74446f7052b1c681a2cce11f46298c5fa93bb
M: 0ab74446f7052b1c681a2cce11f46298c5fa93bb 192.168.198.128:6382
slots:0-1930,5461-7392,10923-12853 (5794 slots) master
1 additional replica(s)
M: 399cbff5edff7258081fd73f98a1cb2857cb9824 192.168.198.128:6381
slots:1931-5460,12854-16383 (7060 slots) master #新增加的slots
192.168.198.128:6381> KEYS * #迁移slots时也会将数据迁移过去
1) "dfkdj"
2) "adk"
3) "king"
4) "a3"
5) "a4"
6) "slave2"
7) "df"
8) "new"
9) "new2"
10) "ui"
11) "ng"
12) "y7"
13) "m4"
#现在才可以正常删除主节点
[root@localhost redis]# redis-trib.rb del-node 192.168.198.128:6380 e97d65ce427e00b4ff89014dcdb05cfdad9f8152 (责任编辑:liangzh) |