企业级NoSql数据库Redis集群

关系型数据库和NoSQL数据库

定义

关系型数据库,是建立在关系模型基础上的数据库,其借助于集合代数等数学概念和方法来处理数据库中的数据主流的 MySQL、Oracle、MS SQL Server 和 DB2 都属于这类传统数据库。

NoSQL 数据库,全称为 Not Only SQL,意思就是适用关系型数据库的时候就使用关系型数据库,不适用的时候也没有必要非使用关系型数据库不可,可以考虑使用更加合适的数据存储。主要分为临时性键值存储(memcached、Redis)、永久性键值存储(ROMA、Redis)、面向文档的数据库(MongoDB、CouchDB)、面向列的数据库(Cassandra、HBase),每种 NoSQL 都有其特有的使用场景及优点。

为什么还要用NoSQL数据库

主要是由于随着互联网发展,数据量越来越大,对性能要求越来越高,传统数据库存在着先天性的缺陷,即单机(单库)性能瓶颈,并且扩展困难。这样既有单机单库瓶颈,却又扩展困难,自然无法满足日益增长的海量数据存储及其性能要求,所以才会出现了各种不同的 NoSQL 产品,NoSQL 根本性的优势在于在云计算时代,简单、易于大规模分布式扩展,并且读写性能非常高。

关系型数据库(RDBMS)和NoSQL的特点及其优缺点

关系型数据库:

特点:

  • 数据关系模型基于关系模型,结构化存储,完整性约束
  • 基于二维表及其之间的联系,需要连接、并、交、差、除等数据操作
  • 采用结构化的查询语言(SQL)做数据读写
  • 操作需要数据的一致性,需要事务甚至是强一致性

优点:

  • 保持数据的一致性(事务处理)
  • 可以进行join等复杂查询
  • 通用化,技术成熟

缺点:

  • 数据读写必须经过sql解析,大量数据、高并发下读写性能不足
  • 对数据做读写,或修改数据结构时需要加锁,影响并发操作
  • 无法适应非结构化存储
  • 扩展困难
  • 昂贵、复杂

NoSQL数据库

特点:

  • 非结构化的存储
  • 基于多维关系模型
  • 具有特有的使用场景

优点:

  • 高并发,大数据下读写能力较强
  • 基本支持分布式,易于扩展,可伸缩
  • 简单,弱结构化存储

缺点:

  • join等复杂操作能力较弱
  • 事务支持较弱
  • 通用性差
  • 无完整约束复杂业务场景支持较差

Remote Dictionary Server简介

什么是Redis

Redis (Remote Dictionary Server)

Redis,英文全称是Remote Dictionary Server(远程字典服务),是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。

与MySQL数据库不同的是,Redis的数据是存在内存中的。它的读写速度非常快,每秒可以处理超过10万次读写操作。因此redis被广泛应用于缓存,另外,Redis也经常用来做分布式锁。除此之外,Redis支持事务、持久化、LUA 脚本、LRU 驱动事件、多种集群方案。

在2009年发布,开发者是意大利的萨尔瓦多·桑菲利波普(Salvatore Sanfilippo),他本想为自己的公司开发一个用于替换MySQL的产品Redis,但是没有想到他把Redis开源后大受欢迎,短短几年,Redis就有了很大的用户群体,目前国内外使用的公司众多,比如:阿里,百度,新浪微博,知乎网,GitHub,Twitter 等。

Redis是一个开源的、遵循BSD协议的、基于内存的而且目前比较流行的键值数据库(key-valuedatabase),是一个非关系型数据库,redis 提供将内存通过网络远程共享的一种服务,提供类似功能的还有memcached,但相比memcached,redis还提供了易扩展、高性能、具备数据持久性等功能。

Redis 在高并发、低延迟环境要求比较高的环境使用量非常广泛。

Redis特性
  • 速度快: 10W QPS,基于内存,C语言实现

  • 单线程

  • 持久化

  • 支持多种数据结构

  • 支持多种编程语言

  • 功能丰富: 支持Lua脚本,发布订阅,事务,pipeline等功能

  • 简单: 代码短小精悍(单机核心代码只有23000行左右),单线程开发容易,不依赖外部库,使用简单

  • 主从复制

  • 支持高可用和分布式

单线程为何如此快

  • 纯内存
  • 非阻塞
  • 避免线程切换和竞态消耗

在多线程环境中,操作系统需要在不同线程之间进行切换,这种切换会带来一定的时间开销。而单线程不存在线程切换的情况,从而节省了这部分时间。例如,在处理一个连续的任务序列时,单线程可以专注地从头到尾执行任务,无需中断去切换到其他线程。

  1. 内存访问:
    • 多线程同时运行时,可能会竞争内存资源,导致出现缓存一致性问题和内存总线的争用。这会使得内存访问速度变慢,因为处理器需要花费额外的时间来协调对内存的访问。
    • 单线程则不存在这种竞争,能够直接、连续地访问内存,提高了内存访问的效率。
  2. 数据同步:
    • 多线程在访问共享数据时,通常需要使用同步机制,如锁、信号量等,以确保数据的一致性。这些同步机制会引入额外的开销,包括获取和释放锁的时间、等待信号量的时间等。
    • 单线程无需进行数据同步操作,避免了这些开销。
Redis应用场景
  • Session 共享:常见于web集群中的Tomcat或者PHP中多web服务器session共享

  • 缓存:数据查询、电商网站商品信息、新闻内容

  • 计数器:访问排行榜、商品浏览数等和次数相关的数值统计场景

  • 微博/微信社交场合:共同好友,粉丝数,关注,点赞评论等

  • 消息队列:ELK的日志缓存、部分业务的订阅发布系统

  • 地理位置: 基于GEO(地理信息定位),实现摇一摇,附近的人,外卖等功能

Redis的安装

官方下载地址:http://download.redis.io/releases/

rpm方式安装
[root@redis-node1 ~]# yum install redis -y
源码安装
[root@redis-node1 ~]# tar zxf redis-7.4.0.tar.gz
[root@redis-node1 ~]# ls
redis-7.4.0 redis-7.4.0.tar.gz

#安装编译工具
[root@redis-node1 redis-7.4.0]# dnf install make gcc initscripts-10.11.6-1.el9.x86_64 -y

#执行编译命令
[root@redis-node1 redis-7.4.0]# make
[root@redis-node1 redis-7.4.0]# make install

#启动Redis
[root@redis-node1 redis-7.4.0]# cd utils/
[root@redis-node1 utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
This systems seems to use systemd. #提示系统使用的是systemd的初始化方式
Please take a look at the provided example service unit files in this directory,
and adapt and install them. Sorry!

#解决报错问题
[root@redis-node1 utils]# vim install_server.sh 
#bail if this system is managed by systemd
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
# echo "This systems seems to use systemd."
# echo "Please take a look at the provided example service unit files in
this directory, and adapt and install them. Sorry!"
# exit 1
#fi
[root@redis-node1 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-node1 utils]# vim /etc/redis/6379.conf
bind * -::*
[root@redis-node1 utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...

[root@redis-node1 utils]# netstat -antlpe | grep redis
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 0
67267 38417/redis-server
tcp6 0 0 :::6379

#查看信息
[root@redis-node1 utils]# redis-cli
127.0.0.1:6379> info

Redis的基本操作

命令说明
config get *查看配置
select 1选择数据库
flushdb清空当前数据库
flushall清空所有数据库
move key 1移动key
del key删除
rename oldkey newkey改名
expire key 10设置过期时间
persist key设置持久化
keys user*查询
exists key判断是否存在

Redis主从复制

环境部署

主机角色IP
redis-node1master172.25.254.10
redis-node2slave172.25.254.20
redis-node3slave172.25.254.30
# 在三台主机上关闭protected模式
# node2和node3与node1上一样
[root@redis-node1 ~]# vim /etc/redis/6379.conf
protected-mode no #关闭protected模式

[root@redis-node1 ~]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...

# 配置slave节点
# node3和node2一样的操作
[root@redis-node2 ~]# vim /etc/redis/6379.conf
replicaof 172.25.254.10 6379
[root@redis-node2 ~]# /etc/init.d/redis_6379 restart
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...

# 测试效果
#在mastser节点
[root@redis-node1 ~]# redis-cli
127.0.0.1:6379> set name lee
OK

#在slave节点查看
[root@redis-node2 ~]# redis-cli
127.0.0.1:6379> get name
"lee"

[root@redis-node3 ~]# redis-cli 
127.0.0.1:6379> get name
"lee"

主从复制过程

在这里插入图片描述

redis的主从复制分为三个阶段:

1)Slave连接Master**(建立连接阶段)**

2)Master同步数据到Slave**(数据同步阶段)**

3)期间Master接到来自客户端"写"的命令之后需要将数据同步到Slave**(命令传播阶段)**

1. 建立连接阶段

在主从配置的建立连接阶段Master与Slave之间会做如下操作:

1)Slave发送Slaveof Masterhost Masterport命令连接Master

2)Master接到来自Slave的连接,并开始响应对方。

3)Slave得到响应之后将Masterhost与Masterport及一些其他的Master信息保存到Slave端。

4)Slave确保连接无误后开始创建socket通道,用于后续的数据复制工作。

5)Slave与Master之间周期性的发送ping心跳,检查Slave与Master之间是否通信正常。

6)Master接收到Slave的ping心跳后会给对应的Slave响应pong。ping/pong机制

7)Slave发送本机设置的Master密码Masterauth来到Master进行验证(Master有设置密码的情况下)。

8)Master进行身份识别,如果认证错误,尝试重新连接。Slave服务器端报如下错误:
Master aborted replication with an error: NOAUTH Authentication required.

9)Master身份识别成功后,Slave会将自己的ip、端口等信息发送到Master,Master将保存此Slave的ip、端口以及一些其他状态信息,记录在info Replication。

2. 数据同步阶段
在这里插入图片描述
在这里插入图片描述

  • slave节点发送同步亲求到master节点

  • slave节点通过master节点的认证开始进行同步

  • master节点会开启bgsave进程发送内存rbd到slave节点,在此过程中是异步操作,也就是说,master节点仍然可以进行写入动作

  • slave节点收到rdb后首先清空自己的所有数据

  • slave节点加载rdb并进行数据恢复

  • 在master和slave同步过程中master还会开启新的bgsave进程把没有同步的数据进行缓存

  • 然后通过自有的replactionfeedslave函数把未通过内存快照发动到slave的数据一条一条写入到slave中

Redis的哨兵(高可用)

在这里插入图片描述
Sentinel 进程是用于监控redis集群中Master主服务器工作的状态,在Master主服务器发生故障的时候,可以实现Master和Slave服务器的切换,保证系统的高可用,此功能在redis2.6+的版本已引用,Redis的哨兵模式到了2.8版本之后就稳定了下来。一般在生产环境也建议使用Redis的2.8版本的以后版本。

每个哨兵(Sentinel)进程会向其它哨兵(Sentinel)、Master、Slave定时发送消息,以确认对方是否”活”着,如果发现对方在指定配置时间(此项可配置)内未得到回应,则暂时认为对方已离线,也就是所谓的”主观认为宕机” (主观:是每个成员都具有的独自的而且可能相同也可能不同的意识),英文名称:Subjective Down,简称SDOWN。

有主观宕机,对应的有客观宕机。当“哨兵群”中的多数Sentinel进程在对Master主服务器做出SDOWN 的判断,并且通过 SENTINEL is-master-down-by-addr 命令互相交流之后,得出的Master Server下线判断,这种方式就是“客观宕机”(客观:是不依赖于某种意识而已经实际存在的一切事物),英文名称是:Objectively Down, 简称 ODOWN。

通过一定的vote算法,从剩下的slave从服务器节点中,选一台提升为Master服务器节点,然后自动修改相关配置,并开启故障转移(failover)

Sentinel 机制可以解决master和slave角色的自动切换问题,但单个 Master 的性能瓶颈问题无法解决,类似于MySQL中的MHA功能。

Redis Sentinel中的Sentinel节点个数应该为大于等于3且最好为奇数。

sentinel中的三个定时任务

  • 每10秒每个sentinel对master和slave执行info

    • 发现slave节点
    • 确认主从关系
  • 每2秒每个sentinel通过master节点的channel交换信息(pub/sub)

    • 通过sentinel__:hello频道交互
    • 交互对节点的“看法”和自身信息
  • 每1秒每个sentinel对其他sentinel和redis执行ping

哨兵机制

主要功能:

  1. 故障检测: 哨兵定期检查主服务器(master)和从服务器(slaves)是否正常工作。它通过发送命令并等待响应来检测服务器是否在线且运行正常。如果某个服务器未能正确响应,哨兵会认为该服务器是处于下线状态。

  2. 自动故障转移: 如果主服务器发生故障,哨兵会自动进行故障转移操作。它会从现有的从服务器中选举出一个新的主服务器,并指示其他从服务器改变同步方向,同步新的主服务器的数据。这样能确保系统的快速恢复和数据的最大程度保存。

  3. 配置提供者: 哨兵还充当配置提供者的角色。客户端可以询问哨兵当前哪个是主服务器,从而连接到正确的服务器进行读写操作。

  4. 通知: 哨兵可以在检测到故障或其他重要事件时通过API向管理员或其他应用发送通知。

哨兵的工作原理:

  1. 监控: 哨兵会监控所有的 Redis 主从服务器实例,检查它们的健康状态。
  2. 选举: 如果主服务器无法响应,哨兵之间会进行选举,决定哪一个哨兵负责执行自动故障转移。
  3. 故障转移: 负责故障转移的哨兵会自动选择一个从服务器,提升为新的主服务器,并配置其他从服务器重新指向这个新的主服务器。
哨兵的实验过程

在所有node节点中关闭 protected-mode no

# 在master节点
#编辑配置文件
[root@redis-node1 ~]# cd redis-7.4.0/
[root@redis-node1 redis-7.4.0]# cp sentinel.conf /etc/redis/
[root@redis-node1 redis-7.4.0]# vim /etc/redis/sentinel.conf
protected-mode no	#关闭保护模式
port 26379			#监听端口
daemonize no		#进入不打入后台
pidfile /var/run/redis-sentinel.pid		#sentinel进程pid文件
loglevel notice		#日志级别
sentinel monitor mymaster 172.25.254.10 6379 2		#创建sentinel监控监控master主机,2表示必须得到2票
sentinel down-after-milliseconds mymaster 10000		#master中断时长,10秒连不上视为master下线
sentinel parallel-syncs mymaster 1		#发生故障转移后,同时开始同步新master数据的slave数量
sentinel failover-timeout mymaster 180000		#整个故障切换的超时时间为3分钟

####复制配置文件到其他节点
[root@redis-node1 redis-7.4.0]# scp /etc/redis/sentinel.conf root@172.25.254.20:/etc/redis/sentinel.conf
root@172.25.254.20's password: 
sentinel.conf                                                 100%   14KB  16.3MB/s   00:00    
[root@redis-node1 redis-7.4.0]# scp /etc/redis/sentinel.conf root@172.25.254.30:/etc/redis/sentinel.conf
root@172.25.254.30's password: 
sentinel.conf                                                 100%   14KB  16.6MB/s   00:00    
[root@redis-node1 redis-7.4.0]#
[root@redis-node1 redis-7.4.0]# cd /etc/redis/
[root@redis-node1 redis]# cp sentinel.conf sentinel.conf.bak   #备份,哨兵模式会更改配置文件的内容,node2和node3都需要备份

# 启动服务
[root@redis-node1 redis]# redis-sentinel /etc/redis/sentinel.conf
38910:X 26 Aug 2024 18:18:12.468 # 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.
38910:X 26 Aug 2024 18:18:12.468 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
38910:X 26 Aug 2024 18:18:12.468 * Redis version=7.4.0, bits=64, commit=00000000, modified=0, pid=38910, just started
38910:X 26 Aug 2024 18:18:12.468 * Configuration loaded
38910:X 26 Aug 2024 18:18:12.468 * Increased maximum number of open files to 10032 (it was originally set to 1024).
38910:X 26 Aug 2024 18:18:12.468 * monotonic clock: POSIX clock_gettime
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis Community Edition      
  .-`` .-```.  ```\/    _.,_ ''-._     7.4.0 (00000000/0) 64 bit
 (    '      ,       .-`  | `,    )     Running in sentinel mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 26379
 |    `-._   `._    /     _.-'    |     PID: 38910
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

38910:X 26 Aug 2024 18:18:12.470 * Sentinel new configuration saved on disk
38910:X 26 Aug 2024 18:18:12.470 * Sentinel ID is 61e179d420bf67b09e7fc4ea881e8318477d6da1
38910:X 26 Aug 2024 18:18:12.470 # +monitor master mymaster 172.25.254.10 6379 quorum 2
38910:X 26 Aug 2024 18:18:12.471 * +slave slave 172.25.254.30:6379 172.25.254.30 6379 @ mymaster 172.25.254.10 6379
38910:X 26 Aug 2024 18:18:12.472 * Sentinel new configuration saved on disk
38910:X 26 Aug 2024 18:18:12.472 * +slave slave 172.25.254.20:6379 172.25.254.20 6379 @ mymaster 172.25.254.10 6379
38910:X 26 Aug 2024 18:18:12.473 * Sentinel new configuration saved on disk

测试

# 在开一个master节点终端
[root@redis-node1 ~]# redis-cli
127.0.0.1:6379> SHUTDOWN

# node2成为master
[root@redis-node2 redis]# redis-cli 
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1  # node1下线了所以只有一个slave
slave0:ip=172.25.254.30,port=6379,state=online,offset=138247,lag=0
master_failover_state:no-failover
master_replid:135d57f58355cb30633ae745a391f5e2897a6fa0
master_replid2:5e78b73f8b9b2361091372e1165a2bb41637b230
master_repl_offset:138247
second_repl_offset:125564
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:138233

# 将node1上线
[root@redis-node1 redis]# /etc/init.d/redis_6379 start 
Starting Redis server...

[root@redis-node2 redis]# redis-cli 
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2  # node1也成为slave
slave0:ip=172.25.254.30,port=6379,state=online,offset=154794,lag=0
slave1:ip=172.25.254.10,port=6379,state=online,offset=154794,lag=1
master_failover_state:no-failover
master_replid:135d57f58355cb30633ae745a391f5e2897a6fa0
master_replid2:5e78b73f8b9b2361091372e1165a2bb41637b230
master_repl_offset:154794
second_repl_offset:125564
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:154780
127.0.0.1:6379> 
在整个架构中可能会出现的问题

问题:

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

解决:

master在被写入数据时会持续连接slave,mater确保有2个slave可以写入我才允许写入,如果slave数量少于2个便拒绝写入

# 在master中设定,现在node2为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(无中心化设计)

Redis Cluster工作原理

在哨兵sentinel机制中,可以解决redis高可用问题,即当master故障后可以自动将slave提升为master,从而可以保证redis服务的正常使用,但是无法解决redis单机写入的瓶颈问题,即单机redis写入性能受限于单机的内存大小、并发数量、网卡速率等因素。

redis 3.0版本之后推出了无中心架构的redis cluster机制,在无中心的redis集群当中,其每个节点保存当前节点数据和整个集群状态,每个节点都和其他所有节点连接

Redis Cluster特点
  1. 所有Redis节点使用(PING机制)互联
  2. 集群中某个节点的是否失效,是由整个集群中超过半数的节点监测都失效,才能算真正的失效
  3. 客户端不需要proxy即可直接连接redis,应用程序中需要配置有全部的redis服务器IP
  4. redis cluster把所有的redis node 平均映射到 0-16383个槽位(slot)上,读写需要到指定的redis node上进行操作,因此有多少个redis node相当于redis 并发扩展了多少倍,每个redis node 承担16384/N个槽位
  5. Redis cluster预先分配16384个(slot)槽位,当需要在redis集群中写入一个key -value的时候,会使用CRC16(key) mod 16384之后的值,决定将key写入值哪一个槽位从而决定写入哪一个Redis节点上,从而有效解决单机瓶颈。
Redis Cluster架构

在这里插入图片描述
假如三个主节点分别是:A, B, C 三个节点,采用哈希槽 (hash slot)的方式来分配16384个slot 的话它们三个节点分别承担的slot 区间可以是:

节点A覆盖 0-5460
节点B覆盖 5461-10922
节点C覆盖 10923-16383

在这里插入图片描述

Redis Cluster主从架构

Redis cluster的架构虽然解决了并发的问题,但是又引入了一个新的问题,每个Redis master的高可用如何解决?

那就是对每个master 节点都实现主从复制,从而实现 redis 高可用性
在这里插入图片描述
Redis Cluster部署架构说明
在这里插入图片描述

创建Redis Cluster的前提

  1. 每个redis node节点采用相同的硬件配置、相同的密码、相同的redis版本。

  2. 每个节点必须开启的参数

    • cluster-enabled yes #必须开启集群状态,开启后redis进程会有cluster显示
    • cluster-config-file nodes-6380.conf #此文件有redis cluster集群自动创建和维护,不需要任何手动操作
  3. 所有redis服务器必须没有任何数据

  4. 先启动为单机redis且没有任何key value

部署Redis Cluster

实验环境:

六台虚拟机,纯净实验环境,构建三主三从的架构。IP分别为:172.25.254.10,172.25.254.20,172.25.254.30,172.25.254.110,172.25.254.120,172.25.254.130

# 实验rpm安装 redis ,六台都要安装 
[root@redis-node1 ~]# yum install redis -y
[root@redis-node1 ~]# vim /etc/redis/redis.conf
masterauth "123456"		#集群主从认证
requirepass "123456"	#redis登陆密码 redis-cli 命令连接redis后要用“auth 密码”进行认证
cluster-enabled yes 	#开启cluster集群功能
cluster-config-file nodes-6379.conf 	#指定集群配置文件
cluster-node-timeout 15000 	#节点加入集群的超时时间单位是ms
# 六台主机同样的操作,但是为了方便可以直接将配置文件复制到其他5台节点
[root@redis-node1 ~]# for i in 20 30 110 120 130
> do
> scp /etc/redis/redis.conf root@172.25.254.$i:/etc/redis/redis.conf
> done
# 启动服务
[root@redis-node1 ~]# systemctl enable --now redis
创建Redis-Cluster
[root@redis-node1 ~]# redis-cli --cluster create -a 123456 \
> 172.25.254.10:6379 172.25.254.20:6379 172.25.254.30:6379 \
> 172.25.254.110:6379 172.25.254.120:6379 172.25.254.130:6379 \
> --cluster-replicas 1
>>> 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.110:6379 to 172.25.254.30:6379
M: 5ab2e93f4f0783983676f7bd118efaacfb202bd1 172.25.254.10:6379
slots:[0-5460] (5461 slots) master
M: ba504e78f14df5944280f9035543277a0cf5976b 172.25.254.20:6379
slots:[5461-10922] (5462 slots) master
M: 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7 172.25.254.30:6379
slots:[10923-16383] (5461 slots) master
S: c20c9b5465b2e64868161c0e285d55bc81358ba4 172.25.254.110:6379
replicates 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7
S: d458f34fa900d83212c021dc1e65396e490b5495 172.25.254.120:6379
replicates 5ab2e93f4f0783983676f7bd118efaacfb202bd1
S: 83d7a82fe896cf9f4d8212cb533058659bba16ce 172.25.254.130:6379
replicates ba504e78f14df5944280f9035543277a0cf5976b
Can I set the above configuration? (type 'yes' to accept): yes

>>> Performing Cluster Check (using node 172.25.254.10:6379)
M: 5ab2e93f4f0783983676f7bd118efaacfb202bd1 172.25.254.10:6379
	slots:[0-5460] (5461 slots) master
	1 additional replica(s)
S: c20c9b5465b2e64868161c0e285d55bc81358ba4 172.25.254.110:6379
	slots: (0 slots) slave
	replicates 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7
M: ba504e78f14df5944280f9035543277a0cf5976b 172.25.254.20:6379
	slots:[5461-10922] (5462 slots) master
	1 additional replica(s)
S: 83d7a82fe896cf9f4d8212cb533058659bba16ce 172.25.254.130:6379
	slots: (0 slots) slave
	replicates ba504e78f14df5944280f9035543277a0cf5976b
M: 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7 172.25.254.30:6379
	slots:[10923-16383] (5461 slots) master
	1 additional replica(s)
S: d458f34fa900d83212c021dc1e65396e490b5495 172.25.254.120:6379
	slots: (0 slots) slave
	replicates 5ab2e93f4f0783983676f7bd118efaacfb202bd1
[OK] All nodes agree about slots configuration.
>>> Check for open slots... 	#检查打开的哈希槽位
>>> Check slots coverage... 	#检查槽位覆盖范围
[OK] All 16384 slots covered. 	#所有槽位分配完成

# 配置文件位置
[root@redis-node1 ~]# ll /var/lib/redis/nodes-6379.conf
检测Redis集群状态
# 查看集群状态
[root@redis-node1 ~]# redis-cli -a 123456 --cluster info 172.25.254.10:6379
Warning: Using a password with '-a' or '-u' option on the command line interface
may not be safe.
172.25.254.10:6379 (5ab2e93f...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.20:6379 (ba504e78...) -> 0 keys | 5462 slots | 1 slaves.
172.25.254.30:6379 (1fcaeb1d...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

[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:821
cluster_stats_messages_pong_sent:801
cluster_stats_messages_sent:1622
cluster_stats_messages_ping_received:796
cluster_stats_messages_pong_received:821
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1622

# 检测集群
[root@redis-node1 ~]# redis-cli -a 123456 --cluster check 172.25.254.10:6379
Warning: Using a password with '-a' or '-u' option on the command line interface
may not be safe.
172.25.254.10:6379 (5ab2e93f...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.20:6379 (ba504e78...) -> 0 keys | 5462 slots | 1 slaves.
172.25.254.30:6379 (1fcaeb1d...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.25.254.10:6379)
M: 5ab2e93f4f0783983676f7bd118efaacfb202bd1 172.25.254.10:6379
	slots:[0-5460] (5461 slots) master
	1 additional replica(s)
S: c20c9b5465b2e64868161c0e285d55bc81358ba4 172.25.254.110:6379
	slots: (0 slots) slave
	replicates 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7
M: ba504e78f14df5944280f9035543277a0cf5976b 172.25.254.20:6379
	slots:[5461-10922] (5462 slots) master
	1 additional replica(s)
S: 83d7a82fe896cf9f4d8212cb533058659bba16ce 172.25.254.130:6379
	slots: (0 slots) slave
	replicates ba504e78f14df5944280f9035543277a0cf5976b
M: 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7 172.25.254.30:6379
	slots:[10923-16383] (5461 slots) master
	1 additional replica(s)
S: d458f34fa900d83212c021dc1e65396e490b5495 172.25.254.120:6379
	slots: (0 slots) slave
	replicates 5ab2e93f4f0783983676f7bd118efaacfb202bd1
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

# 写入数据
[root@redis-node1 ~]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface
may not be safe.
127.0.0.1:6379> set key1 value1 	#被分配到20的hash槽位上
(error) MOVED 9189 172.25.254.20:6379

[root@redis-master2 ~]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface
may not be safe.
127.0.0.1:6379> set key1 value1
OK

集群扩容

实验环境

再准备两台虚拟机,将IP为172.25.254.50、172.25.254.150并将其添加到集群里。

#添加master
[root@redis-node1 ~]# redis-cli -a 123456 --cluster add-node 172.25.254.50:6379 172.25.254.10:6379

# 分享槽位给50 ,使其能够分享数据。
[root@redis-node1 ~]# redis-cli -a 123456 --cluster reshard 172.25.254.10:6379
# 要注意receiving node 要填50的id,source node 要填all

# 添加slave
[root@redis-node1 ~]# redis-cli -a 123456 --cluster add-node 172.25.254.150:6379 172.25.254.10:6379 --cluster-slave --cluster-master-id 009571cb206a89afa6658b60b2d403136056ac09

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: 5ab2e93f4f0783983676f7bd118efaacfb202bd1 172.25.254.10:6379
	slots:[1365-5460] (4096 slots) master
	1 additional replica(s)
S: c20c9b5465b2e64868161c0e285d55bc81358ba4 172.25.254.110:6379
	slots: (0 slots) slave
	replicates 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7
M: 009571cb206a89afa6658b60b2d403136056ac09 172.25.254.40:6379
	slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
M: ba504e78f14df5944280f9035543277a0cf5976b 172.25.254.20:6379
	slots:[6827-10922] (4096 slots) master
	1 additional replica(s)
S: 83d7a82fe896cf9f4d8212cb533058659bba16ce 172.25.254.130:6379
	slots: (0 slots) slave
	replicates ba504e78f14df5944280f9035543277a0cf5976b
M: 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7 172.25.254.30:6379
	slots:[12288-16383] (4096 slots) master
	1 additional replica(s)
S: d458f34fa900d83212c021dc1e65396e490b5495 172.25.254.120:6379
	slots: (0 slots) slave
	replicates 5ab2e93f4f0783983676f7bd118efaacfb202bd1
[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.

集群的维护

添加节点的时候是先添加node节点到集群,然后分配槽位,删除节点的操作与添加节点的操作正好相反,是先将被删除的Redis node上的槽位迁移到集群中的其他Redis node节点上,然后再将其删除,如果一个Redis node节点上的槽位没有被完全迁移,删除该node的时候会提示有数据且无法删除。

#移除要下线主机的哈希槽位
[root@redis-node2 ~]# redis-cli -a 123456 --cluster reshard 172.25.254.20: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.20:6379)
M: ba504e78f14df5944280f9035543277a0cf5976b 172.25.254.20:6379
	slots:[6827-10922] (4096 slots) master
	1 additional replica(s)
M: 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7 172.25.254.30:6379
	slots:[12288-16383] (4096 slots) master
	1 additional replica(s)
M: 009571cb206a89afa6658b60b2d403136056ac09 172.25.254.40:6379
	slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
	1 additional replica(s)
S: c20c9b5465b2e64868161c0e285d55bc81358ba4 172.25.254.110:6379
	slots: (0 slots) slave
	replicates 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7
S: d458f34fa900d83212c021dc1e65396e490b5495 172.25.254.120:6379
	slots: (0 slots) slave
	replicates 5ab2e93f4f0783983676f7bd118efaacfb202bd1
M: 5ab2e93f4f0783983676f7bd118efaacfb202bd1 172.25.254.10:6379
	slots:[1365-5460] (4096 slots) master
	1 additional replica(s)
S: 83d7a82fe896cf9f4d8212cb533058659bba16ce 172.25.254.130:6379
	slots: (0 slots) slave
	replicates ba504e78f14df5944280f9035543277a0cf5976b
S: 86a4a8fb08e70e41b5a30f829deb983d23854ea7 172.25.254.140:6379
	slots: (0 slots) slave
	replicates 009571cb206a89afa6658b60b2d403136056ac09
[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? 1fcaeb1dd936b46f4ea1efe4330c54195e66acf7
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: 5ab2e93f4f0783983676f7bd118efaacfb202bd1
Source node #2: done

#删除master
[root@redis-node2 ~]# redis-cli -a 123456 --cluster del-node 172.25.254.120:6379 d458f34fa900d83212c021dc1e65396e490b5495

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

[root@redis-node2 ~]# redis-cli -a 123456 --cluster del-node 172.25.254.10:6379 5ab2e93f4f0783983676f7bd118efaacfb202bd1

Warning: Using a password with '-a' or '-u' option on the command line interface
may not be safe.
>>> Removing node 5ab2e93f4f0783983676f7bd118efaacfb202bd1 from cluster
172.25.254.10:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
  • 19
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

acro_09

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值