虚拟机中搭建去中心化Redis伪集群
2021/6/12 19:22:39
本文主要是介绍虚拟机中搭建去中心化Redis伪集群,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
简介:Redis集群
Redis 集群实现了对Redis的水平扩容,即启动N个redis节点,将整个数据库分布存储在这N个节点中,每个节点存储总数据的1/N。
Redis 集群通过分区(partition)来提供一定程度的可用性(availability): 即使集群中有一部分节点失效或者无法进行通讯, 集群也可以继续处理命令请求。
第一步:集群架构设计
制作6个实例,3组主从复制模式,每组2个redis server 实例
如下图:
当web request过来的时候,可以到任何一台master上,master根据request params进行相互转发,获得相应的请求数据
第二步:编写redis server配置文件
以6379 server实例的配置文件为例:
include /usr/local/redis/bin/myconfs/redis.conf pidfile "/var/run/redis_6379.pid" port 6379 dbfilename "dump6379.rdb" cluster-enabled yes cluster-config-file nodes-6379.conf cluster-node-timeout 15000
其他几个节点:6380、6381、6389、6390、6391
配置相同,只是对应的数字变化一下
创建好配置文件之后,启动所有的redis server实例
[root@linuxprobe bin]# ./redis-server ./myconfs/redis6379.conf [root@linuxprobe bin]# ./redis-server ./myconfs/redis6380.conf [root@linuxprobe bin]# ./redis-server ./myconfs/redis6381.conf [root@linuxprobe bin]# ./redis-server ./myconfs/redis6389.conf [root@linuxprobe bin]# ./redis-server ./myconfs/redis6390.conf [root@linuxprobe bin]# ./redis-server ./myconfs/redis6391.conf
检测是否启动成功
[root@linuxprobe bin]# ps -ef | grep redis
对应的node配置文件也自动生成
第三步:节点合成集群
首先,进入redis解压文件夹中的src目录中
我的位置是:
/root/ProjectNeeds/Softwares/Redis/redis-6.2.3/src
这个目录里,主要用到以下两个工具
节点合成集群的命令:
./redis-cli --cluster create --cluster-replicas 1 192.168.0.112:6379 192.168.0.112:6380 192.168.0.112:6381 192.168.0.112:6389 192.168.0.112:6390 192.168.0.112:6391
注意:
这里IP必须写真实IP,不能用127.0.0.1
–replicas 1 的作用是,采用最简单的方式配置集群,一台主机,一台从机,正好三组
该命令需要在src目录里执行
运行成功的话,结果如下:
[root@linuxprobe src]# ./redis-cli --cluster create --cluster-replicas 1 192.168.0.112:6379 192.168.0.112:6380 192.168.0.112:6381 192.168.0.112:6389 192.168.0.112:6390 192.168.0.112:6391 >>> 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.0.112:6390 to 192.168.0.112:6379 Adding replica 192.168.0.112:6391 to 192.168.0.112:6380 Adding replica 192.168.0.112:6389 to 192.168.0.112:6381 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: 80ac7bf79b0abec10b4183a3a11086452dc1c93f 192.168.0.112:6379 slots:[0-5460] (5461 slots) master M: 4d93a26da64ddfcf6568ea1225138ef435d24b62 192.168.0.112:6380 slots:[5461-10922] (5462 slots) master M: c08c86afe29d6df8867d5c1ac73b1f723e9eb88f 192.168.0.112:6381 slots:[10923-16383] (5461 slots) master S: 8d9216b8972b7eda65daa6115927c877946b13a1 192.168.0.112:6389 replicates 80ac7bf79b0abec10b4183a3a11086452dc1c93f S: 072f85e9e43b64a57daa0b33588fe3d267927b17 192.168.0.112:6390 replicates 4d93a26da64ddfcf6568ea1225138ef435d24b62 S: 753b8090296d9fd0b3f12089fb6590f35df582f0 192.168.0.112:6391 replicates c08c86afe29d6df8867d5c1ac73b1f723e9eb88f 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.0.112:6379) M: 80ac7bf79b0abec10b4183a3a11086452dc1c93f 192.168.0.112:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: 8d9216b8972b7eda65daa6115927c877946b13a1 192.168.0.112:6389 slots: (0 slots) slave replicates 80ac7bf79b0abec10b4183a3a11086452dc1c93f M: 4d93a26da64ddfcf6568ea1225138ef435d24b62 192.168.0.112:6380 slots:[5461-10922] (5462 slots) master 1 additional replica(s) M: c08c86afe29d6df8867d5c1ac73b1f723e9eb88f 192.168.0.112:6381 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 072f85e9e43b64a57daa0b33588fe3d267927b17 192.168.0.112:6390 slots: (0 slots) slave replicates 4d93a26da64ddfcf6568ea1225138ef435d24b62 S: 753b8090296d9fd0b3f12089fb6590f35df582f0 192.168.0.112:6391 slots: (0 slots) slave replicates c08c86afe29d6df8867d5c1ac73b1f723e9eb88f [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
第四步:连接集群测试
连接集群的命令:
可以通过任何一个节点连接到集群
./redis-cli -c -p 6379
查看集群信息命令:
CLUSTER NODES
这样,我们的Redis伪集群就算搭建完成了
补充:我的redis版本:6.2.3
这篇关于虚拟机中搭建去中心化Redis伪集群的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-30阿里云Redis教程:新手入门指南
- 2024-12-27阿里云Redis学习入门指南
- 2024-12-27阿里云Redis入门详解:轻松搭建与管理
- 2024-12-27阿里云Redis学习:新手入门指南
- 2024-12-24Redis资料:新手入门快速指南
- 2024-12-24Redis资料:新手入门教程与实践指南
- 2024-12-24Redis资料:新手入门教程与实践指南
- 2024-12-07Redis高并发入门详解
- 2024-12-07Redis缓存入门:新手必读指南
- 2024-12-07Redis缓存入门:新手必读教程