麦克斯仇
Think different
159
文章
29503
阅读
首页
INDEX
文章
ARTICLE
关于
ABOUT
Redis集群
创建日期:
2021/05/19
修改日期:
2023/06/29
Linux
Redis
> 本文档整理自教程: 1. Redis官方文档 - 集群教程:[Redis cluster tutorial](https://redis.io/topics/cluster-tutorial) 2. Redis官方文档 - 集群规范:[Redis Cluster Specification](https://redis.io/topics/cluster-spec) 3. 尚硅谷视频:[尚硅谷_Redis6](http://www.atguigu.com/download_detail.shtml?v=323) # 介绍 > 简介 Redis集群实现了对Redis的水平扩容,即启动N个Redis节点,将整个数据库分布存储在这N个节点中,每个节点存储总数据的1/N。Redis集群通过分区(partition)来提供一定程度的可用性(availability):即使集群中有一部分节点失效或者无法进行通讯,集群也可以继续处理命令请求。 > 端口 每个Redis集群节点需要打开两个TCP连接。用于为客户端服务的正常Redis连接端口,例如6379;再加上通过在数据端口上添加10000而获得的端口,例如16379。命令端口和集群总线端口的偏移量是固定的,并且始终是10000。 第二个高端口用于集群总线,这是一个使用二进制协议的节点到节点通信通道。集群总线被节点用于故障检测、配置更新、故障转移授权等。客户端应该永远不要尝试与集群总线端口通信。请确保在防火墙打开两个端口,否则Redis集群节点将无法通信。 > 数据分片 Redis集群不使用一致的散列,而是一种不同形式的分片,每个键在概念上都是我们所说的散列槽的一部分。Redis集群中有`16384`个哈希槽,为了计算一个给定键的哈希槽,`we simply take the CRC16 of the key modulo 16384`(翻译不准确,献上原文)。 Redis集群中的每个节点负责哈希槽的一个子集,例如,可能有一个有3个节点的集群,其中: - 节点A包含从 0 到 5500 的哈希槽位。 - 节点B包含从 5501 到 11000 的哈希槽位。 - 节点C包含从 11001 到 16383 的哈希槽位。 > 主从模型 为了在主节点的一个子集出现故障或无法与大多数节点通信时保持可用性,Redis集群使用一个`主-从模型`,每个哈希槽有1个(主节点本身)到N个副本(N-1个额外的从节点)。 在节点A、B、C的示例集群中,如果节点B失败,集群将无法继续,因为不再有办法提供5501-11000范围内的哈希槽。但是,当创建集群(或以后)时,我们向每个主节点添加一个从节点,最终的集群由主节点A、B、C和从节点A1、B1、C1组成。如果节点B发生故障,节点B1复制B,集群将把节点B1提升为新的主节点,并继续运行。但是,如果节点B和B1同时故障,Redis集群将无法继续运行。 # 配置文件 Redis集群在`redis.conf`文件有如下常用配置参数。 - `cluster-enabled<yes/no>`:是否开启Redis集群的支持 - `cluster-config-file<filename>`:集群节点配置文件名称(由系统生成,不需要手动创建,且无需修改),该文件位置以`dir`配置的路径下 - `cluster-node-timeout<milliseconds>`:集群节点超时时间(单位:毫秒) # ☆ 实战 > 步骤1:准备6个节点 准备三台服务器。例如: ``` 192.168.220.101 192.168.220.102 192.168.220.103 ``` 按照教程:[CentOS7/CentOS8安装Redis6.2.3](https://maxqiu.com/article/detail/50)在每台服务器上都安装两个Redis,总共6个实例。例如: ``` 192.168.220.101:6379 192.168.220.101:6380 192.168.220.102:6379 192.168.220.102:6380 192.168.220.103:6379 192.168.220.103:6380 ``` 安装时注意如下事项: - 安装时使用的`Redis`源码包在任意一台机器上保留一份,后面会用到 - 在相同的服务器上安装第二个Redis时请注意安装路径和端口 - 若设置密码,所有库的密码应当设置相同 - 在修改`redis.conf`配置文件时,同时修改集群的配置。如下: ```ini # 主库密码(所有库都要设置,具体原因见主从复制相关教程) masterauth 123 # 启用集群模式 cluster-enabled yes # 集群配置文件名 cluster-config-file nodes.conf # 超时时间 cluster-node-timeout 15000 ``` 启动后效果如下: ![](https://cdn2.maxqiu.com/upload/d5bf28c5f1e34cb7bd5a7598ce946f73.jpg) > 步骤2:启动集群 在任意一台服务器上执行如下命令开启集群(若有密码,需要添加`-a <password>`): ```bash redis-cli -a 123 --cluster create \ 192.168.220.101:6379 192.168.220.101:6380 \ 192.168.220.102:6379 192.168.220.102:6380 \ 192.168.220.103:6379 192.168.220.103:6380 \ --cluster-replicas 1 ``` 执行后看到如下命令,提示主库是哪个,从库时哪个,如下: ``` [root@centos7 redis]# redis-cli -a 123 --cluster create \ > 192.168.220.101:6379 192.168.220.101:6380 \ > 192.168.220.102:6379 192.168.220.102:6380 \ > 192.168.220.103:6379 192.168.220.103:6380 \ > --cluster-replicas 1 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 192.168.220.102:6380 to 192.168.220.101:6379 Adding replica 192.168.220.103:6380 to 192.168.220.102:6379 Adding replica 192.168.220.101:6380 to 192.168.220.103:6379 M: de5eb63134c54777c5a00bd7c7e9395db48c8356 192.168.220.101:6379 slots:[0-5460] (5461 slots) master S: 40c8ffcfb2a06329b5467499cc14bbcaca664f69 192.168.220.101:6380 replicates 8da7efd9eb7be92c6b5a7dce52b87bd6e7a5dbbb M: 99d301c796fb50c0b6e824b0971b1affff570717 192.168.220.102:6379 slots:[5461-10922] (5462 slots) master S: afa01e70439a0281b44fa0cb9b4ae96fbe24a5ad 192.168.220.102:6380 replicates de5eb63134c54777c5a00bd7c7e9395db48c8356 M: 8da7efd9eb7be92c6b5a7dce52b87bd6e7a5dbbb 192.168.220.103:6379 slots:[10923-16383] (5461 slots) master S: 0a9cd9e6263a1ac909045d09cc9e3da4fabc4f3b 192.168.220.103:6380 replicates 99d301c796fb50c0b6e824b0971b1affff570717 Can I set the above configuration? (type 'yes' to accept): ``` 大概意思就是下图 ![](https://cdn2.maxqiu.com/upload/32ce3ae8f35e47819318a7a3092631a0.jpg) 之后输入`yes`并回车确认执行,确认后看到如下输出: ``` 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 192.168.220.101:6379) M: de5eb63134c54777c5a00bd7c7e9395db48c8356 192.168.220.101:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 0a9cd9e6263a1ac909045d09cc9e3da4fabc4f3b 192.168.220.103:6380 slots: (0 slots) slave replicates 99d301c796fb50c0b6e824b0971b1affff570717 S: 40c8ffcfb2a06329b5467499cc14bbcaca664f69 192.168.220.101:6380 slots: (0 slots) slave replicates 8da7efd9eb7be92c6b5a7dce52b87bd6e7a5dbbb M: 99d301c796fb50c0b6e824b0971b1affff570717 192.168.220.102:6379 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: afa01e70439a0281b44fa0cb9b4ae96fbe24a5ad 192.168.220.102:6380 slots: (0 slots) slave replicates de5eb63134c54777c5a00bd7c7e9395db48c8356 M: 8da7efd9eb7be92c6b5a7dce52b87bd6e7a5dbbb 192.168.220.103:6379 slots:[10923-16383] (5461 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. ``` 大概意思就是完成了 > 步骤3:登录,查看节点信息 在任意客户端使用`-c`集群模式连接任意一个节点,若有密码,需要添加`-a <password>`,命令如下: PS:一定要在`redis-cli`后面添加密码指令,不能在连接之后使用`AUTH`验证密码,否则无法自动切换集群节点 ```bash [root@centos7 redis2]# redis-cli -c -h 192.168.220.101 -p 6380 -a 123 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.220.101:6380> PING PONG ``` 使用`CLUSTER NODES`查看节点信息 ```bash 192.168.220.101:6380> CLUSTER NODES 40c8ffcfb2a06329b5467499cc14bbcaca664f69 192.168.220.101:6380@16380 myself,slave 8da7efd9eb7be92c6b5a7dce52b87bd6e7a5dbbb 0 1621430957000 5 connected de5eb63134c54777c5a00bd7c7e9395db48c8356 192.168.220.101:6379@16379 master - 0 1621430960000 1 connected 0-5460 8da7efd9eb7be92c6b5a7dce52b87bd6e7a5dbbb 192.168.220.103:6379@16379 master - 0 1621430958000 5 connected 10923-16383 99d301c796fb50c0b6e824b0971b1affff570717 192.168.220.102:6379@16379 master - 0 1621430960726 3 connected 5461-10922 afa01e70439a0281b44fa0cb9b4ae96fbe24a5ad 192.168.220.102:6380@16380 slave de5eb63134c54777c5a00bd7c7e9395db48c8356 0 1621430957000 1 connected 0a9cd9e6263a1ac909045d09cc9e3da4fabc4f3b 192.168.220.103:6380@16380 slave 99d301c796fb50c0b6e824b0971b1affff570717 0 1621430959703 3 connected ``` > 步骤4:测试存取 ```bash 192.168.220.101:6380> SET a a -> Redirected to slot [15495] located at 192.168.220.103:6379 OK 192.168.220.103:6379> GET a "a" 192.168.220.103:6379> SET b b -> Redirected to slot [3300] located at 192.168.220.101:6379 OK ``` 此时可以看到根据键的分片信息自动切换库,并且是主库 > 步骤5:测试宕机 关闭其中一个节点的主库,可以看到对应的从库自动变更为主库 ``` 192.168.220.101:6380> CLUSTER NODES 40c8ffcfb2a06329b5467499cc14bbcaca664f69 192.168.220.101:6380@16380 myself,slave 8da7efd9eb7be92c6b5a7dce52b87bd6e7a5dbbb 0 1621431112000 5 connected de5eb63134c54777c5a00bd7c7e9395db48c8356 192.168.220.101:6379@16379 master - 0 1621431113000 1 connected 0-5460 8da7efd9eb7be92c6b5a7dce52b87bd6e7a5dbbb 192.168.220.103:6379@16379 master - 1621431101797 1621431098000 5 disconnected 10923-16383 99d301c796fb50c0b6e824b0971b1affff570717 192.168.220.102:6379@16379 master - 0 1621431113098 3 connected 5461-10922 afa01e70439a0281b44fa0cb9b4ae96fbe24a5ad 192.168.220.102:6380@16380 slave de5eb63134c54777c5a00bd7c7e9395db48c8356 0 1621431114125 1 connected 0a9cd9e6263a1ac909045d09cc9e3da4fabc4f3b 192.168.220.103:6380@16380 slave 99d301c796fb50c0b6e824b0971b1affff570717 0 1621431115160 3 connected 192.168.220.101:6380> 192.168.220.101:6380> 192.168.220.101:6380> CLUSTER NODES 40c8ffcfb2a06329b5467499cc14bbcaca664f69 192.168.220.101:6380@16380 myself,master - 0 1621431121000 7 connected 10923-16383 de5eb63134c54777c5a00bd7c7e9395db48c8356 192.168.220.101:6379@16379 master - 0 1621431122000 1 connected 0-5460 8da7efd9eb7be92c6b5a7dce52b87bd6e7a5dbbb 192.168.220.103:6379@16379 master,fail - 1621431101797 1621431098000 5 disconnected 99d301c796fb50c0b6e824b0971b1affff570717 192.168.220.102:6379@16379 master - 0 1621431121338 3 connected 5461-10922 afa01e70439a0281b44fa0cb9b4ae96fbe24a5ad 192.168.220.102:6380@16380 slave de5eb63134c54777c5a00bd7c7e9395db48c8356 0 1621431123392 1 connected 0a9cd9e6263a1ac909045d09cc9e3da4fabc4f3b 192.168.220.103:6380@16380 slave 99d301c796fb50c0b6e824b0971b1affff570717 0 1621431122365 3 connected ``` 恢复节点,原先的主库变更为从库 ``` 192.168.220.101:6380> CLUSTER NODES 40c8ffcfb2a06329b5467499cc14bbcaca664f69 192.168.220.101:6380@16380 myself,master - 0 1621431216000 7 connected 10923-16383 de5eb63134c54777c5a00bd7c7e9395db48c8356 192.168.220.101:6379@16379 master - 0 1621431218000 1 connected 0-5460 8da7efd9eb7be92c6b5a7dce52b87bd6e7a5dbbb 192.168.220.103:6379@16379 slave 40c8ffcfb2a06329b5467499cc14bbcaca664f69 0 1621431217715 7 connected 99d301c796fb50c0b6e824b0971b1affff570717 192.168.220.102:6379@16379 master - 0 1621431217000 3 connected 5461-10922 afa01e70439a0281b44fa0cb9b4ae96fbe24a5ad 192.168.220.102:6380@16380 slave de5eb63134c54777c5a00bd7c7e9395db48c8356 0 1621431218000 1 connected 0a9cd9e6263a1ac909045d09cc9e3da4fabc4f3b 192.168.220.103:6380@16380 slave 99d301c796fb50c0b6e824b0971b1affff570717 0 1621431218735 3 connected ```
20
全部评论