数据库redis集群

redis的介绍

‌Redis是一个开源的内存数据结构存储系统,它可以用作数据库、缓存和消息中间件。‌ Redis支持多种数据结构,如字符串、哈希表、列表、集合、有序集合、位图、HyperLogLog、地理空间索引和流等,这些功能使得Redis在处理复杂数据结构时表现出色。由于Redis数据存储在内存中,因此具有极高的读写性能,适合对响应速度要求高的场景。Redis的核心概念是键值对存储,每个数据都由一个唯一的键标识,并与一个值关联,这个值可以是多种数据类型之一。

Redis提供了两种主要的持久化方式:RDB(Redis Database File)和AOF(Append Only File)。RDB方式通过在指定的时间间隔内生成数据的快照并将其保存到磁盘,而AOF方式则将每个写操作日志记录下来,并在Redis启动时重新执行这些操作以恢复数据。这两种方式共同保证了Redis数据的持久性和可靠性。

Redis还支持简单的事务处理,允许在一个操作序列中执行多个命令,并且保证这些命令按顺序执行。此外,Redis还提供了发布/订阅功能,使得它能够作为一个消息中间件使用。

Redis的读写速度非常快,每秒可以处理超过10万次读写操作,这使得它被广泛应用于缓存领域。除此之外,Redis也经常用来做分布式锁,并且支持事务、持久化、LUA脚本、LRU驱动事件、多种集群方案。由于其高性能和丰富的功能集,Redis成为了许多应用程序的首选数据存储和缓存解决方案‌1。

redis的特点和功能

Redis(Remote Dictionary Server)是一个开源的内存数据库,遵守 BSD 协议,它提供了一个高性能的键值(key-value)存储系统,常用于缓存、消息队列、会话存储等应用场景。

性能极高:Redis 以其极高的性能而著称,能够支持每秒数十万次的读写操作24。这使得Redis成为处理高并发请求的理想选择,尤其是在需要快速响应的场景中,如缓存、会话管理、排行榜等。

丰富的数据类型:Redis 不仅支持基本的键值存储,还提供了丰富的数据类型,包括字符串、列表、集合、哈希表、有序集合等。这些数据类型为开发者提供了灵活的数据操作能力,使得Redis可以适应各种不同的应用场景。

原子性操作:Redis 的所有操作都是原子性的,这意味着操作要么完全执行,要么完全不执行。这种特性对于确保数据的一致性和完整性至关重要,尤其是在高并发环境下处理事务时。

持久化:Redis 支持数据的持久化,可以将内存中的数据保存到磁盘中,以便在系统重启后恢复数据。这为 Redis 提供了数据安全性,确保数据不会因为系统故障而丢失。

支持发布/订阅模式:Redis 内置了发布/订阅模式(Pub/Sub),允许客户端之间通过消息传递进行通信。这使得 Redis 可以作为消息队列和实时数据传输的平台。

单线程模型:尽管 Redis 是单线程的,但它通过高效的事件驱动模型来处理并发请求,确保了高性能和低延迟。单线程模型也简化了并发控制的复杂性。

主从复制:Redis 支持主从复制,可以通过从节点来备份数据或分担读请求,提高数据的可用性和系统的伸缩性。

应用场景广泛:Redis 被广泛应用于各种场景,包括但不限于缓存系统、会话存储、排行榜、实时分析、地理空间数据索引等。

社区支持:Redis 拥有一个活跃的开发者社区,提供了大量的文档、教程和第三方库,这为开发者提供了强大的支持和丰富的资源。

跨平台兼容性:Redis 可以在多种操作系统上运行,包括 Linux、macOS 和 Windows,这使得它能够在不同的技术栈中灵活部署。

redis的五种数据类型

string(字符串): 基本的数据存储单元,可以存储字符串、整数或者浮点数。
hash(哈希):一个键值对集合,可以存储多个字段。
list(列表):一个简单的列表,可以存储一系列的字符串元素。
set(集合):一个无序集合,可以存储不重复的字符串元素。
zset(sorted set:有序集合): 类似于集合,但是每个元素都有一个分数(score)与之关联

String(字符串)

string 是 redis 最基本的类型,你可以理解成与 Memcached 一模一样的类型,一个 key 对应一个 value。

string 类型是二进制安全的。意思是 redis 的 string 可以包含任何数据,比如jpg图片或者序列化的对象。

string 类型是 Redis 最基本的数据类型,string 类型的值最大能存储 512MB

命令

SET key value:设置键的值。
GET key:获取键的值。
INCR key:将键的值加 1。
DECR key:将键的值减 1。
APPEND key value:将值追加到键的值之后

List(列表)

Redis 列表是简单的字符串列表,按照插入顺序排序。你可以添加一个元素到列表的头部(左边)或者尾部(右边)。

列表最多可以存储 2^32 - 1 个元素

命令

LPUSH key value:将值插入到列表头部。
RPUSH key value:将值插入到列表尾部。
LPOP key:移出并获取列表的第一个元素。
RPOP key:移出并获取列表的最后一个元素。
LRANGE key start stop:获取列表在指定范围内的元素

Set(集合)

Redis 的 Set 是 string 类型的无序集合。

集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1

命令

SADD key value:向集合添加一个或多个成员。
SREM key value:移除集合中的一个或多个成员。
SMEMBERS key:返回集合中的所有成员。
SISMEMBER key value:判断值是否是集合的成员

Hash(哈希)

Redis hash 是一个键值(key=>value)对集合,类似于一个小型的 NoSQL 数据库。

Redis hash 是一个 string 类型的 field 和 value 的映射表,hash 特别适合用于存储对象。

每个哈希最多可以存储 2^32 - 1 个键值对

命令

HSET key field value:设置哈希表中字段的值。
HGET key field:获取哈希表中字段的值。
HGETALL key:获取哈希表中所有字段和值。
HDEL key field:删除哈希表中的一个或多个字段。

zset(sorted set:有序集合)

Redis zset 和 set 一样也是string类型元素的集合,且不允许重复的成员。
不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。

zset的成员是唯一的,但分数(score)却可以重复。

命令

ZADD key score value:向有序集合添加一个或多个成员,或更新已存在成员的分数。
ZRANGE key start stop [WITHSCORES]:返回指定范围内的成员。
ZREM key value:移除有序集合中的一个或多个成员。
ZSCORE key value:返回有序集合中,成员的分数值。

redis的原理图

redis的安装,需要三台虚拟机

源码安装,需要到官网下载需要的版本,在通过xftp传输到虚拟机上

解压

[root@redis-1 ~]# tar zxf redis-7.4.0.tar.gz 

安装编译所需要的环境,有gcc,make,initscripts

[root@redis-1 ~]# dnf install make gcc initscripts -y


正在更新 Subscription Management 软件仓库。
无法读取客户身份

本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。

上次元数据过期检查:0:04:37 前,执行于 2024年08月25日 星期日 22时28分19秒。
依赖关系解决。
==============================================================
 软件包          架构   版本                  仓库       大小
==============================================================
安装:
 gcc             x86_64 11.3.1-2.1.el9        AppStream  32 M
 initscripts     x86_64 10.11.5-1.el9         BaseOS    234 k
 make            x86_64 1:4.3-7.el9           BaseOS    542 k
安装依赖关系:
 chkconfig       x86_64 1.20-2.el9            BaseOS    184 k
 glibc-devel     x86_64 2.34-40.el9           AppStream  47 k
 glibc-headers   x86_64 2.34-40.el9           AppStream 547 k
 kernel-headers  x86_64 5.14.0-162.6.1.el9_1  AppStream 3.4 M
 libxcrypt-devel x86_64 4.4.18-3.el9          AppStream  32 k

事务概要
==============================================================
安装  8 软件包

总计:37 M
安装大小:96 M
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                               1/1 
  安装    : make-1:4.3-7.el9.x86_64                       1/8 
  安装    : chkconfig-1.20-2.el9.x86_64                   2/8 
  安装    : kernel-headers-5.14.0-162.6.1.el9_1.x86_64    3/8 
  安装    : glibc-headers-2.34-40.el9.x86_64              4/8 
  安装    : libxcrypt-devel-4.4.18-3.el9.x86_64           5/8 
  安装    : glibc-devel-2.34-40.el9.x86_64                6/8 
  安装    : gcc-11.3.1-2.1.el9.x86_64                                                                                                 7/8 
  安装    : initscripts-10.11.5-1.el9.x86_64                                                                                          8/8 
  运行脚本: initscripts-10.11.5-1.el9.x86_64                                                                                          8/8 
Created symlink /etc/systemd/system/sysinit.target.wants/import-state.service → /usr/lib/systemd/system/import-state.service.
Created symlink /etc/systemd/system/sysinit.target.wants/loadmodules.service → /usr/lib/systemd/system/loadmodules.service.

  验证    : gcc-11.3.1-2.1.el9.x86_64                                                                                                 1/8 
  验证    : glibc-devel-2.34-40.el9.x86_64                                                                                            2/8 
  验证    : glibc-headers-2.34-40.el9.x86_64                                                                                          3/8 
  验证    : kernel-headers-5.14.0-162.6.1.el9_1.x86_64                                                                                4/8 
  验证    : libxcrypt-devel-4.4.18-3.el9.x86_64                                                                                       5/8 
  验证    : chkconfig-1.20-2.el9.x86_64                                                                                               6/8 
  验证    : initscripts-10.11.5-1.el9.x86_64                                                                                          7/8 
  验证    : make-1:4.3-7.el9.x86_64                                                                                                   8/8 
已更新安装的产品。

已安装:
  chkconfig-1.20-2.el9.x86_64                 gcc-11.3.1-2.1.el9.x86_64                glibc-devel-2.34-40.el9.x86_64                    
  glibc-headers-2.34-40.el9.x86_64            initscripts-10.11.5-1.el9.x86_64         kernel-headers-5.14.0-162.6.1.el9_1.x86_64        
  libxcrypt-devel-4.4.18-3.el9.x86_64         make-1:4.3-7.el9.x86_64                 

完毕!

进入目录下检测

[root@redis-1 ~]# cd redis-7.4.0/

[root@redis-1 redis-7.4.0]# make

编译

[root@redis-1 redis-7.4.0]# make install

修改启动文件的启动参数(注释掉systemd的相关参数),跳过systemd的检测

[root@redis-1 ~]# vim redis-7.4.0/utils/install_server.sh 

启动服务,直接回车默认配置

[root@redis-1 ~]# cd redis-7.4.0/utils/

[root@redis-1 utils]# ./install_server.sh 

Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!

配置redis的文件

[root@redis-1 ~]# vim /etc/redis/6379.conf 

改为所有端口

关闭密码

重启redis服务

[root@redis-1 ~]# /etc/init.d/redis_6379 restart

Stopping ...
Redis stopped
Starting Redis server...

可以查看端口是否开启

其他两台也可以这样配置,但也可以直接远程传输,可以加快配置

在第一台redis上

[root@redis-1 ~]# cd /usr/local/bin/

[root@redis-1 bin]# ls
redis-benchmark  redis-check-rdb  redis-sentinel  vmset.sh
redis-check-aof  redis-cli        redis-server

[root@redis-1 bin]# rsync -al * root@172.25.254.20:/usr/local/bin

The authenticity of host '172.25.254.20 (172.25.254.20)' can't be established.
ED25519 key fingerprint is SHA256:r2X1s506k4aWuWLd/lJrXQZeanKuSr2fYbfqsqzHYCE.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '172.25.254.20' (ED25519) to the list of known hosts.
root@172.25.254.20's password: 
[root@redis-1 ~]# scp -r redis-7.4.0 root@172.25.254.20:/root

在第二台redis上

安装环境

[root@redis-2 ~]# dnf install initscripts -y


正在更新 Subscription Management 软件仓库。
无法读取客户身份

本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。

上次元数据过期检查:0:27:34 前,执行于 2024年08月25日 星期日 22时42分56秒。
依赖关系解决。
==============================================================
 软件包         架构      版本                仓库       大小
==============================================================
安装:
 initscripts    x86_64    10.11.5-1.el9       BaseOS    234 k
安装依赖关系:
 chkconfig      x86_64    1.20-2.el9          BaseOS    184 k

事务概要
==============================================================
安装  2 软件包

总计:418 k
安装大小:1.8 M
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                               1/1 
  安装    : chkconfig-1.20-2.el9.x86_64                   1/2 
  安装    : initscripts-10.11.5-1.el9.x86_64              2/2 
  运行脚本: initscripts-10.11.5-1.el9.x86_64              2/2 
Created symlink /etc/systemd/system/sysinit.target.wants/import-state.service → /usr/lib/systemd/system/import-state.service.
Created symlink /etc/systemd/system/sysinit.target.wants/loadmodules.service → /usr/lib/systemd/system/loadmodules.service.

  验证    : chkconfig-1.20-2.el9.x86_64                                                                                               1/2 
  验证    : initscripts-10.11.5-1.el9.x86_64                                                                                          2/2 
已更新安装的产品。

已安装:
  chkconfig-1.20-2.el9.x86_64                                       initscripts-10.11.5-1.el9.x86_64                                      

完毕!

直接启动服务

[root@redis-2 ~]# cd redis-7.4.0/utils/

[root@redis-2 utils]# ./install_server.sh 

Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379] 
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] 
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] 
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] 
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] 
Selected config:
Port           : 6379
Config file    : /etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!

同理修改redis的文件

[root@redis-1 ~]# vim /etc/redis/6379.conf 

重启服务

[root@redis-2 ~]# /etc/init.d/redis_6379 restart

Stopping ...
Redis stopped
Starting Redis server...

第三台同理(不演示)

redis的主从复制

作为master的主机一定要要关闭protected-mode模式,(刚刚已经关闭)

而slave需要在redis配置文件添加参数,指定master的IP和端口

[root@redis-2 ~]# vim /etc/redis/6379.conf 

重启服务

[root@redis-2 ~]# /etc/init.d/redis_6379 restart

Stopping ...
Redis stopped
Starting Redis server...

另一台同理(不演示)

测试

在master输入数据

[root@redis-1 ~]# redis-cli 

127.0.0.1:6379> set name mqw
OK

而在slave设备上可以查看到

[root@redis-2 ~]# redis-cli 

127.0.0.1:6379> get name
"mqw"

或者直接查看

127.0.0.1:6379> info replication

# Replication
role:master
connected_slaves:2
slave0:ip=172.25.254.20,port=6379,state=online,offset=1161,lag=0
slave1:ip=172.25.254.30,port=6379,state=online,offset=1161,lag=1
master_failover_state:no-failover
master_replid:52d012dc6980d9125faaac435e3b5e222e6d6975
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1161
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1161

注意在slave上不能输入数据(不会成功)

配置redis的哨兵

所有的redis都要关闭protected-mode(已经关闭)

在master上,复制

[root@redis-1 ~]# cd redis-7.4.0/

[root@redis-1 redis-7.4.0]# cp sentinel.conf /etc/redis/

编辑文件

指向master的IP

该为10秒,可以更好的看实验结果

在其他两台的虚拟机上也要配置,也可以复制,方便些

[root@redis-1 redis-7.4.0]# scp /etc/redis/sentinel.conf root@172.25.254.20:/etc/redis/

root@172.25.254.20's password: 
sentinel.conf               100%   14KB  15.8MB/s   00:00    

[root@redis-1 redis-7.4.0]# scp /etc/redis/sentinel.conf root@172.25.254.30:/etc/redis/

root@172.25.254.30's password: 
sentinel.conf               100%   14KB   8.4MB/s   00:00    

启动服务,因为没有修该在后台,所以他会直接占用终端,所以提前复制一个会话

[root@redis-1 ~]# /etc/init.d/redis_6379 restart


Stopping ...
Redis stopped
Starting Redis server...
[root@redis-1 ~]# redis-sentinel /etc/redis/sentinel.conf
39108:X 25 Aug 2024 23:56:30.177 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
39108:X 25 Aug 2024 23:56:30.177 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
39108:X 25 Aug 2024 23:56:30.178 * Redis version=7.4.0, bits=64, commit=00000000, modified=0, pid=39108, just started
39108:X 25 Aug 2024 23:56:30.178 * Configuration loaded
39108:X 25 Aug 2024 23:56:30.179 * Increased maximum number of open files to 10032 (it was originally set to 1024).
39108:X 25 Aug 2024 23:56:30.179 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis Community Edition      
  .-`` .-```.  ```\/    _.,_ ''-._     7.4.0 (00000000/0) 64 bit
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 39108
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

39108:X 25 Aug 2024 23:56:30.187 * Sentinel new configuration saved on disk
39108:X 25 Aug 2024 23:56:30.188 * Sentinel ID is 229888b5a961c95ef6d862c1d0780e05089c47d4
39108:X 25 Aug 2024 23:56:30.188 # +monitor master mymaster 172.25.254.10 6379 quorum 2

其他两台也开启

测试

此时下线master

[root@redis-1 ~]# redis-cli 

127.0.0.1:6379> SHUTDOWN

此时查看可以看出master变成20主机了

也可以直接远程查看

[root@redis-1 ~]# ssh root@172.25.254.20

[root@redis-2 ~]# redis-cli 

127.0.0.1:6379> info replication

# Replication
role:master
connected_slaves:1
slave0:ip=172.25.254.30,port=6379,state=online,offset=27965,lag=1
master_failover_state:no-failover
master_replid:95f6ccb00cc14dd8ffa4bd5abcd101ecdd7c8ddc
master_replid2:3a07f24d1df7bba6de46f7ab1542cc57826e22cd
master_repl_offset:28247
second_repl_offset:7217
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2408
repl_backlog_histlen:25840

如果此时10主机上线,但他不会和20主机抢master,他会以slave的身份运行

/etc/redis/sentinel.conf 文件在用哨兵程序调用后会更改其配置文件,如果需要重新做需要删掉文 件重新编辑

由于哨兵的存在会把master提出去 当网络恢复后,master发现环境发生改变,master就会把自己的身份转换成slave master变成slave后会把网络故障那段时间写入自己中的数据清掉,这样数据就丢失了。

解决方法

在master上配置

root@redis-node2 ~]# redis-cli
127.0.0.1:6379> CONFIG GET min-slaves-to-write
1) "min-slaves-to-write"
2) "0"
127.0.0.1:6379> CONFIG set min-slaves-to-write 2
OK
127.0.0.1:6379> CONFIG GET min-slaves-to-write
1) "min-slaves-to-write"
2) "2"

#如果要永久保存写到配置文件中/etc/redis/6379.conf

Redis Cluster(无中心化设计)

配置,(需要6台虚拟机)

由于源码的redis压力大,需要6台,所以把源码安装的删除,直接安装自带版本的redis

删除

安装make和gcc,没装的都装

[root@redis-3 ~]# dnf install make -y

[root@redis-2 ~]# dnf install gcc -y

关闭服务,都关

[root@redis-1 ~]# /etc/init.d/redis_6379 stop

删除

[root@redis-1 ~]# cd redis-7.4.0/

[root@redis-1 redis-7.4.0]# make uninstall 

安装redis,6台都装

[root@redis-1 ~]# yum install redis -y

正在更新 Subscription Management 软件仓库。
无法读取客户身份

本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。

上次元数据过期检查:2:32:09 前,执行于 2024年08月25日 星期日 22时28分19秒。
依赖关系解决。
==============================================================
 软件包     架构        版本             仓库            大小
==============================================================
安装:
 redis      x86_64      6.2.7-1.el9      AppStream      1.3 M

事务概要
==============================================================
安装  1 软件包

总计:1.3 M
安装大小:4.7 M
下载软件包:
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                               1/1 
  运行脚本: redis-6.2.7-1.el9.x86_64                      1/1 
  安装    : redis-6.2.7-1.el9.x86_64                      1/1 
警告:/etc/redis/sentinel.conf 已建立为 /etc/redis/sentinel.conf.rpmnew 

  运行脚本: redis-6.2.7-1.el9.x86_64                      1/1 
  运行脚本: redis-6.2.7-1.el9.x86_64                                                                                                  1/1 
  验证    : redis-6.2.7-1.el9.x86_64                                                                                                  1/1 
已更新安装的产品。

已安装:
  redis-6.2.7-1.el9.x86_64                                                                                                                

完毕!

编辑主配置文件

[root@redis-1 ~]# vim /etc/redis/redis.conf 

开启所有端口

添加密码

去掉注释

启动服务

[root@redis-1 ~]# systemctl enable --now redis

Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.

其他5台主机也要配置,但可以直接复制过去

for i in 20 30 100 120 130 
> do 
> scp /etc/redis/redis.conf root@172.25.254.$i:/etc/redis/redis.conf
> done

启动服务,都启

[root@redis-1 ~]# systemctl enable --now redis 

创建redis-cluster

[root@redis-1 ~]# redis-cli --cluster create -a 123 \
> 172.25.254.10:6379 172.25.254.20:6379 172.25.254.30:6379 \
> 172.25.254.100:6379 172.25.254.120:6379 172.25.254.130: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 172.25.254.120:6379 to 172.25.254.10:6379
Adding replica 172.25.254.130:6379 to 172.25.254.20:6379
Adding replica 172.25.254.100:6379 to 172.25.254.30:6379
M: ef5807e605f5912940f0a4768e1d4e3faf8d6e68 172.25.254.10:6379
   slots:[0-5460] (5461 slots) master
M: e578b1dbfcfb7f6d4353d6979e6613fdd45b5c8b 172.25.254.20:6379
   slots:[5461-10922] (5462 slots) master
M: 69a6ec0ca69931e4bdd60a2aa2121379b25544a0 172.25.254.30:6379
   slots:[10923-16383] (5461 slots) master
S: 7788bd516ac32196488a80b6c81b7af6f52ce8d1 172.25.254.100:6379
   replicates 69a6ec0ca69931e4bdd60a2aa2121379b25544a0
S: 5c3ef33b311e7a9b5dcd22c7763230612516f583 172.25.254.120:6379
   replicates ef5807e605f5912940f0a4768e1d4e3faf8d6e68
S: 0c1067aa60160b87e0f5da5dc57c9dac2c62a77a 172.25.254.130:6379
   replicates e578b1dbfcfb7f6d4353d6979e6613fdd45b5c8b
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 172.25.254.10:6379)
M: ef5807e605f5912940f0a4768e1d4e3faf8d6e68 172.25.254.10:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 5c3ef33b311e7a9b5dcd22c7763230612516f583 172.25.254.120:6379
   slots: (0 slots) slave
   replicates ef5807e605f5912940f0a4768e1d4e3faf8d6e68
M: e578b1dbfcfb7f6d4353d6979e6613fdd45b5c8b 172.25.254.20:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 69a6ec0ca69931e4bdd60a2aa2121379b25544a0 172.25.254.30:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 0c1067aa60160b87e0f5da5dc57c9dac2c62a77a 172.25.254.130:6379
   slots: (0 slots) slave
   replicates e578b1dbfcfb7f6d4353d6979e6613fdd45b5c8b
S: 7788bd516ac32196488a80b6c81b7af6f52ce8d1 172.25.254.100:6379
   slots: (0 slots) slave
   replicates 69a6ec0ca69931e4bdd60a2aa2121379b25544a0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

检测redis集群信息

[root@redis-1 ~]# redis-cli -a 123 --cluster check 172.25.254.10:6379

写入数据,此时他提示要落在hash层上的5798

[root@redis-1 ~]# redis-cli -a 123
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> set name mqe
(error) MOVED 5798 172.25.254.20:6379

所以根据前面查看信息,可以看出要在20主机上才能写入

[root@redis-2 ~]# redis-cli -a 123

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

127.0.0.1:6379> set name mqw
OK

而且其他主机也不能查看数据

redis集群扩展和维护

添加多两台主机

安装redis(不演示)

直接传输已经修改好的配置文件传给40主机和140主机

[root@redis-1 ~]# scp /etc/redis/redis.conf root@172.25.254.40:/etc/redis/redis.conf

The authenticity of host '172.25.254.40 (172.25.254.40)' can't be established.
ED25519 key fingerprint is SHA256:r2X1s506k4aWuWLd/lJrXQZeanKuSr2fYbfqsqzHYCE.
This host key is known by the following other names/addresses:
    ~/.ssh/known_hosts:1: 172.25.254.20
    ~/.ssh/known_hosts:4: 172.25.254.30
    ~/.ssh/known_hosts:5: 172.25.254.100
    ~/.ssh/known_hosts:6: 172.25.254.120
    ~/.ssh/known_hosts:7: 172.25.254.130
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '172.25.254.40' (ED25519) to the list of known hosts.
root@172.25.254.40's password: 
redis.conf                                                                                              100%   92KB  24.1MB/s   00:00  


[root@redis-1 ~]# scp /etc/redis/redis.conf root@172.25.254.140:/etc/redis/redis.conf

The authenticity of host '172.25.254.40 (172.25.254.40)' can't be established.
ED25519 key fingerprint is SHA256:r2X1s506k4aWuWLd/lJrXQZeanKuSr2fYbfqsqzHYCE.
This host key is known by the following other names/addresses:
    ~/.ssh/known_hosts:1: 172.25.254.20
    ~/.ssh/known_hosts:4: 172.25.254.30
    ~/.ssh/known_hosts:5: 172.25.254.100
    ~/.ssh/known_hosts:6: 172.25.254.120
    ~/.ssh/known_hosts:7: 172.25.254.130
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '172.25.254.40' (ED25519) to the list of known hosts.
root@172.25.254.40's password: 
redis.conf                                                                                              100%   92KB  24.1MB/s   00:00  

启动服务

[root@redis-4 ~]# systemctl enable --now redis

把40主机加到集群里

[root@redis-1 ~]# redis-cli -a 123 --cluster add-node 172.25.254.40:6379 172.25.254.10:6379


Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 172.25.254.40:6379 to cluster 172.25.254.10:6379
>>> Performing Cluster Check (using node 172.25.254.10:6379)
M: ef5807e605f5912940f0a4768e1d4e3faf8d6e68 172.25.254.10:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 5c3ef33b311e7a9b5dcd22c7763230612516f583 172.25.254.120:6379
   slots: (0 slots) slave
   replicates ef5807e605f5912940f0a4768e1d4e3faf8d6e68
M: e578b1dbfcfb7f6d4353d6979e6613fdd45b5c8b 172.25.254.20:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 69a6ec0ca69931e4bdd60a2aa2121379b25544a0 172.25.254.30:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 0c1067aa60160b87e0f5da5dc57c9dac2c62a77a 172.25.254.130:6379
   slots: (0 slots) slave
   replicates e578b1dbfcfb7f6d4353d6979e6613fdd45b5c8b
S: 7788bd516ac32196488a80b6c81b7af6f52ce8d1 172.25.254.100:6379
   slots: (0 slots) slave
   replicates 69a6ec0ca69931e4bdd60a2aa2121379b25544a0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 172.25.254.40:6379 to make it join the cluster.
[OK] New node added correctly.

给40主机分享槽位,使他能能存数据(id为信息里那后面的一大串数字)

[root@redis-1 ~]# redis-cli -a 123 --cluster reshard 172.25.254.10:6379

给40主机添加slave

[root@redis-1 ~]# redis-cli -a 123 --cluster add-node 172.25.254.140:6379 172.25.254.10:6379 --cluster-slave --cluster-master-id 3a30b7f81fcb659f7d49be767a87c88f15461d59


Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 172.25.254.140:6379 to cluster 172.25.254.10:6379
>>> Performing Cluster Check (using node 172.25.254.10:6379)
M: ef5807e605f5912940f0a4768e1d4e3faf8d6e68 172.25.254.10:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
M: 3a30b7f81fcb659f7d49be767a87c88f15461d59 172.25.254.40:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
S: 5c3ef33b311e7a9b5dcd22c7763230612516f583 172.25.254.120:6379
   slots: (0 slots) slave
   replicates ef5807e605f5912940f0a4768e1d4e3faf8d6e68
M: e578b1dbfcfb7f6d4353d6979e6613fdd45b5c8b 172.25.254.20:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
M: 69a6ec0ca69931e4bdd60a2aa2121379b25544a0 172.25.254.30:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 0c1067aa60160b87e0f5da5dc57c9dac2c62a77a 172.25.254.130:6379
   slots: (0 slots) slave
   replicates e578b1dbfcfb7f6d4353d6979e6613fdd45b5c8b
S: 7788bd516ac32196488a80b6c81b7af6f52ce8d1 172.25.254.100:6379
   slots: (0 slots) slave
   replicates 69a6ec0ca69931e4bdd60a2aa2121379b25544a0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 172.25.254.140:6379 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 172.25.254.40:6379.
[OK] New node added correctly.

查看

[root@redis-2 ~]# redis-cli -a 123 --cluster check 172.25.254.20:6379


Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.25.254.20:6379 (e578b1db...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.10:6379 (ef5807e6...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.30:6379 (69a6ec0c...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.40:6379 (3a30b7f8...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.25.254.20:6379)
M: e578b1dbfcfb7f6d4353d6979e6613fdd45b5c8b 172.25.254.20:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 29ccd24f84087345db917d6e0b8c7e816f93d9a8 172.25.254.140:6379
   slots: (0 slots) slave
   replicates 3a30b7f81fcb659f7d49be767a87c88f15461d59
S: 0c1067aa60160b87e0f5da5dc57c9dac2c62a77a 172.25.254.130:6379
   slots: (0 slots) slave
   replicates e578b1dbfcfb7f6d4353d6979e6613fdd45b5c8b
M: ef5807e605f5912940f0a4768e1d4e3faf8d6e68 172.25.254.10:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
M: 69a6ec0ca69931e4bdd60a2aa2121379b25544a0 172.25.254.30:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 5c3ef33b311e7a9b5dcd22c7763230612516f583 172.25.254.120:6379
   slots: (0 slots) slave
   replicates ef5807e605f5912940f0a4768e1d4e3faf8d6e68
M: 3a30b7f81fcb659f7d49be767a87c88f15461d59 172.25.254.40:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 additional replica(s)
S: 7788bd516ac32196488a80b6c81b7af6f52ce8d1 172.25.254.100:6379
   slots: (0 slots) slave
   replicates 69a6ec0ca69931e4bdd60a2aa2121379b25544a0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

维护

移除40的slave

[root@redis-1 ~]# redis-cli -a 123 --cluster del-node 172.25.254.140:6379 29ccd24f84087345db917d6e0b8c7e816f93d9a8


Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 29ccd24f84087345db917d6e0b8c7e816f93d9a8 from cluster 172.25.254.140:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
[root@redis-1 ~]# 

移除40主机的槽位

[root@redis-1 ~]# redis-cli -a 123 --cluster reshard 172.25.254.10:6379

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 172.25.254.10:6379)
M: ef5807e605f5912940f0a4768e1d4e3faf8d6e68 172.25.254.10:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
M: 3a30b7f81fcb659f7d49be767a87c88f15461d59 172.25.254.40:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
S: 5c3ef33b311e7a9b5dcd22c7763230612516f583 172.25.254.120:6379
   slots: (0 slots) slave
   replicates ef5807e605f5912940f0a4768e1d4e3faf8d6e68
M: e578b1dbfcfb7f6d4353d6979e6613fdd45b5c8b 172.25.254.20:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
M: 69a6ec0ca69931e4bdd60a2aa2121379b25544a0 172.25.254.30:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 0c1067aa60160b87e0f5da5dc57c9dac2c62a77a 172.25.254.130:6379
   slots: (0 slots) slave
   replicates e578b1dbfcfb7f6d4353d6979e6613fdd45b5c8b
S: 7788bd516ac32196488a80b6c81b7af6f52ce8d1 172.25.254.100:6379
   slots: (0 slots) slave
   replicates 69a6ec0ca69931e4bdd60a2aa2121379b25544a0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID? ef5807e605f5912940f0a4768e1d4e3faf8d6e68
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1: 3a30b7f81fcb659f7d49be767a87c88f15461d59
Source node #2: done

移除40主机

[root@redis-1 ~]# redis-cli -a 123 --cluster del-node 172.25.254.40:6379 3a30b7f81fcb659f7d49be767a87c88f15461d59

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 3a30b7f81fcb659f7d49be767a87c88f15461d59 from cluster 172.25.254.40:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.

查看,此时40主机已经从集群里移除

[root@redis-2 ~]# redis-cli -a 123 --cluster check 172.25.254.20:6379

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
172.25.254.20:6379 (e578b1db...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.10:6379 (ef5807e6...) -> 1 keys | 8192 slots | 1 slaves.
172.25.254.30:6379 (69a6ec0c...) -> 0 keys | 4096 slots | 1 slaves.
[OK] 1 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.25.254.20:6379)
M: e578b1dbfcfb7f6d4353d6979e6613fdd45b5c8b 172.25.254.20:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 0c1067aa60160b87e0f5da5dc57c9dac2c62a77a 172.25.254.130:6379
   slots: (0 slots) slave
   replicates e578b1dbfcfb7f6d4353d6979e6613fdd45b5c8b
M: ef5807e605f5912940f0a4768e1d4e3faf8d6e68 172.25.254.10:6379
   slots:[0-6826],[10923-12287] (8192 slots) master
   1 additional replica(s)
M: 69a6ec0ca69931e4bdd60a2aa2121379b25544a0 172.25.254.30:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 5c3ef33b311e7a9b5dcd22c7763230612516f583 172.25.254.120:6379
   slots: (0 slots) slave
   replicates ef5807e605f5912940f0a4768e1d4e3faf8d6e68
S: 7788bd516ac32196488a80b6c81b7af6f52ce8d1 172.25.254.100:6379
   slots: (0 slots) slave
   replicates 69a6ec0ca69931e4bdd60a2aa2121379b25544a0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值