Redis集群

一、Redis简介

1. 什么是Redis

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

二、Redis安装

2.1 源码安装

在一台主机中不能即用rpm安装也用源码安装

[root@redis-node1 ~]# ls
公共  模板  视频  图片  文档  下载  音乐  桌面  anaconda-ks.cfg  redis-7.4.0.tar.gz
[root@redis-node1 ~]# tar zxf redis-7.4.0.tar.gz
[root@redis-node1 ~]# ls
公共  视频  文档  音乐  anaconda-ks.cfg  redis-7.4.0.tar.gz
模板  图片  下载  桌面  redis-7.4.0
​
#下载编译依赖包
[root@redis-node1 ~]# dnf install make gcc initscripts.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

三、Redis主从复制

1.主从同步过程

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

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

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

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

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

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

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

2.配置主从同步

2.1 修改master节点配置文件
[root@redis-node1 ~]# vim /etc/redis/6379.conf
protected-mode no         #关闭protected模式
​
[root@redis-node1 &node2 & node3 ~]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...
​
2.2 配置slave节点
[root@redis-node2 & 3 ~]# vim /etc/redis/6379.conf
replicaof 172.25.254.10 6379
[root@redis-node2 & 3 ~]# /etc/init.d/redis_6379 restart
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...

2.3 测试
#master节点插入信息
[root@redis-node1 utils]# redis-cli
127.0.0.1:6379> set name wang
OK
127.0.0.1:6379> get name
"wang"
​
#slave节点查看
[root@redis-node2 utils]# redis-cli
127.0.0.1:6379> get name
"wang"
​
[root@redis-node3 utils]# redis-cli
127.0.0.1:6379> get name
"wang"

四、Redis哨兵(高可用)

1.Redis哨兵

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

每个哨兵(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中的三个定时任务: 1.每10秒每个sentinel对master和slave执行info 发现slave节点 确认主从关系 2.每2秒每个sentinel通过master节点的channel交换信息(pub/sub) 通过sentinel__:hello频道交互 交互对节点的“看法”和自身信息 3.每1秒每个sentinel对其他sentinel和redis执行pi

2.哨兵实验过程

1.在master节点
------------在所有阶段中关闭 protected-mode no -----------
#编辑配置文件
[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分钟
​
​
####复制配置文件到两台slave节点中
[root@redis-node1 redis-7.4.0]# scp /etc/redis/sentinel.conf
root@172.25.254.201:/etc/redis/
root@172.25.254.20's password:
sentinel.conf
100% 14KB 9.7MB/s 00:00
[root@redis-node1 redis-7.4.0]# scp /etc/redis/sentinel.conf
root@172.25.254.200:/etc/redis/
root@172.25.254.30's password:
sentinel.conf
​
​
#做备份
[root@redis-node1 redis-7.4.0]# cp sentinel.conf sentinel.conf.bak

2.启动服务
[root@redis-node1 redis-7.4.0]# redis-sentinel /etc/redis/sentinel.conf
[root@redis-node2 redis-7.4.0]# redis-sentinel /etc/redis/sentinel.conf
[root@redis-node3 redis-7.4.0]# redis-sentinel /etc/redis/sentinel.conf

3.测试
#在开一个master节点终端
[root@redis-node1 ~]# redis-cli
127.0.0.1:6379> SHUTDOWN
​
#node2
36715:X 25 Aug 2024 15:55:42.002 # -odown master mymaster 172.25.254.10 6379
36715:X 25 Aug 2024 15:55:42.664 * +slave-reconf-inprog slave 172.25.254.20:6379 172.25.254.20 6379 @ mymaster 172.25.254.10 6379
36715:X 25 Aug 2024 15:55:42.664 * +slave-reconf-done slave 172.25.254.20:6379 172.25.254.20 6379 @ mymaster 172.25.254.10 6379
36715:X 25 Aug 2024 15:55:42.755 # +failover-end master mymaster 172.25.254.10 6379
36715:X 25 Aug 2024 15:55:42.756 # +switch-master mymaster 172.25.254.10 6379 172.25.254.30 6379
36715:X 25 Aug 2024 15:55:42.756 * +slave slave 172.25.254.20:6379 172.25.254.20 6379 @ mymaster 172.25.254.30 6379
36715:X 25 Aug 2024 15:55:42.756 * +slave slave 172.25.254.10:6379 172.25.254.10 6379 @ mymaster 172.25.254.30 6379
36715:X 25 Aug 2024 15:55:42.757 * Sentinel new configuration saved on disk
36715:X 25 Aug 2024 15:55:52.765 # +sdown slave 172.25.254.10:6379 172.25.254.10 6379 @ mymaster 172.25.254.30 6379
​
​
[root@redis-node2 ~]# redis-cli
127.0.0.1:6379> info replications
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.25.254.201,port=6379,state=online,offset=211455,lag=1
slave1:ip=172.25.254.100,port=6379,state=online,offset=211455,lag=1
master_failover_state:no-failover
master_replid:d42fd72f3dfae94c84ca722ad1653417495ef4fd
master_replid2:290c3407108cc6120086981b7149a6fa377594c4
master_repl_offset:211598
second_repl_offset:185931
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:150986
repl_backlog_histlen:60613
​

master更改为node3

node3查看:

配置文件中更改

五、Redis Cluster

5.1 部署redis cluster

在所有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
​
#重启
[root@redis-node1 ~]# systemctl enable --now redis
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systs.service.
​
#查看6379端口已开启
[root@redis-node1 ~]# netstat -antlupe | grep redis
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      977      282/redis-server
tcp        0      0 0.0.0.0:16379           0.0.0.0:*               LISTEN      977      282/redis-server
tcp6       0      0 :::6379                 :::*                    LISTEN      977      282/redis-server
tcp6       0      0 :::16379                :::*                    LISTEN      977      282/redis-server
​
#复制到其他node
[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 systemctl enable  --now redis
​
​
#node1中无法写入
[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> set name wang
(error) CLUSTERDOWN Hash slot not served
127.0.0.1:6379> quit
​

5.2 创建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
Warning: Using a password with '-a' or '-u' option on the command line interface may not
>>> 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: c7a8b0d93c302e0285acdf93cee4d69cce614d90 172.25.254.10:6379
   slots:[0-5460] (5461 slots) master
M: 3eba692261902cf9d4ecbe22e62dceff02b1c6ea 172.25.254.20:6379
   slots:[5461-10922] (5462 slots) master
M: a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08 172.25.254.30:6379
   slots:[10923-16383] (5461 slots) master
S: 86f23467487b7f1e1985264bc27ebd3de02e7f5f 172.25.254.110:6379
   replicates a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08
S: d9a4b0d1a92c15224773ff937aa7783ca8a8b56d 172.25.254.120:6379
   replicates c7a8b0d93c302e0285acdf93cee4d69cce614d90
S: bb5bbc578128be4c4d4975484d94a472ecb87302 172.25.254.130:6379
   replicates 3eba692261902cf9d4ecbe22e62dceff02b1c6ea
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: c7a8b0d93c302e0285acdf93cee4d69cce614d90 172.25.254.10:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: d9a4b0d1a92c15224773ff937aa7783ca8a8b56d 172.25.254.120:6379
   slots: (0 slots) slave
   replicates c7a8b0d93c302e0285acdf93cee4d69cce614d90
M: a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08 172.25.254.30:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 86f23467487b7f1e1985264bc27ebd3de02e7f5f 172.25.254.110:6379
   slots: (0 slots) slave
   replicates a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08
M: 3eba692261902cf9d4ecbe22e62dceff02b1c6ea 172.25.254.20:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: bb5bbc578128be4c4d4975484d94a472ecb87302 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3eba692261902cf9d4ecbe22e62dceff02b1c6ea
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
 

主从分配情况(随机)

检测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
172.25.254.10:6379 (c7a8b0d9...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.30:6379 (a8ecbc9c...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.20:6379 (3eba6922...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
​
#检测集群
[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
172.25.254.10:6379 (c7a8b0d9...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.30:6379 (a8ecbc9c...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.20:6379 (3eba6922...) -> 0 keys | 5462 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: c7a8b0d93c302e0285acdf93cee4d69cce614d90 172.25.254.10:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: d9a4b0d1a92c15224773ff937aa7783ca8a8b56d 172.25.254.120:6379
   slots: (0 slots) slave
   replicates c7a8b0d93c302e0285acdf93cee4d69cce614d90
M: a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08 172.25.254.30:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 86f23467487b7f1e1985264bc27ebd3de02e7f5f 172.25.254.110:6379
   slots: (0 slots) slave
   replicates a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08
M: 3eba692261902cf9d4ecbe22e62dceff02b1c6ea 172.25.254.20:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: bb5bbc578128be4c4d4975484d94a472ecb87302 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3eba692261902cf9d4ecbe22e62dceff02b1c6ea
[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
127.0.0.1:6379> set name wang
(error) MOVED 5798 172.25.254.20:6379   #分配到20的hash槽
127.0.0.1:6379> get name
(error) MOVED 5798 172.25.254.20:6379    
127.0.0.1:6379>
​
#只能在20上写入和读取
[root@redis-node2 ~]# redis-cli -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not
127.0.0.1:6379> set name wang
OK
127.0.0.1:6379> get name
"wang"
127.0.0.1:6379>
​

5.3 集群扩容

添加master
[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
​
#添加master
[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: c7a8b0d93c302e0285acdf93cee4d69cce614d90 172.25.254.10:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: d9a4b0d1a92c15224773ff937aa7783ca8a8b56d 172.25.254.120:6379
   slots: (0 slots) slave
   replicates c7a8b0d93c302e0285acdf93cee4d69cce614d90
M: a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08 172.25.254.30:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 86f23467487b7f1e1985264bc27ebd3de02e7f5f 172.25.254.110:6379
   slots: (0 slots) slave
   replicates a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08
M: 3eba692261902cf9d4ecbe22e62dceff02b1c6ea 172.25.254.20:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: bb5bbc578128be4c4d4975484d94a472ecb87302 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3eba692261902cf9d4ecbe22e62dceff02b1c6ea
[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 (c7a8b0d9...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.30:6379 (a8ecbc9c...) -> 0 keys | 5461 slots | 1 slaves.
172.25.254.50:6379 (56986691...) -> 0 keys | 0 slots | 0 slaves.
172.25.254.20:6379 (3eba6922...) -> 1 keys | 5462 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: c7a8b0d93c302e0285acdf93cee4d69cce614d90 172.25.254.10:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: d9a4b0d1a92c15224773ff937aa7783ca8a8b56d 172.25.254.120:6379
   slots: (0 slots) slave
   replicates c7a8b0d93c302e0285acdf93cee4d69cce614d90
M: a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08 172.25.254.30:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 86f23467487b7f1e1985264bc27ebd3de02e7f5f 172.25.254.110:6379
   slots: (0 slots) slave
   replicates a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08
M: 569866915eebbb86b2d59deda26f7c3467627ddf 172.25.254.50:6379
   slots: (0 slots) master
M: 3eba692261902cf9d4ecbe22e62dceff02b1c6ea 172.25.254.20:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: bb5bbc578128be4c4d4975484d94a472ecb87302 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3eba692261902cf9d4ecbe22e62dceff02b1c6ea
[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 --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: c7a8b0d93c302e0285acdf93cee4d69cce614d90 172.25.254.10:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: d9a4b0d1a92c15224773ff937aa7783ca8a8b56d 172.25.254.120:6379
   slots: (0 slots) slave
   replicates c7a8b0d93c302e0285acdf93cee4d69cce614d90
M: a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08 172.25.254.30:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 86f23467487b7f1e1985264bc27ebd3de02e7f5f 172.25.254.110:6379
   slots: (0 slots) slave
   replicates a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08
M: 569866915eebbb86b2d59deda26f7c3467627ddf 172.25.254.50:6379
   slots: (0 slots) master
M: 3eba692261902cf9d4ecbe22e62dceff02b1c6ea 172.25.254.20:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: bb5bbc578128be4c4d4975484d94a472ecb87302 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3eba692261902cf9d4ecbe22e62dceff02b1c6ea
[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?     569866915eebbb86b2d59deda26f7c3467627ddf    #给50分配槽位,50ID
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 569866915eebbb86b2d59deda26f7c3467627ddf
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 172.25.254.150:6379 to cluster 172.25.254.10:6379
>>> Performing Cluster Check (using node 172.25.254.10:6379)
M: c7a8b0d93c302e0285acdf93cee4d69cce614d90 172.25.254.10:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: d9a4b0d1a92c15224773ff937aa7783ca8a8b56d 172.25.254.120:6379
   slots: (0 slots) slave
   replicates c7a8b0d93c302e0285acdf93cee4d69cce614d90
M: a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08 172.25.254.30:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 86f23467487b7f1e1985264bc27ebd3de02e7f5f 172.25.254.110:6379
   slots: (0 slots) slave
   replicates a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08
M: 569866915eebbb86b2d59deda26f7c3467627ddf 172.25.254.50:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
M: 3eba692261902cf9d4ecbe22e62dceff02b1c6ea 172.25.254.20:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: bb5bbc578128be4c4d4975484d94a472ecb87302 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3eba692261902cf9d4ecbe22e62dceff02b1c6ea
[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.150:6379 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 172.25.254.50:6379.
[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 (c7a8b0d9...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.30:6379 (a8ecbc9c...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.50:6379 (56986691...) -> 1 keys | 4096 slots | 1 slaves.
172.25.254.20:6379 (3eba6922...) -> 0 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: c7a8b0d93c302e0285acdf93cee4d69cce614d90 172.25.254.10:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: d9a4b0d1a92c15224773ff937aa7783ca8a8b56d 172.25.254.120:6379
   slots: (0 slots) slave
   replicates c7a8b0d93c302e0285acdf93cee4d69cce614d90
M: a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08 172.25.254.30:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 4ce9a070db536e0d8a85527928753e9ce96b1e1a 172.25.254.150:6379
   slots: (0 slots) slave
   replicates 569866915eebbb86b2d59deda26f7c3467627ddf
S: 86f23467487b7f1e1985264bc27ebd3de02e7f5f 172.25.254.110:6379
   slots: (0 slots) slave
   replicates a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08
M: 569866915eebbb86b2d59deda26f7c3467627ddf 172.25.254.50:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 additional replica(s)
M: 3eba692261902cf9d4ecbe22e62dceff02b1c6ea 172.25.254.20:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: bb5bbc578128be4c4d4975484d94a472ecb87302 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3eba692261902cf9d4ecbe22e62dceff02b1c6ea
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

扩容成功

5.4 cluster集群维护

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

删除slave
#删除slave
[root@redis-node1 ~]# redis-cli -a 123456 --cluster del-node 172.25.254.150:6379  4ce9a070db536e0d8a85527928753e9ce96b1e1a
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 4ce9a070db536e0d8a85527928753e9ce96b1e1a 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 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 (c7a8b0d9...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.30:6379 (a8ecbc9c...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.50:6379 (56986691...) -> 1 keys | 4096 slots | 0 slaves.
172.25.254.20:6379 (3eba6922...) -> 0 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: c7a8b0d93c302e0285acdf93cee4d69cce614d90 172.25.254.10:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: d9a4b0d1a92c15224773ff937aa7783ca8a8b56d 172.25.254.120:6379
   slots: (0 slots) slave
   replicates c7a8b0d93c302e0285acdf93cee4d69cce614d90
M: a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08 172.25.254.30:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 86f23467487b7f1e1985264bc27ebd3de02e7f5f 172.25.254.110:6379
   slots: (0 slots) slave
   replicates a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08
M: 569866915eebbb86b2d59deda26f7c3467627ddf 172.25.254.50:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
M: 3eba692261902cf9d4ecbe22e62dceff02b1c6ea 172.25.254.20:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: bb5bbc578128be4c4d4975484d94a472ecb87302 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3eba692261902cf9d4ecbe22e62dceff02b1c6ea
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

150被删除

移除槽位
[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: c7a8b0d93c302e0285acdf93cee4d69cce614d90 172.25.254.10:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: d9a4b0d1a92c15224773ff937aa7783ca8a8b56d 172.25.254.120:6379
   slots: (0 slots) slave
   replicates c7a8b0d93c302e0285acdf93cee4d69cce614d90
M: a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08 172.25.254.30:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 86f23467487b7f1e1985264bc27ebd3de02e7f5f 172.25.254.110:6379
   slots: (0 slots) slave
   replicates a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08
M: 569866915eebbb86b2d59deda26f7c3467627ddf 172.25.254.50:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
M: 3eba692261902cf9d4ecbe22e62dceff02b1c6ea 172.25.254.20:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: bb5bbc578128be4c4d4975484d94a472ecb87302 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3eba692261902cf9d4ecbe22e62dceff02b1c6ea
[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? c7a8b0d93c302e0285acdf93cee4d69cce614d90    #移给谁,(10的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: 569866915eebbb86b2d59deda26f7c3467627ddf                          #移除谁的(50d的ID)
Source node #2: done

#查看 50槽位为空
[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 (c7a8b0d9...) -> 1 keys | 8192 slots | 2 slaves.
172.25.254.30:6379 (a8ecbc9c...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.20:6379 (3eba6922...) -> 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: c7a8b0d93c302e0285acdf93cee4d69cce614d90 172.25.254.10:6379
   slots:[0-6826],[10923-12287] (8192 slots) master
   2 additional replica(s)
S: d9a4b0d1a92c15224773ff937aa7783ca8a8b56d 172.25.254.120:6379
   slots: (0 slots) slave
   replicates c7a8b0d93c302e0285acdf93cee4d69cce614d90
M: a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08 172.25.254.30:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 86f23467487b7f1e1985264bc27ebd3de02e7f5f 172.25.254.110:6379
   slots: (0 slots) slave
   replicates a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08
S: 569866915eebbb86b2d59deda26f7c3467627ddf 172.25.254.50:6379
   slots: (0 slots) slave
   replicates c7a8b0d93c302e0285acdf93cee4d69cce614d90
M: 3eba692261902cf9d4ecbe22e62dceff02b1c6ea 172.25.254.20:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: bb5bbc578128be4c4d4975484d94a472ecb87302 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3eba692261902cf9d4ecbe22e62dceff02b1c6ea
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

移除完显示50没有数据,即成功

50槽位为空

删除master
[root@redis-node1 ~]# redis-cli -a 123456 --cluster del-node 172.25.254.50:6379 569866915eebbb86b2d59deda26f7c3467627ddf
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 569866915eebbb86b2d59deda26f7c3467627ddf 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 ~]# 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 (c7a8b0d9...) -> 1 keys | 8192 slots | 1 slaves.
172.25.254.30:6379 (a8ecbc9c...) -> 0 keys | 4096 slots | 1 slaves.
172.25.254.20:6379 (3eba6922...) -> 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: c7a8b0d93c302e0285acdf93cee4d69cce614d90 172.25.254.10:6379
   slots:[0-6826],[10923-12287] (8192 slots) master
   1 additional replica(s)
S: d9a4b0d1a92c15224773ff937aa7783ca8a8b56d 172.25.254.120:6379
   slots: (0 slots) slave
   replicates c7a8b0d93c302e0285acdf93cee4d69cce614d90
M: a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08 172.25.254.30:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: 86f23467487b7f1e1985264bc27ebd3de02e7f5f 172.25.254.110:6379
   slots: (0 slots) slave
   replicates a8ecbc9c54bdd70e1f68b6c02e1ce5d4b907ff08
M: 3eba692261902cf9d4ecbe22e62dceff02b1c6ea 172.25.254.20:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: bb5bbc578128be4c4d4975484d94a472ecb87302 172.25.254.130:6379
   slots: (0 slots) slave
   replicates 3eba692261902cf9d4ecbe22e62dceff02b1c6ea
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值