详细解读企业级NoSql数据库Redis集群

一 关系型数据库和 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 数据库呢?

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

1.3 RDBMS和NOSQL的特点及优缺点:

二 Remote Dictionary Server 简介

2.1 什么是redis

Redis (Remote Dictionary Server)
在2009年发布,开发者是意大利的萨尔瓦多·桑菲利波普(Salvatore Sanfilippo),他本想为自己的公司开发一个用于替换MySQL的产品Redis,但是没有想到他把Redis开源后大受欢迎,短短几年,Redis就有了很大的用户群体,目前国内外使用的公司众多,比如:阿里,百度,新浪微博,知乎网,GitHub,Twitter 等Redis是一个开源的、遵循BSD协议的、基于内存的而且目前比较流行的键值数据库(key-valuedatabase),是一个非关系型数据库,redis 提供将内存通过网络远程共享的一种服务,提供类似功能的还有memcached,但相比memcached,redis还提供了易扩展、高性能、具备数据持久性等功能。Redis 在高并发、低延迟环境要求比较高的环境使用量非常广泛

2.2 Redis特性

  • 速度快: 10W QPS,基于内存,C语言实现
  • 单线程
  • 持久化
  • 支持多种数据结构
  • 支持多种编程语言
  • 功能丰富: 支持Lua脚本,发布订阅,事务,pipeline等功能
  • 简单: 代码短小精悍(单机核心代码只有23000行左右),单线程开发容易,不依赖外部库,使用简单
  • 主从复制
  • 支持高可用和分布

单线程为何如此快?

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

2.3 Redis应用场景

  • Session 共享:常见于web集群中的Tomcat或者PHP中多web服务器session共享
  • 缓存:数据查询、电商网站商品信息、新闻内容
  • 计数器:访问排行榜、商品浏览数等和次数相关的数值统计场景
  • 微博/微信社交场合:共同好友,粉丝数,关注,点赞评论等
  • 消息队列:ELK的日志缓存、部分业务的订阅发布系统
  • 地理位置: 基于GEO(地理信息定位),实现摇一摇,附近的人,外卖等功能

2.4 缓存的实现流程

数据更新操作流程

数据读操作流程

三 Redis的安装

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

实验环境:

IP角色
172.25.254.10master
172.25.254.20slave
172.25.254.30slave

3.1源码安装Redis

#安装所需软件:
[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

3.1.1配置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.conf

bind * -::*                                                   #修改端口

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 redis


172.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.conf

bind * -::*                                                    #修改端口

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

3.1.2测试数据是否能同步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 xiaowang           #写入一个数据
OK
127.0.0.1:6379> 
127.0.0.1:6379> get name
"xiaowang"
127.0.0.1:6379> 


#20和30上面是否能同步到:
[root@redis-node2 ~]# redis-cli
127.0.0.1:6379> 
127.0.0.1:6379> get name
"xiaowang"
127.0.0.1:6379> 


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

3.2Redis哨兵

#在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.conf

sentinel 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

可以看到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]# 

在整个架构中可能会出现的问题

问题:

在生产环境中如果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集群(无中心化设计)

工作原理:

在哨兵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)上,读写需要到指定的redisnode上进行操作,因此有多少个redis node相当于redis 并发扩展了多少倍,每个redis node 承担16384/N个槽位

  5. 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

实验环境:

IP角色
172.25.254.10master
172.25.254.20master
172.25.254.30master
172.25.254.110slave
172.25.254.120slave
172.25.254.130slave

部署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 xiaowang                 #现在还写入不了数据,因为还没有分配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          #检查端口是否都开启

创建集群

[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:6379
   slots:[0-5460] (5461 slots) master
M: 3b58f505402276ee39a9acdaf1e9c73529250dd7 172.25.254.20:6379
   slots:[5461-10922] (5462 slots) master
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379
   slots:[10923-16383] (5461 slots) master
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379
   replicates c89826602345977f14b0a82faa354cd52dfd9196
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379
   replicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379
   replicates 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后面三位一样的就是谁的slave
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379
   slots: (0 slots) slave
   replicates c89826602345977f14b0a82faa354cd52dfd9196
M: 3b58f505402276ee39a9acdaf1e9c73529250dd7 172.25.254.20:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3b58f505402276ee39a9acdaf1e9c73529250dd7
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379
   slots: (0 slots) slave
   replicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379
   slots:[10923-16383] (5461 slots) master
   1 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 xiaowang
(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 xiaowang
OK
127.0.0.1:6379>             #其他主机上面是看不到写入的数据,因为数据会分别存到hash槽中

集群扩容

再开两台主机 172.25.254.50 172.25.254.150

[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:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379
   slots: (0 slots) slave
   replicates c89826602345977f14b0a82faa354cd52dfd9196
M: 3b58f505402276ee39a9acdaf1e9c73529250dd7 172.25.254.20:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3b58f505402276ee39a9acdaf1e9c73529250dd7
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379
   slots: (0 slots) slave
   replicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379
   slots:[10923-16383] (5461 slots) master
   1 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:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379
   slots: (0 slots) slave
   replicates c89826602345977f14b0a82faa354cd52dfd9196
M: 3b58f505402276ee39a9acdaf1e9c73529250dd7 172.25.254.20:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3b58f505402276ee39a9acdaf1e9c73529250dd7
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379
   slots: (0 slots) slave
   replicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379
   slots:[10923-16383] (5461 slots) master
   1 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:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379
   slots: (0 slots) slave
   replicates c89826602345977f14b0a82faa354cd52dfd9196
M: 3b58f505402276ee39a9acdaf1e9c73529250dd7 172.25.254.20:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3b58f505402276ee39a9acdaf1e9c73529250dd7
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379
   slots: (0 slots) slave
   replicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: ae5b1d985c911d539d643d91db648719f9a8e68b 172.25.254.50:6379
   slots: (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:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379
   slots: (0 slots) slave
   replicates c89826602345977f14b0a82faa354cd52dfd9196
M: 3b58f505402276ee39a9acdaf1e9c73529250dd7 172.25.254.20:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3b58f505402276ee39a9acdaf1e9c73529250dd7
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379
   slots: (0 slots) slave
   replicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: e2f96e1f607e0ee8ebf1e1a13a341b5a97dd7563 172.25.254.150:6379
   slots: (0 slots) slave
   replicates ae5b1d985c911d539d643d91db648719f9a8e68b
M: ae5b1d985c911d539d643d91db648719f9a8e68b 172.25.254.50:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 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 ~]# 

clsuter集群维护

添加节点的时候是先添加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:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379
   slots: (0 slots) slave
   replicates c89826602345977f14b0a82faa354cd52dfd9196
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379
   slots: (0 slots) slave
   replicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3b58f505402276ee39a9acdaf1e9c73529250dd7
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: 5acbd4f2754d3c6168197d20623ab710c7a7dd94 172.25.254.10:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
M: ae5b1d985c911d539d643d91db648719f9a8e68b 172.25.254.50:6379
   slots:[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:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379
   slots: (0 slots) slave
   replicates c89826602345977f14b0a82faa354cd52dfd9196
M: 3b58f505402276ee39a9acdaf1e9c73529250dd7 172.25.254.20:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3b58f505402276ee39a9acdaf1e9c73529250dd7
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379
   slots: (0 slots) slave
   replicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: ae5b1d985c911d539d643d91db648719f9a8e68b 172.25.254.50:6379
   slots:[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:6379
   slots:[0-6826],[10923-12287] (8192 slots) master
   2 additional replica(s)
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379
   slots: (0 slots) slave
   replicates c89826602345977f14b0a82faa354cd52dfd9196
M: 3b58f505402276ee39a9acdaf1e9c73529250dd7 172.25.254.20:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3b58f505402276ee39a9acdaf1e9c73529250dd7
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379
   slots: (0 slots) slave
   replicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: ae5b1d985c911d539d643d91db648719f9a8e68b 172.25.254.50:6379     # 50上的hash槽位已经没了
   slots: (0 slots) slave
   replicates 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:6379
   slots:[0-6826],[10923-12287] (8192 slots) master
   1 additional replica(s)
S: 0aa139616179475ae13895ce5e34278d1a4ac771 172.25.254.110:6379
   slots: (0 slots) slave
   replicates c89826602345977f14b0a82faa354cd52dfd9196
M: 3b58f505402276ee39a9acdaf1e9c73529250dd7 172.25.254.20:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 52d45e025790388fef8ba556976d6f63f307dff5 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3b58f505402276ee39a9acdaf1e9c73529250dd7
S: b9f49140a5e8b9f883f9a7300896d635e6a24791 172.25.254.120:6379
   slots: (0 slots) slave
   replicates 5acbd4f2754d3c6168197d20623ab710c7a7dd94
M: c89826602345977f14b0a82faa354cd52dfd9196 172.25.254.30:6379
   slots:[12288-16383] (4096 slots) master
   1 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
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值