欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

从零开始的linux学习(11)

程序员文章站 2024-03-14 11:50:04
...

编译安装redis

https://download.redis.io/releases/redis-6.0.8.tar.gz下载源码包

[14:41:44 aaa@qq.com ~]#yum -y install gcc jemalloc-devel #安装编译安装redis依赖包
[14:45:56 aaa@qq.com ~]#wget https://download.redis.io/releases/redis-6.0.8.tar.gz #下载redis源码包
[14:48:29 aaa@qq.com ~]#tar xf redis-6.0.8.tar.gz #解压源码包
[14:49:46 aaa@qq.com ~]#cd redis-6.0.8/
[14:53:09 aaa@qq.com redis-6.0.8]#make PREFIX=/apps/redis install #指定安装路径,并编译安装
[14:53:09 aaa@qq.com redis-6.0.8]#echo 'PATH=/apps/redis/bin:$PATH' > /etc/profile.d/redis.sh #配置环境变量
[14:57:29 aaa@qq.com redis-6.0.8]#source /etc/profile.d/redis.sh #更改环境变量
[14:58:35 aaa@qq.com redis-6.0.8]#mkdir /apps/redis/{etc,log,data,run} #创建配置文件、日志、数据等鲁姆
[14:58:54 aaa@qq.com redis-6.0.8]#cp redis.conf /apps/redis/etc/ #拷贝配置文件到redis配置文件目录
[14:59:04 aaa@qq.com redis-6.0.8]#useradd -r -s /sbin/nologin redis #创建redis账号,使redis账号无法登录
[15:10:37 aaa@qq.com redis-6.0.8]#vi /usr/lib/systemd/system/redis.service  #创建redis服务启动脚本

[Unit]                                                                                                     
Description=Redis persistent key-value database
After=network.target
[Service]
ExecStart=/apps/redis/bin/redis-server /apps/redis/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT $MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target

[15:08:01 aaa@qq.com redis-6.0.8]#systemctl daemon-reload #重新加载服务启动脚本
[15:08:16 aaa@qq.com redis-6.0.8]#systemctl start redis 

redis的主要配置项

bind 0.0.0.0 #监听地址,可以用空格隔开后监听多个IP
protected-mod yes #redis3.2之后加入的新特性,在没有设置bind IP和密码的时候,redis只允许访问127.0.0.1:6379,可以远程链接,但当访问时将提示警告信息并拒绝远程访问
port 6379 #监听端口,默认tcp 6379
tcp-backlog 511 #三次握手的时候服务端收到客户端ack确认号之后的队列值,即全连接队列长度
timeout 0 #客户端和redis服务端链接超时时间,默认0是永不超时
tcp-keepalive 300 #tcp 会话保持时间300秒
daemonize no #默认no,直接运行redis-server程序时,不作为守护进程运行,而是以前台方式运行,如果想在后台运行需改成yes,当redis作为守护进程运行的时候,他会写一个pid到/var/run/redis.pid文件中
supervised no #和OS相关参数,可设置通过upstart和systemd管理redis守护进程,centos7后都使用sysytemd
pidfile /var/run/redis_6379.pid #pid文件路径,可以修改为/apps/redis/run/redis_6379.pid
loglevel notice #记录日志级别
logfile "/path/redis.log" #日志文件路径
databases 16 #设置数据库数量,默认16个库,0-15
always-show-logo yes #在启动redis后是否在日志中记录redis的log
save 900 1 #在900秒内有1个key内容发生更改,就执行快照机制
save 300 10 #在300秒内有10个key内容发生更改,就执行快照机制
save 60 10000 #在60秒内如果有10000个key以上的变化,就自动快照备份
stop-writes-on-bgsave-error yes #默认为yes,可能会因空间满等原因快照无法保持出错时,会禁止redis写操作,生产中建议为no     #此选项只针对配置文件中的自动save有效
rdbcompression yes #持久化到rdb文件时,是否压缩,yes为压缩,no为不压缩
rdbchecksum yes #是否对备份文件开启RC64校验,默认开启
dbfilename dump.rdb #默认快照文件名
dir ./ #快照文件保存路径

#redis主从复制相关选项

# replicaof <masterip> <masterport>  #指定复制的master主机地址和端口,5.0版本之前的指令为slaveof
# masterauth <master-password> #指定复制的master主机的密码
replica-serve-stale-data yes #当从库同主库失去连接或复制正在进行,从库有两种运行方式:
#1.设置yes时,从库会继续相应客户端的读请求,建议设置yes
#2.设置no时,出去特定命令外的任何请求都会返回一个错误”SYNC with master in progeress“
replica-read-only yes #是否设置从库只读
repl-diskless-sync no #是否使用socket方式复制数据(无盘同步),新从节点第一次链接主节点时需要做数据的全量同步,redis server就要从内存备份出新的rdb文件,然后从master传到slave,有两种方式传给客户端:
#1.基于硬盘:为no时,master创建一个新进程备份生成rdb文件,生成之后由主进程将rdb发送到从节点上
#2.基于socket:master创建一个新进程直接备份rdb文件到从节点的网络socket,不经过主进程和硬盘
#推荐使用基于硬盘的方式,因为rdb文件创建后,可以同时传输给更多的slave,但是基于socket的方式,新从节点连接到主节点之后需要逐个同步数据,只有当磁盘i/o较慢且网络较快时,可用此模式
repl-diskless-sync-delay 5 #diskless时复制的服务器等待的延迟时间,设置0为关闭,在延迟时间内到达客户端,会一起通过diskless方式同步数据,但是一旦复制开始,主节点不会在接受新从节点的复制请求,知道下一次同步开始才再接受新请求。即无法为延迟事件后到达的新副本提供服务,新副本将排队等待下一次RDB传输,因此服务器会等待一段时间才能让更多副本到达。建议设置30-60
repl-ping-replica-period 10 #从节点根据主节点指定的时间进行周期性的ping主节点,用于监测主节点的状态
repl-timeout 60 #复制连接的超时时间,需要大于repl-ping-slave-period,否则会经常报超时
repl-disbale-tcp-nodelay no #是否在从节点套接字发送sync之后禁用TCP_NODELAY,如果选择yes,redis将合并多个保温为一个大的保温,从而使用更少数量的包向从节点发送数据,但是将使数据传输到从节点上有延迟,linux内核的默认配置会达到40毫秒,如果设置为no,数据传输到从节点的延迟会减少,弹药使用更多的带宽
repl-backlog-size 512mb #复制缓冲区内存大小,当从节点断开连接一段时间后,该缓冲区会累积复制副本数据,因此当从节点重新连接时,通常不需要重新完全同步,只需要传递在副本中的断开连接后没有同步的部分数据即可。只有在至少有一个从节点连接之后才分配此内存空间,建立主从时此值需要调大一些,或者在低峰期配置,否则可能会导致同步到从节点失败
repl-backlog-ttl 3600 #多长时间内主节点没有从节点连接,就情况backlog缓冲区
replica-priority 100 #当主节点不可用,烧饼会根据从节点的优先级选举一个新的主节点,此值最低的从节点会当选,二配置为0时,永远不会被选举,一般多个从节点配置相同的值,让哨兵自动选择
#min-replicas-to-write 3 #至少有3个可连接的从节点,主节点才接受写操作
#min-replicas-max-lag 10 #和上面至少3个从节点的ping延迟不能超过10秒,否则主节点也将停止写操作
requirepass foobared #设置redis连接密码,之后需要AUTH pass,如果有特殊符号,需要用”“引起来
rename-command #重命名一些高危命令,rename-command FLUSHALL "" 为禁用命令
maxmemory <bytes> #redis使用的最大内存,单位为字节,0为不限制,建议设为物理内存一般,需要注意缓冲区不计算在maxmemory中,生产中如果不设置此项,可能会导致OOM
appendonly no #是否开启AOF日志记录,默认redis使用的是rdb方式持久化,这种方式在许多应用中已经足够用了,但是redis如果中途宕机,会导致可能有几分钟的数据丢失(取决于备份数据的间隔时间),根据save来策略持久化,append only file 是另一种持久化方式,可以提供更好的持久化特性,redis会吧每次写入的数据在接收后都写入appendonly.aof文件,每次启动redis都会把这个文件的数据读入内存里,先忽略rdb为念,默认不启用
appendfilename "appendon1y.aof"  #文本文件AOF的文件名,存放在dir指令指定的目录中
appendfsync everysec #aof持久化策略的配置
#no表示由操作系统保证数据同步到磁盘, Linux的默认fsync策略是30秒,最多会丢失30s的数据#a7ways表示每次写入都执行fsync,以保证数据同步到磁盘,安全性高,性能较差
#everysec表示每秒执行一次fsync,可能会导致丢失这1s数据,此为默认值,也是生产建议值
#同时在执行bgrewriteaof操作和主进程写aof文件的操作,两者都会操作磁盘,而bgrewriteaof往往会涉及大量磁盘操作,这样就会造成主进程在写aof文件的时候出现阻塞的情形,以下参数实现控制
no-appendfsync-on-rewrite no #在aof rewrite期间,是否对aof新记录的append暂缓使用文件同步策略,主要考虑磁盘Io开支和请求阻塞时间。
#默认为no,表示"不暂缓" ,新的aof记录仍然会被立即同步到磁盘,是最安全的方式,不会丢失数据,但是要忍受阻塞的问题
#为yes ,相当于将appendfsync设置为no,这说明并没有执行磁盘操作,只是写入了缓冲区,因此这样并不会造成阻塞(因为没有竞争磁盘〉,但是如果这个时候redis挂掉,就会丢失数据。丢失多少数据呢? Linux的默认fsync策略是30秒,最多会丢失30s的数据,但由于yes性能较好而且会避免出现阻塞因此比较推荐
#rewrite 即对aof文件进行整理,将空闲空间回收,从而可以减少恢复数据时间
auto-aof-rewrite-percentage 100 #当Aof 1og增长超过指定百分比例时,重写AOF文件,设置为O表示不自动重写Aof日志,重写是为了使aof体积保持最小,但是还可以确保保存最完整的数据
auto-aof-rewrite-min-size 64mb #触发aof rewrite的最小文件大小
aof-load-truncated yes #是否加载由于某些原因导致的末尾异常的AOF文件(主进程被ki11/断电等),建议yes
aof-use-rdb-preamble no #redis4.0新增RDB-AOF混合持久化格式,在开启了这个功能之后,AOF重写产生的文件将同时包含RDB格式的内容和AOF格式的内容,其中RDB格式的内容用于记录已有的数据,而AOF格式的内容则用于记录最近发生了变化的数据,这样Redis就可以同时兼有RDB持久化和AOF持久化的优点
7ua-time-7imit 5000 #1ua脚本的最大执行时间,单位为毫秒
cluster-enabled yes #是否开启集群模式,默认不开启,即单机模式cluster-config-file nodes-6379.conf #由node节点自动生成的集群配置文件名称
cluster-config-file nodes-6379.conf #由node节点自动生成的集群配置文件名称
cluster-replica-validity-factor 10 #单位为次,在执行故障转移的时候可能有些节点和master断开一段时间导致数据比较旧,这些节点就不适用于选举为master,超过这个时间的就不会被进行故障转移,不能当选master,计算公式:(node-timeout * replica-validity-factor) + rep1-ping-replica-period
cluster-migration-barrier 1#集群迁移屏障,一个主节点至少拥有1个正常工作的从节点,即如果主节点的s1ave节点故障后会将多余的从节点分配到当前主节点成为其新的从节点。
cluster-require-fu77-coverage yes#集群请求槽位全部覆盖,如果一个主库宕机且没有备库就会出现集群槽位不全,那么yes时redis集群槽位验证不全,就不再对外提供服务(对key赋值时,会出现CLUSTERDOwN The cluster is down的提示, cluster_state:fai1,但ping 仍PONG),而no则可以继续使用,但是会出现查询数据查不到的情况(因为有数据丢失)。生产建议为no
cluster-replica-no-failover no #如果为yes,此选项阻止在主服务器发生故障时尝试对其主服务器进行故障转移。但是,主服务器仍然可以执行手动强制故障转移,一般为no
#Slow log是 Redis用来记录超过指定执行时间的日志系统,执行时间不包括与客户端交谈,发送回复等工/o操作,而是实际执行命令所需的时间((在该阶段线程被阻塞并且不能同时为其它请求提供服务),由于slow log 保存在内存里面,读写速度非常快,因此可放心地使用,不必担心因为开启 s1ow log而影响Redis的速度
slowTog-log-slower-than 10000 #以微秒为单位的慢日志记录,为负数会禁用慢日志,为O会记录每个命令操作。默认值为10ms ,一般一条命令执行都在微秒级,生产建议设为1ms
slowlog-max-len 128#最多记录多少条慢日志的保存队列长度,达到此长度后,记录新命令会将最旧的命令从命令队列中删除,以此滚动删除,即,先进先出,队列固定长度,默认128,值偏小,生产建议设为1000以上

解决启动时的三个警告

1、tcp-backlog

1940:M 23 Oct 2020 18:38:45.794 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

backlog参数控制的是三次握手的时候服务端接受客户端ack确认号之后的队列值,可以修改扩大内核参数来取消该报警
[18:38:45 aaa@qq.com ~]#vi /etc/sysctl.conf 

# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.core.somaxconn = 1024

[18:44:28 aaa@qq.com ~]#sysctl -p #使系统读取新修改的内核参数
net.core.somaxconn = 1024

2、vm.overcommit_memory

1940:M 23 Oct 2020 18:38:45.794 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

0 表示内核将检查是否有足够的可用内存供应用进程使用;如果有足够的可用内存,内存申请允许;否则,内存申请失败,并把错误返回给应用进程。
1 表示内核允许分配所有的物理内存,而不管当前的内存状态如何
2 表示内核允许分配超过所有物理内存和交换空间总和的内存

[18:46:24 aaa@qq.com ~]#vi /etc/sysctl.conf 

# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.core.somaxconn = 1024
vm.overcommit_memory = 1 
[18:46:37 aaa@qq.com ~]#sysctl -p #使系统读取新修改的内核参数
net.core.somaxconn = 1024
vm.overcommit_memory = 1

3、Transparent Huge Pages

1940:M 23 Oct 2020 18:38:45.794 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.

翻译: 警告:您在内核中启用了透明大页面(THP,不同于一般内存页的4k为2M)支持。这将在Redis中造成延迟和内存使用问题。要解决此问题,请以root用户身份运行命令"echo never > /sys/kernel/mm/transparent_hugepage/enabled”,并将其添加到您的/etc/rc.loca1中,以便在重启后保留设置。禁用THP后,必须重新启动Redis。

[18:46:39 aaa@qq.com ~]#vi /etc/rc.d/rc.local 

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local

echo never > /sys/kernel/mm/transparent_hugepage/enabled
[18:49:48 aaa@qq.com ~]#chmod +x /etc/rc.d/rc.local #为rc.local添加执行权限
[18:51:58 aaa@qq.com ~]#. /etc/rc.d/rc.local 

重启redis服务验证更改是否成功

[18:51:59 aaa@qq.com ~]#systemctl restart redis
[18:56:31 aaa@qq.com ~]#cat /var/log/redis/redis.log
			   _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.3 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 1977
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

1977:M 23 Oct 2020 18:56:23.276 # Server initialized
1977:M 23 Oct 2020 18:56:23.276 * DB loaded from disk: 0.000 seconds
1977:M 23 Oct 2020 18:56:23.276 * Ready to accept connections

Redis常用命令

INFO

显示当前节点redis的运行状态信息

127.0.0.1:6379> info
# Server
redis_version:5.0.3
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:28849dbea6f07cc8
redis_mode:standalone
os:Linux 4.18.0-193.el8.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:8.3.1
process_id:1977
...

SELECT

切换数据库,redis在cluster模式下不支持多个数据库,只支持0数据库

127.0.0.1:6379> SELECT 1
OK

KEYS

查看当前库下所有的key,支持通配符,keys * 可能会导致服务器崩溃,慎用

BGSAVE

手动在后台执行RDB持久化操作

DBSIZE

查询当前库下所有key的数量

FLUSHDB

强制清空当前库中所有的key,慎用

FLUSHALL

强制情况当前redis服务器所有数据库中的key,慎用,建议禁用此命令

SHUTDOWN

SHUTDOWN 命令执行以下操作:

    1停止所有客户端
    2如果有至少一个保存点在等待,执行 SAVE 命令
    3如果 AOF 选项被打开,更新 AOF 文件
    4关闭 redis 服务器(server)

如果持久化被打开的话, SHUTDOWN 命令会保证服务器正常关闭而不丢失任何数据。

另一方面,假如只是单纯地执行 SAVE 命令,然后再执行 QUIT 命令,则没有这一保证 —— 因为在执行 SAVE 之后、执行 QUIT 之前的这段时间中间,其他客户端可能正在和服务器进行通讯,这时如果执行 QUIT 就会造成数据丢失。

SAVE 和 NOSAVE 修饰符

通过使用可选的修饰符,可以修改 SHUTDOWN 命令的表现。比如说:

    执行 SHUTDOWN SAVE 会强制让数据库执行保存操作,即使没有设定(configure)保存点
    执行 SHUTDOWN NOSAVE 会阻止数据库执行保存操作,即使已经设定有一个或多个保存点(你可以将这一用法看作是强制停止服务器的一个假想的 ABORT 命令)

Redis主从复制

全量同步原理

从零开始的linux学习(11)

1、从节点主动向主节点发送同步请求

2、主节点接收到从的消息后发送自己的runid和offset发送给从节点

3、从节点保存主节点发送过来的runid和offset

4、主节点后台执行RDB持久化,并将新写入的数据写入到缓冲区中

5、主节点将RDB文件发送到从节点

6、主节点在把RDB文件发送到从节点后,把buffer中的数据以redis协议格式发送到从节点

7、从节点清空自己旧的数据

8、从节点把主节点推送过来的RDB文件中的数据载入内存,再加载所有缓冲区中的内容完成全量同步

增量同步原理

全量同步完成后,如从节点需要继续同步增量数据的话,会先将自己的offset位置发送给主节点,主节点在收到后会根据从节点的offset,将从节点offset位置之后的数据发送回从节点,同时也将在缓冲区的数据也发送给从节点

实现redis主从复制

#主节点配置
[14:23:27 aaa@qq.com ~]#grep -Ev '^#|^$' /etc/redis.conf 
bind 0.0.0.0 #修改监听地址
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis.log
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
requirepass 123456 #修改redis连接密码
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

#从节点配置

[14:41:13 aaa@qq.com ~]#grep -Ev '^#|^$' /etc/redis.conf 
bind 0.0.0.0 #修改监听地址
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis.log
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
replicaof 10.0.0.8 6379 #指向主节点的IP和端口
masterauth 123456 #主节点redis登录密码
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
requirepass 123456
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

[14:41:38 aaa@qq.com ~]#redis-cli -a 123456 info
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Server
redis_version:5.0.3
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:28849dbea6f07cc8
redis_mode:standalone
os:Linux 4.18.0-193.el8.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:8.3.1
process_id:1529
run_id:8e3c59a18e004109478f55b0d195c119dacd7784
tcp_port:6379
uptime_in_seconds:1615
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:9687765
executable:/usr/bin/redis-server
config_file:/etc/redis.conf

# Clients
connected_clients:2
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0

# Memory
used_memory:1923424
used_memory_human:1.83M
used_memory_rss:10305536
used_memory_rss_human:9.83M
used_memory_peak:1923424
used_memory_peak_human:1.83M
used_memory_peak_perc:101.15%
used_memory_overhead:1906216
used_memory_startup:791024
used_memory_dataset:17208
used_memory_dataset_perc:1.52%
allocator_allocated:1986896
allocator_active:2277376
allocator_resident:4722688
total_system_memory:829939712
total_system_memory_human:791.49M
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:1.15
allocator_frag_bytes:290480
allocator_rss_ratio:2.07
allocator_rss_bytes:2445312
rss_overhead_ratio:2.18
rss_overhead_bytes:5582848
mem_fragmentation_ratio:5.54
mem_fragmentation_bytes:8445016
mem_not_counted_for_evict:0
mem_replication_backlog:1048576
mem_clients_slaves:0
mem_clients_normal:66616
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1603521670
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:0
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0

# Stats
total_connections_received:1
total_commands_processed:159
instantaneous_ops_per_sec:0
total_net_input_bytes:2427
total_net_output_bytes:57667
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.04
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0

# Replication
role:slave #主从状态为从
master_host:10.0.0.8 #主的IP地址
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:2212
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:4748257a3b239514e35f96519b72de2b446ad785
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2212
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2212

# CPU
used_cpu_sys:1.735199
used_cpu_user:0.169179
used_cpu_sys_children:0.000000
used_cpu_user_children:0.000000

# Cluster
cluster_enabled:0

# Keyspace

故障迁移

首先找到一个offset最新的节点,用REPLICAOF NO ONE停止该节点的同步,停止以后会把该节点转化为主节点,然后把其他从节点重新指向该节点即可

#找到offse最新的节点并停止向主同步
[15:08:05 aaa@qq.com ~]#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> REPLICAOF NO ONE
OK
127.0.0.1:6379> INFO REPLICATION  #成功转换为主节点
# Replication
role:master
connected_slaves:0
master_replid:6d4a3855aa3759d24bbd6875c2172a4a43260958
master_replid2:4748257a3b239514e35f96519b72de2b446ad785
master_repl_offset:5908
second_repl_offset:5909
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:5908
127.0.0.1:6379> 

#其余从节点修改主节点的地址
[16:02:02 aaa@qq.com ~]#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> REPLICAOF 10.0.0.18 6379 #指向新主节点地址
OK
127.0.0.1:6379>CONFIG SET masterauth "123456" #设置新主节点密码
oK
127.0.0.1:6379> INFO REPLICATION
# Replication
role:slave
master_host:10.0.0.18 #成功切换主节点
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_repl_offset:5950
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:6d4a3855aa3759d24bbd6875c2172a4a43260958
master_replid2:4748257a3b239514e35f96519b72de2b446ad785
master_repl_offset:5950
second_repl_offset:5909
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:5783
repl_backlog_histlen:168

redis级联复制

redis级联复制不同于mysql的级联复制,中间从节点无需做其他配置,只需将连接中间节点的从节点的主节点指向中间从节点即可

#从节点配置
[16:17:24 aaa@qq.com ~]#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> REPLICAOF 10.0.0.18 6379 #修改主节点
OK
127.0.0.1:6379>CONFIG SET masterauth "123456" #设置主节点密码
oK
127.0.0.1:6379> INFO REPLICATION
# Replication
role:slave
master_host:10.0.0.18
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:154
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:410446a1f52b14293374607722c85c6d77196a87
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:154
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:15
repl_backlog_histlen:140

redis主从复制优化相关选项

repl-diskless-sync no #是否使用无盘同步RDB文件,默认为no,no为不使用无盘,需要将RDB文件保存到磁盘后再发送给slave,yes为支持无盘,支持无盘就是RDB文件不需要保存至本地磁盘,而且直接通过socket文件发送给slave
rep7-diskless-sync-delay 5 #diskless时复制的服务器等待的延迟时间
rep7-ping-slave-period 10 #slave端向server端发送ping的时间间隔,默认为10秒
rep7-timeout 60 #设置主从ping连接超时时间,超过此值无法连接,master_link_status显示为down,并记录错误日志
rep7-disable-tcp-nodelay no #是否启用TCP_NODELAY,如设置成yes,则redis会合并小的TCP包从而节省带宽,但会增加同步延迟(40ms),造成master与slave数据不一致,假如设置成no,则redismaster会立即发送同步数据,没有延迟,yes关注性能,no关注redis服务中的数据一致性
rep7-backlog-size 1mb #master的写入数据缓冲区,用于记录自上一次同步后到下一次同步过程中间的写入命令,计算公式: rep7-backlog-size = 允许从节点最大中断时长*主实例offset每秒写入量,比如master每秒最大写入64mb,最大允许60秒,那么就要设置为64mb*60秒=3840MB(3.8G),建议此值是设置的足够大
repl-backlog-ttl 3600 #如果一段时间后没有slave连接到master,则backlog size的内存将会被释放。如果值为0则表示永远不释放这部份内存。
slave-priority 100 #slave端的优先级设置,值是一个整数,数字越小表示优先级越高。当master故障时将会按照优先级来选择slave端进行恢复,如果值设置为0,则表示该slave永远不会被选择。
min-replicas-to-write 1#设置一个master的可用slave不能少于多少个,否则master无法执行写
min-slaves-max-lag 20 #设置至少有上面数量的slave延迟时间都大于多少秒时,master不接收写操作(拒绝写入)

redis哨兵

哨兵工作原理

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

哨兵(Sentinel)是一个分布式系统,可以在一个架构中运行多个哨兵(sentinel)进程,这些进程使用流言协议(gossip protocols)来接收关于Master主服务器是否下线的信息,并使用投票协议(AgreementProtocols)来决定是否执行自动故障迁移,以及选择哪个Slave作为新的Master

每个哨兵(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节点集合,不再是具体的Redis节点,但Sentinel只是配置中心不是代理。

Redis Sentinel节点与普通redis没有区别,要实现读写分离依赖于客户端程序

redis 3.0之前版本中,生产环境一般使用哨兵模式,但3.0后推出redis cluster功能后,可以支持更大规模的生产环境

哨兵在完成故障转移后会自动修改节点的配置文件

哨兵的三个定时任务

每10秒每个sentinel对master和slave执行info发现slave节点,确认主从关系

每2秒每个sentinel通过master节点的channel交换信息(pub/sub)通过sentinel_:hello频道交互,交互对节点的"看法"和自身信息

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

实现哨兵

#主节点redis配置
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis.log
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
masterauth 123456
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
requirepass 123456
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

#从节点redis配置
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis.log
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
replicaof 10.0.0.8 6379 #指定主节点IP和端口
masterauth 123456 #指定主节点连接密码
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
requirepass 123456
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

[19:10:03 aaa@qq.com ~]#grep -Ev '^#|^$' /etc/redis-sentinel.conf #修改哨兵的配置文件,所有的哨兵配置都一样
port 26379
daemonize no
pidfile /var/run/redis-sentinel.pid
dir /tmp
sentinel monitor mymaster 10.0.0.8 6379 2 #指定mymaster集群master的IP和端口,当有两个以上的哨兵认为主节点宕机,执行故障迁移,哨兵数为总哨兵数的一半以上,redis和哨兵建议为奇数,否则容易出现脑裂现象
sentinel auth-pass mymaster 123456 #指定当前mymaster集群中master的密码
sentinel down-after-milliseconds mymaster 3000 #判断当前mymaster集群中所有节点的主观下线时间,建议设定为3000,单位是毫秒
sentinel parallel-syncs mymaster 1 #发生故障转移后,同时向新master同步数据的slave数量,越少越可以减轻新master的压力,但是相应的同步时间也会更长
sentinel failover-timeout mymaster 180000 #所有slave指向新的master的超时时间,单位是毫秒
sentinel deny-scripts-reconfig yes #禁止修改脚本
logfile /var/log/redis/sentinel.log #哨兵的日志

[20:13:11 aaa@qq.com ~]#systemctl enable --now redis-sentinel.service #在所有节点上启动哨兵

ttl 查某个key的有效期,字符串默认永久有效

set KEY 值 EX 有效期,以秒为单位

set KEY 值 PX 有效期,以毫秒为单位

set KEY 值 NX 检查是否存在,如存在则不会创建

set KEY 值 XX 检查是否存在,如存在则修改

mset 一次性创建多个键值对

mget 一次查找多个键的值

Redis Cluster

Redis Cluster的特点

1.所有Redis节点使用(PING机制)互联

⒉集群中某个节点的是否失效,是由整个集群中超过半数的节点监测都失效,才能算真正的失效

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节点上,从而有效解决单机瓶颈。

利用原生命令手动部署redis cluster

使用六台centos8服务器

[13:03:10 aaa@qq.com ~]#yum -y install redis #在所有6个节点上安装redis服务
[13:04:02 aaa@qq.com ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf #修改所有节点的配置文件,按顺序排序分别是:修改redis监听端口;修改主节点密码;修改自身登录密码;开启cluster模式;配置cluster的配置文件路径,记录主从关系和槽位信息;关闭集群请求槽位全覆盖,该选项开启的话,如果集群中有一个主库宕机且无备库就会使整个集群不对外提供服务
[13:31:49 aaa@qq.com ~]#systemctl enable --now redis #启动redis并设置为开机启动
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.
[14:18:47 aaa@qq.com ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet 10.0.0.18 6379 #使用meet使所有节点实现互相通信,登录任意一台节点来meet其他所有要加入到本集群中的节点
OK
[14:24:12 aaa@qq.com ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet 10.0.0.28 6379
OK
[14:24:19 aaa@qq.com ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet 10.0.0.38 6379
OK
[14:24:22 aaa@qq.com ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet 10.0.0.48 6379
OK
[14:24:24 aaa@qq.com ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet 10.0.0.58 6379
OK
[14:32:31 aaa@qq.com ~]#vi addslot.sh #编写一个分配槽位的脚本,并利用脚本分配槽位,redis cluster的槽位范围为0-16364

#!/bin/bash
# 
#********************************************************************
#Author:            rzx
#QQ:                970707452
#Date:              2020-10-25
#FileName:         addslot.sh
#URL:               https://www.cnblogs.com/rzx-006/
#Description:      The test script
#Copyright (C):    2020 All rights reserved
#********************************************************************
host=$1
port=$2
start=$3
end=$4
pass=123456
for slot in `seq ${start} ${end}`;do
    echo slot:$slot
    redis-cli -h ${host} -p ${port} -a ${pass} --no-auth-warning cluster addslots ${slot}
done

[14:46:05 aaa@qq.com ~]#bash addslot.sh 10.0.0.8 6379 0 5461 #利用脚本为10.0.0.8分配槽位
[14:46:05 aaa@qq.com ~]#redis-cli -h 10.0.0.8 -a 123456 cluster info #查看10.0.0.8的集群状态
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:ok
cluster_slots_assigned:5462 #整个集群当中已经分配的槽位数量
cluster_slots_ok:5462
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:1
cluster_current_epoch:5
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1342
cluster_stats_messages_pong_sent:1341
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:2688
cluster_stats_messages_ping_received:1341
cluster_stats_messages_pong_received:1347
cluster_stats_messages_received:2688
[14:47:02 aaa@qq.com ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster nodes
66173bbc45b3f8e9212286827bb5fc594aab0cd7 10.0.0.58:aaa@qq.com master - 0 1603608771000 5 connected
4ad896ab9a5e573061decaea13ca72f1acc16d22 10.0.0.48:aaa@qq.com master - 0 1603608772000 3 connected
e1e8b3a666bb3f15b3b73c689782d9a1236beab3 10.0.0.38:aaa@qq.com master - 0 1603608773663 0 connected
208ee5ef0b688df38824552b2154b64869175b9e 10.0.0.8:aaa@qq.com myself,master - 0 1603608773000 1 connected 0-5461 #分配到的槽位号
b75c9952210283a64f1ce10c5e1ad85d18dc1b36 10.0.0.28:aaa@qq.com master - 0 1603608771616 4 connected
50083258803ac6bc106182853e6d0bc1ebdee45d 10.0.0.18:aaa@qq.com master - 0 1603608772647 2 connected

[14:54:10 aaa@qq.com ~]#bash addslot.sh 10.0.0.18 6379 5462 10922 #利用脚本为10.0.0.18分配槽位
[14:55:29 aaa@qq.com ~]#bash addslot.sh 10.0.0.28 6379 10923 16383 #利用脚本为10.0.0.28分配槽位

[14:55:29 aaa@qq.com ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster nodes
66173bbc45b3f8e9212286827bb5fc594aab0cd7 10.0.0.58:aaa@qq.com master - 0 1603609539000 5 connected
4ad896ab9a5e573061decaea13ca72f1acc16d22 10.0.0.48:aaa@qq.com master - 0 1603609540900 3 connected
e1e8b3a666bb3f15b3b73c689782d9a1236beab3 10.0.0.38:aaa@qq.com master - 0 1603609541915 0 connected
208ee5ef0b688df38824552b2154b64869175b9e 10.0.0.8:aaa@qq.com myself,master - 0 1603609536000 1 connected 0-5461
b75c9952210283a64f1ce10c5e1ad85d18dc1b36 10.0.0.28:aaa@qq.com master - 0 1603609539884 4 connected 10923-16383
50083258803ac6bc106182853e6d0bc1ebdee45d 10.0.0.18:aaa@qq.com master - 0 1603609540000 2 connected 5462-10922

[15:05:42 aaa@qq.com ~]#redis-cli -h 10.0.0.38 -a 123456 --no-auth-warning cluster replicate 208ee5ef0b688df38824552b2154b64869175b9e
OK #连接到10.0.0.38,为其指定master节点,master节点ID可以通过cluster nodes查看
[15:14:31 aaa@qq.com ~]#redis-cli -h 10.0.0.48 -a 123456 --no-auth-warning cluster replicate b75c9952210283a64f1ce10c5e1ad85d18dc1b36
OK #连接到10.0.0.48,为其指定master节点,master节点ID可以通过cluster nodes查看
[15:14:51 aaa@qq.com ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning cluster replicate 50083258803ac6bc106182853e6d0bc1ebdee45d
OK #连接到10.0.0.58,为其指定master节点,master节点ID可以通过cluster nodes查看

[15:17:08 aaa@qq.com ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning cluster nodes
208ee5ef0b688df38824552b2154b64869175b9e 10.0.0.8:aaa@qq.com master - 0 1603610406000 1 connected 0-5461
4ad896ab9a5e573061decaea13ca72f1acc16d22 10.0.0.48:aaa@qq.com slave b75c9952210283a64f1ce10c5e1ad85d18dc1b36 0 1603610406176 4 connected
50083258803ac6bc106182853e6d0bc1ebdee45d 10.0.0.18:aaa@qq.com master - 0 1603610404000 2 connected 5462-10922
b75c9952210283a64f1ce10c5e1ad85d18dc1b36 10.0.0.28:aaa@qq.com master - 0 1603610405162 4 connected 10923-16383
e1e8b3a666bb3f15b3b73c689782d9a1236beab3 10.0.0.38:aaa@qq.com slave 208ee5ef0b688df38824552b2154b64869175b9e 0 1603610407196 1 connected
66173bbc45b3f8e9212286827bb5fc594aab0cd7 10.0.0.58:aaa@qq.com myself,slave 50083258803ac6bc106182853e6d0bc1ebdee45d 0 1603610403000 5 connected
[15:20:07 aaa@qq.com ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning cluster info #完成搭建后查看集群状态
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:5
cluster_my_epoch:2
cluster_stats_messages_ping_sent:3333
cluster_stats_messages_pong_sent:3184
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:6518
cluster_stats_messages_ping_received:3179
cluster_stats_messages_pong_received:3334
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:6518
[15:21:05 aaa@qq.com ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning cluster slots #查看主从关系以及槽位信息
1) 1) (integer) 0
   2) (integer) 5461
   3) 1) "10.0.0.8"
      2) (integer) 6379
      3) "208ee5ef0b688df38824552b2154b64869175b9e"
   4) 1) "10.0.0.38"
      2) (integer) 6379
      3) "e1e8b3a666bb3f15b3b73c689782d9a1236beab3"
2) 1) (integer) 5462
   2) (integer) 10922
   3) 1) "10.0.0.18"
      2) (integer) 6379
      3) "50083258803ac6bc106182853e6d0bc1ebdee45d"
   4) 1) "10.0.0.58"
      2) (integer) 6379
      3) "66173bbc45b3f8e9212286827bb5fc594aab0cd7"
3) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "10.0.0.28"
      2) (integer) 6379
      3) "b75c9952210283a64f1ce10c5e1ad85d18dc1b36"
   4) 1) "10.0.0.48"
      2) (integer) 6379
      3) "4ad896ab9a5e573061decaea13ca72f1acc16d22"

[15:27:42 aaa@qq.com ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning set key1 v1 #当写入或读取的数据不保存至本机的槽位中时,会提示需要到另一台节点执行
(error) MOVED 9189 10.0.0.18:6379
[15:27:50 aaa@qq.com ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning -c set key1 v1 #加-c选项会自动重定向到目标节点执行
OK
[15:30:42 aaa@qq.com ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning -c get key1
"v1"
[15:31:15 aaa@qq.com ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning  get key1
(error) MOVED 9189 10.0.0.18:6379
[16:12:29 aaa@qq.com ~]#redis-cli -a 123456 --no-auth-warning cluster keyslot 123 #计算key对应的槽位值
(integer) 5970

[16:14:39 aaa@qq.com ~]#dnf -y instal1 python3 #安装python环境
[16:15:02 aaa@qq.com ~]#pip3 insta71 redis-py-cluster #安装模块
[16:51:50 aaa@qq.com ~]#vi redis_cluster_test.py #redis cluster批量写入数据py脚本

#!/usr/bin/env python3
from rediscluster import RedisCluster
startup_nodes = [
    {"host":"10.0.0.8", "port":6379},
    {"host":"10.0.0.18", "port":6379},
    {"host":"10.0.0.28", "port":6379},
    {"host":"10.0.0.38", "port":6379},
    {"host":"10.0.0.48", "port":6379},
    {"host":"10.0.0.58", "port":6379}
]
redis_conn= RedisCluster(startup_nodes=startup_nodes,password='123456',decode_responses=True)
for i in range(0,10000):
    redis_conn.set('key'+str(i),'value'+str(i))
    print('key'+str(i)+':',redis_conn.get('key'+str(i))) 

基于redis 5.0部署redis cluster

使用六台centos8服务器

[15:43:10 aaa@qq.com ~]#yum -y install redis #在所有6个节点上安装redis服务
[15:44:02 aaa@qq.com ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf #修改所有节点的配置文件,按顺序排序分别是:修改redis监听端口;修改主节点密码;修改自身登录密码;开启cluster模式;配置cluster的配置文件路径,记录主从关系和槽位信息;关闭集群请求槽位全覆盖,该选项开启的话,如果集群中有一个主库宕机且无备库就会使整个集群不对外提供服务
[15:48:49 aaa@qq.com ~]#systemctl enable --now redis #启动redis并设置为开机启动
[15:51:40 aaa@qq.com ~]#redis-cli -a 123456 --cluster create 10.0.0.8:6379 10.0.0.18:6379 10.0.0.28:6379 10.0.0.38:6379 10.0.0.48:6379 10.0.0.58:6379 --cluster-replicas 1 #一条命令实现原生命令的meet过程、分配槽位、设置主从关系,--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 10.0.0.38:6379 to 10.0.0.8:6379 #自动配置主从关系
Adding replica 10.0.0.48:6379 to 10.0.0.18:6379
Adding replica 10.0.0.58:6379 to 10.0.0.28:6379
M: b530ab922788d06da1cb85012d384ffb4fb7824f 10.0.0.8:6379 #显示节点的主从关系和槽位信息
   slots:[0-5460] (5461 slots) master
M: 43e25d5358edef69bac0dee5c964d20dda20ca4c 10.0.0.18:6379
   slots:[5461-10922] (5462 slots) master
M: 5f5f4a0032a71f307a3c710ae4fda6f6eb40c3d1 10.0.0.28:6379
   slots:[10923-16383] (5461 slots) master
S: 82ae5c91bbe25cfcd9815e4d9bd4c4d90ad4e7ab 10.0.0.38:6379
   replicates b530ab922788d06da1cb85012d384ffb4fb7824f
S: 9c21796fbc967ebd57ea30fcf7516926bdba6988 10.0.0.48:6379
   replicates 43e25d5358edef69bac0dee5c964d20dda20ca4c
S: 4acdb613601f3538eebf023df81f6cb887b617a0 10.0.0.58:6379
   replicates 5f5f4a0032a71f307a3c710ae4fda6f6eb40c3d1
Can I set the above configuration? (type 'yes' to accept): yes #输入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 10.0.0.8:6379)
M: b530ab922788d06da1cb85012d384ffb4fb7824f 10.0.0.8:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 43e25d5358edef69bac0dee5c964d20dda20ca4c 10.0.0.18:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 5f5f4a0032a71f307a3c710ae4fda6f6eb40c3d1 10.0.0.28:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 4acdb613601f3538eebf023df81f6cb887b617a0 10.0.0.58:6379
   slots: (0 slots) slave
   replicates 5f5f4a0032a71f307a3c710ae4fda6f6eb40c3d1
S: 9c21796fbc967ebd57ea30fcf7516926bdba6988 10.0.0.48:6379
   slots: (0 slots) slave
   replicates 43e25d5358edef69bac0dee5c964d20dda20ca4c
S: 82ae5c91bbe25cfcd9815e4d9bd4c4d90ad4e7ab 10.0.0.38:6379
   slots: (0 slots) slave
   replicates b530ab922788d06da1cb85012d384ffb4fb7824f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered. #所有槽位分配完成
#redis 5.0的redis cluster配置完成
[16:09:32 aaa@qq.com ~]#redis-cli -a 123456 cluster nodes #检查一下集群节点信息
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
43e25d5358edef69bac0dee5c964d20dda20ca4c 10.0.0.18:aaa@qq.com master - 0 1603613374183 2 connected 5461-10922
b530ab922788d06da1cb85012d384ffb4fb7824f 10.0.0.8:aaa@qq.com myself,master - 0 1603613373000 1 connected 0-5460
5f5f4a0032a71f307a3c710ae4fda6f6eb40c3d1 10.0.0.28:aaa@qq.com master - 0 1603613372000 3 connected 10923-16383
4acdb613601f3538eebf023df81f6cb887b617a0 10.0.0.58:aaa@qq.com slave 5f5f4a0032a71f307a3c710ae4fda6f6eb40c3d1 0 1603613374000 6 connected
9c21796fbc967ebd57ea30fcf7516926bdba6988 10.0.0.48:aaa@qq.com slave 43e25d5358edef69bac0dee5c964d20dda20ca4c 0 1603613375205 5 connected
82ae5c91bbe25cfcd9815e4d9bd4c4d90ad4e7ab 10.0.0.38:aaa@qq.com slave b530ab922788d06da1cb85012d384ffb4fb7824f 0 1603613375000 4 connected

基于rendis 4.0部署redis cluster

使用六台centos7服务器

[17:20:10 aaa@qq.com ~]#vi install_redis_for_centos.sh  #自动编译安装redis4.0.14脚本

#!/bin/bash
#
#********************************************************************
#Author:        wangxiaochun
#URL:           http://www.magedu.com
#Description:      The test script
#Copyright (C):     2020 All rights reserved
#********************************************************************
. /etc/init.d/functions 
VERSION=redis-4.0.14
PASSWORD=123456
INSTALL_DIR=/apps/redis

install() {
yum  -y install gcc jemalloc-devel || { action "安装软件包失败,请检查网络配置" false ; exit; }

wget http://download.redis.io/releases/${VERSION}.tar.gz || { action "Redis 源码下载失败" false ; exit; }



tar xf ${VERSION}.tar.gz
cd ${VERSION}


ln -s ${INSTALL_DIR}/bin/redis-*  /usr/bin/
mkdir -p ${INSTALL_DIR}/{etc,log,data,run}
cp redis.conf  ${INSTALL_DIR}/etc/



if id redis &> /dev/null ;then 
    action "Redis 用户已存在" false  
else
    useradd -r -s /sbin/nologin redis
    action "Redis 用户创建成功"
fi

chown -R redis.redis ${INSTALL_DIR}

vm.overcommit_memory = 1
EOF
sysctl -p 

echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
/etc/rc.d/rc.local

cat > /usr/lib/systemd/system/redis.service <<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=${INSTALL_DIR}/bin/redis-server ${INSTALL_DIR}/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT \$MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

EOF
systemctl daemon-reload 
systemctl enable --now  redis &> /dev/null && action "Redis 服务启动成功,Redis信息如下:" || { action "Redis 启动失败" false ;exit; } 

redis-cli -a $PASSWORD INFO Server 2> /dev/null

}

install

[17:24:51 aaa@qq.com ~]#bash install_redis_for_centos.sh #在所有节点执行脚本来安装redis
[17:53:59 aaa@qq.com ~]#sed -i -e '/^# masterauth/a masterauth 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /apps/redis/etc/redis.conf #修改所有reids的配置文件
[17:41:51 aaa@qq.com ~]#systemctl restart redis #重启redis
[17:56:24 aaa@qq.com ~]#cp redis-4.0.14/src/redis-trib.rb /usr/bin/ #拷贝redis-trib.rb工具到/usr/bin下,方便使用
[18:24:54 aaa@qq.com ~]#yum -y install ruby #安装ruby环境,但是由于cneots7yum安装ruby版本太低,需要执行其他操作
[18:52:33 aaa@qq.com ~]#yum -y install gcc openssl-devel zlib-devel #安装编译ruby依赖包
[18:53:00 aaa@qq.com ~]#wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz #下载ruby2.5.5的源码包
--2020-10-25 18:53:24--  https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz
Resolving cache.ruby-lang.org (cache.ruby-lang.org)... 151.101.77.178, 2a04:4e42:12::434
Connecting to cache.ruby-lang.org (cache.ruby-lang.org)|151.101.77.178|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 15996436 (15M) [application/x-tar]
Saving to: ‘ruby-2.5.5.tar.gz’

100%[=================================================================>] 15,996,436  82.9KB/s   in 2m 57s 

2020-10-25 18:56:27 (88.2 KB/s) - ‘ruby-2.5.5.tar.gz’ saved [15996436/15996436]


[18:56:27 aaa@qq.com ~]#tar xf ruby-2.5.5.tar.gz  #解压源码包
[18:58:51 aaa@qq.com ruby-2.5.5]#cd ruby-2.5.5 
[18:58:51 aaa@qq.com ruby-2.5.5]#./configure #执行configure脚本生成makefile文件
[18:59:33 aaa@qq.com ruby-2.5.5]#make && make install #编译安装ruby
[19:06:18 aaa@qq.com ruby-2.5.5]#ruby -v
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux] #查看ruby版本,然后重新登录终端
[19:17:37 aaa@qq.com ~]#gem install -l redis-4.1.3.gem #安装redis模块,此为安装离线包,需提前将文件下载好,下载链接:https://rubygems.org/downloads/redis-4.1.3.gem ,注意下载此包需要*
Successfully installed redis-4.1.3
Parsing documentation for redis-4.1.3
Installing ri documentation for redis-4.1.3
Done installing documentation for redis after 0 seconds
1 gem installed
[19:20:38 aaa@qq.com ~]#vi /usr/local/lib/ruby/gems/2.5.0/gems/redis-4.1.3/lib/redis/client.rb 

# frozen_string_literal: true

require_relative "errors"
require "socket"
require "cgi"

class Redis
  class Client

    DEFAULTS = {
      :url => lambda { ENV["REDIS_URL"] },
      :scheme => "redis",
      :host => "127.0.0.1",
      :port => 6379,
      :path => nil,
      :timeout => 5.0,
      :password => 123456,   #修改脚本中连接redis的密码                                                                               
      :db => 0,
      :driver => nil,
      :id => nil,
      :tcp_keepalive => 0,
      :reconnect_attempts => 1,
      :reconnect_delay => 0,
      :reconnect_delay_max => 0.5,
      :inherit_socket => false
    }

    attr_reader :options

[19:21:49 aaa@qq.com ~]#redis-trib.rb create --replicas 1 10.0.0.7:6379 10.0.0.17:6379 10.0.0.27:6379 10.0.0.37:6379 10.0.0.47:6379 10.0.0.57:6379 #创建redis cluster集群,与redis5.0集群不同的是,从节点会随机分配给主节点,而非按顺序一一对应
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.0.0.7:6379
10.0.0.17:6379
10.0.0.27:6379
Adding replica 10.0.0.47:6379 to 10.0.0.7:6379
Adding replica 10.0.0.57:6379 to 10.0.0.17:6379
Adding replica 10.0.0.37:6379 to 10.0.0.27:6379
M: bf18eec99802798b5215ed2f8ddc12eb1248b3cb 10.0.0.7:6379
   slots:0-5460 (5461 slots) master
M: a0c915ad50c0ad66a0a4c7b6393d9e127576ad64 10.0.0.17:6379
   slots:5461-10922 (5462 slots) master
M: 97b1c4b57dbf5469bab57577f0bc022bd59917e6 10.0.0.27:6379
   slots:10923-16383 (5461 slots) master
S: b18f0f29e766120656abd946b957ef0bd712d638 10.0.0.37:6379
   replicates 97b1c4b57dbf5469bab57577f0bc022bd59917e6
S: fc17f6fea452277328d913324b463a0c91029cd3 10.0.0.47:6379
   replicates bf18eec99802798b5215ed2f8ddc12eb1248b3cb
S: aca9ab569ef599a17449bd2b9088eee4533f515e 10.0.0.57:6379
   replicates a0c915ad50c0ad66a0a4c7b6393d9e127576ad64
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 10.0.0.7:6379)
M: bf18eec99802798b5215ed2f8ddc12eb1248b3cb 10.0.0.7:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 97b1c4b57dbf5469bab57577f0bc022bd59917e6 10.0.0.27:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: aca9ab569ef599a17449bd2b9088eee4533f515e 10.0.0.57:6379
   slots: (0 slots) slave
   replicates a0c915ad50c0ad66a0a4c7b6393d9e127576ad64
S: fc17f6fea452277328d913324b463a0c91029cd3 10.0.0.47:6379
   slots: (0 slots) slave
   replicates bf18eec99802798b5215ed2f8ddc12eb1248b3cb
M: a0c915ad50c0ad66a0a4c7b6393d9e127576ad64 10.0.0.17:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: b18f0f29e766120656abd946b957ef0bd712d638 10.0.0.37:6379
   slots: (0 slots) slave
   replicates 97b1c4b57dbf5469bab57577f0bc022bd59917e6
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

[19:31:05 aaa@qq.com ~]#redis-cli -a 123456 cluster slots #查看集群主从关系以及槽位信息
Warning: Using a password with '-a' option on the command line interface may not be safe.
1) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "10.0.0.27"
      2) (integer) 6379
      3) "97b1c4b57dbf5469bab57577f0bc022bd59917e6"
   4) 1) "10.0.0.37"
      2) (integer) 6379
      3) "b18f0f29e766120656abd946b957ef0bd712d638"
2) 1) (integer) 0
   2) (integer) 5460
   3) 1) "10.0.0.7"
      2) (integer) 6379
      3) "bf18eec99802798b5215ed2f8ddc12eb1248b3cb"
   4) 1) "10.0.0.47"
      2) (integer) 6379
      3) "fc17f6fea452277328d913324b463a0c91029cd3"
3) 1) (integer) 5461
   2) (integer) 10922
   3) 1) "10.0.0.17"
      2) (integer) 6379
      3) "a0c915ad50c0ad66a0a4c7b6393d9e127576ad64"
   4) 1) "10.0.0.57"
      2) (integer) 6379
      3) "aca9ab569ef599a17449bd2b9088eee4533f515e"

扩容节点

redis5.0扩容

#新增节点准备工作,安装redis,并修改配置文件
[20:38:04 aaa@qq.com ~]#yum -y install redis
[20:38:12 aaa@qq.com ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf #修改所有节点的配置文件,按顺序排序分别是:修改redis监听端口;修改主节点密码;修改自身登录密码;开启cluster模式;配置cluster的配置文件路径,记录主从关系和槽位信息;关闭集群请求槽位全覆盖,该选项开启的话,如果集群中有一个主库宕机且无备库就会使整个集群不对外提供服务
[20:40:44 aaa@qq.com ~]#systemctl enable --now redis 
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.

[20:49:44 aaa@qq.com ~]#redis-cli -a 123456 --cluster add-node 10.0.0.68:6379 10.0.0.48:6379 #添加新增节点到本集群当中,最后一个IP地址使本集群中任意一个节点的IP,新增节点需要在已经在集群中的主机上进行
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 10.0.0.68:6379 to cluster 10.0.0.48:6379
>>> Performing Cluster Check (using node 10.0.0.48:6379)
S: 149324ce0e4faf93008f057474e678bb92b98ec3 10.0.0.48:6379
   slots: (0 slots) slave
   replicates 5c833a7ad6294042ef324d3f5c3e61007eb26ee6
M: 5c833a7ad6294042ef324d3f5c3e61007eb26ee6 10.0.0.18:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: d58a2c70c48bea19a176fd129e77ea9ce888704b 10.0.0.58:6379
   slots: (0 slots) slave
   replicates 1ab53ba1195fe4c16f8bfc9c776cb8526158c0ef
S: f8e64bdce7ce695c458946441f9f8e4785e3d2c5 10.0.0.38:6379
   slots: (0 slots) slave
   replicates 7ff9332869251b112084a7258e8df93b97b15fd4
M: 7ff9332869251b112084a7258e8df93b97b15fd4 10.0.0.8:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 1ab53ba1195fe4c16f8bfc9c776cb8526158c0ef 10.0.0.28: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 10.0.0.68:6379 to make it join the cluster.
[OK] New node added correctly.

[20:50:13 aaa@qq.com ~]#redis-cli -a 123456 cluster nodes #新增节点已成功加入集群
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
c332d9e30ca9092177acbfae78fee969bdd7b3de 10.0.0.68:aaa@qq.com master - 0 1603630259257 0 connected
f8e64bdce7ce695c458946441f9f8e4785e3d2c5 10.0.0.38:aaa@qq.com slave 7ff9332869251b112084a7258e8df93b97b15fd4 0 1603630260277 4 connected
1ab53ba1195fe4c16f8bfc9c776cb8526158c0ef 10.0.0.28:aaa@qq.com master - 0 1603630257000 3 connected 10923-16383
5c833a7ad6294042ef324d3f5c3e61007eb26ee6 10.0.0.18:aaa@qq.com master - 0 1603630258000 2 connected 5461-10922
d58a2c70c48bea19a176fd129e77ea9ce888704b 10.0.0.58:aaa@qq.com slave 1ab53ba1195fe4c16f8bfc9c776cb8526158c0ef 0 1603630259000 6 connected
149324ce0e4faf93008f057474e678bb92b98ec3 10.0.0.48:aaa@qq.com slave 5c833a7ad6294042ef324d3f5c3e61007eb26ee6 0 1603630257213 5 connected
7ff9332869251b112084a7258e8df93b97b15fd4 10.0.0.8:aaa@qq.com myself,master - 0 1603630257000 1 connected 0-5460
#虽然新节点已成功加入集群,但是没有槽位,所以重新分配槽位,注意!重分槽位会导致数据丢失!重分前注意做好数据备份
[20:56:17 aaa@qq.com ~]#redis-cli -a 123456 --cluster reshard 10.0.0.18:6379 #重新分配槽位,后面的IP为本集群中任意节点IP
................
How many s1ots do you want to move (from 1。 to 16384)?4096 #新分配多少个槽位,槽位数=16384/master个数

What is the receiving node ID? d6e2eca6b338b717923f64866bd31d42e52edc98 #新的主节点的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 a11 the source nodes IDS .
Source node #1: a11 #将哪些源主机的槽位分配给新的节点,a11是自动在所有的redis node选择划分,如果是从redis cluster删除 某个主机可以使用此方式将指定主机上的槽位全部移动到别的redis主机
.........
Do you want to proceed with the proposed reshard plan (yes/no)?  yes #确认分配
.............
[21:02:16 aaa@qq.com ~]#redis-cli -a 123456 cluster nodes #查看槽位分配结果
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
c332d9e30ca9092177acbfae78fee969bdd7b3de 10.0.0.68:aaa@qq.com master - 0 1603630957000 7 connected 0-1364 5461-6826 10923-12287
f8e64bdce7ce695c458946441f9f8e4785e3d2c5 10.0.0.38:aaa@qq.com slave 7ff9332869251b112084a7258e8df93b97b15fd4 0 1603630957717 4 connected
1ab53ba1195fe4c16f8bfc9c776cb8526158c0ef 10.0.0.28:aaa@qq.com master - 0 1603630958741 3 connected 12288-16383
5c833a7ad6294042ef324d3f5c3e61007eb26ee6 10.0.0.18:aaa@qq.com master - 0 1603630956000 2 connected 6827-10922
d58a2c70c48bea19a176fd129e77ea9ce888704b 10.0.0.58:aaa@qq.com slave 1ab53ba1195fe4c16f8bfc9c776cb8526158c0ef 0 1603630958000 6 connected
149324ce0e4faf93008f057474e678bb92b98ec3 10.0.0.48:aaa@qq.com slave 5c833a7ad6294042ef324d3f5c3e61007eb26ee6 0 1603630955000 5 connected
7ff9332869251b112084a7258e8df93b97b15fd4 10.0.0.8:aaa@qq.com myself,master - 0 1603630955000 1 connected 1365-5460
#添加新节点的同时将此节点指向新增主节点
[21:03:21 aaa@qq.com ~]#redis-cli -a 123456 --cluster add-node 10.0.0.78:6379 10.0.0.8:6379 --cluster-slave --cluster-master-id c332d9e30ca9092177acbfae78fee969bdd7b3de
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 10.0.0.78:6379 to cluster 10.0.0.8:6379
>>> Performing Cluster Check (using node 10.0.0.8:6379)
M: 7ff9332869251b112084a7258e8df93b97b15fd4 10.0.0.8:6379
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
M: c332d9e30ca9092177acbfae78fee969bdd7b3de 10.0.0.68:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
S: f8e64bdce7ce695c458946441f9f8e4785e3d2c5 10.0.0.38:6379
   slots: (0 slots) slave
   replicates 7ff9332869251b112084a7258e8df93b97b15fd4
M: 1ab53ba1195fe4c16f8bfc9c776cb8526158c0ef 10.0.0.28:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: 5c833a7ad6294042ef324d3f5c3e61007eb26ee6 10.0.0.18:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: d58a2c70c48bea19a176fd129e77ea9ce888704b 10.0.0.58:6379
   slots: (0 slots) slave
   replicates 1ab53ba1195fe4c16f8bfc9c776cb8526158c0ef
S: 149324ce0e4faf93008f057474e678bb92b98ec3 10.0.0.48:6379
   slots: (0 slots) slave
   replicates 5c833a7ad6294042ef324d3f5c3e61007eb26ee6
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 10.0.0.78:6379 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of 10.0.0.68:6379.
[OK] New node added correctly.
[21:05:34 aaa@qq.com ~]#redis-cli -a 123456 cluster nodes #确认成功加到集群中,并成为新增主节点的从节点
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
5c833a7ad6294042ef324d3f5c3e61007eb26ee6 10.0.0.18:aaa@qq.com master - 0 1603631139000 2 connected 6827-10922
4f910e4b56f8777a05588ae584cf6b871cbf3ba8 10.0.0.78:aaa@qq.com myself,slave c332d9e30ca9092177acbfae78fee969bdd7b3de 0 1603631138000 0 connected
7ff9332869251b112084a7258e8df93b97b15fd4 10.0.0.8:aaa@qq.com master - 0 1603631139000 1 connected 1365-5460
149324ce0e4faf93008f057474e678bb92b98ec3 10.0.0.48:aaa@qq.com slave 5c833a7ad6294042ef324d3f5c3e61007eb26ee6 0 1603631137000 2 connected
c332d9e30ca9092177acbfae78fee969bdd7b3de 10.0.0.68:aaa@qq.com master - 0 1603631139614 7 connected 0-1364 5461-6826 10923-12287
d58a2c70c48bea19a176fd129e77ea9ce888704b 10.0.0.58:aaa@qq.com slave 1ab53ba1195fe4c16f8bfc9c776cb8526158c0ef 0 1603631139000 3 connected
f8e64bdce7ce695c458946441f9f8e4785e3d2c5 10.0.0.38:aaa@qq.com slave 7ff9332869251b112084a7258e8df93b97b15fd4 0 1603631140642 1 connected
1ab53ba1195fe4c16f8bfc9c776cb8526158c0ef 10.0.0.28:aaa@qq.com master - 0 1603631141664 3 connected 12288-16383

redis4.0扩容

#利用相同的脚本自动编译安装redis,并修改配置文件使新增节点配置文件与集群中配置文件一致
[21:18:59 aaa@qq.com ~]#sed -i -e '/^# masterauth/a masterauth 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /apps/redis/etc/redis.conf #修改所有reids的配置文件
[21:19:35 aaa@qq.com ~]#systemctl restart redis #重启redis
[21:50:03 aaa@qq.com ~]#redis-trib.rb add-node 10.0.0.67:6379 10.0.0.17:6379 #添加新节点至本集群中,最后一个IP是本集群中任意一节点的IP
>>> Adding node 10.0.0.67:6379 to cluster 10.0.0.17:6379
>>> Performing Cluster Check (using node 10.0.0.17:6379)
M: a9c41df1c7a790c3b373c86db252a69309f88f8f 10.0.0.17:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 6bdcc85fd3d80c33353e695a5b9004dcd31f2dcd 10.0.0.27:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 363fd54455f7dff913e2aae61390fd55363e0c08 10.0.0.57:6379
   slots: (0 slots) slave
   replicates a9c41df1c7a790c3b373c86db252a69309f88f8f
S: 7aadcd632de1ecc77112f6f1e70a879523b901f9 10.0.0.37:6379
   slots: (0 slots) slave
   replicates 6bdcc85fd3d80c33353e695a5b9004dcd31f2dcd
S: ed720d99e646b6dbaa1a260bbffd702724b258f3 10.0.0.47:6379
   slots: (0 slots) slave
   replicates a5fac7d1962a3640e6eca8389173d63363b30401
M: a5fac7d1962a3640e6eca8389173d63363b30401 10.0.0.7:6379
   slots:0-5460 (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 10.0.0.67:6379 to make it join the cluster.
[OK] New node added correctly.
#虽然新节点已成功加入集群,但是没有槽位,所以重新分配槽位,注意!重分槽位会导致数据丢失!重分前注意做好数据备份
[20:56:17 aaa@qq.com ~]#redis-cli -a 123456 --cluster reshard 10.0.0.18:6379 #重新分配槽位,后面的IP为本集群中任意节点IP
................
How many s1ots do you want to move (from 1。 to 16384)?4096 #新分配多少个槽位,槽位数=16384/master个数

What is the receiving node ID? d6e2eca6b338b717923f64866bd31d42e52edc98 #新的主节点的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 a11 the source nodes IDS .
Source node #1: a11 #将哪些源主机的槽位分配给新的节点,a11是自动在所有的redis node选择划分,如果是从redis cluster删除 某个主机可以使用此方式将指定主机上的槽位全部移动到别的redis主机
.........
Do you want to proceed with the proposed reshard plan (yes/no)?  yes #确认分配
.............
#验证重新分配槽位信息
[21:53:21 aaa@qq.com ~]#redis-cli -a 123456 cluster nodes
Warning: Using a password with '-a' option on the command line interface may not be safe.
363fd54455f7dff913e2aae61390fd55363e0c08 10.0.0.57:aaa@qq.com slave a9c41df1c7a790c3b373c86db252a69309f88f8f 0 1603634003179 6 connected
6bdcc85fd3d80c33353e695a5b9004dcd31f2dcd 10.0.0.27:aaa@qq.com master - 0 1603634001137 3 connected 12288-16383
7aadcd632de1ecc77112f6f1e70a879523b901f9 10.0.0.37:aaa@qq.com slave 6bdcc85fd3d80c33353e695a5b9004dcd31f2dcd 0 1603634001000 4 connected
a5fac7d1962a3640e6eca8389173d63363b30401 10.0.0.7:aaa@qq.com myself,master - 0 1603633996000 1 connected 1365-5460
13e712f60407e3a941e52489f96223b08bbef0ab 10.0.0.67:aaa@qq.com master - 0 1603634000000 7 connected 0-1364 5461-6826 10923-12287
ed720d99e646b6dbaa1a260bbffd702724b258f3 10.0.0.47:aaa@qq.com slave a5fac7d1962a3640e6eca8389173d63363b30401 0 1603634001000 5 connected
a9c41df1c7a790c3b373c86db252a69309f88f8f 10.0.0.17:aaa@qq.com master - 0 1603634002161 2 connected 6827-10922
#添加新节点的同时将此节点指向新增主节点
[21:53:24 aaa@qq.com ~]#redis-trib.rb add-node --slave --master-id 13e712f60407e3a941e52489f96223b08bbef0ab 10.0.0.77:6379 10.0.0.7:6379
>>> Adding node 10.0.0.77:6379 to cluster 10.0.0.7:6379
>>> Performing Cluster Check (using node 10.0.0.7:6379)
M: a5fac7d1962a3640e6eca8389173d63363b30401 10.0.0.7:6379
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
S: 363fd54455f7dff913e2aae61390fd55363e0c08 10.0.0.57:6379
   slots: (0 slots) slave
   replicates a9c41df1c7a790c3b373c86db252a69309f88f8f
M: 6bdcc85fd3d80c33353e695a5b9004dcd31f2dcd 10.0.0.27:6379
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
S: 7aadcd632de1ecc77112f6f1e70a879523b901f9 10.0.0.37:6379
   slots: (0 slots) slave
   replicates 6bdcc85fd3d80c33353e695a5b9004dcd31f2dcd
M: 13e712f60407e3a941e52489f96223b08bbef0ab 10.0.0.67:6379
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   0 additional replica(s)
S: ed720d99e646b6dbaa1a260bbffd702724b258f3 10.0.0.47:6379
   slots: (0 slots) slave
   replicates a5fac7d1962a3640e6eca8389173d63363b30401
M: a9c41df1c7a790c3b373c86db252a69309f88f8f 10.0.0.17:6379
   slots:6827-10922 (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.
>>> Send CLUSTER MEET to node 10.0.0.77:6379 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 10.0.0.67:6379.
[OK] New node added correctly.
#验证添加节点结果
[21:54:51 aaa@qq.com ~]#redis-cli -a 123456 cluster nodes
Warning: Using a password with '-a' option on the command line interface may not be safe.
363fd54455f7dff913e2aae61390fd55363e0c08 10.0.0.57:aaa@qq.com slave a9c41df1c7a790c3b373c86db252a69309f88f8f 0 1603634138904 6 connected
6b22b2d767c67e238293e186b32bf49bc549896b 10.0.0.77:aaa@qq.com slave 13e712f60407e3a941e52489f96223b08bbef0ab 0 1603634137000 7 connected
6bdcc85fd3d80c33353e695a5b9004dcd31f2dcd 10.0.0.27:aaa@qq.com master - 0 1603634136862 3 connected 12288-16383
7aadcd632de1ecc77112f6f1e70a879523b901f9 10.0.0.37:aaa@qq.com slave 6bdcc85fd3d80c33353e695a5b9004dcd31f2dcd 0 1603634138000 4 connected
a5fac7d1962a3640e6eca8389173d63363b30401 10.0.0.7:aaa@qq.com myself,master - 0 1603634135000 1 connected 1365-5460
13e712f60407e3a941e52489f96223b08bbef0ab 10.0.0.67:aaa@qq.com master - 0 1603634139000 7 connected 0-1364 5461-6826 10923-12287
ed720d99e646b6dbaa1a260bbffd702724b258f3 10.0.0.47:aaa@qq.com slave a5fac7d1962a3640e6eca8389173d63363b30401 0 1603634136000 5 connected
a9c41df1c7a790c3b373c86db252a69309f88f8f 10.0.0.17:aaa@qq.com master - 0 1603634139927 2 connected 6827-10922

缩容节点

redis5.0缩容

[22:22:25 aaa@qq.com ~]#redis-cli -a 123456 --cluster reshard 10.0.0.18:6379 #后面的IP为集群中任意一个节点的IP
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 10.0.0.18:6379)
M: 5c833a7ad6294042ef324d3f5c3e61007eb26ee6 10.0.0.18:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: 4f910e4b56f8777a05588ae584cf6b871cbf3ba8 10.0.0.78:6379
   slots: (0 slots) slave
   replicates c332d9e30ca9092177acbfae78fee969bdd7b3de
S: 149324ce0e4faf93008f057474e678bb92b98ec3 10.0.0.48:6379
   slots: (0 slots) slave
   replicates 5c833a7ad6294042ef324d3f5c3e61007eb26ee6
M: 1ab53ba1195fe4c16f8bfc9c776cb8526158c0ef 10.0.0.28:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
S: d58a2c70c48bea19a176fd129e77ea9ce888704b 10.0.0.58:6379
   slots: (0 slots) slave
   replicates 1ab53ba1195fe4c16f8bfc9c776cb8526158c0ef
S: f8e64bdce7ce695c458946441f9f8e4785e3d2c5 10.0.0.38:6379
   slots: (0 slots) slave
   replicates 7ff9332869251b112084a7258e8df93b97b15fd4
M: c332d9e30ca9092177acbfae78fee969bdd7b3de 10.0.0.68:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 additional replica(s)
M: 7ff9332869251b112084a7258e8df93b97b15fd4 10.0.0.8:6379
   slots:[1365-5460] (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.
How many slots do you want to move (from 1 to 16384)? 1365 #要移动多少个槽位
What is the receiving node ID? 7ff9332869251b112084a7258e8df93b97b15fd4 #将槽位分给节点的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:c332d9e30ca9092177acbfae78fee969bdd7b3de #提供槽位的节点
Source node #2:done
.....
Do you want to proceed with the proposed reshard plan (yes/no)?  yes #确认分配
 
[22:25:24 aaa@qq.com ~]#redis-cli -a 123456 cluster nodes #查看收缩槽位结果
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
5c833a7ad6294042ef324d3f5c3e61007eb26ee6 10.0.0.18:aaa@qq.com master - 0 1603635926547 2 connected 6827-10922
149324ce0e4faf93008f057474e678bb92b98ec3 10.0.0.48:aaa@qq.com slave 5c833a7ad6294042ef324d3f5c3e61007eb26ee6 0 1603635927552 5 connected
d58a2c70c48bea19a176fd129e77ea9ce888704b 10.0.0.58:aaa@qq.com slave 1ab53ba1195fe4c16f8bfc9c776cb8526158c0ef 0 1603635924535 6 connected
c332d9e30ca9092177acbfae78fee969bdd7b3de 10.0.0.68:aaa@qq.com master - 0 1603635923000 7 connected 5461-6826 10923-12287
7ff9332869251b112084a7258e8df93b97b15fd4 10.0.0.8:aaa@qq.com myself,master - 0 1603635924000 8 connected 0-5460
4f910e4b56f8777a05588ae584cf6b871cbf3ba8 10.0.0.78:aaa@qq.com slave c332d9e30ca9092177acbfae78fee969bdd7b3de 0 1603635924000 7 connected
f8e64bdce7ce695c458946441f9f8e4785e3d2c5 10.0.0.38:aaa@qq.com slave 7ff9332869251b112084a7258e8df93b97b15fd4 0 1603635926000 8 connected
1ab53ba1195fe4c16f8bfc9c776cb8526158c0ef 10.0.0.28:aaa@qq.com master - 0 1603635927000 3 connected 12288-16383
#重复上述步骤,直至需要删除的节点槽位清空
#删除主节点
[22:28:16 aaa@qq.com ~]#redis-cli -a 123456 --cluster del-node 10.0.0.8:6379 c332d9e30ca9092177acbfae78fee969bdd7b3de #中间IP为集群任意节点IP,后面为需要删除的节点的ID
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node c332d9e30ca9092177acbfae78fee969bdd7b3de from cluster 10.0.0.8:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
[22:28:44 aaa@qq.com ~]#redis-cli -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
5c833a7ad6294042ef324d3f5c3e61007eb26ee6 10.0.0.18:aaa@qq.com master - 0 1603636138000 9 connected 5461-10922
149324ce0e4faf93008f057474e678bb92b98ec3 10.0.0.48:aaa@qq.com slave 5c833a7ad6294042ef324d3f5c3e61007eb26ee6 0 1603636138282 9 connected
d58a2c70c48bea19a176fd129e77ea9ce888704b 10.0.0.58:aaa@qq.com slave 1ab53ba1195fe4c16f8bfc9c776cb8526158c0ef 0 1603636137000 10 connected
7ff9332869251b112084a7258e8df93b97b15fd4 10.0.0.8:aaa@qq.com myself,master - 0 1603636135000 8 connected 0-5460
4f910e4b56f8777a05588ae584cf6b871cbf3ba8 10.0.0.78:aaa@qq.com slave 1ab53ba1195fe4c16f8bfc9c776cb8526158c0ef 0 1603636136000 10 connected
f8e64bdce7ce695c458946441f9f8e4785e3d2c5 10.0.0.38:aaa@qq.com slave 7ff9332869251b112084a7258e8df93b97b15fd4 0 1603636137265 8 connected
1ab53ba1195fe4c16f8bfc9c776cb8526158c0ef 10.0.0.28:aaa@qq.com master - 0 1603636136248 10 connected 10923-16383
[22:28:59 aaa@qq.com ~]#redis-cli -a 123456 --cluster del-node 10.0.0.8:6379 4f910e4b56f8777a05588ae584cf6b871cbf3ba8
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 4f910e4b56f8777a05588ae584cf6b871cbf3ba8 from cluster 10.0.0.8:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

redis4.0缩容

[22:01:23 aaa@qq.com ~]#redis-trib.rb reshard 10.0.0.67:6379 #后面的IP为集群中任意一个节点的IP
>>> Performing Cluster Check (using node 10.0.0.67:6379)
M: 13e712f60407e3a941e52489f96223b08bbef0ab 10.0.0.67:6379
   slots:0-1819,5461-7281,10923-12742 (5461 slots) master
   1 additional replica(s)
S: 7aadcd632de1ecc77112f6f1e70a879523b901f9 10.0.0.37:6379
   slots: (0 slots) slave
   replicates 6bdcc85fd3d80c33353e695a5b9004dcd31f2dcd
M: a9c41df1c7a790c3b373c86db252a69309f88f8f 10.0.0.17:6379
   slots:7282-10922 (3641 slots) master
   1 additional replica(s)
S: ed720d99e646b6dbaa1a260bbffd702724b258f3 10.0.0.47:6379
   slots: (0 slots) slave
   replicates a5fac7d1962a3640e6eca8389173d63363b30401
S: 6b22b2d767c67e238293e186b32bf49bc549896b 10.0.0.77:6379
   slots: (0 slots) slave
   replicates 13e712f60407e3a941e52489f96223b08bbef0ab
M: 6bdcc85fd3d80c33353e695a5b9004dcd31f2dcd 10.0.0.27:6379
   slots:12743-16383 (3641 slots) master
   1 additional replica(s)
M: a5fac7d1962a3640e6eca8389173d63363b30401 10.0.0.7:6379
   slots:1820-5460 (3641 slots) master
   1 additional replica(s)
S: 363fd54455f7dff913e2aae61390fd55363e0c08 10.0.0.57:6379
   slots: (0 slots) slave
   replicates a9c41df1c7a790c3b373c86db252a69309f88f8f
[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)? 1356  #移动多少个槽位
What is the receiving node ID? 13e712f60407e3a941e52489f96223b08bbef0ab #将槽位分给节点的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:13e712f60407e3a941e52489f96223b08bbef0ab #提供槽位的节点
Source node #2:done
.....
Do you want to proceed with the proposed reshard plan (yes/no)?  yes #确认分配

[22:09:10 aaa@qq.com ~]#redis-cli -a 123456 cluster nodes #验证缩容结果
Warning: Using a password with '-a' option on the command line interface may not be safe.
363fd54455f7dff913e2aae61390fd55363e0c08 10.0.0.57:aaa@qq.com slave a9c41df1c7a790c3b373c86db252a69309f88f8f 0 1603634952790 8 connected
6b22b2d767c67e238293e186b32bf49bc549896b 10.0.0.77:aaa@qq.com slave 13e712f60407e3a941e52489f96223b08bbef0ab 0 1603634951782 7 connected
6bdcc85fd3d80c33353e695a5b9004dcd31f2dcd 10.0.0.27:aaa@qq.com master - 0 1603634950573 3 connected 12288-16383
7aadcd632de1ecc77112f6f1e70a879523b901f9 10.0.0.37:aaa@qq.com slave 6bdcc85fd3d80c33353e695a5b9004dcd31f2dcd 0 1603634951000 4 connected
a5fac7d1962a3640e6eca8389173d63363b30401 10.0.0.7:aaa@qq.com myself,master - 0 1603634949000 1 connected 0-5460
13e712f60407e3a941e52489f96223b08bbef0ab 10.0.0.67:aaa@qq.com master - 0 1603634948000 7 connected 5461-6826 10923-12287
ed720d99e646b6dbaa1a260bbffd702724b258f3 10.0.0.47:aaa@qq.com slave a5fac7d1962a3640e6eca8389173d63363b30401 0 1603634952000 5 connected
a9c41df1c7a790c3b373c86db252a69309f88f8f 10.0.0.17:aaa@qq.com master - 0 1603634953797 8 connected 6827-10922

#重复上述步骤,直至需要删除的节点槽位清空
#删除主节点
[22:15:46 aaa@qq.com ~]#redis-trib.rb del-node 10.0.0.7:6379 13e712f60407e3a941e52489f96223b08bbef0ab #中间IP为集群任意节点IP,后面为需要删除的节点的ID
>>> Removing node 13e712f60407e3a941e52489f96223b08bbef0ab from cluster 10.0.0.7:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
[22:16:51 aaa@qq.com ~]#redis-cli -a 123456 cluster nodes
Warning: Using a password with '-a' option on the command line interface may not be safe.
363fd54455f7dff913e2aae61390fd55363e0c08 10.0.0.57:aaa@qq.com slave a9c41df1c7a790c3b373c86db252a69309f88f8f 0 1603635465357 8 connected
6b22b2d767c67e238293e186b32bf49bc549896b 10.0.0.77:aaa@qq.com slave 6bdcc85fd3d80c33353e695a5b9004dcd31f2dcd 0 1603635467000 9 connected
6bdcc85fd3d80c33353e695a5b9004dcd31f2dcd 10.0.0.27:aaa@qq.com master - 0 1603635468000 9 connected 10923-16383
7aadcd632de1ecc77112f6f1e70a879523b901f9 10.0.0.37:aaa@qq.com slave 6bdcc85fd3d80c33353e695a5b9004dcd31f2dcd 0 1603635468416 9 connected
a5fac7d1962a3640e6eca8389173d63363b30401 10.0.0.7:aaa@qq.com myself,master - 0 1603635467000 1 connected 1820-5460
ed720d99e646b6dbaa1a260bbffd702724b258f3 10.0.0.47:aaa@qq.com slave a5fac7d1962a3640e6eca8389173d63363b30401 0 1603635469432 5 connected
a9c41df1c7a790c3b373c86db252a69309f88f8f 10.0.0.17:aaa@qq.com master - 0 1603635467395 8 connected 0-1819 5461-10922
[22:17:49 aaa@qq.com ~]#redis-trib.rb del-node 10.0.0.7:6379 6b22b2d767c67e238293e186b32bf49bc549896b
>>> Removing node 6b22b2d767c67e238293e186b32bf49bc549896b from cluster 10.0.0.7:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node. 
[22:18:20 aaa@qq.com ~]#redis-cli -a 123456 cluster nodes  #成功删除节点
Warning: Using a password with '-a' option on the command line interface may not be safe.
363fd54455f7dff913e2aae61390fd55363e0c08 10.0.0.57:aaa@qq.com slave a9c41df1c7a790c3b373c86db252a69309f88f8f 0 1603635501838 8 connected
6bdcc85fd3d80c33353e695a5b9004dcd31f2dcd 10.0.0.27:aaa@qq.com master - 0 1603635500000 9 connected 10923-16383
7aadcd632de1ecc77112f6f1e70a879523b901f9 10.0.0.37:aaa@qq.com slave 6bdcc85fd3d80c33353e695a5b9004dcd31f2dcd 0 1603635499000 9 connected
a5fac7d1962a3640e6eca8389173d63363b30401 10.0.0.7:aaa@qq.com myself,master - 0 1603635498000 1 connected 1820-5460
ed720d99e646b6dbaa1a260bbffd702724b258f3 10.0.0.47:aaa@qq.com slave a5fac7d1962a3640e6eca8389173d63363b30401 0 1603635502862 5 connected
a9c41df1c7a790c3b373c86db252a69309f88f8f 10.0.0.17:aaa@qq.com master - 0 1603635501000 8 connected 0-1819 5461-10922

作业

1、RDB和AOF的优缺点

RDB的优点

RDB快照保存了某个时间点的数据,可以通过脚本执行redis指令bgsave(非阻塞,后台执行)或者save(会阻塞写操作,前台执行,不推荐)命令自定义时间点备份,可以保留多个备份,当出现问题可以恢复到不同时间点的版本,很适合备份,并且文件格式也支持有不少第三方工具可以进行后续的数据分析。比如:

可以在24小时内,每小时备份一次RDB文件,并且在每个月的第一天,也备份一个RDB文件。这样的话即使遇上问题,也可以随时将数据集还原到不同的版本。

RDB可以最大化Redis性能,父进程在保存RDB文件时唯一要做的就是fork出一个子进程,然后这个子进程就会处理接下来的所有保存工作,父进程无需执行任何磁盘I/O操作。

RDB在大量数据,比如几个G的数据,恢复的速度比AOF的快

RDB的缺点

不能实时保存数据,可能会丢失自上一次执行RDB备份到当前的内存数据

虽然Redis允许设置不同的保存点来控制保存RDB文件的频率,但是因为RDB文件需要保存整个数据集的状态,所以他并不是一个轻松的操作。因此可能会至少5分钟才保存一次RDB文件。在这种情况下,服务器一旦宕机,就会丢失好几分钟的数据。

当数据量非常大的时候,从父进程fork子进程进行保存值RDB文件需要一点时间,可能是毫秒或秒,取决于磁盘I/O性能

在数据量比较庞大是,fork可能会非常耗时,造成服务器在一定时间内停止处理客户端过来的请求,如果数据集非常巨大,并且CPU时间非常紧张的话,那么这种停止时间甚至可能会长达整整一秒或更久。虽然AOF重写也需要fork,但无论AOF重写的执行间隔有多长,数据的持久性都不会有任何损失

AOF的优点

数据安全性相对较高,根据所使用的fsync策略(fsync是同步内存中redis所有已经修改的文件到存储设备),默认是appendfsync everysec,即每秒执行一次,在这种配置下,Redis仍然可以保持良好的性能,并且就算发生故障停机,也最多只会损失一秒钟的数据(fsync会在后台线程执行,所以主线程可以继续努力地处理命令请求)

由于该机制对日志文件的写入操作采用的追加模式,以你在写入过程中不需要seek,即使出现宕机现象,也不会破坏日志文件中已经存在的内容,然而如果本次操作只是写了一半数据就出现了系统崩溃问题,不用担心,在redis下一次启动之前,可以通过redis-check-aof工具来解决数据一致性的问题

redis可以在AOF文件体积变得过大时,自动地在后台对AOF进行重写,重写后的新AOF文件包含了恢复当前数据集所需的最小命令集合,整个重写操作是绝对安全的,因为redis在创建新AOF文件的过程中,追加模式不断的将修改数据追加到现有的AOF文件里面,即使重写过程中发生停机,现有的AOF文件也不会丢失。而一旦新AOF文件创建完毕,redis就会从旧AOF文件切换到新AOF文件,并开始对新AOF文件产生追加操作

AOF包含一个格式清晰、易于理解的日志文件用于记录所有的修改操作。事实上,也可以通过该文件完成数据的重建

AOF文件有序保存了对数据库执行的所有写入操作,这些写入操作以redis协议的格式保存,因此AOF文件的内容非常容易被人读懂,对文件进行分析也很轻松。导出AOF文件也非常简单,比如,不小心执行了flushall命令,但只要AOF文件未被重写,那么只要停止服务器,移除AOF文件末尾的flushall命令,并重启redis,就可以将数据集恢复到flushall执行之前的状态

AOF的缺点

即使有些操作是重复的也会全部记录,AOF的文件大小要大于RDB格式的文件

AOF在恢复大数据集时的速度比RDB的恢复速度要慢

根据fsync策略不同,AOF速度可能会慢于RDB

bug出现的可能性更多

2、master和slave同步过程

1、从节点主动向主节点发送同步请求

2、主节点接收到从的消息后发送自己的runid和offset发送给从节点

3、从节点保存主节点发送过来的runid和offset

4、主节点后台执行RDB持久化,并将新写入的数据写入到缓冲区中

5、主节点将RDB文件发送到从节点

6、主节点在把RDB文件发送到从节点后,把buffer中的数据以redis协议格式发送到从节点

7、从节点清空自己旧的数据

8、从节点把主节点推送过来的RDB文件中的数据载入内存,再加载所有缓冲区中的内容完成全量同步

9、全量同步完成后,如从节点需要继续同步增量数据的话,会先将自己的offset位置发送给主节点,主节点在收到后会根据从节点的offset,将从节点offset位置之后的数据发送回从节点,同时也将在缓冲区的数据也发送给从节点

3、哨兵的使用和实现机制

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

哨兵(Sentinel)是一个分布式系统,可以在一个架构中运行多个哨兵(sentinel)进程,这些进程使用流言协议(gossip protocols)来接收关于Master主服务器是否下线的信息,并使用投票协议(AgreementProtocols)来决定是否执行自动故障迁移,以及选择哪个Slave作为新的Master

每个哨兵(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节点集合,不再是具体的Redis节点,但Sentinel只是配置中心不是代理。

Redis Sentinel节点与普通redis没有区别,要实现读写分离依赖于客户端程序

redis 3.0之前版本中,生产环境一般使用哨兵模式,但3.0后推出redis cluster功能后,可以支持更大规模的生产环境

#主节点redis配置
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis.log
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
masterauth 123456
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
requirepass 123456
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

#从节点redis配置
bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
supervised no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis.log
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
replicaof 10.0.0.8 6379 #指定主节点IP和端口
masterauth 123456 #指定主节点连接密码
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
requirepass 123456
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

[19:10:03 aaa@qq.com ~]#grep -Ev '^#|^$' /etc/redis-sentinel.conf #修改哨兵的配置文件,所有的哨兵配置都一样
port 26379
daemonize no
pidfile /var/run/redis-sentinel.pid
dir /tmp
sentinel monitor mymaster 10.0.0.8 6379 2 #指定mymaster集群master的IP和端口,当有两个以上的哨兵认为主节点宕机,执行故障迁移,哨兵数为总哨兵数的一半以上,redis和哨兵建议为奇数,否则容易出现脑裂现象
sentinel auth-pass mymaster 123456 #指定当前mymaster集群中master的密码
sentinel down-after-milliseconds mymaster 3000 #判断当前mymaster集群中所有节点的主观下线时间,建议设定为3000,单位是毫秒
sentinel parallel-syncs mymaster 1 #发生故障转移后,同时向新master同步数据的slave数量,越少越可以减轻新master的压力,但是相应的同步时间也会更长
sentinel failover-timeout mymaster 180000 #所有slave指向新的master的超时时间,单位是毫秒
sentinel deny-scripts-reconfig yes #禁止修改脚本
logfile /var/log/redis/sentinel.log #哨兵的日志

[20:13:11 aaa@qq.com ~]#systemctl enable --now redis-sentinel.service #在所有节点上启动哨兵

4、redis cluster集群创建和使用

利用原生命令手动部署redis cluster

使用六台centos8服务器

[13:03:10 aaa@qq.com ~]#yum -y install redis #在所有6个节点上安装redis服务
[13:04:02 aaa@qq.com ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf #修改所有节点的配置文件,按顺序排序分别是:修改redis监听端口;修改主节点密码;修改自身登录密码;开启cluster模式;配置cluster的配置文件路径,记录主从关系和槽位信息;关闭集群请求槽位全覆盖,该选项开启的话,如果集群中有一个主库宕机且无备库就会使整个集群不对外提供服务
[13:31:49 aaa@qq.com ~]#systemctl enable --now redis #启动redis并设置为开机启动
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.
[14:18:47 aaa@qq.com ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet 10.0.0.18 6379 #使用meet使所有节点实现互相通信,登录任意一台节点来meet其他所有要加入到本集群中的节点
OK
[14:24:12 aaa@qq.com ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet 10.0.0.28 6379
OK
[14:24:19 aaa@qq.com ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet 10.0.0.38 6379
OK
[14:24:22 aaa@qq.com ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet 10.0.0.48 6379
OK
[14:24:24 aaa@qq.com ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster meet 10.0.0.58 6379
OK
[14:32:31 aaa@qq.com ~]#vi addslot.sh #编写一个分配槽位的脚本,并利用脚本分配槽位,redis cluster的槽位范围为0-16364

#!/bin/bash
# 
#********************************************************************
#Author:            rzx
#QQ:                970707452
#Date:              2020-10-25
#FileName:         addslot.sh
#URL:               https://www.cnblogs.com/rzx-006/
#Description:      The test script
#Copyright (C):    2020 All rights reserved
#********************************************************************
host=$1
port=$2
start=$3
end=$4
pass=123456
for slot in `seq ${start} ${end}`;do
    echo slot:$slot
    redis-cli -h ${host} -p ${port} -a ${pass} --no-auth-warning cluster addslots ${slot}
done

[14:46:05 aaa@qq.com ~]#bash addslot.sh 10.0.0.8 6379 0 5461 #利用脚本为10.0.0.8分配槽位
[14:46:05 aaa@qq.com ~]#redis-cli -h 10.0.0.8 -a 123456 cluster info #查看10.0.0.8的集群状态
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:ok
cluster_slots_assigned:5462 #整个集群当中已经分配的槽位数量
cluster_slots_ok:5462
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:1
cluster_current_epoch:5
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1342
cluster_stats_messages_pong_sent:1341
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:2688
cluster_stats_messages_ping_received:1341
cluster_stats_messages_pong_received:1347
cluster_stats_messages_received:2688
[14:47:02 aaa@qq.com ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster nodes
66173bbc45b3f8e9212286827bb5fc594aab0cd7 10.0.0.58:aaa@qq.com master - 0 1603608771000 5 connected
4ad896ab9a5e573061decaea13ca72f1acc16d22 10.0.0.48:aaa@qq.com master - 0 1603608772000 3 connected
e1e8b3a666bb3f15b3b73c689782d9a1236beab3 10.0.0.38:aaa@qq.com master - 0 1603608773663 0 connected
208ee5ef0b688df38824552b2154b64869175b9e 10.0.0.8:aaa@qq.com myself,master - 0 1603608773000 1 connected 0-5461 #分配到的槽位号
b75c9952210283a64f1ce10c5e1ad85d18dc1b36 10.0.0.28:aaa@qq.com master - 0 1603608771616 4 connected
50083258803ac6bc106182853e6d0bc1ebdee45d 10.0.0.18:aaa@qq.com master - 0 1603608772647 2 connected

[14:54:10 aaa@qq.com ~]#bash addslot.sh 10.0.0.18 6379 5462 10922 #利用脚本为10.0.0.18分配槽位
[14:55:29 aaa@qq.com ~]#bash addslot.sh 10.0.0.28 6379 10923 16383 #利用脚本为10.0.0.28分配槽位

[14:55:29 aaa@qq.com ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster nodes
66173bbc45b3f8e9212286827bb5fc594aab0cd7 10.0.0.58:aaa@qq.com master - 0 1603609539000 5 connected
4ad896ab9a5e573061decaea13ca72f1acc16d22 10.0.0.48:aaa@qq.com master - 0 1603609540900 3 connected
e1e8b3a666bb3f15b3b73c689782d9a1236beab3 10.0.0.38:aaa@qq.com master - 0 1603609541915 0 connected
208ee5ef0b688df38824552b2154b64869175b9e 10.0.0.8:aaa@qq.com myself,master - 0 1603609536000 1 connected 0-5461
b75c9952210283a64f1ce10c5e1ad85d18dc1b36 10.0.0.28:aaa@qq.com master - 0 1603609539884 4 connected 10923-16383
50083258803ac6bc106182853e6d0bc1ebdee45d 10.0.0.18:aaa@qq.com master - 0 1603609540000 2 connected 5462-10922

[15:05:42 aaa@qq.com ~]#redis-cli -h 10.0.0.38 -a 123456 --no-auth-warning cluster replicate 208ee5ef0b688df38824552b2154b64869175b9e
OK #连接到10.0.0.38,为其指定master节点,master节点ID可以通过cluster nodes查看
[15:14:31 aaa@qq.com ~]#redis-cli -h 10.0.0.48 -a 123456 --no-auth-warning cluster replicate b75c9952210283a64f1ce10c5e1ad85d18dc1b36
OK #连接到10.0.0.48,为其指定master节点,master节点ID可以通过cluster nodes查看
[15:14:51 aaa@qq.com ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning cluster replicate 50083258803ac6bc106182853e6d0bc1ebdee45d
OK #连接到10.0.0.58,为其指定master节点,master节点ID可以通过cluster nodes查看

[15:17:08 aaa@qq.com ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning cluster nodes
208ee5ef0b688df38824552b2154b64869175b9e 10.0.0.8:aaa@qq.com master - 0 1603610406000 1 connected 0-5461
4ad896ab9a5e573061decaea13ca72f1acc16d22 10.0.0.48:aaa@qq.com slave b75c9952210283a64f1ce10c5e1ad85d18dc1b36 0 1603610406176 4 connected
50083258803ac6bc106182853e6d0bc1ebdee45d 10.0.0.18:aaa@qq.com master - 0 1603610404000 2 connected 5462-10922
b75c9952210283a64f1ce10c5e1ad85d18dc1b36 10.0.0.28:aaa@qq.com master - 0 1603610405162 4 connected 10923-16383
e1e8b3a666bb3f15b3b73c689782d9a1236beab3 10.0.0.38:aaa@qq.com slave 208ee5ef0b688df38824552b2154b64869175b9e 0 1603610407196 1 connected
66173bbc45b3f8e9212286827bb5fc594aab0cd7 10.0.0.58:aaa@qq.com myself,slave 50083258803ac6bc106182853e6d0bc1ebdee45d 0 1603610403000 5 connected
[15:20:07 aaa@qq.com ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning cluster info #完成搭建后查看集群状态
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:5
cluster_my_epoch:2
cluster_stats_messages_ping_sent:3333
cluster_stats_messages_pong_sent:3184
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:6518
cluster_stats_messages_ping_received:3179
cluster_stats_messages_pong_received:3334
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:6518
[15:21:05 aaa@qq.com ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning cluster slots #查看主从关系以及槽位信息
1) 1) (integer) 0
   2) (integer) 5461
   3) 1) "10.0.0.8"
      2) (integer) 6379
      3) "208ee5ef0b688df38824552b2154b64869175b9e"
   4) 1) "10.0.0.38"
      2) (integer) 6379
      3) "e1e8b3a666bb3f15b3b73c689782d9a1236beab3"
2) 1) (integer) 5462
   2) (integer) 10922
   3) 1) "10.0.0.18"
      2) (integer) 6379
      3) "50083258803ac6bc106182853e6d0bc1ebdee45d"
   4) 1) "10.0.0.58"
      2) (integer) 6379
      3) "66173bbc45b3f8e9212286827bb5fc594aab0cd7"
3) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "10.0.0.28"
      2) (integer) 6379
      3) "b75c9952210283a64f1ce10c5e1ad85d18dc1b36"
   4) 1) "10.0.0.48"
      2) (integer) 6379
      3) "4ad896ab9a5e573061decaea13ca72f1acc16d22"

[15:27:42 aaa@qq.com ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning set key1 v1 #当写入或读取的数据不保存至本机的槽位中时,会提示需要到另一台节点执行
(error) MOVED 9189 10.0.0.18:6379
[15:27:50 aaa@qq.com ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning -c set key1 v1 #加-c选项会自动重定向到目标节点执行
OK
[15:30:42 aaa@qq.com ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning -c get key1
"v1"
[15:31:15 aaa@qq.com ~]#redis-cli -h 10.0.0.58 -a 123456 --no-auth-warning  get key1
(error) MOVED 9189 10.0.0.18:6379
[16:12:29 aaa@qq.com ~]#redis-cli -a 123456 --no-auth-warning cluster keyslot 123 #计算key对应的槽位值
(integer) 5970

[16:14:39 aaa@qq.com ~]#dnf -y instal1 python3 #安装python环境
[16:15:02 aaa@qq.com ~]#pip3 insta71 redis-py-cluster #安装模块
[16:51:50 aaa@qq.com ~]#vi redis_cluster_test.py #redis cluster批量写入数据py脚本

#!/usr/bin/env python3
from rediscluster import RedisCluster
startup_nodes = [
    {"host":"10.0.0.8", "port":6379},
    {"host":"10.0.0.18", "port":6379},
    {"host":"10.0.0.28", "port":6379},
    {"host":"10.0.0.38", "port":6379},
    {"host":"10.0.0.48", "port":6379},
    {"host":"10.0.0.58", "port":6379}
]
redis_conn= RedisCluster(startup_nodes=startup_nodes,password='123456',decode_responses=True)
for i in range(0,10000):
    redis_conn.set('key'+str(i),'value'+str(i))
    print('key'+str(i)+':',redis_conn.get('key'+str(i))) 

基于redis 5.0部署redis cluster

使用六台centos8服务器

[15:43:10 aaa@qq.com ~]#yum -y install redis #在所有6个节点上安装redis服务
[15:44:02 aaa@qq.com ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/a masterauth 123456' -e '/# requirepass/a requirepass 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf #修改所有节点的配置文件,按顺序排序分别是:修改redis监听端口;修改主节点密码;修改自身登录密码;开启cluster模式;配置cluster的配置文件路径,记录主从关系和槽位信息;关闭集群请求槽位全覆盖,该选项开启的话,如果集群中有一个主库宕机且无备库就会使整个集群不对外提供服务
[15:48:49 aaa@qq.com ~]#systemctl enable --now redis #启动redis并设置为开机启动
[15:51:40 aaa@qq.com ~]#redis-cli -a 123456 --cluster create 10.0.0.8:6379 10.0.0.18:6379 10.0.0.28:6379 10.0.0.38:6379 10.0.0.48:6379 10.0.0.58:6379 --cluster-replicas 1 #一条命令实现原生命令的meet过程、分配槽位、设置主从关系,--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 10.0.0.38:6379 to 10.0.0.8:6379 #自动配置主从关系
Adding replica 10.0.0.48:6379 to 10.0.0.18:6379
Adding replica 10.0.0.58:6379 to 10.0.0.28:6379
M: b530ab922788d06da1cb85012d384ffb4fb7824f 10.0.0.8:6379 #显示节点的主从关系和槽位信息
   slots:[0-5460] (5461 slots) master
M: 43e25d5358edef69bac0dee5c964d20dda20ca4c 10.0.0.18:6379
   slots:[5461-10922] (5462 slots) master
M: 5f5f4a0032a71f307a3c710ae4fda6f6eb40c3d1 10.0.0.28:6379
   slots:[10923-16383] (5461 slots) master
S: 82ae5c91bbe25cfcd9815e4d9bd4c4d90ad4e7ab 10.0.0.38:6379
   replicates b530ab922788d06da1cb85012d384ffb4fb7824f
S: 9c21796fbc967ebd57ea30fcf7516926bdba6988 10.0.0.48:6379
   replicates 43e25d5358edef69bac0dee5c964d20dda20ca4c
S: 4acdb613601f3538eebf023df81f6cb887b617a0 10.0.0.58:6379
   replicates 5f5f4a0032a71f307a3c710ae4fda6f6eb40c3d1
Can I set the above configuration? (type 'yes' to accept): yes #输入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 10.0.0.8:6379)
M: b530ab922788d06da1cb85012d384ffb4fb7824f 10.0.0.8:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 43e25d5358edef69bac0dee5c964d20dda20ca4c 10.0.0.18:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 5f5f4a0032a71f307a3c710ae4fda6f6eb40c3d1 10.0.0.28:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 4acdb613601f3538eebf023df81f6cb887b617a0 10.0.0.58:6379
   slots: (0 slots) slave
   replicates 5f5f4a0032a71f307a3c710ae4fda6f6eb40c3d1
S: 9c21796fbc967ebd57ea30fcf7516926bdba6988 10.0.0.48:6379
   slots: (0 slots) slave
   replicates 43e25d5358edef69bac0dee5c964d20dda20ca4c
S: 82ae5c91bbe25cfcd9815e4d9bd4c4d90ad4e7ab 10.0.0.38:6379
   slots: (0 slots) slave
   replicates b530ab922788d06da1cb85012d384ffb4fb7824f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered. #所有槽位分配完成
#redis 5.0的redis cluster配置完成
[16:09:32 aaa@qq.com ~]#redis-cli -a 123456 cluster nodes #检查一下集群节点信息
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
43e25d5358edef69bac0dee5c964d20dda20ca4c 10.0.0.18:aaa@qq.com master - 0 1603613374183 2 connected 5461-10922
b530ab922788d06da1cb85012d384ffb4fb7824f 10.0.0.8:aaa@qq.com myself,master - 0 1603613373000 1 connected 0-5460
5f5f4a0032a71f307a3c710ae4fda6f6eb40c3d1 10.0.0.28:aaa@qq.com master - 0 1603613372000 3 connected 10923-16383
4acdb613601f3538eebf023df81f6cb887b617a0 10.0.0.58:aaa@qq.com slave 5f5f4a0032a71f307a3c710ae4fda6f6eb40c3d1 0 1603613374000 6 connected
9c21796fbc967ebd57ea30fcf7516926bdba6988 10.0.0.48:aaa@qq.com slave 43e25d5358edef69bac0dee5c964d20dda20ca4c 0 1603613375205 5 connected
82ae5c91bbe25cfcd9815e4d9bd4c4d90ad4e7ab 10.0.0.38:aaa@qq.com slave b530ab922788d06da1cb85012d384ffb4fb7824f 0 1603613375000 4 connected

基于rendis 4.0部署redis cluster

使用六台centos7服务器

[17:20:10 aaa@qq.com ~]#vi install_redis_for_centos.sh  #自动编译安装redis4.0.14脚本

#!/bin/bash
#
#********************************************************************
#Author:        wangxiaochun
#URL:           http://www.magedu.com
#Description:      The test script
#Copyright (C):     2020 All rights reserved
#********************************************************************
. /etc/init.d/functions 
VERSION=redis-4.0.14
PASSWORD=123456
INSTALL_DIR=/apps/redis

install() {
yum  -y install gcc jemalloc-devel || { action "安装软件包失败,请检查网络配置" false ; exit; }

wget http://download.redis.io/releases/${VERSION}.tar.gz || { action "Redis 源码下载失败" false ; exit; }



tar xf ${VERSION}.tar.gz
cd ${VERSION}


ln -s ${INSTALL_DIR}/bin/redis-*  /usr/bin/
mkdir -p ${INSTALL_DIR}/{etc,log,data,run}
cp redis.conf  ${INSTALL_DIR}/etc/



if id redis &> /dev/null ;then 
    action "Redis 用户已存在" false  
else
    useradd -r -s /sbin/nologin redis
    action "Redis 用户创建成功"
fi

chown -R redis.redis ${INSTALL_DIR}

vm.overcommit_memory = 1
EOF
sysctl -p 

echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
/etc/rc.d/rc.local

cat > /usr/lib/systemd/system/redis.service <<EOF
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=${INSTALL_DIR}/bin/redis-server ${INSTALL_DIR}/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT \$MAINPID
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target

EOF
systemctl daemon-reload 
systemctl enable --now  redis &> /dev/null && action "Redis 服务启动成功,Redis信息如下:" || { action "Redis 启动失败" false ;exit; } 

redis-cli -a $PASSWORD INFO Server 2> /dev/null

}

install

[17:24:51 aaa@qq.com ~]#bash install_redis_for_centos.sh #在所有节点执行脚本来安装redis
[17:53:59 aaa@qq.com ~]#sed -i -e '/^# masterauth/a masterauth 123456' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /apps/redis/etc/redis.conf #修改所有reids的配置文件
[17:41:51 aaa@qq.com ~]#systemctl restart redis #重启redis
[17:56:24 aaa@qq.com ~]#cp redis-4.0.14/src/redis-trib.rb /usr/bin/ #拷贝redis-trib.rb工具到/usr/bin下,方便使用
[18:24:54 aaa@qq.com ~]#yum -y install ruby #安装ruby环境,但是由于cneots7yum安装ruby版本太低,需要执行其他操作
[18:52:33 aaa@qq.com ~]#yum -y install gcc openssl-devel zlib-devel #安装编译ruby依赖包
[18:53:00 aaa@qq.com ~]#wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz #下载ruby2.5.5的源码包
--2020-10-25 18:53:24--  https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz
Resolving cache.ruby-lang.org (cache.ruby-lang.org)... 151.101.77.178, 2a04:4e42:12::434
Connecting to cache.ruby-lang.org (cache.ruby-lang.org)|151.101.77.178|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 15996436 (15M) [application/x-tar]
Saving to: ‘ruby-2.5.5.tar.gz’

100%[=================================================================>] 15,996,436  82.9KB/s   in 2m 57s 

2020-10-25 18:56:27 (88.2 KB/s) - ‘ruby-2.5.5.tar.gz’ saved [15996436/15996436]


[18:56:27 aaa@qq.com ~]#tar xf ruby-2.5.5.tar.gz  #解压源码包
[18:58:51 aaa@qq.com ruby-2.5.5]#cd ruby-2.5.5 
[18:58:51 aaa@qq.com ruby-2.5.5]#./configure #执行configure脚本生成makefile文件
[18:59:33 aaa@qq.com ruby-2.5.5]#make && make install #编译安装ruby
[19:06:18 aaa@qq.com ruby-2.5.5]#ruby -v
ruby 2.5.5p157 (2019-03-15 revision 67260) [x86_64-linux] #查看ruby版本,然后重新登录终端
[19:17:37 aaa@qq.com ~]#gem install -l redis-4.1.3.gem #安装redis模块,此为安装离线包,需提前将文件下载好,下载链接:https://rubygems.org/downloads/redis-4.1.3.gem ,注意下载此包需要*
Successfully installed redis-4.1.3
Parsing documentation for redis-4.1.3
Installing ri documentation for redis-4.1.3
Done installing documentation for redis after 0 seconds
1 gem installed
[19:20:38 aaa@qq.com ~]#vi /usr/local/lib/ruby/gems/2.5.0/gems/redis-4.1.3/lib/redis/client.rb 

# frozen_string_literal: true

require_relative "errors"
require "socket"
require "cgi"

class Redis
  class Client

    DEFAULTS = {
      :url => lambda { ENV["REDIS_URL"] },
      :scheme => "redis",
      :host => "127.0.0.1",
      :port => 6379,
      :path => nil,
      :timeout => 5.0,
      :password => 123456,   #修改脚本中连接redis的密码                                                                               
      :db => 0,
      :driver => nil,
      :id => nil,
      :tcp_keepalive => 0,
      :reconnect_attempts => 1,
      :reconnect_delay => 0,
      :reconnect_delay_max => 0.5,
      :inherit_socket => false
    }

    attr_reader :options

[19:21:49 aaa@qq.com ~]#redis-trib.rb create --replicas 1 10.0.0.7:6379 10.0.0.17:6379 10.0.0.27:6379 10.0.0.37:6379 10.0.0.47:6379 10.0.0.57:6379 #创建redis cluster集群,与redis5.0集群不同的是,从节点会随机分配给主节点,而非按顺序一一对应
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.0.0.7:6379
10.0.0.17:6379
10.0.0.27:6379
Adding replica 10.0.0.47:6379 to 10.0.0.7:6379
Adding replica 10.0.0.57:6379 to 10.0.0.17:6379
Adding replica 10.0.0.37:6379 to 10.0.0.27:6379
M: bf18eec99802798b5215ed2f8ddc12eb1248b3cb 10.0.0.7:6379
   slots:0-5460 (5461 slots) master
M: a0c915ad50c0ad66a0a4c7b6393d9e127576ad64 10.0.0.17:6379
   slots:5461-10922 (5462 slots) master
M: 97b1c4b57dbf5469bab57577f0bc022bd59917e6 10.0.0.27:6379
   slots:10923-16383 (5461 slots) master
S: b18f0f29e766120656abd946b957ef0bd712d638 10.0.0.37:6379
   replicates 97b1c4b57dbf5469bab57577f0bc022bd59917e6
S: fc17f6fea452277328d913324b463a0c91029cd3 10.0.0.47:6379
   replicates bf18eec99802798b5215ed2f8ddc12eb1248b3cb
S: aca9ab569ef599a17449bd2b9088eee4533f515e 10.0.0.57:6379
   replicates a0c915ad50c0ad66a0a4c7b6393d9e127576ad64
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 10.0.0.7:6379)
M: bf18eec99802798b5215ed2f8ddc12eb1248b3cb 10.0.0.7:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 97b1c4b57dbf5469bab57577f0bc022bd59917e6 10.0.0.27:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: aca9ab569ef599a17449bd2b9088eee4533f515e 10.0.0.57:6379
   slots: (0 slots) slave
   replicates a0c915ad50c0ad66a0a4c7b6393d9e127576ad64
S: fc17f6fea452277328d913324b463a0c91029cd3 10.0.0.47:6379
   slots: (0 slots) slave
   replicates bf18eec99802798b5215ed2f8ddc12eb1248b3cb
M: a0c915ad50c0ad66a0a4c7b6393d9e127576ad64 10.0.0.17:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: b18f0f29e766120656abd946b957ef0bd712d638 10.0.0.37:6379
   slots: (0 slots) slave
   replicates 97b1c4b57dbf5469bab57577f0bc022bd59917e6
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

[19:31:05 aaa@qq.com ~]#redis-cli -a 123456 cluster slots #查看集群主从关系以及槽位信息
Warning: Using a password with '-a' option on the command line interface may not be safe.
1) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "10.0.0.27"
      2) (integer) 6379
      3) "97b1c4b57dbf5469bab57577f0bc022bd59917e6"
   4) 1) "10.0.0.37"
      2) (integer) 6379
      3) "b18f0f29e766120656abd946b957ef0bd712d638"
2) 1) (integer) 0
   2) (integer) 5460
   3) 1) "10.0.0.7"
      2) (integer) 6379
      3) "bf18eec99802798b5215ed2f8ddc12eb1248b3cb"
   4) 1) "10.0.0.47"
      2) (integer) 6379
      3) "fc17f6fea452277328d913324b463a0c91029cd3"
3) 1) (integer) 5461
   2) (integer) 10922
   3) 1) "10.0.0.17"
      2) (integer) 6379
      3) "a0c915ad50c0ad66a0a4c7b6393d9e127576ad64"
   4) 1) "10.0.0.57"
      2) (integer) 6379
      3) "aca9ab569ef599a17449bd2b9088eee4533f515e"

66120656abd946b957ef0bd712d638 10.0.0.37:6379
replicates 97b1c4b57dbf5469bab57577f0bc022bd59917e6
S: fc17f6fea452277328d913324b463a0c91029cd3 10.0.0.47:6379
replicates bf18eec99802798b5215ed2f8ddc12eb1248b3cb
S: aca9ab569ef599a17449bd2b9088eee4533f515e 10.0.0.57:6379
replicates a0c915ad50c0ad66a0a4c7b6393d9e127576ad64
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 10.0.0.7:6379)
M: bf18eec99802798b5215ed2f8ddc12eb1248b3cb 10.0.0.7:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 97b1c4b57dbf5469bab57577f0bc022bd59917e6 10.0.0.27:6379
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: aca9ab569ef599a17449bd2b9088eee4533f515e 10.0.0.57:6379
slots: (0 slots) slave
replicates a0c915ad50c0ad66a0a4c7b6393d9e127576ad64
S: fc17f6fea452277328d913324b463a0c91029cd3 10.0.0.47:6379
slots: (0 slots) slave
replicates bf18eec99802798b5215ed2f8ddc12eb1248b3cb
M: a0c915ad50c0ad66a0a4c7b6393d9e127576ad64 10.0.0.17:6379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: b18f0f29e766120656abd946b957ef0bd712d638 10.0.0.37:6379
slots: (0 slots) slave
replicates 97b1c4b57dbf5469bab57577f0bc022bd59917e6
[OK] All nodes agree about slots configuration.

Check for open slots…
Check slots coverage…
[OK] All 16384 slots covered.

[19:31:05 aaa@qq.com ~]#redis-cli -a 123456 cluster slots #查看集群主从关系以及槽位信息
Warning: Using a password with ‘-a’ option on the command line interface may not be safe.

    1. (integer) 10923
    2. (integer) 16383
      1. “10.0.0.27”
      2. (integer) 6379
      3. “97b1c4b57dbf5469bab57577f0bc022bd59917e6”
      1. “10.0.0.37”
      2. (integer) 6379
      3. “b18f0f29e766120656abd946b957ef0bd712d638”
    1. (integer) 0
    2. (integer) 5460
      1. “10.0.0.7”
      2. (integer) 6379
      3. “bf18eec99802798b5215ed2f8ddc12eb1248b3cb”
      1. “10.0.0.47”
      2. (integer) 6379
      3. “fc17f6fea452277328d913324b463a0c91029cd3”
    1. (integer) 5461
    2. (integer) 10922
      1. “10.0.0.17”
      2. (integer) 6379
      3. “a0c915ad50c0ad66a0a4c7b6393d9e127576ad64”
      1. “10.0.0.57”
      2. (integer) 6379
      3. “aca9ab569ef599a17449bd2b9088eee4533f515e”

相关标签: redis linux