【Redis详解】Redis安装+主从复制+哨兵模式+Redis Cluster

目录

一、Redis简介

[1.1 关系型数据库和NoSQL数据库](#1.1 关系型数据库和NoSQL数据库)

二、Redis安装

[2.1 rpm 安装](#2.1 rpm 安装)

[2.2 源码安装](#2.2 源码安装)

三、Redis基本操作

四、Redis主从复制

[4.1 配置主从同步](#4.1 配置主从同步)

[4.2 主从同步过程](#4.2 主从同步过程)

五、Redis高可用--哨兵模式

[5.1 哨兵的实验过程](#5.1 哨兵的实验过程)

六、数据保留

[七、Redis Cluster](#七、Redis Cluster)

[7.1 部署redis cluster](#7.1 部署redis cluster)


一、Redis简介

Redis(Remote Dictionary Server)是一个开源的、遵循BSD协议的、基于内存的键值数据库(key-calue database),是一个非关系数据库,由意大利Salvatore Sanfilippo于2009年开发并发布。redis提供将内存通过网络远程共享的一种服务,还提供易扩展、高性能、具备数据持久性等功能,应用于高并发、低延迟等环境。

**特性:**速度快、单线程、持久化、支持多种数据结构和编程语言、功能丰富、使用简单、主从复制、支持高可用和分布式。

单线程速度快的原因-> 纯内存、非阻塞、避免线程切换和竞态消耗

缓存实现流程

(1)数据更新操作流程

(2)数据读写操作流程

1.1 关系型数据库和NoSQL数据库

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

NoSQL数据库:根据应用场景选择是否使用关系型数据库,或考虑使用更加合适的数据存储。

数据存储分类如下表所示:

存储方式 数据库
临时性键值存储 memcaches、Redis
永久性键值存储 ROMA、Redis
面向文档 MongoDB、CouchDB
面向列 Cassandra、HBase

RDBMS和NOSQL的比较

二、Redis安装

官方网站下载地址:Index of /releases/

注意:同一台主机中不能既使用rpm安装,又使用源码安装

资源包获取链接:百度网盘 请输入提取码 提取码:yzdb

2.1 rpm 安装

bash 复制代码
[root@redis-node1 ~]# yum install redis -y

2.2 源码安装

【node1】

bash 复制代码
#解压资源包
[root@redis-node1 ~]# tar zxf redis-7.4.0.tar.gz
#安装编译工具
[root@redis-node1 ~]# cd redis-7.4.0/
[root@redis-node1 redis-7.4.0]# yum install make gcc initscripts -y
#执行编译
[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
#解决报错问题
[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 * -::*
#开启redis服务
[root@redis-node1 utils]# /etc/init.d/redis_6379 restart
#查看端口
[root@redis-node1 utils]# netstat -anltupe | grep redis
#查看信息
[root@redis-node1 utils]# redis-cli
#远程传输文件到其他主机
[root@redis-node1 utils]# scp -r redis-7.4.0 root@172.25.254.20:/root
[root@redis-node1 utils]# scp -r redis-7.4.0 root@172.25.254.30:/root
[root@redis-node1 utils]# cd /usr/local/bin
[root@redis-node1 utils]# rsync -al * root@172.25.254.20:/usr/local/bin
[root@redis-node1 utils]# rsync -al * root@172.25.254.30:/usr/local/bin

【node2/node3】

两台主机命令行配置相同,以redis-node2为例

bash 复制代码
[root@redis-node2 ~]# cd redis-7.4.0/
[root@redis-node2 redis-7.4.0]# yum install initscripts -y
[root@redis-node2 redis-7.4.0]# cd utils/
[root@redis-node2 utils]# ./install_server.sh
[root@redis-node2 utils]# vim /etc/redis/6379.conf
bind * -::*
[root@redis-node2 utils]# /etc/init.d/redis_6379 restart

三、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主从复制

环境配置如图:

4.1 配置主从同步

【master】

bash 复制代码
三台主机都要做
#关闭protexted模式
[root@redis-node1 ~]# vim /etc/redis/6379.conf
protected-mode no
#重启服务
[root@redis-node1 ~]# /etc/init.d/redis_6379 restart 

【slave】以node2为例

bash 复制代码
[root@redis-node2 ~]# vim /etc/redis/6379.conf
replicaof 172.25.254.10 6379
[root@redis-node2 ~]# /etc/init.d/redis_6379 restart

测试:

bash 复制代码
#在master节点上查看
[root@redis-node1 ~]# redis-cli
#在slave节点上查看
[root@redis-node2 ~]# redis-cli

4.2 主从同步过程

(1)slave节点发送同步亲求到master节点 ;

(2)slave节点通过master节点的认证开始进行同步;

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

(4)slave节点收到rdb后首先清空自己的所有数据 ;

(5)slave节点加载rdb并进行数据恢复 ;

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

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

五、Redis高可用--哨兵模式

Sentinel进程用于监控redis集群中Master主服务器工作的状态,在Master主服务器发生故障的时候,可以实现Master和Slave服务器的切换,保证系统的高可用性。

每个哨兵(Sentine)进程会向其他哨兵(Sentinel)、Master\Slave定时发送消息,以确认对方是否"存活"着,如果发现对方在指定配置时间内未得到回应,则暂时认为对方已离线,通过一定的vole算法,从剩下的slave从服务器节点中,选一台提升为Master服务器节点,然后自动修改相关配置,并开启故障转移(failover),Sentinel机制可以解决master和slave角色的自动切换问题。

Sentinel定时任务

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

    • 发现slave节点

    • 确认主从关系

  • 每2秒每个sentinel通过master节点的channel交换信息(pub/sub)

    • 通过sentinel_:hello频道交互

    • 交互对节点的"看法"和自身信息

  • 每1秒每个sentinel对其他sentinel和redis执行pi

5.1 哨兵的实验过程

前置条件:所有节点中关闭protected-mode no

【master】

bash 复制代码
[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/redias/sentinel.conf root@172.25.254.20:/etc/redis/sentinel.conf
[root@redis-node1 redis-7.4.0]# scp /etc/redias/sentinel.conf root@172.25.254.30:/etc/redis/sentinel.conf
#备份配置文件
[root@redis-node1 redis-7.4.0]# cd /etc/redis/
[root@redis-node1 redis]# cd sentinel.conf sentinel.conf.bak
#重启服务
[root@redis-node1 redis-7.4.0]# redis-sentinel /etc/redis/sentinel.conf

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

测试:

bash 复制代码
#打开master节点终端
[root@redis-node1 ~]# redis-cli

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

六、数据保留

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

解决:

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

bash 复制代码
#在matster中设定
[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

工作原理:

当master故障后可以自动将slave提为master,从而可以保证redis服务的正常使用,在无中心的redis集群中,其每个节点保存当前节点数据和整个集群状态,每个节点都和其他所有节点连接。

特点:

  • 高可用性

  • 自动故障转移:当某个节点出现故障时,集群能自动检测到将该节点负责的槽转移到其他正常的节点上。而当进行数据访问时,会自动重定向到新的负责节点,保证服务的连续性。

  • 主从复制:当主节点出现故障时,从节点可以自动提升为主节点,继续提供服务。

  • 可扩展性

    • 动态添加和移除节点:根据实际需求,在不影响整个集群服务的情况下,动态地向集群中添加新的节点和移除现有节点。添加节点时集群会自动进行数据的重新分片,将一部分槽分配给新节点;移除节点时,会将节点上的槽转移到其他节点上。

    • 水平扩展:通过增加节点数量,可以线性地提高集群的存储容量和处理能力,满足不断增长的数据存储和访问需求。

  • 数据分区

    • 槽分配:将数据集划分为16384个槽,每个节点负责一部分槽。通过对键进行哈希运算,确定键所属的槽,进而确定存储在哪个节点上,提高集群的存储效率和性能。

    • 数据局限性:由于数据时按照槽进行分区的,可以根据数据的访问模式,将相关的数据存储在同一个节点或相邻的节点上,提高数据的局限性,减少网络开销。

  • 高性能

    • 并行处理:客户端可以同时连接多个节点,并行地执行多个操作,提高数据的读写性能。

    • 无中心节点:Redis Cluster是一个去中心化的架构,没有中心节点,所有节点地位平等。避免了单点故障和性能瓶颈,提高了集群的整体性能。

  • 简单易用

    • 客户端支持:大多数Redis客户端都支持Redis Cluster,可以直接连接到集群进行进行数据的读写操作,无需额外的中间件。

    • 命令兼容:Redis Cluster支持大部分Redis命令,使用方便。

创建前提:

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

  • 每个节点必须开启的参数

    • cluster-enabled yes

    • cluster-config-file nodes-6380.conf

  • 所有redis服务器必须没有任何数据

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

7.1 部署redis cluster

实验环境准备:

172.25.254.50/24 172.25.254.60/24 172.25.254.70/24

172.25.254.110/24 172.25.254.120/24 172.25.254.130/24

(1)所有redis主机中

bash 复制代码
[root@redis-masterx ~]# 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
[root@redis-master1 ~]# systemctl restart redis.service
[root@redis-master1 ~]# redis-cli
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> info
cluster_enabled:1

(2)redis-cli --cluster参数说明

bash 复制代码
[root@redis-master1 ~]# redis-cli --cluster help
Cluster Manager Commands:
create host1:port1 ... hostN:portN #创建集群
--cluster-replicas <arg> #指定master的副本数
check <host:port> or <host> <port> #检测集群信息
info <host:port> or <host> <port> #查看集群信息
fix <host:port> or <host> <port> #修复集群
reshard <host:port> or <host> <port> #在线热迁移集群指定主机的slots数据
rebalance <host:port> or <host> <port> #平衡各集群主机的slot数量
add-node new_host:new_port existing_host:existing_port #添加主机
del-node host:port node_id #删除主机
import host:port #导入外部redis服务器的数据到当前集群

(3)创建redis-cluster

bash 复制代码
[root@redis-master1 ~]# redis-cli --cluster create -a 123456 \
> 172.25.254.50:6379 172.25.254.60:6379 172.25.254.70: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 #主从分配情况
.....
> >> Check for open slots... #检查打开的哈希槽位
> >> Check slots coverage... #检查槽位覆盖范围
> >> [OK] All 16384 slots covered. #所有槽位分配完成
> >> #配置文件位置
> >> [root@redis-master1 ~]# ll /var/lib/redis/nodes-6379.conf

(4)检测redis集群状态

bash 复制代码
[root@redis-master1 ~]# redis-cli -a 123456 --cluster info 172.25.254.50:6379
#查看集群状态
Warning: Using a password with '-a' or '-u' option on the command line interface
may not be safe.
172.25.254.50:6379 (5ab2e93f...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.60:6379 (ba504e78...) -> 0 keys | 5462 slots | 1 slaves.
172.25.254.70:6379 (1fcaeb1d...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
[root@redis-master1 ~]# redis-cli -a 123456 cluster info
Warning: Using a password with '-a' or '-u' option on the command line interface
....
[root@redis-master1 ~]# 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.50:6379 (5ab2e93f...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.60:6379 (ba504e78...) -> 0 keys | 5462 slots | 1 slaves.
172.25.254.70:6379 (1fcaeb1d...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
....

(5)写入数据

bash 复制代码
[root@redis-master1 ~]# 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 
(error) MOVED 9189 172.25.254.60: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

(6)集群扩容

bash 复制代码
#添加master
[root@redis-master1 ~]# redis-cli -a 123456 --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.
.....
>>> do not support function command (error = 'ERR unknown command `FUNCTION`, with
>>> args beginning with: `DUMP`, ')
>>> Send CLUSTER MEET to node 172.25.254.40:6379 to make it join the cluster.
>>> [OK] New node added correctly.
>>> [root@redis-master1 ~]# 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.50:6379 (5ab2e93f...) -> 0 keys | 5461 slots | 1 slaves.
>>> 172.25.254.60:6379 (ba504e78...) -> 1 keys | 5462 slots | 1 slaves.
>>> 172.25.254.70:6379 (1fcaeb1d...) -> 0 keys | 5461 slots | 1 slaves.
>>> [OK] 1 keys in 4 masters.
>>> 0.00 keys per slot on average.
>>> #分配槽位
>>> [root@redis-master1 ~]# redis-cli -a 123456 --cluster reshard
....

#添加salve
[root@redis-master1 ~]# redis-cli -a 123456 --cluster add-node
172.25.254.130:6379 172.25.254.50: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.
....
>>> [OK] New node added correctly.

(6)cluster集群维护

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

bash 复制代码
#移除要下线主机的哈希槽位
[root@redis-master2 ~]# redis-cli -a 123456 --cluster reshard 172.25.254.60:6379
Warning: Using a password with '-a' or '-u' option on the command line interface
may not be safe.

>>> #删除master
>>> [root@redis-master2 ~]# redis-cli -a 123456 --cluster del-node

>>> [root@redis-master2 ~]# redis-cli -a 123456 --cluster del-node
相关推荐
lzb_kkk3 小时前
【Redis】redis5种数据类型(哈希)
开发语言·redis·算法·缓存·哈希算法
longlongqin5 小时前
redis的 stream数据类型实现 消息队列?
数据库·redis·缓存
kingandlog6 小时前
Redis网络模型、通信协议、内存回收
java·网络·redis
喜欢吃animal milk10 小时前
Redis - 缓存
redis·缓存
lcn2911 小时前
【Redis】主从复制 - 源码
redis
IT_Octopus11 小时前
Springboot redis luttuce lua脚本 存储压缩的protobuf key:byte[] value:map<byte,byte[]>
spring boot·redis·lua
程序员-杨胡广11 小时前
Redis重要知识点:哨兵是什么?哨兵如何选择Redis主服务器
redis·git·bootstrap·github
康提扭狗兔12 小时前
Caffenie配合Redis做两级缓存,Redis发布订阅实现缓存一致更新
数据库·redis·缓存
CopyLower12 小时前
Redis中的AOF重写过程及其实际应用
java·数据库·redis
Daniel_18712 小时前
TinyRedis项目复盘
数据库·redis