潇湘夜雨移动版

主页 > 数据库 >

redis cluster集群配置和运维(5)

>>> Removing node e97d65ce427e00b4ff89014dcdb05cfdad9f8152 from cluster 192.168.198.128:6380
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
 
集群中已经没有删除的节点 
192.168.198.128:6382> CLUSTER nodes
976c18cecb71b74d7b9b90d3d61ef6ea9bfd4e89 192.168.198.128:6383 slave 0ab74446f7052b1c681a2cce11f46298c5fa93bb 0 1496993803988 17 connected
14cc31f74c23fa6b237334b11d984989c6d8cc4e 192.168.198.130:5380 slave 399cbff5edff7258081fd73f98a1cb2857cb9824 0 1496993808031 18 connected
659b9e99cfe7cfe198d10231bc4526a34c316cdc 192.168.198.128:6379 master - 0 1496993807022 16 connected 7393-10922
0ab74446f7052b1c681a2cce11f46298c5fa93bb 192.168.198.128:6382 myself,master - 0 0 17 connected 0-1930 5461-7392 10923-12853
f4c656b100cc40650cbcbb266cb5523020fb4b38 192.168.198.130:5379 slave 0ab74446f7052b1c681a2cce11f46298c5fa93bb 0 1496993806522 17 connected
399cbff5edff7258081fd73f98a1cb2857cb9824 192.168.198.128:6381 master - 0 1496993806014 18 connected 1931-5460 12854-16383
8e2ad457f09186549b714c2260742773de1db658 192.168.198.130:5381 slave 659b9e99cfe7cfe198d10231bc4526a34c316cdc 0 1496993809046 16 connected
 
(2) 删除节点重新加入集群
[root@localhost redis]# redis-trib.rb add-node 192.168.198.128:6380 192.168.198.128:6379
>>> Adding node 192.168.198.128:6380 to cluster 192.168.198.128:6379
>>> Performing Cluster Check (using node 192.168.198.128:6379)
M: 659b9e99cfe7cfe198d10231bc4526a34c316cdc 192.168.198.128:6379
  slots:7393-10922 (3530 slots) master
  1 additional replica(s)
S: 8e2ad457f09186549b714c2260742773de1db658 192.168.198.130:5381
  slots: (0 slots) slave
  replicates 659b9e99cfe7cfe198d10231bc4526a34c316cdc
S: f4c656b100cc40650cbcbb266cb5523020fb4b38 192.168.198.130:5379
  slots: (0 slots) slave
  replicates 0ab74446f7052b1c681a2cce11f46298c5fa93bb
S: 14cc31f74c23fa6b237334b11d984989c6d8cc4e 192.168.198.130:5380
  slots: (0 slots) slave
  replicates 399cbff5edff7258081fd73f98a1cb2857cb9824
S: 976c18cecb71b74d7b9b90d3d61ef6ea9bfd4e89 192.168.198.128:6383
  slots: (0 slots) slave
  replicates 0ab74446f7052b1c681a2cce11f46298c5fa93bb
M: 0ab74446f7052b1c681a2cce11f46298c5fa93bb 192.168.198.128:6382
  slots:0-1930,5461-7392,10923-12853 (5794 slots) master
  2 additional replica(s)
M: 399cbff5edff7258081fd73f98a1cb2857cb9824 192.168.198.128:6381
  slots:1931-5460,12854-16383 (7060 slots) master
  1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.198.128:6380 to make it join the cluster.
[OK] New node added correctly.
注意:添加新节点需要清空keys和nodes信息,否则会报错
 
127.0.0.1:6380> cluster replicate 0ab74446f7052b1c681a2cce11f46298c5fa93bb #指定新节点的主节点
OK
127.0.0.1:6380> CLUSTER NODES
f4c656b100cc40650cbcbb266cb5523020fb4b38 192.168.198.130:5379 slave 0ab74446f7052b1c681a2cce11f46298c5fa93bb 0 1496994951449 17 connected
0ab74446f7052b1c681a2cce11f46298c5fa93bb 192.168.198.128:6382 master - 0 1496994947925 17 connected 0-1930 5461-7392 10923-12853
976c18cecb71b74d7b9b90d3d61ef6ea9bfd4e89 192.168.198.128:6383 slave 0ab74446f7052b1c681a2cce11f46298c5fa93bb 0 1496994945808 17 connected
399cbff5edff7258081fd73f98a1cb2857cb9824 192.168.198.128:6381 master - 0 1496994950442 18 connected 1931-5460 12854-16383
659b9e99cfe7cfe198d10231bc4526a34c316cdc 192.168.198.128:6379 master - 0 1496994947417 16 connected 7393-10922
1c0989f1ec5e3fcc1e52e4b29a100b528d0b040b 192.168.198.128:6380 myself,slave 0ab74446f7052b1c681a2cce11f46298c5fa93bb 0 0 0 connected
8e2ad457f09186549b714c2260742773de1db658 192.168.198.130:5381 slave 659b9e99cfe7cfe198d10231bc4526a34c316cdc 0 1496994949437 16 connected
14cc31f74c23fa6b237334b11d984989c6d8cc4e 192.168.198.130:5380 slave 399cbff5edff7258081fd73f98a1cb2857cb9824 0 1496994948429 18 connected
127.0.0.1:6380> info replication
# Replication
role:slave
master_host:192.168.198.128
master_port:6382
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
 
 
8、手动切换主节点
(2)查看主节点的从节点
192.168.198.128:6382> info replication
# Replication
role:master
connected_slaves:3
slave0:ip=192.168.198.130,port=5379,state=online,offset=10949,lag=0
slave1:ip=192.168.198.128,port=6383,state=online,offset=10949,lag=0
slave2:ip=192.168.198.128,port=6380,state=online,offset=10949,lag=1
master_repl_offset:10949
 
(2) 在任意从节点执行切换命令
127.0.0.1:6380> CLUSTER FAILOVER
OK
127.0.0.1:6380> info replication
# Replication
role:master #此时从节点已经接管为主节点
connected_slaves:3
slave0:ip=192.168.198.128,port=6382,state=online,offset=11006,lag=0
slave1:ip=192.168.198.128,port=6383,state=online,offset=11006,lag=0
slave2:ip=192.168.198.130,port=5379,state=online,offset=11006,lag=0
 
(3) 原来的主节点成为从节点
S: 0ab74446f7052b1c681a2cce11f46298c5fa93bb 192.168.198.128:6382
  slots: (0 slots) slave
  replicates 1c0989f1ec5e3fcc1e52e4b29a100b528d0b040b
 
 
9、客户端cluster命令详解: 
CLUSTER INFO 打印集群的信息  
CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。  
CLUSTER RESET  reset
CLUSTER SAVECONFIG  强制节点保存集群当前状态到磁盘上。
CLUSTER SLOTS 获得slot在节点上的映射关系
  
CLUSTER MEET <ip> <port> 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。  
CLUSTER FORGET <node_id> 从集群中移除 node_id 指定的节点。  
CLUSTER REPLICATE <node_id> 将当前节点设置为 node_id 指定的节点的从节点。    
CLUSTER SLAVES <node_id>  列出该slave节点的master节点
 
CLUSTER ADDSLOTS <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。  
CLUSTER DELSLOTS <slot> [slot ...] 移除一个或多个槽对当前节点的指派。  
CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。  
CLUSTER SETSLOT <slot> NODE <node_id> 将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。  
CLUSTER SETSLOT <slot> MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中。  
CLUSTER SETSLOT <slot> IMPORTING <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点。  
CLUSTER SETSLOT <slot> STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。   
 
CLUSTER KEYSLOT <key> 计算键 key 应该被放置在哪个槽上。  
CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的键值对数量。  
CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 个 slot 槽中的键
 
READONLY  在集群中的salve节点开启只读模式
READWRITE  禁止读取请求跳转到集群中的salve节点上
 
 
192.168.198.128:6379> CLUSTER nodes
e97d65ce427e00b4ff89014dcdb05cfdad9f8152 :0 master,noaddr - 1496994271962 1496994268332 14 disconnected
 
192.168.198.128:6379> CLUSTER FORGET e97d65ce427e00b4ff89014dcdb05cfdad9f8152 #删除无效的节点
 
127.0.0.1:5381> CLUSTER SAVECONFIG #有时删除无效节点后,重启仍然显示无效节点,可以强制保存集群信息到配置文件
OK
 
默认情况下不能从slaves读取数据,但建立连接后,执行一次命令READONLY,该slaves即可读取数据。
127.0.0.1:6382> READONLY
OK
127.0.0.1:6382> get df
"kdf" (责任编辑:liangzh)