Redis-集群

Redis-集群

什么是集群
Redis 集群实现了对Redis的水平扩容,即启动N个redis节点,将整个数据库分布存储在这N个节点中,每个节点存储总数据的1/N。
Redis 集群通过分区(partition)来提供一定程度的可用性(availability): 即使集群中有一部分节点失效或者无法进行通讯, 集群也可以继续处理命令请求。


步骤:
(1)、删除持久化数据,将rdb,aof文件都删除掉。
在这里插入图片描述
(2)、配置基本信息

	制作6个实例,6379,6380,6381,6389,6390,6391
配置基本信息

开启daemonize yes
Pid文件名字
指定端口
Log文件名字
Dump.rdb名字
Appendonly 关掉或者换名字
cluster-enabled yes    打开集群模式
cluster-config-file nodes-6379.conf  设定节点配置文件名
cluster-node-timeout 15000   设定节点失联时间,超过该时间(毫秒),集群自动进行主从切换。

6379端口号 配置文件
include /myredis/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

(4)、修改好redis6379.conf文件,拷贝多个redis.conf文件
在这里插入图片描述
在这里插入图片描述
(4)、使用查找替换修改另外5个文件

%s/6379/6380  

(5)、启动6个redis服务
在这里插入图片描述
在这里插入图片描述
(6)、将六个节点合成一个集群

组合之前,请确保所有redis实例启动后,nodes-xxxx.conf文件都生成正常。
redis-cli --cluster create --cluster-replicas 1 47.115.33.204:6379 47.115.33.204:6380 47.115.33.204:6381 47.115.33.204:6389 47.115.33.204:6390 47.115.33.204:6391

此处不要用127.0.0.1, 请用真实IP地址
--replicas 1 采用最简单的方式配置集群,一台主机,一台从机,正好三组。
[root@wei-aliyun src]# redis-cli --cluster create --cluster-replicas 1 47.115.33.204:6379 47.115.33.204:6380 47.115.33.204:6381 47.115.33.204:6389 47.115.33.204:6390 47.115.33.204: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 47.115.33.204:6390 to 47.115.33.204:6379
Adding replica 47.115.33.204:6391 to 47.115.33.204:6380
Adding replica 47.115.33.204:6389 to 47.115.33.204:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: f7240f3311a5a675fac6a85ea540a305ebddc282 47.115.33.204:6379
   slots:[0-5460] (5461 slots) master
M: bcf6a0702661c46cd27dddeb334491ced44dc121 47.115.33.204:6380
   slots:[5461-10922] (5462 slots) master
M: e163496354c2cce0fd34a3de211a58b15357eafc 47.115.33.204:6381
   slots:[10923-16383] (5461 slots) master
S: 3dbd891273ce5b5eeb41785a19d188c10933a8b4 47.115.33.204:6389
   replicates e163496354c2cce0fd34a3de211a58b15357eafc
S: 991271f8eee403e72a4e4f06ed605fa895fadccd 47.115.33.204:6390
   replicates f7240f3311a5a675fac6a85ea540a305ebddc282
S: 24f8b35c2135a6dd71684e4ea04c16d4b025f428 47.115.33.204:6391
   replicates bcf6a0702661c46cd27dddeb334491ced44dc121
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 47.115.33.204:6379)
M: f7240f3311a5a675fac6a85ea540a305ebddc282 47.115.33.204:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: e163496354c2cce0fd34a3de211a58b15357eafc 47.115.33.204:6381
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 24f8b35c2135a6dd71684e4ea04c16d4b025f428 47.115.33.204:6391
   slots: (0 slots) slave
   replicates bcf6a0702661c46cd27dddeb334491ced44dc121
S: 991271f8eee403e72a4e4f06ed605fa895fadccd 47.115.33.204:6390
   slots: (0 slots) slave
   replicates f7240f3311a5a675fac6a85ea540a305ebddc282
M: bcf6a0702661c46cd27dddeb334491ced44dc121 47.115.33.204:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 3dbd891273ce5b5eeb41785a19d188c10933a8b4 47.115.33.204:6389
   slots: (0 slots) slave
   replicates e163496354c2cce0fd34a3de211a58b15357eafc
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

如果用了阿里云,记得去安全组开6380 6381 6389 6390 6391端口,
同时还要开放集群总线端口是redis客户端连接的端口+10000,
即还要开放 16380 16381 16389 16390 16391端口

-c 采用集群策略连接,设置数据会自动切换到相应的写主机

在这里插入图片描述

通过 cluster nodes 命令查看集群信息

在这里插入图片描述

一个集群至少要有三个主节点。
选项 --cluster-replicas 1 表示我们希望为集群中的每个主节点创建一个从节点。
分配原则尽量保证每个主数据库运行在不同的IP地址,每个从库和主库不在一个IP地址上。

slots

一个 Redis 集群包含 16384 个插槽(hash slot), 数据库中的每个键都属于这 16384 个插槽的其中一个, 
集群使用公式 CRC16(key) % 16384 来计算键 key 属于哪个槽, 其中 CRC16(key) 语句用于计算键 key 的 CRC16 校验和 。
集群中的每个节点负责处理一部分插槽。 举个例子, 如果一个集群可以有主节点, 其中:
节点 A 负责处理 0 号至 5460 号插槽。
节点 B 负责处理 5461 号至 10922 号插槽。
节点 C 负责处理 10923 号至 16383 号插槽。

在集群中录入值

不在一个slot下的键值,是不能使用mget,mset等多键操作。

在这里插入图片描述

可以通过{}来定义组的概念,从而使key中{}内相同内容的键值对放到一个slot中去。

在这里插入图片描述

CLUSTER GETKEYSINSLOT <slot><count> 返回 count 个 slot 槽中的键。

在这里插入图片描述

如果某一段插槽的主从都挂掉,而cluster-require-full-coverage 为yes ,那么 ,整个集群都挂掉
如果某一段插槽的主从都挂掉,而cluster-require-full-coverage 为no ,那么,该插槽数据全都不能使用,也无法存储。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值