redis集群

一。问题?

1.容量不够,redis如何进行扩容?

2.并发操作,redis如何分摊?

另外,从主模式、薪火相传模式、主机宕机、导致主机ip地址发生变化,应用程序中配置需要修改对应的主机地址,端口等信息

之前通过代理主机来解决问题,但是redis3.0提供方案就是无中心化集群配置

无中心化集群

 

二、什么是集群?

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

删除持久化数据:先删除rdb文件

[root@localhost myredis]# rm -rf dump63*

制作六个实例,6379、6380、6381、6389、6390、6391

[root@localhost myredis]# vi redis6379.conf

 cluster-enabled yes   打开集群模式

cluster-config-file nodes-6391.conf    设定节点配置文件名

cluster-node-timeout 15000  设定节点失联时间,超过多少时间(毫秒),集群自动进行主从切换

include /myredis/redis.conf
pidfile "/var/run/redis_6391.pid"
port 6391
dbfilename "dump6391.rdb"
cluster-enabled yes
cluster-config-file nodes-6391.conf
cluster-node-timeout 15000

快速删除原来的conf文件

rm -rf redis63*

 快速复制

 

替换口令:

:%s/6379/6380

后面一个端口为要替换成的端口

启动六个redis服务

[root@localhost myredis]# redis-server redis6379.conf
[root@localhost myredis]# redis-server redis6379.conf
[root@localhost myredis]#  redis-server redis6380.conf
[root@localhost myredis]#  redis-server redis6381.conf
[root@localhost myredis]#  redis-server redis6389.conf
[root@localhost myredis]#  redis-server redis6390.conf
[root@localhost myredis]#  redis-server redis6391.con
12938:C 11 Mar 2022 10:31:20.668 # Fatal error, can't open config file '/root/myredis/redis6391.con': No such file or directory
[root@localhost myredis]#  redis-server redis6391.conf

查看进程

下面将六个节点合成一个集群

     先切换到redis的src目录下

[root@localhost myredis]# cd /usr/local
[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  nginx  redis-6.2.6  sbin  share  src
[root@localhost local]# cd redis-6.2.6
[root@localhost redis-6.2.6]# ll
总用量 248
-rw-rw-r--  1 root root 33624 10月  4 18:59 00-RELEASENOTES
-rw-rw-r--  1 root root    51 10月  4 18:59 BUGS
-rw-rw-r--  1 root root  5026 10月  4 18:59 CONDUCT
-rw-rw-r--  1 root root  3384 10月  4 18:59 CONTRIBUTING
-rw-rw-r--  1 root root  1487 10月  4 18:59 COPYING
drwxrwxr-x  7 root root   213 3月  10 17:07 deps
-rw-r--r--  1 root root    92 3月  10 17:52 dump.rdb
-rw-rw-r--  1 root root    11 10月  4 18:59 INSTALL
-rw-rw-r--  1 root root   151 10月  4 18:59 Makefile
-rw-rw-r--  1 root root  6888 10月  4 18:59 MANIFESTO
-rw-rw-r--  1 root root 21567 10月  4 18:59 README.md
-rw-rw-r--  1 root root 93724 3月  10 17:16 redis.conf
-rwxrwxr-x  1 root root   275 10月  4 18:59 runtest
-rwxrwxr-x  1 root root   279 10月  4 18:59 runtest-cluster
-rwxrwxr-x  1 root root  1079 10月  4 18:59 runtest-moduleapi
-rwxrwxr-x  1 root root   281 10月  4 18:59 runtest-sentinel
-rw-rw-r--  1 root root 13768 10月  4 18:59 sentinel.conf
drwxrwxr-x  3 root root 12288 3月  10 17:09 src
drwxrwxr-x 11 root root   182 10月  4 18:59 tests
-rw-rw-r--  1 root root  3055 10月  4 18:59 TLS.md
drwxrwxr-x  9 root root  4096 10月  4 18:59 utils
[root@localhost redis-6.2.6]# cd /src
-bash: cd: /src: 没有那个文件或目录
[root@localhost redis-6.2.6]# cd src

在redis的src目录下执行下面这段代码 

这里有个小坑,要看自己redis的 /etc/redis.conf 里面的bind

redis-cli --cluster create --cluster-replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6389 127.0.0.1:6390 127.0.0.1:6391

把六个redis服务进行合体成为一个集群,输入yes,下面为合并为一个集群的页面

[root@localhost src]# redis-cli --cluster create --cluster-replicas 1 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6389 127.0.0.1:6390 127.0.0.1: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 127.0.0.1:6390 to 127.0.0.1:6379
Adding replica 127.0.0.1:6391 to 127.0.0.1:6380
Adding replica 127.0.0.1:6389 to 127.0.0.1:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 894d263507ede34cd083aa74f438372bcd4f9200 127.0.0.1:6379
   slots:[0-5460] (5461 slots) master
M: 6f29327986273becb7c3efb75a71e42c68ae33f1 127.0.0.1:6380
   slots:[5461-10922] (5462 slots) master
M: 85979769fd8c30b6b3eb8dcd3553d8fc6402f69e 127.0.0.1:6381
   slots:[10923-16383] (5461 slots) master
S: 0c5a3cc5b3bf224a511c1415d6be9bcce672ae67 127.0.0.1:6389
   replicates 85979769fd8c30b6b3eb8dcd3553d8fc6402f69e
S: 0836dae0134118a97b6a9a0ab992be329c4e50e9 127.0.0.1:6390
   replicates 894d263507ede34cd083aa74f438372bcd4f9200
S: 94d047ca6e8160a27539958fbe33f026ee4734cb 127.0.0.1:6391
   replicates 6f29327986273becb7c3efb75a71e42c68ae33f1
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 127.0.0.1:6379)
M: 894d263507ede34cd083aa74f438372bcd4f9200 127.0.0.1:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 94d047ca6e8160a27539958fbe33f026ee4734cb 127.0.0.1:6391
   slots: (0 slots) slave
   replicates 6f29327986273becb7c3efb75a71e42c68ae33f1
M: 85979769fd8c30b6b3eb8dcd3553d8fc6402f69e 127.0.0.1:6381
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 6f29327986273becb7c3efb75a71e42c68ae33f1 127.0.0.1:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 0836dae0134118a97b6a9a0ab992be329c4e50e9 127.0.0.1:6390
   slots: (0 slots) slave
   replicates 894d263507ede34cd083aa74f438372bcd4f9200
S: 0c5a3cc5b3bf224a511c1415d6be9bcce672ae67 127.0.0.1:6389
   slots: (0 slots) slave
   replicates 85979769fd8c30b6b3eb8dcd3553d8fc6402f69e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@localhost src]#

 把这个提出来

16384 slots covered.

普通redis的连接redis-cli -p 6379 

-c 采用集群策略连接,设置数据会相应切换到相应的写主机,连接任何一个节点都是可以的总共六个节点

redis集群的连接redis-cli -c -p 6379 

[root@localhost src]# redis-cli -c -p 6391
127.0.0.1:6391> cluster nodes

查看节点集群中的信息

127.0.0.1:6391> cluster nodes

 redis cluster 如何分配这六个节点?

一个集群至少要有三个主节点。

选项 --cluster-replicas 1 表示我们希望为集群中的每个主节点创建一个从节点。

分配原则尽量保证每个主数据库运行在不同的IP地址,每个从库和主库不在一个IP地址上。

 分配原则:

尽量保证每个数据库运行在不同的IP地址,每个主库和从库不在一个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 号插槽
 

进行插入值的操作,插入一个key可以识别,插入多个字段的key就需要分组,分组要用中括号,示例如下

127.0.0.1:6391> set k1 vi
-> Redirected to slot [12706] located at 127.0.0.1:6381
OK
127.0.0.1:6381> set k2 v2
-> Redirected to slot [449] located at 127.0.0.1:6379
OK
127.0.0.1:6379> mset name lucy age 20
(error) CROSSSLOT Keys in request don't hash to the same slot
127.0.0.1:6379> mset name(user) lucy age(user) 20 address(user) 北京
(error) CROSSSLOT Keys in request don't hash to the same slot
127.0.0.1:6379> mset name(user) lucy age(user) 10
(error) CROSSSLOT Keys in request don't hash to the same slot
127.0.0.1:6379> mset name{user} lucy age{user} 10
-> Redirected to slot [5474] located at 127.0.0.1:6380
OK
127.0.0.1:6380>

查看你的key的插槽值

cluster keyslot k2
127.0.0.1:6381> set k2 v2
-> Redirected to slot [449] located at 127.0.0.1:6379
OK




127.0.0.1:6380> cluster keyslot k2
(integer) 449

主机的插槽和IP对应

要看插槽里面的值必须到该端口里面去查看

查看指令

cluster countkeysinslot 449

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值