目录
一、NoSql数据库简介
1.1 数据库主要分为两大类:关系型数据库与 NoSQL 数据库
1.2 为什么还要用 NoSQL 数据库呢?
1.3 RDBMS和NOSQL的特点及优缺点:
二 Remote Dictionary Server 简介(redis)
2.1 什么是redis
2.2 Redis特性
2.3 Redis应用场景
2.4 缓存的实现流程
三、安装Redis
3.1 源码安装
四、Redis的基本命令
五、Redis 的主从复制
5.1 主从同步过程
5.2 实验环境:
5.3 配置master:
5.4 配置slave:
5.5 测试数据是否能同步master:
六、Redis的哨兵(高可用)
6.1 Redis哨兵介绍
6.2 Redis哨兵实验
6.3 在整个架构中可能会出现的问题
七、Redis Cluster集群(无中心化设计)
7.1 工作原理
7.2 实验环境
7.3 部署Redis
7.4 创建集群
7.5 集群扩容
7.6 集群维护
一、NoSql数据库简介
1.1 数据库主要分为两大类:关系型数据库与 NoSQL 数据库
- 关系型数据库,是建立在关系模型基础上的数据库,其借助于集合代数等数学概念和方法来处理数据库中的数据主流的 MySQL、Oracle、MS SQL Server 和 DB2 都属于这类传统数据库。
- NoSQL 数据库,全称为 Not Only SQL,意思就是适用关系型数据库的时候就使用关系型数据库,不适用的时候也没有必要非使用关系型数据库不可,可以考虑使用更加合适的数据存储。主要分为临时性键值存储(memcached、Redis)、永久性键值存储(ROMA、Redis)、面向文档的数据库(MongoDB、CouchDB)、面向列的数据库(Cassandra、HBase),每种 NoSQL 都有其特有的使用场景及优点。
1.2 为什么还要用 NoSQL 数据库呢?
1.3 RDBMS和NOSQL的特点及优缺点:
二 Remote Dictionary Server 简介(redis)
2.1 什么是redis
2.2 Redis特性
- 速度快: 10W QPS,基于内存,C语言实现
- 单线程
- 持久化
- 支持多种数据结构
- 支持多种编程语言
- 功能丰富: 支持Lua脚本,发布订阅,事务,pipeline等功能
- 简单: 代码短小精悍(单机核心代码只有23000行左右),单线程开发容易,不依赖外部库,使用简单
- 主从复制
- 支持高可用和分布式
- 纯内存
- 非阻塞
- 避免线程切换和竞态消耗
2.3 Redis应用场景
- Session 共享:常见于web集群中的Tomcat或者PHP中多web服务器session共享
- 缓存:数据查询、电商网站商品信息、新闻内容
- 计数器:访问排行榜、商品浏览数等和次数相关的数值统计场景
- 微博/微信社交场合:共同好友,粉丝数,关注,点赞评论等
- 消息队列:ELK的日志缓存、部分业务的订阅发布系统
- 地理位置: 基于GEO(地理信息定位),实现摇一摇,附近的人,外卖等功能
2.4 缓存的实现流程
数据读操作流程:
三、安装Redis
[root@redis-node1 ~]# dnf install redis -y --------- rpm包安装
3.1 源码安装
安装所需软件:
[root@redis-node1 ~]# dnf install make gcc initscripts-10.11.6-1.el9.x86_64 -y[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]# make && make install注释脚本内容:
[root@redis-node1 redis-7.4.0]# cd utils/
[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
直接全部回车
[root@redis-node1 utils]# vim /etc/redis/6379.conf bind * -::* ---- 修改端口
protected-mode no ------ 关闭protected模式[root@redis-node1 utils]# /etc/init.d/redis_6379 restart启动命令,可以查看信息了
[root@redis-node1 utils]# redis-cli
四、Redis的基本命令
命令参数 | 作用 |
config get * | 查看配置 |
select 1 | 选择数据库 |
flushdb | 清空当前数据库 |
flushall | 清空所有数据库 |
move key 1 | 移动key |
del key | 删除 |
rename oldket newkey | 改名 |
expire key 10 | 设置过期时间 |
persist key | 设置持久化 |
keys user* | 查询 |
exists key | 判断是否存在 |
示例:
#查看配置
127.0.0.1:6379[1]> CONFIG GET bind
1) "bind"
2) "* -::*"
127.0.0.1:6379[1]> CONFIG GET *#写入和读取数据
127.0.0.1:6379> SET name lee
OK
127.0.0.1:6379> GET name
"xiaoding"
127.0.0.1:6379> set name lee ex 5
OK
127.0.0.1:6379> get name
"xiaoding"
127.0.0.1:6379> get name
"xiaoding"
127.0.0.1:6379> get name
"xiaoding"
127.0.0.1:6379> get name
"xiaoding"
127.0.0.1:6379> get name
(nil)
#如果没有设定数据过期时间会一直存在, /var/lib/redis/6379/dump.rdb内存快照中
127.0.0.1:6379> set name xiaoding
OK
127.0.0.1:6379> KEYS * #查看所有key
1) "name"#选择数据库 redisa中有0-15个数据库
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get name
(nil)
127.0.0.1:6379> select 0
127.0.0.1:6379[1]> select 16
(error) ERR DB index is out of range#移动数据
127.0.0.1:6379> set name xiaoding
OK
127.0.0.1:6379> MOVE name 1
(integer) 1
127.0.0.1:6379> GET name
(nil)
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> get name
"xiaoding"#改变键名
127.0.0.1:6379[1]> RENAME name id
OK
127.0.0.1:6379[1]> get name
(nil)
127.0.0.1:6379[1]> get id
"xiaoding"#设定数据过期时间
127.0.0.1:6379> set name xiaoding ex 10000
OK
127.0.0.1:6379> get name
"xiaoding"
127.0.0.1:6379> EXPIRE name 3
(integer) 1
127.0.0.1:6379> get name
"xiaoding"
127.0.0.1:6379> get name
(nil)#删除
127.0.0.1:6379> set name xiaoding
OK
127.0.0.1:6379> get name
"xiaoding"
127.0.0.1:6379> del name
(integer) 1
127.0.0.1:6379> get name
(nil)#持久化保存
127.0.0.1:6379> PERSIST name
(integer) 0#判断key是否存在
127.0.0.1:6379> EXISTS name
(integer) 1
127.0.0.1:6379> EXISTS xiaoding
(integer) 0#清空当前库
127.0.0.1:6379> flushdb
OK
127.0.0.1:6379> GET name
(nil)#清空所有库
127.0.0.1:6379[1]> FLUSHALL
OK
五、Redis 的主从复制
5.1 主从同步过程
- slave节点发送同步亲求到master节点
- slave节点通过master节点的认证开始进行同步
- master节点会开启bgsave进程发送内存rbd到slave节点,在此过程中是异步操作,也就是说 master节点仍然可以进行写入动作
- slave节点收到rdb后首先清空自己的所有数据
- slave节点加载rdb并进行数据恢复
- 在master和slave同步过程中master还会开启新的bgsave进程把没有同步的数据进行缓存
- 然后通过自有的replactionfeedslave函数把未通过内存快照发动到slave的数据一条一条写入到 slave中
5.2 实验环境:
主机名 | IP | 角色 |
redis-node1 | 172.25.254.10 | master |
redis-node2 | 172.25.254.20 | slave |
redis-node3 | 172.25.254.30 | slave |
5.3 配置master:
[root@redis-node1 utils]# ./install_server.sh
直接全部回车
[root@redis-node1 utils]# vim /etc/redis/6379.conf bind * -::* ---- 修改端口
protected-mode no ------ 关闭protected模式[root@redis-node1 utils]# /etc/init.d/redis_6379 restart
5.4 配置slave:
在10上面编译的Redis传到20和30上面:
[root@redis-node1 ~]# scp -r redis-7.4.0 root@172.25.254.20:/root/
[root@redis-node1 ~]# scp -r redis-7.4.0 root@172.25.254.30:/root/
[root@redis-node1 ~]# cd /usr/local/bin/
[root@redis-node1 bin]# rsync -al * root@172.25.254.20:/usr/local/bin/
[root@redis-node1 bin]# rsync -al * root@172.25.254.30:/usr/local/bin/172.25.254.20上:
要安装软件:
[root@redis-node2 ~]# dnf install initscripts-10.11.6-1.el9.x86_64 -y
[root@redis-node2 ~]# cd redis-7.4.0/utils/
[root@redis-node2 utils]# ./install_server.sh
和上面一样全部回车修改配置文件
[root@redis-node2 utils]# vim /etc/redis/6379.confbind * -::* ---- 修改端口protected-mode no ------ 关闭protected模式replicaof 172.25.254.10 6379 --------- 添加master的ip及端口[root@redis-node2 utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...查看端口是否开启:
[root@redis-node2 bin]# netstat -antlpe | grep redis172.25.254.30上:
[root@redis-node3 ~]# dnf install initscripts-10.11.6-1.el9.x86_64 -y
[root@redis-node3 ~]# cd redis-7.4.0/utils/
[root@redis-node3 utils]# ./install_server.sh
和上面一样全部回车修改配置文件
[root@redis-node3 utils]# vim /etc/redis/6379.confbind * -::* ---- 修改端口protected-mode no ------ 关闭protected模式replicaof 172.25.254.10 6379 --------- 添加master的ip及端口[root@redis-node3 utils]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...查看端口是否开启:
[root@redis-node3 bin]# netstat -antlpe | grep redis
5.5 测试数据是否能同步master:
172.25.254.10上:
[root@redis-node1 ~]# redis-cli
127.0.0.1:6379> info replication ---------- 可以查看slave信息
127.0.0.1:6379> set name xiaoding --------- 写入一个数据
OK
127.0.0.1:6379>
127.0.0.1:6379> get name
"xiaoding"
127.0.0.1:6379> 20和30上面是否能同步到:
[root@redis-node2 ~]# redis-cli
127.0.0.1:6379>
127.0.0.1:6379> get name
"xiaoding"
127.0.0.1:6379> [root@redis-node3 ~]# redis-cli
127.0.0.1:6379>
127.0.0.1:6379> get name
"xiaoding"
127.0.0.1:6379>
六、Redis的哨兵(高可用)
实验环境:就使用上面的一主两从
6.1 Redis哨兵介绍
- 每10秒每个sentinel对master和slave执行info
- 每2秒每个sentinel通过master节点的channel交换信息(pub/sub)
1.通过sentinel__:hello频道交互; 2. 交互对节点的“看法”和自身信息
- 每1秒每个sentinel对其他sentinel和redis执行pi
6.2 Redis哨兵实验
在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]#配置参数解释:
protected-mode no #关闭保护模式
port 26379 #监听端口
daemonize no #进入不打如后台
pidfile /var/run/redis-sentinel.pid #sentinel进程pid文件
loglevel notice #日志级别
sentinel monitor mymaster 172.25.254.100 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]# vim sentinel.confsentinel monitor mymaster 172.25.254.10 6379 2 #2表示主观下线之后的两跳,表示它挂了,会选举新的master,如果开着的,会以slave的身份加入集群,就会把自己的数据都删掉sentinel down-after-milliseconds mymaster 10000 #master中断时长,10秒连不上视为master下线[root@redis-node1 redis-7.4.0]# scp /etc/redis/sentinel.conf root@172.25.254.20:/etc/redis/sentinel.conf
[root@redis-node1 redis-7.4.0]# scp /etc/redis/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]# cp sentinel.conf sentinel.conf.bak --- 做个备份。哨兵模式会更改配置文件内容,需要还原的时候用备份文件还原就行了启动哨兵:
[root@redis-node1 redis]# redis-sentinel /etc/redis/sentinel.conf
复制会话看效果:
[root@redis-node1 redis]# vim sentinel.conf
[root@redis-node1 redis]# pwd
/etc/redis
[root@redis-node1 redis]#把10给down掉:
[root@redis-node1 ~]# redis-cli
127.0.0.1:6379> SHUTDOWN
(0.89s)
not connected> quit
可以看到配置文件最后会自动加一些信息
20上面:
30上面:
上图中可以看到30被选举为master:
[root@redis-node1 ~]# ssh -l root 172.25.254.30
[root@redis-node3 ~]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master ----- 可以看到30成为了master
connected_slaves:1 ----- 只有一个slave,因为10挂掉了
slave0:ip=172.25.254.20,port=6379,state=online,offset=102995,lag=1
master_failover_state:no-failover
master_replid:5435161d3440fb2fa306013f69a293295eabdef4
master_replid2:a186c4fed44a6086a5e9ea7c42f91a9219e766f0
master_repl_offset:103136
second_repl_offset:38539
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:155
repl_backlog_histlen:102982
127.0.0.1:6379> quit
[root@redis-node3 ~]# exit
logout
Connection to 172.25.254.30 closed[root@redis-node3 ~]# exit
[root@redis-node1 ~]# /etc/init.d/redis_6379 start
Starting Redis server...
[root@redis-node1 ~]# ssh -l root 172.25.254.30[root@redis-node3 ~]# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2 ---- 就变成两个slave了
slave0:ip=172.25.254.20,port=6379,state=online,offset=120050,lag=0
slave1:ip=172.25.254.10,port=6379,state=online,offset=119909,lag=1
master_failover_state:no-failover
master_replid:5435161d3440fb2fa306013f69a293295eabdef4
master_replid2:a186c4fed44a6086a5e9ea7c42f91a9219e766f0
master_repl_offset:120050
second_repl_offset:38539
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:155
repl_backlog_histlen:119896
127.0.0.1:6379> 这个就解决了主从的单点故障问题。
恢复:
[root@redis-node1 ~]# cd /etc/redis/
[root@redis-node1 redis]# ls
6379.conf sentinel.conf sentinel.conf.bak
[root@redis-node1 redis]# cp sentinel.conf.bak sentinel.conf
cp: overwrite 'sentinel.conf'? y
[root@redis-node1 redis]#
6.3 在整个架构中可能会出现的问题
问题:
在生产环境中如果master和slave中的网络出现故障,由于哨兵的存在会把master提出去
当网络恢复后,master发现环境发生改变,master就会把自己的身份转换成slave
master变成slave后会把网络故障那段时间写入自己中的数据清掉,这样数据就丢失了。
解决:
master在被写入数据时会持续连接slave,mater确保有2个slave可以写入我才允许写入
如果slave数量少于2个便拒绝写入
如何保持数据一致性,如何解决的。
在master中设定: (临时更改)
[root@redis-node3 redis]# redis-cli
127.0.0.1:6379> CONFIG GET min-slaves-to-write
1) "min-slaves-to-write" ---- 现在是没有slave也可以写入数据
2) "0"
127.0.0.1:6379> CONFIG set min-slaves-to-write 2 --- slave数量必须大于两个
OK
127.0.0.1:6379> CONFIG GET min-slaves-to-write
1) "min-slaves-to-write"
2) "2"
127.0.0.1:6379> 永久更改:
[root@redis-node3 redis]# vim /etc/redis/6379.conf -- 写到配置文件里面去就行
# even if no authentication is configured.
protected-mode no
min-slaves-to-write 2
# Redis uses default hardened security configuration directives to reduce the
# attack surface on innocent users. Therefore, several sensitive configuration
七、Redis Cluster集群(无中心化设计)
7.1 工作原理
在哨兵sentinel机制中,可以解决redis高可用问题,即当master故障后可以自动将slave提升为master,
从而可以保证redis服务的正常使用,但是无法解决redis单机写入的瓶颈问题,即单机redis写入性能受
限于单机的内存大小、并发数量、网卡速率等因素。
redis 3.0版本之后推出了无中心架构的redis cluster机制,在无中心的redis集群当中,其每个节点保存
当前节点数据和整个集群状态,每个节点都和其他所有节点连接
Redis Cluster特点如下
-
所有Redis节点使用(PING机制)互联
-
集群中某个节点的是否失效,是由整个集群中超过半数的节点监测都失效,才能算真正的失效
-
客户端不需要proxy即可直接连接redis,应用程序中需要配置有全部的redis服务器IP
-
redis cluster把所有的redis node 平均映射到 0-16383个槽位(slot)上,读写需要到指定的redisnode上进行操作,因此有多少个redis node相当于redis 并发扩展了多少倍,每个redis node 承担16384/N个槽位
-
Redis cluster预先分配16384个(slot)槽位,当需要在redis集群中写入一个key -value的时候,会使用CRC16(key) mod 16384之后的值,决定将key写入值哪一个槽位从而决定写入哪一个Redis节点上,从而有效解决单机瓶颈。
查看三台主机的进程,全部删掉
[root@redis-node1 redis-7.4.0]# ps aux | grep redis-server
[root@redis-node2 redis-7.4.0]# ps aux | grep redis-server
[root@redis-node3 redis-7.4.0]# ps aux | grep redis-server
使用kill命令删掉删除安装:
[root@redis-node1 redis-7.4.0]# make uninstall
cd src && make uninstall
make[1]: Entering directory '/root/redis-7.4.0/src'
rm -f /usr/local/bin/{redis-server,redis-benchmark,redis-cli,redis-check-rdb,redis-check-aof,redis-sentinel}
make[1]: Leaving directory '/root/redis-7.4.0/src'[root@redis-node2redis-7.4.0]# make uninstall
[root@redis-node3redis-7.4.0]# make uninstall
7.2 实验环境
主机名 | IP | 角色 |
redis-node10 | 172.25.254.10 | master |
redis-node20 | 172.25.254.20 | master |
redis-node30 | 172.25.254.30 | master |
redis-node110 | 172.25.254.110 | slave |
redis-node120 | 172.25.254.120 | slave |
redis-node130 | 172.25.254.130 | slave |
7.3 部署Redis
开启多执行模式,六台主机全部安装redis:
[root@redis-node1 ~]# yum install redis -y[root@redis-node1 ~]# vim /etc/redis/redis.conf bind * -::*
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-node1 ~]# systemctl enable --now redis
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.
[root@redis-node1 ~]# netstat -antlupe | grep redis#退出一下,在重新登,刷新一下:
[root@redis-node1 ~]# exit
[root@redis-node1 ~]# redis-cli
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456 ------ 需要认证
OK
127.0.0.1:6379> keys *
(empty array)
127.0.0.1:6379>
127.0.0.1:6379> set name xiaoding -------- 现在还写入不了数据,因为还没有分配hash
(error) CLUSTERDOWN Hash slot not served
127.0.0.1:6379> 把配置好的文件传给其他主机:
[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开启多执行模式:
所有主机都启动redis:
[root@redis-node1 ~]# systemctl enable --now redis
[root@redis-node1 ~]# netstat -antlupe | grep redis --- 检查端口是否都开启
7.4 创建集群
[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
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.110:6379 to 172.25.254.30:6379
M: 5acbd4f2754d3c6168197d20623ab710c7a7dd94 172.25.254.10:6379slots:[0-5460] (5461 slots) master
M: 3b58f505402276ee39a9acdaf1e9c73529250dd7 172.25.254.20:6379slots:[5461-10922] (5462 slots) master
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379slots:[10923-16383] (5461 slots) master
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379replicates c89826602345977f14b0a82faa354cd52dfd9196
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379replicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379replicates 3b58f505402276ee39a9acdaf1e9c73529250dd7
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: 5acbd4f2754d3c6168197d20623ab710c7a7dd94 172.25.254.10:6379 --- 跟master后面三位一样的就是谁的slaveslots:[0-5460] (5461 slots) master1 additional replica(s)
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379slots: (0 slots) slavereplicates c89826602345977f14b0a82faa354cd52dfd9196
M: 3b58f505402276ee39a9acdaf1e9c73529250dd7 172.25.254.20:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379slots: (0 slots) slavereplicates 3b58f505402276ee39a9acdaf1e9c73529250dd7
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379slots: (0 slots) slavereplicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered. #### master挂掉之后,hash槽会迁到slave中,然后存放数据
[root@redis-node1 ~]# 查看信息:
[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 (5acbd4f2...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.20:6379 (3b58f505...) -> 0 keys | 5462 slots | 1 slaves.
172.25.254.30:6379 (c8982660...) -> 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
127.0.0.1:6379> set name xiaoding
(error) MOVED 5798 172.25.254.20:6379 hash槽是5798,看上面创建集群的信息,落到了20 的范围[root@redis-node2 ~]# 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 name xiaoding
OK
127.0.0.1:6379> ------ 其他主机上面是看不到写入的数据,因为数据会分别存到hash槽中
7.5 集群扩容
再开两台主机 :
主机名 | IP | 角色 |
redis-node 50 | 172.25.254.50 | master |
redis-node 150 | 172.25.254.150 | slave |
[root@redis-node50 ~]# yum install redis -y
[root@redis-node150 ~]# yum install redis -y配置文件传过去:
[root@redis-node1 ~]# scp /etc/redis/redis.conf root@172.25.254.50:/etc/redis/redis.conf
[root@redis-node1 ~]# scp /etc/redis/redis.conf root@172.25.254.150:/etc/redis/redis.conf启动redis:
[root@redis-node50 ~]# systemctl enable --now redis
[root@redis-node150 ~]# systemctl enable --now redis扩容集群:
[root@redis-node1 ~]# redis-cli -a 123456 --cluster add-node 172.25.254.50: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.50:6379 to cluster 172.25.254.10:6379
>>> Performing Cluster Check (using node 172.25.254.10:6379)
M: 5acbd4f2754d3c6168197d20623ab710c7a7dd94 172.25.254.10:6379slots:[0-5460] (5461 slots) master1 additional replica(s)
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379slots: (0 slots) slavereplicates c89826602345977f14b0a82faa354cd52dfd9196
M: 3b58f505402276ee39a9acdaf1e9c73529250dd7 172.25.254.20:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379slots: (0 slots) slavereplicates 3b58f505402276ee39a9acdaf1e9c73529250dd7
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379slots: (0 slots) slavereplicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)
[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.50:6379 to make it join the cluster.
[OK] New node added correctly.再次查看集群:
[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 (5acbd4f2...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.20:6379 (3b58f505...) -> 1 keys | 5462 slots | 1 slaves.
172.25.254.30:6379 (c8982660...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.50:6379 (ae5b1d98...) -> 0 keys | 0 slots | 0 slaves.
[OK] 1 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 172.25.254.10:6379)
M: 5acbd4f2754d3c6168197d20623ab710c7a7dd94 172.25.254.10:6379slots:[0-5460] (5461 slots) master1 additional replica(s)
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379slots: (0 slots) slavereplicates c89826602345977f14b0a82faa354cd52dfd9196
M: 3b58f505402276ee39a9acdaf1e9c73529250dd7 172.25.254.20:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379slots: (0 slots) slavereplicates 3b58f505402276ee39a9acdaf1e9c73529250dd7
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379slots: (0 slots) slavereplicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)
M: ae5b1d985c911d539d643d91db648719f9a8e68b 172.25.254.50:6379 ----- 现在还没有hash槽slots: (0 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@redis-node1 ~]# 给50分享hash槽:
[root@redis-node1 ~]# redis-cli -a 123456 --cluster reshard 172.25.254.10:6379
[root@redis-node1 ~]# redis-cli -a 123456 --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: 5acbd4f2754d3c6168197d20623ab710c7a7dd94 172.25.254.10:6379slots:[0-5460] (5461 slots) master1 additional replica(s)
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379slots: (0 slots) slavereplicates c89826602345977f14b0a82faa354cd52dfd9196
M: 3b58f505402276ee39a9acdaf1e9c73529250dd7 172.25.254.20:6379slots:[5461-10922] (5462 slots) master1 additional replica(s)
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379slots: (0 slots) slavereplicates 3b58f505402276ee39a9acdaf1e9c73529250dd7
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379slots: (0 slots) slavereplicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379slots:[10923-16383] (5461 slots) master1 additional replica(s)
M: ae5b1d985c911d539d643d91db648719f9a8e68b 172.25.254.50:6379slots: (0 slots) master
[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 ----- 一共有16384个槽位,分给4个
What is the receiving node ID? ae5b1d985c911d539d643d91db648719f9a8e68b ----分配的id
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: 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 ae5b1d985c911d539d643d91db648719f9a8e68b ----- id去看50的查看是否4主4从:
[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 (5acbd4f2...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.20:6379 (3b58f505...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.30:6379 (c8982660...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.50:6379 (ae5b1d98...) -> 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.10:6379)
M: 5acbd4f2754d3c6168197d20623ab710c7a7dd94 172.25.254.10:6379slots:[1365-5460] (4096 slots) master1 additional replica(s)
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379slots: (0 slots) slavereplicates c89826602345977f14b0a82faa354cd52dfd9196
M: 3b58f505402276ee39a9acdaf1e9c73529250dd7 172.25.254.20:6379slots:[6827-10922] (4096 slots) master1 additional replica(s)
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379slots: (0 slots) slavereplicates 3b58f505402276ee39a9acdaf1e9c73529250dd7
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379slots: (0 slots) slavereplicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379slots:[12288-16383] (4096 slots) master1 additional replica(s)
S: e2f96e1f607e0ee8ebf1e1a13a341b5a97dd7563 172.25.254.150:6379slots: (0 slots) slavereplicates ae5b1d985c911d539d643d91db648719f9a8e68b
M: ae5b1d985c911d539d643d91db648719f9a8e68b 172.25.254.50:6379slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@redis-node1 ~]#
7.6 集群维护
添加节点的时候是先添加node节点到集群,然后分配槽位,删除节点的操作与添加节点的操作正好相反,是先将被删除的Redis node上的槽位迁移到集群中的其他Redis node节点上,然后再将其删除,如果一个Redis node节点上的槽位没有被完全迁移,删除该node的时候会提示有数据且无法删除。
#先把slave节点移掉:
[root@redis-node1 ~]# redis-cli -a 123456 --cluster del-node 172.25.254.150:6379 e2f96e1f607e0ee8ebf1e1a13a341b5a97dd7563
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node e2f96e1f607e0ee8ebf1e1a13a341b5a97dd7563 from cluster 172.25.254.150:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
[root@redis-node1 ~]# 查看是否删除:
[root@redis-node2 ~]# redis-cli -a 123456 --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 (3b58f505...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.30:6379 (c8982660...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.10:6379 (5acbd4f2...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.50:6379 (ae5b1d98...) -> 1 keys | 4096 slots | 0 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: 3b58f505402276ee39a9acdaf1e9c73529250dd7 172.25.254.20:6379slots:[6827-10922] (4096 slots) master1 additional replica(s)
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379slots: (0 slots) slavereplicates c89826602345977f14b0a82faa354cd52dfd9196
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379slots: (0 slots) slavereplicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379slots: (0 slots) slavereplicates 3b58f505402276ee39a9acdaf1e9c73529250dd7
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379slots:[12288-16383] (4096 slots) master1 additional replica(s)
M: 5acbd4f2754d3c6168197d20623ab710c7a7dd94 172.25.254.10:6379slots:[1365-5460] (4096 slots) master1 additional replica(s)
M: ae5b1d985c911d539d643d91db648719f9a8e68b 172.25.254.50:6379slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage... ############ 已经删除
[OK] All 16384 slots covered.
[root@redis-node2 ~]# #把50的hash槽分配给其他人,不然删不掉:
[root@redis-node1 ~]# redis-cli -a 123456 --cluster del-node 172.25.254.150:6379 e2f96e1f607e0ee8ebf1e1a13a341b5a97dd7563
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node e2f96e1f607e0ee8ebf1e1a13a341b5a97dd7563 from cluster 172.25.254.150:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
[root@redis-node1 ~]# redis-cli -a 123456 --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: 5acbd4f2754d3c6168197d20623ab710c7a7dd94 172.25.254.10:6379slots:[1365-5460] (4096 slots) master1 additional replica(s)
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379slots: (0 slots) slavereplicates c89826602345977f14b0a82faa354cd52dfd9196
M: 3b58f505402276ee39a9acdaf1e9c73529250dd7 172.25.254.20:6379slots:[6827-10922] (4096 slots) master1 additional replica(s)
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379slots: (0 slots) slavereplicates 3b58f505402276ee39a9acdaf1e9c73529250dd7
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379slots: (0 slots) slavereplicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379slots:[12288-16383] (4096 slots) master1 additional replica(s)
M: ae5b1d985c911d539d643d91db648719f9a8e68b 172.25.254.50:6379slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
[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 ---- 分配的hash槽位
What is the receiving node ID? 5acbd4f2754d3c6168197d20623ab710c7a7dd94 ---- 分配给谁的id
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: ae5b1d985c911d539d643d91db648719f9a8e68b ---------- 从谁身上分配
Source node #1: done[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 (5acbd4f2...) -> 1 keys | 8192 slots | 2 slaves.
172.25.254.20:6379 (3b58f505...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.30:6379 (c8982660...) -> 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.10:6379)
M: 5acbd4f2754d3c6168197d20623ab710c7a7dd94 172.25.254.10:6379slots:[0-6826],[10923-12287] (8192 slots) master2 additional replica(s)
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379slots: (0 slots) slavereplicates c89826602345977f14b0a82faa354cd52dfd9196
M: 3b58f505402276ee39a9acdaf1e9c73529250dd7 172.25.254.20:6379slots:[6827-10922] (4096 slots) master1 additional replica(s)
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379slots: (0 slots) slavereplicates 3b58f505402276ee39a9acdaf1e9c73529250dd7
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379slots: (0 slots) slavereplicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379slots:[12288-16383] (4096 slots) master1 additional replica(s)
S: ae5b1d985c911d539d643d91db648719f9a8e68b 172.25.254.50:6379 ###### 50上的hash槽位已经没了slots: (0 slots) slavereplicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.删除50:
[root@redis-node1 ~]# redis-cli -a 123456 --cluster del-node 172.25.254.50:6379 ae5b1d985c911d539d643d91db648719f9a8e68b
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node ae5b1d985c911d539d643d91db648719f9a8e68b from cluster 172.25.254.50:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
[root@redis-node1 ~]# 再查看集群,已经变回3主3从了:
[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 (5acbd4f2...) -> 1 keys | 8192 slots | 1 slaves.
172.25.254.20:6379 (3b58f505...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.30:6379 (c8982660...) -> 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.10:6379)
M: 5acbd4f2754d3c6168197d20623ab710c7a7dd94 172.25.254.10:6379slots:[0-6826],[10923-12287] (8192 slots) master1 additional replica(s)
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379slots: (0 slots) slavereplicates c89826602345977f14b0a82faa354cd52dfd9196
M: 3b58f505402276ee39a9acdaf1e9c73529250dd7 172.25.254.20:6379slots:[6827-10922] (4096 slots) master1 additional replica(s)
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379slots: (0 slots) slavereplicates 3b58f505402276ee39a9acdaf1e9c73529250dd7
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379slots: (0 slots) slavereplicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379slots:[12288-16383] (4096 slots) master1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@redis-node1 ~]#