Redis集群
Redis集群
Redis集群实现了对Redis的水平扩容,即启动N个Redis节点,将整个数据库分布存储到这N个 节点中,每个节点存储总数据量的N分之一,Redis集群通过分区来提供一定程度上的 可用性,即使集群中有一部分的节点失效或者无法进行通讯,集群仍然可以继续处理命令请求.
Redis集群搭建步骤
1、安装Ruby环境.
执行yum install ruby
执行yum install rubygems
2、准备6个Redis实例(三主三从),dump目录是为了保存所有的RDB文件,log目录是为了保存所有的日志文件.
3、redis-6379.conf配置文件(6380/6381/6389/6390/6391与6379配置文件一致,只需要修改一下相应的端口即可).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | // 引用外部的redis.conf(redis.conf配置文件是Redis原生的配置文件) include /usr/local/myredis/bin/redis.conf // 开启保护模式 protected -mode no // 指定端口 port 6379 // 允许后台启动Redis服务 daemonize yes // 指定日志文件位置 logfile "/usr/local/myredis/bin/log/redis-6379.log" // 指定RDB文件名称 dbfilename "dump-6379.rdb" // 指定RDB文件的路径 dir "/usr/local/myredis/bin/dump" // 指定pid文件 pidfile "/var/run/redis_6379.pid" // 开启集群 cluster-enabled yes // 指定节点配置文件 cluster-config-file nodes- 6379 .conf // 设置节点超时时间(ms),超过此时间节点将自动进行主从切换 cluster-node-timeout 15000 |
4、启动6个Redis实例,一定要确保启动6个实例之后生成了对应的nodexx.conf文件
5、将六个节点合成一个集群
创建redis集群命令(三主三从,一个主对应一个从,注意redis-5.0.0版本开始才支持--cluster,之前的版本会报错Unrecognized option or bad number of args for:--cluster
1 2 3 4 | ./redis-cli --cluster create 192.168 . 229.129 : 6379 192.168 . 229.129 : 6380 192.168 . 229.129 : 6381 192.168 . 229.129 : 6389 192.168 . 229.129 : 6390 192.168 . 229.129 : 6391 --cluster-replicas 1 |
如果配置项cluster-enabled的值不为yes,则执行时会报错[ERR] Node 192.168.229.128:6381 is not configured as a cluster node.这个时候需要先将cluster-enabled的值改为yes,然后重启redis-server进程,之后才可以重新执行redis-cli创建集群.
redis-cli的参数说明:
1、create
表示创建一个redis集群。
2、--cluster-replicas 1
表示为集群中的每一个主节点指定一个从节点,即一比一的复制.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | // 合并成功后的6个节点 >>> 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 . 229.129 : 6390 to 192.168 . 229.129 : 6379 Adding replica 192.168 . 229.129 : 6391 to 192.168 . 229.129 : 6380 Adding replica 192.168 . 229.129 : 6389 to 192.168 . 229.129 : 6381 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master // 6379是master,对应的ID为 c78230ccc7f24f6722a6c8bab99c9bf1c3919cbd M: c78230ccc7f24f6722a6c8bab99c9bf1c3919cbd 192.168 . 229.129 : 6379 slots:[ 0 - 5460 ] ( 5461 slots) master // 6380是master,对应的ID为 1c2ec35156832a01bbe685232c2004c8dde40b27 M: 1c2ec35156832a01bbe685232c2004c8dde40b27 192.168 . 229.129 : 6380 slots:[ 5461 - 10922 ] ( 5462 slots) master // 6381是master,对应的ID为 5f453cb9cddde68334fe4b704b0fcda69b8e93d9 M: 5f453cb9cddde68334fe4b704b0fcda69b8e93d9 192.168 . 229.129 : 6381 slots:[ 10923 - 16383 ] ( 5461 slots) master // 根据ID可以看出6389是6380的从机 S: 6e35fe94bdb42f4843d9489c83b1c902cd0d8040 192.168 . 229.129 : 6389 replicates 1c2ec35156832a01bbe685232c2004c8dde40b27 // 根据ID可以看出6390是6381的从机 S: 5002e820bfcecd3f36c82c2f0194ca981ba3ad71 192.168 . 229.129 : 6390 replicates 5f453cb9cddde68334fe4b704b0fcda69b8e93d9 // 根据ID可以看出6391是6379的从机 S: 2e48161308203ea729d36642b5c8d1f0f61c154a 192.168 . 229.129 : 6391 replicates c78230ccc7f24f6722a6c8bab99c9bf1c3919cbd 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 . 229.129 : 6379 ) M: c78230ccc7f24f6722a6c8bab99c9bf1c3919cbd 192.168 . 229.129 : 6379 slots:[ 0 - 5460 ] ( 5461 slots) master 1 additional replica(s) M: 1c2ec35156832a01bbe685232c2004c8dde40b27 192.168 . 229.129 : 6380 slots:[ 5461 - 10922 ] ( 5462 slots) master 1 additional replica(s) S: 6e35fe94bdb42f4843d9489c83b1c902cd0d8040 192.168 . 229.129 : 6389 slots: ( 0 slots) slave replicates 1c2ec35156832a01bbe685232c2004c8dde40b27 M: 5f453cb9cddde68334fe4b704b0fcda69b8e93d9 192.168 . 229.129 : 6381 slots:[ 10923 - 16383 ] ( 5461 slots) master 1 additional replica(s) S: 2e48161308203ea729d36642b5c8d1f0f61c154a 192.168 . 229.129 : 6391 slots: ( 0 slots) slave replicates c78230ccc7f24f6722a6c8bab99c9bf1c3919cbd S: 5002e820bfcecd3f36c82c2f0194ca981ba3ad71 192.168 . 229.129 : 6390 slots: ( 0 slots) slave replicates 5f453cb9cddde68334fe4b704b0fcda69b8e93d9 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. |
6、登录客户端.
1 | redis-cli -h 192.168 . 229.129 -c -p 6379 |
7、测试
1 | 192.168 . 229.128 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?