Redis Cluster集群操作

目录

一、Redis Cluster 运维操作

1、添加主节点

2、添加从节点

3、在线reshard 数据 

4、删除一个master节点

5、集群操作小结

二、RedisCluster 常用命令总结

1、集群(cluster) 

2、节点  

3、槽(slot)  

4、键(key)

5、不支持的命令:

6、redis-trib.rb 相关命令

三、PHP连接Redis cluster方法如下


一、Redis Cluster 运维操作

1、添加主节点

  1.1 新增节点配置文件

执行下面的脚本创建脚本配置文件:

# mkdir -p /data/redis_cluster/redis_7008/conf && mkdir /data/redis_cluster/redis_7008/data /data/redis_cluster/redis_7008/log /data/redis_cluster/redis_7008/var && cp /data/redis_cluster/redis_7004/conf/redis.conf /data/redis_cluster/redis_7008/conf/ && sed -i "s/7004/7008/g" /data/redis_cluster/redis_7008/conf/redis.conf

  1.2 启动新增主节点

# redis-server redis_7008/conf/redis.conf

  1.3 添加节点到集群

    现在已经添加了新增一个节点所需的配置文件,但是这个这点还没有添加到集群中,现在让它成为集群中的一个主节点(add-node  将一个节点添加到集群里面, 第一个是新节点ip:port, 第二个是任意一个已存在节点ip:port):

# redis-trib.rb add-node 10.20.5.72:7008 10.20.5.74:7001

# redis-trib.rb add-node 10.20.5.72:7008 10.20.5.74:7001

>>> Adding node 10.20.5.72:7008 to cluster 10.20.5.74:7001

>>> Performing Cluster Check (using node 10.20.5.74:7001)

M: 2dbaa0effafda357f369c5c7001569592b066147 10.20.5.74:7001

   slots:0-5460 (5461 slots) master

   1 additional replica(s)

S: 4b1ec2411229a33c08961d9f3d467f6837abd74e 10.20.5.74:7003

   slots: (0 slots) slave

   replicates aa86858a0966df7d940d4270297da53ec2523edc

M: 3f6a81ef50709eeae61f4fc366115a769f3e47d3 10.20.5.74:7002

   slots:10923-16383 (5461 slots) master

   1 additional replica(s)

S: 96aef14e8e552f97301a3e62a736f2c3741596dd 10.20.5.72:7006

   slots: (0 slots) slave

   replicates 2dbaa0effafda357f369c5c7001569592b066147

M: aa86858a0966df7d940d4270297da53ec2523edc 10.20.5.72:7004

   slots:5461-10922 (5462 slots) master

   1 additional replica(s)

S: ed6e8355f961ec417a8486caf768ce28e0c292fc 10.20.5.72:7005

   slots: (0 slots) slave

   replicates 3f6a81ef50709eeae61f4fc366115a769f3e47d3

[OK] All nodes agree about slots configuration.

>>> Check for open slots...

>>> Check slots coverage...

[OK] All 16384 slots covered.

>>> Send CLUSTER MEET to node 10.20.5.72:7008 to make it join the cluster.

[OK] New node added correctly.

    node:新节点没有包含任何数据, 因为它没有包含任何slot。新加入的加点是一个主节点, 当集群需要将某个从节点升级为新的主节点时, 这个新节点不会被选中,同时新的主节点因为没有包含任何slot,不参加选举和failover。

    再来看看集群信息:

10.20.5.74:7001> cluster info

cluster_state:ok

cluster_slots_assigned:16384

cluster_slots_ok:16384

cluster_slots_pfail:0

cluster_slots_fail:0

cluster_known_nodes:7

cluster_size:3

cluster_current_epoch:6

cluster_my_epoch:1

cluster_stats_messages_ping_sent:94586

cluster_stats_messages_pong_sent:92725

cluster_stats_messages_sent:187311

cluster_stats_messages_ping_received:92719

cluster_stats_messages_pong_received:94586

cluster_stats_messages_meet_received:6

cluster_stats_messages_received:187311

10.20.5.74:7001> cluster nodes

4b1ec2411229a33c08961d9f3d467f6837abd74e 10.20.5.74:7003@17003 slave aa86858a0966df7d940d4270297da53ec2523edc 0 1533298495983 4 connected

3f6a81ef50709eeae61f4fc366115a769f3e47d3 10.20.5.74:7002@17002 master - 0 1533298497986 2 connected 10923-16383

96aef14e8e552f97301a3e62a736f2c3741596dd 10.20.5.72:7006@17006 slave 2dbaa0effafda357f369c5c7001569592b066147 0 1533298497000 6 connected

2dbaa0effafda357f369c5c7001569592b066147 10.20.5.74:7001@17001 myself,master - 0 1533298496000 1 connected 0-5460

aa86858a0966df7d940d4270297da53ec2523edc 10.20.5.72:7004@17004 master - 0 1533298495000 4 connected 5461-10922

627bf0d585fb84c2189d9f9a5dbcbcba85bf98b8 10.20.5.72:7008@17008 master - 0 1533298495000 0 connected

ed6e8355f961ec417a8486caf768ce28e0c292fc 10.20.5.72:7005@17005 slave 3f6a81ef50709eeae61f4fc366115a769f3e47d3 0 1533298497000 5 connected

  1.4 分配槽

    从添加主节点输出信息和查看集群信息中可以看出,我们已经成功的向集群中添加了一个主节点,但是这个主节还没有成为真正的主节点,因为还没有分配槽(slot),也没有从节点,现在要给它分配槽(slot)

# ./redis-trib.rb reshard 10.20.5.72:7008

>>> Performing Cluster Check (using node 10.20.5.72:7008)

M: 1a544a9884e0b3b9a73db80633621bd90ceff64a 10.20.5.72:7008

   ......

[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)? 1024

What is the receiving node ID? 627bf0d585fb84c2189d9f9a5dbcbcba85bf98b8

    系统提示要移动多少个配槽(slot),并且配槽(slot)要移动到哪个节点,任意输入一个数,如1024,再输入新增节点的ID 627bf0d585fb84c2189d9f9a5dbcbcba85bf98b8.

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:all

    然后提示要从哪几个节点中移除1024个槽(slot),这里输入‘all’表示从所有的主节点中随机转移,凑够1024个哈希槽,然后就开始从新分配槽(slot)了。从新分配完后再次查看集群节点信息。

    1. redis-trib.rb reshard 10.20.5.72:7008
    2. #根据提示选择要迁移的slot数量(ps:这里选择1024)  
    3. How many slots do you want to move (from 1 to 16384)? 1024
    4. #选择要接受这些slot的node-id  
    5. What is the receiving node ID? 627bf0d585fb84c2189d9f9a5dbcbcba85bf98b8
    6. #选择slot来源:  
    7. #all表示从所有的master重新分配,  
    8. #或者数据要提取slot的master节点id,最后用done结束  
    9. Please enter all the source node IDs.  
    10.   Type 'all' to use all the nodes as source nodes for the hash slots.  
    11.   Type 'done' once you entered all the source nodes IDs.  
    12. Source node #1:all  
    13. #打印被移动的slot后,输入yes开始移动slot以及对应的数据.  
    14. #Do you want to proceed with the proposed reshard plan (yes/no)? yes  
    1. #结束  

    再来查看集群节点信息:

10.20.5.74:7001> cluster nodes

4b1ec2411229a33c08961d9f3d467f6837abd74e 10.20.5.74:7003@17003 slave aa86858a0966df7d940d4270297da53ec2523edc 0 1533299369000 4 connected

3f6a81ef50709eeae61f4fc366115a769f3e47d3 10.20.5.74:7002@17002 master - 0 1533299370645 2 connected 11264-16383

96aef14e8e552f97301a3e62a736f2c3741596dd 10.20.5.72:7006@17006 slave 2dbaa0effafda357f369c5c7001569592b066147 0 1533299371648 6 connected

2dbaa0effafda357f369c5c7001569592b066147 10.20.5.74:7001@17001 myself,master - 0 1533299370000 1 connected 341-5460

aa86858a0966df7d940d4270297da53ec2523edc 10.20.5.72:7004@17004 master - 0 1533299368641 4 connected 5803-10922

627bf0d585fb84c2189d9f9a5dbcbcba85bf98b8 10.20.5.72:7008@17008 master - 0 1533299370000 7 connected 0-340 5461-5802 10923-11263

ed6e8355f961ec417a8486caf768ce28e0c292fc 10.20.5.72:7005@17005 slave 3f6a81ef50709eeae61f4fc366115a769f3e47d3 0 1533299369000 5 connected

    可见,0-340 5461-5802 10923-11263的槽(slot)被分配给了新增加的节点。三个加起来刚好1024个槽(slot)。

2、添加从节点

  2.1 新增节点配置文件

执行下面的脚本创建脚本配置文件:

# mkdir -p /data/redis_cluster/redis_7007/conf && mkdir /data/redis_cluster/redis_7007/data /data/redis_cluster/redis_7007/log /data/redis_cluster/redis_7007/var && cp /data/redis_cluster/redis_7001/conf/redis.conf /data/redis_cluster/redis_7007/conf/ && sed -i "s/7001/7007/g" /data/redis_cluster/redis_7007/conf/redis.conf

  2.2 启动新增从节点

# redis-server redis_7007/conf/redis.conf

  2.3 添加节点到集群

# redis-trib.rb add-node 10.20.5.74:700710.20.5.74:7001

  2.4 redis-cli连接上新节点shell,输入命令:cluster replicate 对应master的node-id 

# redis-cli -h 10.20.5.74 -c -p 7007

10.20.5.74:7007> cluster replicate 627bf0d585fb84c2189d9f9a5dbcbcba85bf98b8

OK

10.20.5.74:7007>

    注意:在线添加slave 时,需要bgsave整个master数据,并传递到slave,再由 slave加载rdb文件到内存,rdb生成和传输的过程中消耗Master大量内存和网络IO,以此不建议单实例内存过大,线上小心操作。

   再来查看集群节点信息:

10.20.5.74:7001> cluster nodes

16754d3be7cb424b0a680f73f03c17aa91de264a 10.20.5.74:7007@17007 slave 627bf0d585fb84c2189d9f9a5dbcbcba85bf98b8 0 1533300034000 7 connected

4b1ec2411229a33c08961d9f3d467f6837abd74e 10.20.5.74:7003@17003 slave aa86858a0966df7d940d4270297da53ec2523edc 0 1533300035000 4 connected

3f6a81ef50709eeae61f4fc366115a769f3e47d3 10.20.5.74:7002@17002 master - 0 1533300037000 2 connected 11264-16383

96aef14e8e552f97301a3e62a736f2c3741596dd 10.20.5.72:7006@17006 slave 2dbaa0effafda357f369c5c7001569592b066147 0 1533300037937 6 connected

2dbaa0effafda357f369c5c7001569592b066147 10.20.5.74:7001@17001 myself,master - 0 1533300034000 1 connected 341-5460

aa86858a0966df7d940d4270297da53ec2523edc 10.20.5.72:7004@17004 master - 0 1533300036935 4 connected 5803-10922

627bf0d585fb84c2189d9f9a5dbcbcba85bf98b8 10.20.5.72:7008@17008 master - 0 1533300036000 7 connected 0-340 5461-5802 10923-11263

ed6e8355f961ec417a8486caf768ce28e0c292fc 10.20.5.72:7005@17005 slave 3f6a81ef50709eeae61f4fc366115a769f3e47d3 0 1533300035000 5 connected

3、在线reshard 数据 

    对于负载/数据不均匀的情况,可以在线reshard slot来解决,方法与添加新master的reshard一样,只是需要reshard的master节点是已存在的老节点。                                   

4、删除一个master节点

  4.1 删除master节点之前首先要使用reshard移除master的全部slot,然后再删除当前节点 

    1. #把10.20.5.72:7008节点slot和数据迁移到10.20.5.74:7001上  
    2. redis-trib.rb reshard 10.20.5.72:7008  
    3. #根据提示选择要迁移的slot数量(ps:这里选择1024)  
    4. How many slots do you want to move (from 1 to 16384)? 1024(被删除master的所有slot数量)  
    5. #选择要接受这些slot的node-id(10.20.5.74:7001)  
    6. What is the receiving node ID? 2dbaa0effafda357f369c5c7001569592b066147(ps:10.20.5.74:7001的node-id)  
    7. Please enter all the source node IDs.  
    8.   Type 'all' to use all the nodes as source nodes for the hash slots.  
    9.   Type 'done' once you entered all the source nodes IDs.  
    10. Source node #1:627bf0d585fb84c2189d9f9a5dbcbcba85bf98b8 (ps:被删除master的node-id)  
    11. Source node #2:done  
    12. #打印被移动的slot后,输入yes开始移动slot以及对应的数据.  
    13. #Do you want to proceed with the proposed reshard plan (yes/no)? yes    

 (redis-trib.rb一次只能把下线节点的slot迁移到一个节点上,如果需要均衡的迁移到其它节点需要执行多次reshard命令) 

第一步:把10.20.5.72:7008上0-340的341个槽移到10.20.5.74:7001上

    1. redis-trib.rb reshard 10.20.5.72:7008  
    2. #根据提示选择要迁移的slot数量(ps:这里选择341)  
    3. How many slots do you want to move (from 1 to 16384)? 341(被删除master的所有slot数量)  
    4. #选择要接受这些slot的node-id(10.20.5.74:7001)  
    5. What is the receiving node ID? 2dbaa0effafda357f369c5c7001569592b066147(ps:10.20.5.74:7001的node-id)  
    6. Please enter all the source node IDs.  
    7.   Type 'all' to use all the nodes as source nodes for the hash slots.  
    8.   Type 'done' once you entered all the source nodes IDs.  
    9. Source node #1:627bf0d585fb84c2189d9f9a5dbcbcba85bf98b8 (ps:被删除master的node-id)  
    10. Source node #2:done  
    11. #打印被移动的slot后,输入yes开始移动slot以及对应的数据.  
    12. #Do you want to proceed with the proposed reshard plan (yes/no)? yes 

第二步:把10.20.5.72:7008上5461-5802的341个槽移到10.20.5.72:7004上

    1. redis-trib.rb reshard 10.20.5.72:7008  
    2. #根据提示选择要迁移的slot数量(ps:这里选择341)  
    3. How many slots do you want to move (from 1 to 16384)? 341(被删除master的所有slot数量)  
    4. #选择要接受这些slot的node-id(10.20.5.72:7004)  
    5. What is the receiving node ID? aa86858a0966df7d940d4270297da53ec2523edc(ps:10.20.5.72:7004的node-id)  
    6. Please enter all the source node IDs.  
    7.   Type 'all' to use all the nodes as source nodes for the hash slots.  
    8.   Type 'done' once you entered all the source nodes IDs.  
    9. Source node #1:627bf0d585fb84c2189d9f9a5dbcbcba85bf98b8 (ps:被删除master的node-id)  
    10. Source node #2:done  
    11. #打印被移动的slot后,输入yes开始移动slot以及对应的数据.  
    12. #Do you want to proceed with the proposed reshard plan (yes/no)? yes 

第三步:把10.20.5.72:7008上10923-11263的341个槽移到10.20.5.74:7002上

    1. redis-trib.rb reshard 10.20.5.72:7008  
    2. #根据提示选择要迁移的slot数量(ps:这里选择341)  
    3. How many slots do you want to move (from 1 to 16384)? 340(被删除master的所有slot数量)  
    4. #选择要接受这些slot的node-id(10.20.5.74:7002)  
    5. What is the receiving node ID? 3f6a81ef50709eeae61f4fc366115a769f3e47d3(ps:10.20.5.74:7002的node-id)  
    6. Please enter all the source node IDs.  
    7.   Type 'all' to use all the nodes as source nodes for the hash slots.  
    8.   Type 'done' once you entered all the source nodes IDs.  
    9. Source node #1:627bf0d585fb84c2189d9f9a5dbcbcba85bf98b8 (ps:被删除master的node-id)  
    10. Source node #2:done  
    11. #打印被移动的slot后,输入yes开始移动slot以及对应的数据.  
    12. #Do you want to proceed with the proposed reshard plan (yes/no)? yes 

 4.2 删除空master节点

    如果主节点有从节点,将从节点转移到其他主节点(或者删除)。

    如果主节点有slot,去掉分配的slot,然后在删除主节点 。 

# redis-trib.rb del-node 10.20.5.72:7008 '627bf0d585fb84c2189d9f9a5dbcbcba85bf98b8' 

5、集群操作小结

    5.1 从上面过程可以看出,添加节点、分配槽、删除节点的过程,不用停止集群,不阻塞集群的其他操作。命令小结:  

向集群中添加节点,7037是新增节点,7036是集群中已有的节点

# redis-trib.rb add-node 10.20.5.72:700810.20.5.74:7001

重新分配槽

# redis-trib.rb reshard 10.20.5.72:7008

指定当前节点的主节点

# cluster replicate 627bf0d585fb84c2189d9f9a5dbcbcba85bf98b8

删除节点

# redis-trib.rb del-node 10.20.5.72:7008'627bf0d585fb84c2189d9f9a5dbcbcba85bf98b8'

  5.2 Redis cluster命令

集群(cluster) 

cluster info 打印集群的信息 

cluster nodes 列出集群当前已知的所有节点(node),以及这些节点的相关信息节点 

cluster meet 将ip和port所指定的节点添加到集群当中,让它成为集群的一份子

 

cluster forget 从集群中移除node_id指定的节点 

cluster replicate 将当前节点设置为node_id指定的节点的从节点 

cluster saveconfig 将节点的配置文件保存到硬盘里面

 

cluster slaves 列出该slave节点的master节点 

cluster set-config-epoch 强制设置configEpoch槽(slot)

 

cluster addslots [slot …] 将一个或多个槽(slot)指派(assign)给当前节点 

cluster delslots [slot …] 移除一个或多个槽对当前节点的指派 

cluster flushslots 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点

 

cluster setslot node 将槽slot指派给node_id指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽,然后再进行指派 

cluster setslot migrating 将本节点的槽slot迁移到node_id指定的节点中 

cluster setslot importing 从node_id 指定的节点中导入槽slot到本节点

 

cluster setslot stable 取消对槽slot的导入(import)或者迁移(migrate) 键 

cluster keyslot 计算键key应该被放置在哪个槽上 

cluster countkeysinslot 返回槽slot目前包含的键值对数量 

cluster getkeysinslot 返回count个slot槽中的键

 

其它 

cluster myid 返回节点的ID 

cluster slots 返回节点负责的slot 

cluster reset 重置集群,慎用

二、RedisCluster 常用命令总结

1、集群(cluster) 

    1.1、CLUSTER INFO 打印集群的信息  

    1.2、CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息。 

    1.3、CLUSTER FAILOVER 手动故障转移,需要在转移的主节点的从节点上执行 

2、节点  

    2.1、CLUSTER MEET 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。  

    2.2、CLUSTER FORGET 从集群中移除 node_id 指定的节点。  

    2.3、CLUSTER REPLICATE 将当前节点设置为 node_id 指定的节点的从节点。  

    2.4、CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面。  

3、槽(slot)  

    3.1、CLUSTER ADDSLOTS [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点。  

    3.2、CLUSTER DELSLOTS [slot ...] 移除一个或多个槽对当前节点的指派。  

    3.3、CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。  

    3.4、CLUSTER SETSLOT NODE 将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽,然后再进行指派。 

    3.1、CLUSTER SETSLOT MIGRATING 将本节点的槽 slot 迁移到 node_id 指定的节点中。 

    3.2、CLUSTER SETSLOT IMPORTING 从 node_id 指定的节点中导入槽 slot 到本节点。 

    3.3、CLUSTER SETSLOT STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)。  

4、键(key)

   4. 1、CLUSTER KEYSLOT 计算键 key 应该被放置在哪个槽上。  

   4.2、CLUSTER COUNTKEYSINSLOT 返回槽 slot 目前包含的键值对数量。  

    4.3、CLUSTER GETKEYSINSLOT 返回 count 个 slot 槽中的键。    

5、不支持的命令:

    5.1、不支持SELECT 命令,集群只使用数据库 0 

    5.2、不支持多个KEY的操作 如 MSET、SUION、SINTER等命令 (因为KEYS 无法hash到同一个slot中)

6、redis-trib.rb 相关命令

    6.1、redis-trib.rb create [--replicas N] host:ip [host:ip ...]  创建集群

    6.2、redis-trib.rb add-node host:ip host:ip  将前面的host:ip 添加到集群中

    6.3、redis-trib.rb check host:ip 检查集群的状态

    6.4、redis-trib.rb reshard host:ip OR  redis-trib.rb reshard --from host:port --to host:port --slots --yes集群重新分片

    6.5、redis-trib.rb del-node host:ip 'NODE ID' 将节点从集群中移除

三、PHP连接Redis cluster方法如下

'redis' => [
        'cluster' => false,

        'default' => [
            'host'     => env('REDIS_HOST', ''),
            'port'     => env('REDIS_PORT', ''),
            'database' => env('REDIS_DATABASE', 0),
            'password' => env('REDIS_PASSWORD', null),
        ],
        'clusters' => [
            'mycluster1' => [
                [
                    'host' => 'xx.xx.252.1',
                    'password' => '',
                    'port' => 7151,
                    'database' => 0,
                ],
           ],
        ],
],

1.PHP只需要保证至少配置一个或至少一个正常节点就可以访问整个集群;

2.这样就可以通过lvs来代理,方便后期运维;

              

你可能感兴趣的:(Redis,Redis,Cluster)