首页 > 数据库 >基于redis5的redis cluster部署

基于redis5的redis cluster部署

时间:2023-10-07 15:33:06浏览次数:39  
标签:10.0 6379 redis5 0.0 redis cluster slots

 

1.创建 redis cluster集群的环境准备
1.1.#每个redis 节点采用相同的相同的redis版本、相同的密码、硬件配置;所有redis服务器必须没有任何数据;准备六台主机,地址如下:
10.0.0.150
10.0.0.160
10.0.0.170
10.0.0.180
10.0.0.190
10.0.0.200

2.启用redis cluster配置
2.1.#所有6台主机都要执行以下配置
[root@centos8 ~]#dnf -y install redis

2.2.#每个节点修改redis配置,必须开启cluster功能的参数
2.2.1.#手动修改配置文件
[root@redis-node1 ~]vim /etc/redis.conf
bind 0.0.0.0
masterauth 123456   #建议配置,否则后期的master和slave主从复制无法成功,还需再配置
requirepass 123456
cluster-enabled yes #取消此行注释,必须开启集群,开启后redis 进程会有cluster标识
cluster-config-file nodes-6379.conf #取消此行注释,此为集群状态文件,记录主从关系及slot范围信息,由redis cluster 集群自动创建和维护
cluster-require-full-coverage no   #默认值为yes,设为no可以防止一个节点不可用导致整个cluster不可用

2.2.2.#或者执行下面命令,批量修改
[root@redis-node1 ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf

2.2.3.#启动redis服务
[root@redis-node1 ~]#systemctl enable --now redis

2.2.4.#验证当前Redis服务状态:
#开启了16379的cluster的端口,实际的端口=redis port + 10000
[root@redis-node1 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:6379 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 100 127.0.0.1:25 0.0.0.0:*
LISTEN 0 128 127.0.0.1:6010 0.0.0.0:*
LISTEN 0 128 0.0.0.0:16379 0.0.0.0:*

#注意进程有[cluster]状态
[root@redis-node1 ~]# ps -ef|grep redis
redis 31887 1 0 12:56 ? 00:00:00 /usr/bin/redis-server 0.0.0.0:6379 [cluster]
root 31966 31920 0 12:59 pts/0 00:00:00 grep --color=auto redis


3.创建集群
# redis-cli --cluster-replicas 1 表示每个master对应一个slave节点
[root@redis-node1 ~]#redis-cli -a 123456 --cluster create 10.0.0.150:6379 10.0.0.160:6379 10.0.0.170:6379 10.0.0.180:6379 10.0.0.190:6379 10.0.0.200:6379 --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 10.0.0.180:6379 to 10.0.0.150:6379
Adding replica 10.0.0.190:6379 to 10.0.0.160:6379
Adding replica 10.0.0.200:6379 to 10.0.0.170:6379
M: f1e7ed79ab91541f362771c6b9db2543cd1e3044 10.0.0.150:6379
slots:[0-5460] (5461 slots) master
M: b6ff266b891e766ee844bbeda1f069a3077d10b8 10.0.0.160:6379
slots:[5461-10922] (5462 slots) master
M: c4a31f125e3cfceeb80325a890e9af89216009a0 10.0.0.170:6379
slots:[10923-16383] (5461 slots) master
S: 9aca5c144f73a86d88ee566e294d871e10196cc6 10.0.0.180:6379
replicates f1e7ed79ab91541f362771c6b9db2543cd1e3044
S: b77bfc80656b2b5b31f400b676703600c54c0170 10.0.0.190:6379
replicates b6ff266b891e766ee844bbeda1f069a3077d10b8
S: 644c42c71db99c15dc58a12ff6904197e0d73c6c 10.0.0.200:6379
replicates c4a31f125e3cfceeb80325a890e9af89216009a0
Can I set the above configuration? (type 'yes' to accept): yes#输入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 10.0.0.150:6379)
M: f1e7ed79ab91541f362771c6b9db2543cd1e3044 10.0.0.150:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 9aca5c144f73a86d88ee566e294d871e10196cc6 10.0.0.180:6379
slots: (0 slots) slave
replicates f1e7ed79ab91541f362771c6b9db2543cd1e3044
S: 644c42c71db99c15dc58a12ff6904197e0d73c6c 10.0.0.200:6379
slots: (0 slots) slave
replicates c4a31f125e3cfceeb80325a890e9af89216009a0
M: c4a31f125e3cfceeb80325a890e9af89216009a0 10.0.0.170:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: b6ff266b891e766ee844bbeda1f069a3077d10b8 10.0.0.160:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: b77bfc80656b2b5b31f400b676703600c54c0170 10.0.0.190:6379
slots: (0 slots) slave
replicates b6ff266b891e766ee844bbeda1f069a3077d10b8
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.



4.查看主从状态
[root@redis-node1 ~]# redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.180,port=6379,state=online,offset=154,lag=1
master_replid:14c9201de50d54a91599d3eb188e5a94910d3111
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:154
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:154

[root@redis-node4 ~]# redis-cli -a 123456 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.0.0.150
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:252
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:14c9201de50d54a91599d3eb188e5a94910d3111
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:252
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:252


5.验证集群状态
[root@redis-node1 ~]# redis-cli -a 123456 CLUSTER INFO
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:250
cluster_stats_messages_pong_sent:273
cluster_stats_messages_sent:523
cluster_stats_messages_ping_received:268
cluster_stats_messages_pong_received:250
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:523


#查看任意节点的集群状态
[root@redis-node2 ~]# redis-cli -a 123456 --cluster info 10.0.0.150:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.150:6379 (f1e7ed79...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.170:6379 (c4a31f12...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.160:6379 (b6ff266b...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.


6.查看集群node对应关系
[root@redis-node1 ~]# redis-cli -a 123456 CLUSTER NODES
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
9aca5c144f73a86d88ee566e294d871e10196cc6 10.0.0.180:6379@16379 slave f1e7ed79ab91541f362771c6b9db2543cd1e3044 0 1643260442777 4 connected
644c42c71db99c15dc58a12ff6904197e0d73c6c 10.0.0.200:6379@16379 slave c4a31f125e3cfceeb80325a890e9af89216009a0 0 1643260440742 6 connected
f1e7ed79ab91541f362771c6b9db2543cd1e3044 10.0.0.150:6379@16379 myself,master - 0 1643260439000 1 connected 0-5460
c4a31f125e3cfceeb80325a890e9af89216009a0 10.0.0.170:6379@16379 master - 0 1643260441000 3 connected 10923-16383
b6ff266b891e766ee844bbeda1f069a3077d10b8 10.0.0.160:6379@16379 master - 0 1643260441760 2 connected 5461-10922
b77bfc80656b2b5b31f400b676703600c54c0170 10.0.0.190:6379@16379 slave b6ff266b891e766ee844bbeda1f069a3077d10b8 0 1643260440000 5 connected

7.验证集群写入key
7.1.#redis cluster 写入key
#经过算法计算,当前key的槽位需要写入指定的node
[root@redis-node1 ~]# redis-cli -a 123456 -h 10.0.0.150 SET key1 values1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(error) MOVED 9189 10.0.0.160:6379


#指定槽位对应node可写入
[root@redis-node1 ~]# redis-cli -a 123456 -h 10.0.0.160 SET key1 values1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK


#对应的slave节点可以KEYS *,但GET key1失败,可以到master上执行GET key1
[root@redis-node1 ~]# redis-cli -a 123456 -h 10.0.0.190 KEYS "*"
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
1) "key1"
[root@redis-node1 ~]# redis-cli -a 123456 -h 10.0.0.190 get key1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(error) MOVED 9189 10.0.0.160:6379
[root@redis-node1 ~]# redis-cli -a 123456 -h 10.0.0.160 get key1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"values1"



7.2.#redis cluster 计算key所属的slot
[root@redis-node1 ~]# redis-cli -h 10.0.0.150 -a 123456 --no-auth-warning cluster nodes
9aca5c144f73a86d88ee566e294d871e10196cc6 10.0.0.180:6379@16379 slave f1e7ed79ab91541f362771c6b9db2543cd1e3044 0 1643260685207 4 connected
644c42c71db99c15dc58a12ff6904197e0d73c6c 10.0.0.200:6379@16379 slave c4a31f125e3cfceeb80325a890e9af89216009a0 0 1643260688274 6 connected
f1e7ed79ab91541f362771c6b9db2543cd1e3044 10.0.0.150:6379@16379 myself,master - 0 1643260686000 1 connected 0-5460
c4a31f125e3cfceeb80325a890e9af89216009a0 10.0.0.170:6379@16379 master - 0 1643260687249 3 connected 10923-16383
b6ff266b891e766ee844bbeda1f069a3077d10b8 10.0.0.160:6379@16379 master - 0 1643260687000 2 connected 5461-10922
b77bfc80656b2b5b31f400b676703600c54c0170 10.0.0.190:6379@16379 slave b6ff266b891e766ee844bbeda1f069a3077d10b8 0 1643260689295 5 connected

#计算得到hello对应的slot
[root@redis-node1 ~]# redis-cli -h 10.0.0.150 -a 123456 --no-auth-warning cluster keyslot hello
(integer) 866


#使用选项-c 以集群模式连接
[root@redis-node1 ~]# redis-cli -h 10.0.0.150 -a 123456 --no-auth-warning cluster keyslot hello
(integer) 866
[root@redis-node1 ~]# redis-cli -c -h 10.0.0.150 -a 123456 --no-auth-warning
10.0.0.150:6379> cluster keyslot linux
(integer) 12299
10.0.0.150:6379> set linux love
-> Redirected to slot [12299] located at 10.0.0.170:6379
OK
10.0.0.170:6379> get linux
"love"
10.0.0.170:6379> exit


[root@redis-node1 ~]# redis-cli -h 10.0.0.170 -a 123456 --no-auth-warning get linux
"love"

标签:10.0,6379,redis5,0.0,redis,cluster,slots
From: https://www.cnblogs.com/tanll/p/17746431.html

相关文章

  • redis配置类
     EncodeImgUtil类:packagecom.dtinone.springbootredis.utils;importsun.misc.BASE64Decoder;importsun.misc.BASE64Encoder;importjava.io.*;/***图片编码工具类**@authorbinge*/publicclassEncodeImgUtil{/***读取文件返回字节数组......
  • PHP操作redis
    一、安装PHP的redis扩展1)PHP的redis扩展有2个,分别是phpredis和predis扩展;phpredis是PHP官方推荐的,是C写的;predis使用的原生的PHP代码实现的一套Redis-client程序,可以不用安装任何扩展,只引入php代码就可以很方便的使用redis。2)phpredis需要下载扩展->编译安装,而predis不用,直......
  • Redis项目搭建
    Redis项目搭建Redis下载搭建redis首先需要下载Redis,可是Redis官方并没有Windows安装,好在网上从不缺大牛,Github上可以找到Redis的Windows版下载地址:https://github.com/tporadowski/redis/releases(网速很慢)打开上述链接如下图:解压后如下图,我们只需要关注3个部分redis.win......
  • PHP-redis中文文档
    phpredis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系 很有用;以下是redis官方提供的命令使用技巧:   下载地址如下: https://github.com/owlient/phpredis(支持redis2.0.4)Redis::__construct构造函数$redis=newRedis();connect,open ......
  • RedisUtil 工具类
    可以将此工具类看成传统RedisTemplate类对其功能的封装只包含常用功能模块,可以在下述代码中添加自定义功能importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.data.redis.core.RedisTemplate;importorg.springframework.data.redis......
  • Redis知识点
    resis是基于内存的,所有速度很高数据库的发展:网状数据库,层次数据库,关系数据库关系数据库的不足:1.不能有效处理多维数据2.高并发读写性能低3.存储数据容量有限4.数据扩展性和可用性低NoSQL的三大优势:易扩展,大容量,高性能 NoSql与MySQL之间互补Redis是一款基于(key-value)的......
  • 缓存(Redis)与数据库(MySQL)一致性如何解决?
    【零】场景预设我们以12306购票系统为例,结合购票场景完成缓存与数据库双写一致性的相关问题解决【一】业务背景为了满足用户对一趟列车不同站点不同座位类型的余量查询需求,我们采取了一种优化方案。我们将这些余量信息存储在缓存中,以便用户可以快速查询。然而,在用户创建......
  • Redis持久化
    前言​我们都知道Redis的数据都存在内存里,如果突然宕机,数据就会全部丢失,因此必须有一种机制来保证Redis的数据不会因为故障而丢失,这种机制就是Redis的持久化机制。​Redis的持久化机制主要是有两种,第一种是RDB快照,第二种是AOD日志。如果我们的服务器开启了AOF持久化功能,那么......
  • Redis分布式锁演进架构
    【一】引言分布式锁相信大家一定不会陌生,想要用好或者自己写一个却没那么简单。想要达到上述的条件,一定要掌握分布式锁的应用场景,以及分布式锁的不同实现,不同实现之间有什么区别。【二】分布式锁场景如果想真正了解分布式锁,需要结合一定场景;举个例子,某夕夕上抢购AirPod......
  • Redis入门
    【学习教程】:【黑马程序员2023最新Java项目实战《苍穹外卖》,最适合新手的SpringBoot+SSM的企业级Java项目实战】https://www.bilibili.com/video/BV1TP411v7v6/?p=52&share_source=copy_web&vd_source=2c07d62293f5003c919b2df9b2e0549eRedis入门基本介绍Redis是一个基于内存......