1、编译安装redis
官方下载地址:http://download.redis.io/releases/
1.1 redis特性
1、速度快: 10W QPS,基于内存,C语言实现
2、单线程
3、持久化
4、支持多种数据结构
5、支持多种编程语言
6、功能丰富: 支持Lua脚本,发布订阅,事务,pipeline等功能
7、简单: 代码短小精悍(单机核心代码只有23000行左右),单线程开发容易,不依赖外部库,使用简单
8、主从复制
9、支持高可用和分布式
1.2 redis 对比 memcached
1、支持数据的持久化:可以将内存中的数据保持在磁盘中,重启redis服务或者服务器之后可以从备份文件中恢复数据到内存继续使用
2、支持更多的数据类型:支持string(字符串)、hash(哈希数据)、list(列表)、set(集合)、zset(有序集合)
3、支持数据的备份:可以实现类似于数据的master-slave模式的数据备份,另外也支持使用快照+AOF
4、支持更大的value数据:memcache单个key value最大只支持1MB,而redis最大支持512MB(生产不建议超过2M,性能受影响)
5、在Redis6版本前,Redis 是单线程,而memcached是多线程,所以单机情况下没有memcached 并发高,性能更好,但redis 支持分布式集群以实现更高的并发,单Redis实例可以实现数万并发
6、支持集群横向扩展:基于redis cluster的横向扩展,可以实现分布式集群,大幅提升性能和数据安全性
7、都是基于 C 语言开发
1.3 redis典型应用场景
1、Session 共享:常见于web集群中的Tomcat或者PHP中多web服务器session共享
2、缓存:数据查询、电商网站商品信息、新闻内容
3、计数器:访问排行榜、商品浏览数等和次数相关的数值统计场景
4、微博/微信社交场合:共同好友,粉丝数,关注,点赞评论等
5、消息队列:ELK的日志缓存、部分业务的订阅发布系统
6、地理位置: 基于GEO(地理信息定位),实现摇一摇,附近的人,外卖等功能
1.4 编译安装redis
#安装依赖包
[root@redis ~]#yum -y install gcc jemalloc-devel make
#下载源码
[root@redis ~]#wget http://download.redis.io/releases/redis-5.0.7.tar.gz
[root@redis ~]#tar xf redis-5.0.7.tar.gz
#编译安装
[root@redis ~]#cd redis-5.0.7/
#指定redis安装目录
[root@redis redis-5.0.7]#make PREFIX=/apps/redis install
#配置变量
[root@redis redis-5.0.7]#echo ‘PATH=/apps/redis/bin:$PATH‘ >/etc/profile.d/redis.sh
[root@redis redis-5.0.7]#source /etc/profile.d/redis.sh
#目录结构
[root@redis redis-5.0.7]#tree /apps/redis/
/apps/redis/
└── bin
├── redis-benchmark
├── redis-check-aof
├── redis-check-rdb
├── redis-cli
├── redis-sentinel -> redis-server
└── redis-server
1 directory, 6 files
#准备相关目录和配置文件
[root@redis redis-5.0.7]#mkdir /apps/redis/{etc,log,data,run}
[root@redis redis-5.0.7]#cp redis.conf /apps/redis/etc/
[root@redis redis-5.0.7]#tree /apps/redis/
/apps/redis/
├── bin
│?? ├── redis-benchmark
│?? ├── redis-check-aof
│?? ├── redis-check-rdb
│?? ├── redis-cli
│?? ├── redis-sentinel -> redis-server
│?? └── redis-server
├── data
├── etc
│?? └── redis.conf
├── log
└── run
5 directories, 7 files
1.5 创建 redis 用户和数据目录
[root@redis ~]#useradd -r -s /sbin/nologin redis
[root@redis ~]#chown -R redis.redis /apps/redis/
1.6 启动redis
#前台执行
[root@redis ~]#redis-server /apps/redis/etc/redis.conf
#修改配置文件为后台执行,且修改监听地址
[root@redis ~]#vim /apps/redis/etc/redis.conf
bind 0.0.0.0
daemonize yes
pidfile /apps/redis/run/redis_6379.pid
logfile "/apps/redis/log/redis_6379.log"
dbfilename dump_6379.rdb
dir /apps/redis/data
#重启
[root@redis ~]#redis-server /apps/redis/etc/redis.conf
1.7 解决三个启动时的警告提示
WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
[root@redis ~]#vim /etc/sysctl.conf
net.core.somaxconn = 1024
[root@redis ~]#sysctl -p
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.
[root@redis ~]#vim /etc/sysctl.conf
vm.overcommit_memory = 1
[root@redis ~]#sysctl -p
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.
[root@redis ~]#echo ‘echo never > /sys/kernel/mm/transparent_hugepage/enabled‘ >> /etc/rc.d/rc.local
[root@redis ~]#chmod +x /etc/rc.d/rc.local
1.8 准备redis服务启动的文件,实现开机自启动
#复制yum安装生成的redis.service文件
[root@10 system]#scp redis.service 10.0.0.136:`pwd`
[root@10 system]#cat /usr/lib/systemd/system/redis.service
[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
#system启动时先把redis-server启动的kill掉
[root@redis ~]#systemctl enable --now redis
[root@redis ~]#systemctl status redis
● redis.service - Redis persistent key-value database
Loaded: loaded (/usr/lib/systemd/system/redis.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2021-07-22 10:56:02 CST; 4min 9s ago
Main PID: 1205 (redis-server)
Tasks: 4 (limit: 12237)
Memory: 1.5M
CGroup: /system.slice/redis.service
└─1205 /apps/redis/bin/redis-server 0.0.0.0:6379
Jul 22 10:56:02 redis systemd[1]: Starting Redis persistent key-value database...
Jul 22 10:56:02 redis systemd[1]: Started Redis persistent key-value database.
1.9 连接
[root@redis ~]#redis-cli
127.0.0.1:6379> ping
PONG
2、redis服务配置文件详解
通用配置
bind 0.0.0.0 #监听地址,可以用空格隔开后多个监听IP
protected-mode yes #redis3.2之后加入的新特性,在没有设置bind IP和密码的时候,redis只允许访问127.0.0.1:6379,可以远程连接,但当访问将提示警告信息并拒绝远程访问
port 6379 #监听端口,默认6379/tcp
tcp-backlog 511 #三次握手的时候server端收到client ack确认号之后的队列值,即全连接队列长度
timeout 0 #客户端和Redis服务端的连接超时时间,默认是0,表示永不超时
tcp-keepalive 300 #tcp 会话保持时间300s
daemonize no #默认no,即直接运行redis-server程序时,不作为守护进程运行,而是以前台方式运行,如果想在后台运行需改成yes,当redis作为守护进程运行的时候,它会写一个 pid 到 指定/var/run/redis.pid 文件
supervised no #和OS相关参数,可设置通过upstart和systemd管理Redis守护进程,centos7后都使用systemd
pidfile /var/run/redis_6379.pid #pid文件路径,可以修改为/apps/redis/run/redis_6379.pid
loglevel notice #日志级别
logfile "/path/redis.log" #日志路径,示例:logfile "/apps/redis/log/redis_6379.log"(注意权限)
databases 16 #设置数据库数量,默认:0-15,共16个库
always-show-logo yes #在启动redis 时是否显示或在日志中记录记录redis的logo
requirepass foobared #设置redis连接密码,之后需要AUTH pass,如果有特殊符号,用" "引起来,生产建议设置
rename-command #重命名一些高危命令,示例:rename-command FLUSHALL "" 禁用命令 ? #示例: rename-command del magedu
maxclients 10000 #Redis最大连接客户端
maxmemory <bytes> #redis使用的最大内存,单位为bytes字节,0为不限制,建议设为物理内存一半,8G内存的计算方式8(G)*1024(MB)1024(KB)*1024(Kbyte),需要注意的是缓冲区是不计算在maxmemory内,生产中如果不设置此项,可能会导致OOM
lua-time-limit 5000 #lua脚本的最大执行时间,单位为毫秒
和持久化有关
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 ./ #快照文件保存路径,示例:dir "/apps/redis/data"
appendonly no #是否开启AOF日志记录,默认redis使用的是rdb方式持久化,这种方式在许多应用中已经足够用了,但是redis如果中途宕机,会导致可能有几分钟的数据丢失(取决于dump数据的间隔时间),根据save来策略进行持久化,Append Only File是另一种持久化方式,可以提供更好的持久化特性,Redis会把每次写入的数据在接收后都写入 appendonly.aof 文件,每次启动时Redis都会先把这个文件的数据读入内存里,先忽略RDB文件。默认不启用此功能
appendfilename "appendonly.aof" #文本文件AOF的文件名,存放在dir指令指定的目录中
appendfsync everysec #aof持久化策略的配置 #no表示由操作系统保证数据同步到磁盘,Linux的默认fsync策略是30秒,最多会丢失30s的数据 #always表示每次写入都执行fsync,以保证数据同步到磁盘,安全性高,性能较差 #everysec表示每秒执行一次fsync,可能会导致丢失这1s数据,此为默认值,也生产建议值
no-appendfsync-on-rewrite no #在aof rewrite期间,是否对aof新记录的append暂缓使用文件同步策略,主要考虑磁盘IO开支和请求阻塞时间。
auto-aof-rewrite-percentage 100 #当Aof log增长超过指定百分比例时,重写AOF文件,设置为0表示不自动重写Aof日志,重写是为了使aof体积保持最小,但是还可以确保保存最完整的数据
auto-aof-rewrite-min-size 64mb #触发aof rewrite的最小文件大小
aof-load-truncated yes #是否加载由于某些原因导致的末尾异常的AOF文件(主进程被kill/断电等),建议yes
aof-use-rdb-preamble no #redis4.0新增RDB-AOF混合持久化格式,在开启了这个功能之后,AOF重写产生的文件将同时包含RDB格式的内容和AOF格式的内容,其中RDB格式的内容用于记录已有的数据,而AOF格式的内容则用于记录最近发生了变化的数据,这样Redis就可以同时兼有RDB持久化和AOF持久化的优点(既能够快速地生成重写文件,也能够在出现问题时,快速地载入数据),默认为no,即不启用此功能
和主从复制有关
replicaof <masterip> <masterport> #指定复制的master主机地址和端口,5.0版之前的指令为slaveof
masterauth <master-password> #指定复制的master主机的密码
replica-serve-stale-data yes #当从库同主库失去连接或者复制正在进行,从机库有两种运行方式:
1、设置为yes(默认设置),从库会继续响应客户端的读请求,此为建议值
2、设置为no,除去特定命令外的任何请求都会返回一个错误"SYNC with master in progress"。
replica-read-only yes #是否设置从库只读,建议值为yes,否则主库同步从库时可能会覆盖数据,造成数据丢失
repl-diskless-sync no #是否使用socket方式复制数据(无盘同步),新slave第一次连接master时需要做数据的全量同步,redis server就要从内存dump出新的RDB文件,然后从master传到slave,有两种方式把RDB文件传输给客户端:
1、基于硬盘(disk-backed):为no时,master创建一个新进程dump生成RDB磁盘文件,RDB完成之后由父进程(即主进程)将RDB文件发送给slaves,此为默认值
2、基于socket(diskless):master创建一个新进程直接dump RDB至slave的网络socket,不经过主进程和硬盘
#推荐使用基于硬盘(为no),是因为RDB文件创建后,可以同时传输给更多的slave,但是基于socket(为yes), 新slave连接到master之后得逐个同步数据。只有当磁盘I/O较慢且网络较快时,可用diskless(yes),否则一般建议使用磁盘(no)
repl-diskless-sync-delay 5 #diskless时复制的服务器等待的延迟时间,设置0为关闭,在延迟时间内到达的客户端,会一起通过diskless方式同步数据,但是一旦复制开始,master节点不会再接收新slave的复制请求,直到下一次同步开始才再接收新请求。即无法为延迟时间后到达的新副本提供服务,新副本将排队等待下一次RDB传输,因此服务器会等待一段时间才能让更多副本到达。推荐值:30-60
repl-ping-replica-period 10 #slave根据master指定的时间进行周期性的PING master,用于监测master状态,默认10s
repl-timeout 60 #复制连接的超时时间,需要大于repl-ping-slave-period,否则会经常报超时
repl-disable-tcp-nodelay no #是否在slave套接字发送SYNC之后禁用 TCP_NODELAY,如果选择"yes",Redis将合并多个报文为一个大的报文,从而使用更少数量的包向slaves发送数据,但是将使数据传输到slave上有延迟,Linux内核的默认配置会达到40毫秒,如果 "no" ,数据传输到slave的延迟将会减少,但要使用更多的带宽
repl-backlog-size 512mb #复制缓冲区内存大小,当slave断开连接一段时间后,该缓冲区会累积复制副本数据,因此当slave 重新连接时,通常不需要完全重新同步,只需传递在副本中的断开连接后没有同步的部分数据即可。只有在至少有一个slave连接之后才分配此内存空间,建议建立主从时此值要调大一些或在低峰期配置,否则会导致同步到slave失败
repl-backlog-ttl 3600 #多长时间内master没有slave连接,就清空backlog缓冲区
replica-priority 100 #当master不可用,哨兵Sentinel会根据slave的优先级选举一个master,此值最低的slave会优先当选master,而配置成0,永远不会被选举,一般多个slave都设为一样的值,让其自动选择
主从同步性能相关
repl-diskless-sync no # 是否使用无盘同步RDB文件,默认为no,no为不使用无盘,需要将RDB文件保存到磁盘后再发送给slave,yes为支持无盘,支持无盘就是RDB文件不需要保存至本地磁盘,而且直接通过socket文件发送给slave
repl-diskless-sync-delay 5 #diskless时复制的服务器等待的延迟时间
repl-ping-slave-period 10 #slave端向server端发送ping的时间间隔,默认为10秒
repl-timeout 60 #设置主从ping连接超时时间,超过此值无法连接,master_link_status显示为down,并记录错误日志
repl-disable-tcp-nodelay no #是否启用TCP_NODELAY,如设置成yes,则redis会合并小的TCP包从而节省带宽, 但会增加同步延迟(40ms),造成master与slave数据不一致,假如设置成no,则redis master会立即发送同步数据,没有延迟,yes关注性能,no关注redis服务中的数据一致性
repl-backlog-size 1mb #master的写入数据缓冲区,用于记录自上一次同步后到下一次同步过程中间的写入命令,计算公式:repl-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不接收写操作(拒绝写入)
集群相关
cluster-enabled yes #是否开启集群模式,默认不开启,即单机模式
cluster-config-file nodes-6379.conf #由node节点自动生成的集群配置文件名称
cluster-node-timeout 15000 #集群中node节点连接超时时间,单位ms,超过此时间,会踢出集群
cluster-replica-validity-factor 10 #单位为次,在执行故障转移的时候可能有些节点和master断开一段时间导致数据比较旧,这些节点就不适用于选举为master,超过这个时间的就不会被进行故障转移,不能当选master,计算公式:(node-timeout * replica-validity-factor) + repl-ping-replica-period
cluster-migration-barrier 1 #集群迁移屏障,一个主节点至少拥有1个正常工作的从节点,即如果主节点的slave节点故障后会将多余的从节点分配到当前主节点成为其新的从节点。
cluster-require-full-coverage yes #集群请求槽位全部覆盖,如果一个主库宕机且没有备库就会出现集群槽位不全,那么yes时redis集群槽位验证不全,就不再对外提供服务(对key赋值时,会出现CLUSTERDOWN The cluster is down的提示,cluster_state:fail,但ping 仍PONG),而no则可以继续使用,但是会出现查询数据查不到的情况(因为有数据丢失)。生产建议为no
cluster-replica-no-failover no #如果为yes,此选项阻止在主服务器发生故障时尝试对其主服务器进行故障转移。 但是,主服务器仍然可以执行手动强制故障转移,一般为no
#Slow log 是 Redis 用来记录超过指定执行时间的日志系统,执行时间不包括与客户端交谈,发送回复等I/O操作,而是实际执行命令所需的时间(在该阶段线程被阻塞并且不能同时为其它请求提供服务),由于 slow log 保存在内存里面,读写速度非常快,因此可放心地使用,不必担心因为开启 slow log 而影响 Redis 的速度
slowlog-log-slower-than 10000 #以微秒为单位的慢日志记录,为负数会禁用慢日志,为0会记录每个命令操作。默认值为10ms,一般一条命令执行都在微秒级,生产建议设为1ms-10ms之间
slowlog-max-len 128 #最多记录多少条慢日志的保存队列长度,达到此长度后,记录新命令会将最旧的命令从命令队列中删除,以此滚动删除,即,先进先出,队列固定长度,默认128,值偏小,生产建议设为1000以上
3、RDB、AOF详解及优缺点总结
3.1 RDB模式
RDB(Redis DataBase):基于时间的快照,其默认只保留当前最新的一次快照,特点是执行速度比较快,缺点是可能会丢失从上次快照到当前时间点之间未做快照的数据
RDB bgsave 实现快照的具体过程:
1、Redis从master主进程先fork出一个子进程,使用写时复制机制,子进程将内存的数据保存为一个临时文件,比如:tmp-.rdb,当数据保存完成之后再将上一次保存的RDB文件替换掉,然后关闭子进程,这样可以保证每一次做RDB快照保存的数据都是完整的
\
2、因为直接替换RDB文件的时候,可能会出现突然断电等问题,而导致RDB文件还没有保存完整就因为突然关机停止保存,而导致数据丢失的情况.后续可以手动将每次生成的RDB文件进行备份,这样可以最大化保存历史数据
RDB相关配置
save 900 1
save 300 10
save 60 10000
dbfilename dump.rdb
dir ./ ? ? ? ? #编泽编译安装,默认RDB文件存放在启动redis的工作目录,建议明确指定存入目录
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
实现RDB方式
- save: 同步,会阻赛其它命令,不推荐使用
- bgsave: 异步后台执行,不影响其它命令的执行,推荐使用
- 自动: 制定规则,自动执行
3.2 RDB模式的优缺点
优点
- RDB快照保存了某个时间点的数据,可以通过脚本执行redis指令bgsave(非阻塞,后台执行)或者save(会阻塞写操作,不推荐)命令自定义时间点备份,可以保留多个备份,当出现问题可以恢复到不同时间点的版本,很适合备份,并且此文件格式也支持有不少第三方工具可以进行后续的数据分析比如: 可以在最近的24小时内,每小时备份一次RDB文件,并且在每个月的每一天,也备份一个ROB文件。这样的话,即使遇上问题,也可以随时将数据集还原到不同的版本。
- RDB可以最大化Redis的性能,父进程在保存 RDB文件时唯一要做的就是fork出一个子进程,然后这个子进程就会处理接下来的所有保存工作,父进程无须执行任何磁盘I/O操作。
- RDB在大量数据,比如几个G的数据,恢复的速度比AOF的快
缺点
- 不能实时保存数据,可能会丢失自上一次执行RDB备份到当前的内存数据如果你需要尽量避免在服务器故障时丢失数据,那么RDB不适合。虽然Redis允许你设置不同的保存点(save point)来控制保存RDB文件的频率,但是,因为ROB文件需要保存整个数据集的状态,所以它并不是一个轻松的操作。因此你可能会至少5分钟才保存一次RDB文件。在这种情况下,一旦发生故障停机,你就可能会丢失好几分钟的数据。
- 当数据量非常大的时候,从父进程fork子进程进行保存至RDB文件时需要一点时间,可能是毫秒或者秒,取决于磁盘IO性能在数据集比较庞大时,fork()可能会非常耗时,造成服务器在一定时间内停止处理客户端﹔如果数据集非常巨大,并且CPU时间非常紧张的话,那么这种停止时间甚至可能会长达整整一秒或更久。虽然 AOF重写也需要进行fork(),但无论AOF重写的执行间隔有多长,数据的持久性都不会有任何损失。
3.3 AOF模式
AOF:AppendOnylFile,按照操作顺序依次将操作追加到指定的日志文件末尾
\
AOF 和 RDB 一样使用了写时复制机制,AOF默认为每秒钟 fsync一次,即将执行的命令保存到AOF文件当中,这样即使redis服务器发生故障的话最多只丢失1秒钟之内的数据,也可以设置不同的fsync策略always,即设置每次执行命令的时候执行fsync,fsync会在后台执行线程,所以主线程可以继续处理用户的正常请求而不受到写入AOF文件的I/O影响
\
同时启用RDB和AOF,进行恢复时,默认AOF文件优先级高于RDB文件,即会使用AOF文件进行恢复
\
注意: AOF 模式默认是关闭的,第一次开启AOF后,并重启服务生效后,会因为AOF的优先级高于RDB,而AOF默认没有文件存在,从而导致所有数据丢失
范例:启用AOF功能的正确方式[root@10 ~]#ll /var/lib/redis/ total 4 -rw-r--r-- 1 redis redis 92 Jul 22 09:21 dump.rdb [root@10 ~]#redis-cli 127.0.0.1:6379> config get appendonly 1) "appendonly" 2) "no" 127.0.0.1:6379> config set appendonly yes OK [root@10 ~]#ll /var/lib/redis/ total 8 -rw-r--r-- 1 redis redis 92 Jul 22 17:00 appendonly.aof -rw-r--r-- 1 redis redis 92 Jul 22 09:21 dump.rdb
#改完不需要重启
[root@10 ~]#vim /etc/redis.conf
appendonly yes
#### AOF 相关配置
appendonly yes
appendfilename "appendonly-${port}.aof"
appendfsync everysec
dir /path
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
#### 3.4 AOF 模式优缺点
#### AOF模式优点
* 数据安全性相对较高,根据所使用的fsync策略(fsync是同步内存中redis所有已经修改的文件到存储设备),默认是appendfsync everysec,即每秒执行一次 fsync,在这种配置下,Redis 仍然可以保持良好的性能,并且就算发生故障停机,也最多只会丢失一秒钟的数据( fsync会在后台线程执行,所以主线程可以继续努力地处理命令请求)
* 由于该机制对日志文件的写入操作采用的是append模式,因此在写入过程中不需要seek, 即使出现宕机现象,也不会破坏日志文件中已经存在的内容。然而如果本次操作只是写入了一半数据就出现了系统崩溃问题,不用担心,在Redis下一次启动之前,可以通过 redis-check-aof 工具来解决数据一致性的问题
* Redis可以在 AOF文件体积变得过大时,自动地在后台对AOF进行重写,重写后的新AOF文件包含了恢复当前数据集所需的最小命令集合。整个重写操作是绝对安全的,因为Redis在创建新 AOF文件的过程中,append模式不断的将修改数据追加到现有的 AOF文件里面,即使重写过程中发生停机,现有的 AOF文件也不会丢失。而一旦新AOF文件创建完毕,Redis就会从旧AOF文件切换到新AOF文件,并开始对新AOF文件进行追加操作。
* AOF包含一个格式清晰、易于理解的日志文件用于记录所有的修改操作。事实上,也可以通过该文件完成数据的重建AOF文件有序地保存了对数据库执行的所有写入操作,这些写入操作以Redis协议的格式保存,因此 AOF文件的内容非常容易被人读懂,对文件进行分析(parse)也很轻松。导出(export)AOF文件也非常简单:举个例子,如果你不小心执行了FLUSHALL.命令,但只要AOF文件未被重写,那么只要停止服务器,移除 AOF文件末尾的FLUSHAL命令,并重启Redis ,就可以将数据集恢复到FLUSHALL执行之前的状态。
#### AOF模式缺点
* 即使有些操作是重复的也会全部记录,AOF 的文件大小要大于 RDB 格式的文件
* AOF 在恢复大数据集时的速度比 RDB 的恢复速度要慢
* 根据fsync策略不同,AOF速度可能会慢于RDB
* bug 出现的可能性更多
#### 3.5 RDB和AOF 对比后的选择
如果主要充当缓存功能,或者可以承受数分钟数据的丢失, 通常生产环境一般只需启用RDB即可,此也是默认值
如果数据需要持久保存,一点不能丢失,可以选择同时开启RDB和AOF,一般不建议只开启AOF
### 4、Redis Cluster搭建
redis cluster相关命令
#### 企业案例:基于redis5的redis cluster部署
#### 4.1 创建 redis cluster集群的环境准备
1、每个redis 节点采用相同的硬件配置、相同的密码、相同的redis版本
2、所有redis服务器必须没有任何数据
3、准备六台主机,地址如下:
10.0.0.138
10.0.0.139
10.0.0.140
10.0.0.141
10.0.0.142
10.0.0.143
#### 4.2 启用 redis cluster 配置
#六台主机都执行
[root@redis-3 ~]#dnf install redis -y
#六台主机都修改redis配置,手动修改配置文件
[root@redis-3 ~]#vim /etc/redis.conf
bind 0.0.0.0
masterauth 123456 ? #建议配置,否则后期的master和slave主从复制无法成功,还需再配置
requirepass 123456
cluster-enabled yes #取消此行注释,必须开启集群,开启后redis 进程会有cluster显示
cluster-config-file nodes-6379.conf #取消此行注释,此为集群状态文件,记录主从关系及slot范围信息,由redis cluster 集群自动创建和维护
cluster-require-full-coverage no ? #默认值为yes,设为no可以防止一个节点不可用导致整个cluster不可能
#或者执行下面命令,批量修改
[root@redis-3 ~]#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-requirefull-coverage yes/c cluster-require-full-coverage no‘ /etc/redis.conf
[root@redis-3 ~]#systemctl enable --now redis
#验证当前redis服务状态
[root@redis-3 ~]#ss -lnt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:6379 0.0.0.0:
LISTEN 0 128 0.0.0.0:22 0.0.0.0:
LISTEN 0 128 0.0.0.0:16379 0.0.0.0:
LISTEN 0 128 [::]:22 [::]:
[root@redis-3 ~]#ps -ef |grep redis
redis 1501 1 0 09:40 ? 00:00:01 /usr/bin/redis-server 0.0.0.0:6379 [cluster]
root 1607 1035 0 10:03 pts/0 00:00:00 grep --color=auto redis
#### 4.3 创建集群
redis-cli --cluster-replicas 1 表示每个master对应一个slave节点
[root@redis-3 ~]#redis-cli -a 123456 --cluster create 10.0.0.138:6379 10.0.0.139:6379 10.0.0.140:6379 10.0.0.141:6379 10.0.0.142:6379 10.0.0.143:6379 --cluster-replicas 1
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.0.0.141:6379 to 10.0.0.138:6379
Adding replica 10.0.0.142:6379 to 10.0.0.139:6379
Adding replica 10.0.0.143:6379 to 10.0.0.140:6379
M: 5e026e48110a3e6d4ab857e1401c7ac4acdeec66 10.0.0.138:6379 #带M的为master
slots:[0-5460] (5461 slots) master #当前master的槽位起始和结束位
M: 1184c1f6e224bdd408e31dc66653db39317911cc 10.0.0.139:6379
slots:[5461-10922] (5462 slots) master
M: 250cbf9b6bf83fd8cdfd61732d98449fc4eed005 10.0.0.140:6379
slots:[10923-16383] (5461 slots) master
S: aa6576bb77b65399cb612fe0cd8c656c4091b6bb 10.0.0.141:6379 #带S的slave
replicates 5e026e48110a3e6d4ab857e1401c7ac4acdeec66
S: 93a65f9e2bc09febacb473e4cf9489bb36091c96 10.0.0.142:6379
replicates 1184c1f6e224bdd408e31dc66653db39317911cc
S: fa141734962e0e15e2e2bbd7a79a930f4225c9ba 10.0.0.143:6379
replicates 250cbf9b6bf83fd8cdfd61732d98449fc4eed005
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.138:6379)
M: 5e026e48110a3e6d4ab857e1401c7ac4acdeec66 10.0.0.138:6379
slots:[0-5460] (5461 slots) master #已经分配的槽位
1 additional replica(s) #分配了一个slave
M: 1184c1f6e224bdd408e31dc66653db39317911cc 10.0.0.139:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 250cbf9b6bf83fd8cdfd61732d98449fc4eed005 10.0.0.140:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 93a65f9e2bc09febacb473e4cf9489bb36091c96 10.0.0.142:6379
slots: (0 slots) slave #slave没有分配槽位
replicates 1184c1f6e224bdd408e31dc66653db39317911cc #对应的master的10.0.0.139的ID
S: aa6576bb77b65399cb612fe0cd8c656c4091b6bb 10.0.0.141:6379
slots: (0 slots) slave
replicates 5e026e48110a3e6d4ab857e1401c7ac4acdeec66 #对应的master的10.0.0.138的ID
S: fa141734962e0e15e2e2bbd7a79a930f4225c9ba 10.0.0.143:6379
slots: (0 slots) slave
replicates 250cbf9b6bf83fd8cdfd61732d98449fc4eed005 #对应的master的10.0.0.140的ID
[OK] All nodes agree about slots configuration. #所有节点槽位分配完成
Check for open slots... #检查打开的槽位
Check slots coverage... #检查插槽覆盖范围
[OK] All 16384 slots covered. #所有槽位(16384个)分配完成
#观察以上结果,可以看到3组master/slave
master:10.0.0.138---slave:10.0.0.141
master:10.0.0.139---slave:10.0.0.142
master:10.0.0.140---slave:10.0.0.143
#### 4.4 查看主从状态
[root@redis-3 ~]#redis-cli -a 123456 -c INFO replication
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.141,port=6379,state=online,offset=25886,lag=1
master_replid:2a3808b8007fb0ecd0163608b0b29754959f90b7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:25900
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:25900
[root@redis-4 ~]#redis-cli -a 123456 -c INFO replication
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.142,port=6379,state=online,offset=25956,lag=1
master_replid:9100414a8921710b91abc62338c1ae98667ee0ed
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:25956
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:25956
[root@redis-5 ~]#redis-cli -a 123456 INFO replication
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.143,port=6379,state=online,offset=26026,lag=1
master_replid:f1a081f80af4c4f3e99ed988ff96fb7984d7d9e1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:26026
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:26026
[root@redis-6 ~]#redis-cli -a 123456 INFO replication
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
Replication
role:slave
master_host:10.0.0.138
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:26054
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:2a3808b8007fb0ecd0163608b0b29754959f90b7
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:26054
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:26054
[root@redis-7 ~]#redis-cli -a 123456 INFO replication
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
Replication
role:slave
master_host:10.0.0.139
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:26124
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:9100414a8921710b91abc62338c1ae98667ee0ed
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:26124
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:26124
[root@redis-8 ~]#redis-cli -a 123456 INFO replication
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
Replication
role:slave
master_host:10.0.0.140
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_repl_offset:26152
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:f1a081f80af4c4f3e99ed988ff96fb7984d7d9e1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:26152
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:26152
#### 4.5 查看集群状态
[root@redis-8 ~]#redis-cli -a 123456 CLUSTER INFO
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
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:6
cluster_my_epoch:3
cluster_stats_messages_ping_sent:18831
cluster_stats_messages_pong_sent:18747
cluster_stats_messages_meet_sent:3
cluster_stats_messages_sent:37581
cluster_stats_messages_ping_received:18745
cluster_stats_messages_pong_received:18834
cluster_stats_messages_meet_received:2
cluster_stats_messages_received:37581
#### 4.6 python脚本实现redis cluster集群写入
[root@redis-3 ~]#dnf -y install python3
[root@redis-3 ~]#pip3 install redis-py-cluster
[root@redis-3 ~]#cat redis_cluster_test.py
#!/usr/bin/env python3
from rediscluster import RedisCluster
startup_nodes = [
{"host":"10.0.0.138","port":6379},
{"host":"10.0.0.139","port":6379},
{"host":"10.0.0.140","port":6379},
{"host":"10.0.0.141","port":6379},
{"host":"10.0.0.142","port":6379},
{"host":"10.0.0.143","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)))
#### 4.7 模拟master故障,对应slave节点自动提升为master
[root@redis-3 ~]#systemctl stop redis
#观察主从切换情况
[root@redis-3 ~]#cat /var/lib/redis/nodes-6379.conf
### 5、Redis Cluster扩容
因公司业务发展迅猛,现有的三主三从的redis cluster架构可能无法满足现有业务的并发写入需求,因此公司紧急采购两台服务器10.0.0.144,10.0.0.145,需要将其动态添加到集群当中,但不能影响业务使用和数据丢失。
#### 5.1 添加节点准备
增加Redis node节点,需要与之前的Redis node版本相同、配置一致,然后分别再启动两台Redis node,应为一主一从。
#配置node7节点
[root@redis-9 ~]#dnf install redis -y
[root@redis-9 ~]#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
[root@redis-9 ~]#systemctl enable --now redis
#配置node8节点
[root@redis-10 ~]#dnf install redis -y
[root@redis-10 ~]#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
[root@redis-10 ~]#systemctl enable --now redis
#### 5.2 添加新的master节点到集群
#### Redis 5 添加方式:
#将一台新的主机10.0.0.144加入集群,以下示例中10.0.0.138可以是任意存在的集群节点
[root@redis-3 ~]#redis-cli -a 123456 --cluster add-node 10.0.0.144:6379 10.0.0.138:6379
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
Adding node 10.0.0.144:6379 to cluster 10.0.0.138:6379
Performing Cluster Check (using node 10.0.0.138:6379)
M: 5e026e48110a3e6d4ab857e1401c7ac4acdeec66 10.0.0.138:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 1184c1f6e224bdd408e31dc66653db39317911cc 10.0.0.139:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: aa6576bb77b65399cb612fe0cd8c656c4091b6bb 10.0.0.141:6379
slots: (0 slots) slave
replicates 5e026e48110a3e6d4ab857e1401c7ac4acdeec66
S: 93a65f9e2bc09febacb473e4cf9489bb36091c96 10.0.0.142:6379
slots: (0 slots) slave
replicates 1184c1f6e224bdd408e31dc66653db39317911cc
S: fa141734962e0e15e2e2bbd7a79a930f4225c9ba 10.0.0.143:6379
slots: (0 slots) slave
replicates 250cbf9b6bf83fd8cdfd61732d98449fc4eed005
M: 250cbf9b6bf83fd8cdfd61732d98449fc4eed005 10.0.0.140: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.144:6379 to make it join the cluster.
[OK] New node added correctly.
#观察到该节点已经加入成功,但此节点上没有slot位,也无从节点,而且新的节点是master
[root@redis-9 ~]#redis-cli -a 123456 --cluster info 10.0.0.138:6379
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
10.0.0.138:6379 (5e026e48...) -> 3331 keys | 5461 slots | 1 slaves.
10.0.0.139:6379 (1184c1f6...) -> 3340 keys | 5462 slots | 1 slaves.
10.0.0.144:6379 (a02da590...) -> 0 keys | 0 slots | 0 slaves.
10.0.0.140:6379 (250cbf9b...) -> 3329 keys | 5461 slots | 1 slaves.
[OK] 10000 keys in 4 masters.
0.61 keys per slot on average.
[root@redis-9 ~]#redis-cli -a 123456 --cluster check 10.0.0.138:6379
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
10.0.0.138:6379 (5e026e48...) -> 3331 keys | 5461 slots | 1 slaves.
10.0.0.139:6379 (1184c1f6...) -> 3340 keys | 5462 slots | 1 slaves.
10.0.0.144:6379 (a02da590...) -> 0 keys | 0 slots | 0 slaves.
10.0.0.140:6379 (250cbf9b...) -> 3329 keys | 5461 slots | 1 slaves.
[OK] 10000 keys in 4 masters.
0.61 keys per slot on average.
Performing Cluster Check (using node 10.0.0.138:6379)
M: 5e026e48110a3e6d4ab857e1401c7ac4acdeec66 10.0.0.138:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 1184c1f6e224bdd408e31dc66653db39317911cc 10.0.0.139:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: aa6576bb77b65399cb612fe0cd8c656c4091b6bb 10.0.0.141:6379
slots: (0 slots) slave
replicates 5e026e48110a3e6d4ab857e1401c7ac4acdeec66
M: a02da590151e7c6de105b939a7a95aad547afb80 10.0.0.144:6379
slots: (0 slots) master
S: 93a65f9e2bc09febacb473e4cf9489bb36091c96 10.0.0.142:6379
slots: (0 slots) slave
replicates 1184c1f6e224bdd408e31dc66653db39317911cc
S: fa141734962e0e15e2e2bbd7a79a930f4225c9ba 10.0.0.143:6379
slots: (0 slots) slave
replicates 250cbf9b6bf83fd8cdfd61732d98449fc4eed005
M: 250cbf9b6bf83fd8cdfd61732d98449fc4eed005 10.0.0.140: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.
[root@redis-3 ~]#cat /var/lib/redis/nodes-6379.conf
1184c1f6e224bdd408e31dc66653db39317911cc 10.0.0.139:6379@16379 master - 0 1627117661000 2 connected 5461-10922
aa6576bb77b65399cb612fe0cd8c656c4091b6bb 10.0.0.141:6379@16379 slave 5e026e48110a3e6d4ab857e1401c7ac4acdeec66 0 1627117661459 11 connected
a02da590151e7c6de105b939a7a95aad547afb80 10.0.0.144:6379@16379 master - 0 1627117662384 0 connected
93a65f9e2bc09febacb473e4cf9489bb36091c96 10.0.0.142:6379@16379 slave 1184c1f6e224bdd408e31dc66653db39317911cc 0 1627117661000 5 connected
fa141734962e0e15e2e2bbd7a79a930f4225c9ba 10.0.0.143:6379@16379 slave 250cbf9b6bf83fd8cdfd61732d98449fc4eed005 0 1627117659000 9 connected
5e026e48110a3e6d4ab857e1401c7ac4acdeec66 10.0.0.138:6379@16379 myself,master - 0 1627117657000 11 connected 0-5460
250cbf9b6bf83fd8cdfd61732d98449fc4eed005 10.0.0.140:6379@16379 master - 0 1627117660432 9 connected 10923-16383
vars currentEpoch 11 lastVoteEpoch 0
#集群状态
[root@redis-3 ~]#redis-cli -a 123456 CLUSTER INFO
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:7
cluster_size:3
cluster_current_epoch:11
cluster_my_epoch:11
cluster_stats_messages_ping_sent:5831
cluster_stats_messages_pong_sent:2003
cluster_stats_messages_auth-req_sent:10
cluster_stats_messages_sent:7844
cluster_stats_messages_ping_received:1998
cluster_stats_messages_pong_received:2120
cluster_stats_messages_meet_received:1
cluster_stats_messages_fail_received:6
cluster_stats_messages_auth-req_received:2
cluster_stats_messages_auth-ack_received:2
cluster_stats_messages_received:4129
#### 5.3 在新的master上重新分配槽位
新的node节点加到集群之后,默认是master节点,但是没有slots,需要重新分配
添加主机之后需要对添加至集群种的新主机重新分片,否则其没有分片也就无法写入数据。
#### 注意: 重新分配槽位需要清空数据,所以需要先备份数据,扩展后再恢复数据
Redis 5:
[root@redis-3 ~]#redis-cli -a 123456 --cluster reshard 10.0.0.138:6379
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.138:6379)
M: 5e026e48110a3e6d4ab857e1401c7ac4acdeec66 10.0.0.138:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 1184c1f6e224bdd408e31dc66653db39317911cc 10.0.0.139:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: aa6576bb77b65399cb612fe0cd8c656c4091b6bb 10.0.0.141:6379
slots: (0 slots) slave
replicates 5e026e48110a3e6d4ab857e1401c7ac4acdeec66
M: a02da590151e7c6de105b939a7a95aad547afb80 10.0.0.144:6379
slots: (0 slots) master
S: 93a65f9e2bc09febacb473e4cf9489bb36091c96 10.0.0.142:6379
slots: (0 slots) slave
replicates 1184c1f6e224bdd408e31dc66653db39317911cc
S: fa141734962e0e15e2e2bbd7a79a930f4225c9ba 10.0.0.143:6379
slots: (0 slots) slave
replicates 250cbf9b6bf83fd8cdfd61732d98449fc4eed005
M: 250cbf9b6bf83fd8cdfd61732d98449fc4eed005 10.0.0.140: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.
How many slots do you want to move (from 1 to 16384)? 4096 ##新分配多少个槽位=16384/master个数
What is the receiving node ID? a02da590151e7c6de105b939a7a95aad547afb80
#新的master的ID
Please enter all the source node IDs.
Type ‘all‘ to use all the nodes as source nodes for the hash slots.
Type ‘done‘ once you entered all the source nodes IDs.
Source node #1: all #将哪些源主机的槽位分配给新的节点,all是自动在所有的redis node选择划分,如果是从redis cluster删除某个主机可以使用此方式将指定主机上的槽位全部移动到别的redis主机
......
Do you want to proceed with the proposed reshard plan (yes/no)? ?yes #确认分配
......
#确定slot分配成功
[root@redis-3 ~]#redis-cli -a 123456 --cluster check 10.0.0.138:6379
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
10.0.0.138:6379 (5e026e48...) -> 2511 keys | 4096 slots | 1 slaves.
10.0.0.139:6379 (1184c1f6...) -> 2515 keys | 4096 slots | 1 slaves.
10.0.0.144:6379 (a02da590...) -> 2474 keys | 4096 slots | 0 slaves.
10.0.0.140:6379 (250cbf9b...) -> 2500 keys | 4096 slots | 1 slaves.
[OK] 10000 keys in 4 masters.
0.61 keys per slot on average.
Performing Cluster Check (using node 10.0.0.138:6379)
M: 5e026e48110a3e6d4ab857e1401c7ac4acdeec66 10.0.0.138:6379
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
M: 1184c1f6e224bdd408e31dc66653db39317911cc 10.0.0.139:6379
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: aa6576bb77b65399cb612fe0cd8c656c4091b6bb 10.0.0.141:6379
slots: (0 slots) slave
replicates 5e026e48110a3e6d4ab857e1401c7ac4acdeec66
M: a02da590151e7c6de105b939a7a95aad547afb80 10.0.0.144:6379
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
S: 93a65f9e2bc09febacb473e4cf9489bb36091c96 10.0.0.142:6379
slots: (0 slots) slave
replicates 1184c1f6e224bdd408e31dc66653db39317911cc
S: fa141734962e0e15e2e2bbd7a79a930f4225c9ba 10.0.0.143:6379
slots: (0 slots) slave
replicates 250cbf9b6bf83fd8cdfd61732d98449fc4eed005
M: 250cbf9b6bf83fd8cdfd61732d98449fc4eed005 10.0.0.140:6379
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.#### 5.4 为新的master添加新的slave节点 #### redis5 一步添加从节点方法
[root@redis-3 ~]#redis-cli -a 123456 --cluster add-node 10.0.0.145:6379 10.0.0.138:6379 --cluster-slave --cluster-master-id a02da590151e7c6de105b939a7a95aad547afb80
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
Adding node 10.0.0.145:6379 to cluster 10.0.0.138:6379
Performing Cluster Check (using node 10.0.0.138:6379)
M: 5e026e48110a3e6d4ab857e1401c7ac4acdeec66 10.0.0.138:6379
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
M: 1184c1f6e224bdd408e31dc66653db39317911cc 10.0.0.139:6379
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: aa6576bb77b65399cb612fe0cd8c656c4091b6bb 10.0.0.141:6379
slots: (0 slots) slave
replicates 5e026e48110a3e6d4ab857e1401c7ac4acdeec66
M: a02da590151e7c6de105b939a7a95aad547afb80 10.0.0.144:6379
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
S: 93a65f9e2bc09febacb473e4cf9489bb36091c96 10.0.0.142:6379
slots: (0 slots) slave
replicates 1184c1f6e224bdd408e31dc66653db39317911cc
S: fa141734962e0e15e2e2bbd7a79a930f4225c9ba 10.0.0.143:6379
slots: (0 slots) slave
replicates 250cbf9b6bf83fd8cdfd61732d98449fc4eed005
M: 250cbf9b6bf83fd8cdfd61732d98449fc4eed005 10.0.0.140:6379
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.
Send CLUSTER MEET to node 10.0.0.145:6379 to make it join the cluster.
Waiting for the cluster to joinConfigure node as replica of 10.0.0.144:6379.
[OK] New node added correctly.
#验证是否成功
[root@redis-3 ~]#redis-cli -a 123456 --cluster check 10.0.0.138:6379
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
10.0.0.138:6379 (5e026e48...) -> 2511 keys | 4096 slots | 1 slaves.
10.0.0.139:6379 (1184c1f6...) -> 2515 keys | 4096 slots | 1 slaves.
10.0.0.144:6379 (a02da590...) -> 2474 keys | 4096 slots | 1 slaves.
10.0.0.140:6379 (250cbf9b...) -> 2500 keys | 4096 slots | 1 slaves.
[OK] 10000 keys in 4 masters.
0.61 keys per slot on average.
Performing Cluster Check (using node 10.0.0.138:6379)
M: 5e026e48110a3e6d4ab857e1401c7ac4acdeec66 10.0.0.138:6379
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
S: 402845a7c82716fa4dff01105888be1976a97161 10.0.0.145:6379
slots: (0 slots) slave
replicates a02da590151e7c6de105b939a7a95aad547afb80
M: 1184c1f6e224bdd408e31dc66653db39317911cc 10.0.0.139:6379
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: aa6576bb77b65399cb612fe0cd8c656c4091b6bb 10.0.0.141:6379
slots: (0 slots) slave
replicates 5e026e48110a3e6d4ab857e1401c7ac4acdeec66
M: a02da590151e7c6de105b939a7a95aad547afb80 10.0.0.144:6379
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
1 additional replica(s)
S: 93a65f9e2bc09febacb473e4cf9489bb36091c96 10.0.0.142:6379
slots: (0 slots) slave
replicates 1184c1f6e224bdd408e31dc66653db39317911cc
S: fa141734962e0e15e2e2bbd7a79a930f4225c9ba 10.0.0.143:6379
slots: (0 slots) slave
replicates 250cbf9b6bf83fd8cdfd61732d98449fc4eed005
M: 250cbf9b6bf83fd8cdfd61732d98449fc4eed005 10.0.0.140:6379
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.### 6、Redis Cluster缩容 由于10.0.0.8服务器使用年限已经超过三年,已经超过厂商质保期而且硬盘出现异常报警,经运维部架构师提交方案并同开发同事开会商议,决定将现有Redis集群的8台主服务器中的master 10.0.0.144和对应的slave 10.0.0.145 临时下线,三台服务器的并发写入性能足够支出未来1-2年的业务需求 #### 删除节点过程: 添加节点的时候是先添加node节点到集群,然后分配槽位,删除节点的操作与添加节点的操作正好相反,是先将被删除的Redis node上的槽位迁移到集群中的其他Redis node节点上,然后再将其删除,如果一个Redis node节点上的槽位没有被完全迁移,删除该node的时候会提示有数据且无法删除。 #### 6.1 迁移master 的槽位之其他master #### 注意: 被迁移Redis master源服务器必须保证没有数据,否则迁移报错并会被强制中断。 redis5版本
#查看当前状态
[root@redis-3 ~]#redis-cli -a 123456 --cluster check 10.0.0.138:6379
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
10.0.0.138:6379 (5e026e48...) -> 2511 keys | 4096 slots | 1 slaves.
10.0.0.139:6379 (1184c1f6...) -> 2515 keys | 4096 slots | 1 slaves.
10.0.0.144:6379 (a02da590...) -> 2474 keys | 4096 slots | 1 slaves.
10.0.0.140:6379 (250cbf9b...) -> 2500 keys | 4096 slots | 1 slaves.
[OK] 10000 keys in 4 masters.
0.61 keys per slot on average.
Performing Cluster Check (using node 10.0.0.138:6379)
M: 5e026e48110a3e6d4ab857e1401c7ac4acdeec66 10.0.0.138:6379
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
S: 402845a7c82716fa4dff01105888be1976a97161 10.0.0.145:6379
slots: (0 slots) slave
replicates a02da590151e7c6de105b939a7a95aad547afb80
M: 1184c1f6e224bdd408e31dc66653db39317911cc 10.0.0.139:6379
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: aa6576bb77b65399cb612fe0cd8c656c4091b6bb 10.0.0.141:6379
slots: (0 slots) slave
replicates 5e026e48110a3e6d4ab857e1401c7ac4acdeec66
M: a02da590151e7c6de105b939a7a95aad547afb80 10.0.0.144:6379
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
1 additional replica(s)
S: 93a65f9e2bc09febacb473e4cf9489bb36091c96 10.0.0.142:6379
slots: (0 slots) slave
replicates 1184c1f6e224bdd408e31dc66653db39317911cc
S: fa141734962e0e15e2e2bbd7a79a930f4225c9ba 10.0.0.143:6379
slots: (0 slots) slave
replicates 250cbf9b6bf83fd8cdfd61732d98449fc4eed005
M: 250cbf9b6bf83fd8cdfd61732d98449fc4eed005 10.0.0.140:6379
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.
#连接到任意集群节点,#最后1365个slot从10.0.0.144移动到第一个master节点10.0.0.138上
[root@redis-3 ~]#redis-cli -a 123456 --cluster reshard 10.0.0.138:6379
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.138:6379)
M: 5e026e48110a3e6d4ab857e1401c7ac4acdeec66 10.0.0.138:6379
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
S: 402845a7c82716fa4dff01105888be1976a97161 10.0.0.145:6379
slots: (0 slots) slave
replicates a02da590151e7c6de105b939a7a95aad547afb80
M: 1184c1f6e224bdd408e31dc66653db39317911cc 10.0.0.139:6379
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: aa6576bb77b65399cb612fe0cd8c656c4091b6bb 10.0.0.141:6379
slots: (0 slots) slave
replicates 5e026e48110a3e6d4ab857e1401c7ac4acdeec66
M: a02da590151e7c6de105b939a7a95aad547afb80 10.0.0.144:6379
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
1 additional replica(s)
S: 93a65f9e2bc09febacb473e4cf9489bb36091c96 10.0.0.142:6379
slots: (0 slots) slave
replicates 1184c1f6e224bdd408e31dc66653db39317911cc
S: fa141734962e0e15e2e2bbd7a79a930f4225c9ba 10.0.0.143:6379
slots: (0 slots) slave
replicates 250cbf9b6bf83fd8cdfd61732d98449fc4eed005
M: 250cbf9b6bf83fd8cdfd61732d98449fc4eed005 10.0.0.140:6379
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 1356 #共4096/3分别给其它三个master节点
What is the receiving node ID? 5e026e48110a3e6d4ab857e1401c7ac4acdeec66 #master 10.0.0.138
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: a02da590151e7c6de105b939a7a95aad547afb80 #输入要删除10.0.0.144节点ID
Source node #2: done
......
Do you want to proceed with the proposed reshard plan (yes/no)? yes #确定
#非交互式方式
#再将1365个slot从10.0.0.144移动到第二个master节点10.0.0.139上
[root@redis-3 ~]#redis-cli -a 123456 --cluster reshard 10.0.0.138:6379 --cluster-slots 1365 --cluster-from a02da590151e7c6de105b939a7a95aad547afb80 --cluster-to 1184c1f6e224bdd408e31dc66653db39317911cc --cluster-yes
#最后的slot从10.0.0.144移动到第三个master节点10.0.0.140上
[root@redis-3 ~]#redis-cli -a 123456 --cluster reshard 10.0.0.138:6379 --cluster-slots 1365 --cluster-from a02da590151e7c6de105b939a7a95aad547afb80 --cluster-to 250cbf9b6bf83fd8cdfd61732d98449fc4eed005 --cluster-yes
#确认10.0.0.144的所有slot都移走了,上面的slave也自动删除,成为其它master的slave
[root@redis-3 ~]#redis-cli -a 123456 --cluster check 10.0.0.138:6379
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
10.0.0.138:6379 (5e026e48...) -> 3327 keys | 5452 slots | 1 slaves.
10.0.0.139:6379 (1184c1f6...) -> 3337 keys | 5461 slots | 1 slaves.
10.0.0.144:6379 (a02da590...) -> 0 keys | 0 slots | 0 slaves.
10.0.0.140:6379 (250cbf9b...) -> 3336 keys | 5471 slots | 2 slaves.
[OK] 10000 keys in 4 masters.
0.61 keys per slot on average.
Performing Cluster Check (using node 10.0.0.138:6379)
M: 5e026e48110a3e6d4ab857e1401c7ac4acdeec66 10.0.0.138:6379
slots:[0-1355],[1365-5460] (5452 slots) master
1 additional replica(s)
S: 402845a7c82716fa4dff01105888be1976a97161 10.0.0.145:6379
slots: (0 slots) slave
replicates 250cbf9b6bf83fd8cdfd61732d98449fc4eed005
M: 1184c1f6e224bdd408e31dc66653db39317911cc 10.0.0.139:6379
slots:[1356-1364],[5461-6816],[6827-10922] (5461 slots) master
1 additional replica(s)
S: aa6576bb77b65399cb612fe0cd8c656c4091b6bb 10.0.0.141:6379
slots: (0 slots) slave
replicates 5e026e48110a3e6d4ab857e1401c7ac4acdeec66
M: a02da590151e7c6de105b939a7a95aad547afb80 10.0.0.144:6379
slots: (0 slots) master
S: 93a65f9e2bc09febacb473e4cf9489bb36091c96 10.0.0.142:6379
slots: (0 slots) slave
replicates 1184c1f6e224bdd408e31dc66653db39317911cc
S: fa141734962e0e15e2e2bbd7a79a930f4225c9ba 10.0.0.143:6379
slots: (0 slots) slave
replicates 250cbf9b6bf83fd8cdfd61732d98449fc4eed005
M: 250cbf9b6bf83fd8cdfd61732d98449fc4eed005 10.0.0.140:6379
slots:[6817-6826],[10923-16383] (5471 slots) master
2 additional replica(s)
[OK] All nodes agree about slots configuration.
Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.
#原有的10.0.0.145自动成为10.0.0.140的slave
[root@redis-3 ~]#redis-cli -a 123456 -h 10.0.0.140 INFO replication
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
Replication
role:master
connected_slaves:2
slave0:ip=10.0.0.143,port=6379,state=online,offset=255178,lag=1
slave1:ip=10.0.0.145,port=6379,state=online,offset=255178,lag=1
master_replid:854af54a5edb496b1dc54b607508ccc8704f0c8e
master_replid2:e5065d7984c16a74a1d228ee3bbc5200436ba483
master_repl_offset:255178
second_repl_offset:168929
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:168887
repl_backlog_histlen:86292
[root@redis-3 ~]#redis-cli -a 123456 -h 10.0.0.138 --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:8 #集群中8个节点
cluster_size:3 #少了一个主从的slot
cluster_current_epoch:15
cluster_my_epoch:13
cluster_stats_messages_ping_sent:8478
cluster_stats_messages_pong_sent:4638
cluster_stats_messages_auth-req_sent:10
cluster_stats_messages_update_sent:26
cluster_stats_messages_sent:13152
cluster_stats_messages_ping_received:4632
cluster_stats_messages_pong_received:4767
cluster_stats_messages_meet_received:2
cluster_stats_messages_fail_received:6
cluster_stats_messages_auth-req_received:2
cluster_stats_messages_auth-ack_received:2
cluster_stats_messages_received:9411
#### 6.2 从集群中删除服务器
虽然槽位已经迁移完成,但是服务器IP信息还在集群当中,因此还需要将IP信息从集群删除注意: 删除服务器前,必须清除主机上面的槽位,否则会删除主机失败
#### redis5
[root@redis-3 ~]#redis-cli -a 123456 --cluster del-node 10.0.0.144:6379 a02da590151e7c6de105b939a7a95aad547afb80
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
Removing node a02da590151e7c6de105b939a7a95aad547afb80 from cluster 10.0.0.144:6379
Sending CLUSTER FORGET messages to the cluster...
SHUTDOWN the node.
#删除节点后,redis进程自动关闭
#删除节点信息
[root@redis-9 ~]#rm -f /var/lib/redis/nodes-6379.conf
#### 6.3 删除多余的slave节点验证结果
#验证删除成功
[root@redis-9 ~]#ss -lnt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:
LISTEN 0 128 [::]:22 [::]:
[root@redis-9 ~]#redis-cli -a 123456 --cluster check 10.0.0.138:6379
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
10.0.0.138:6379 (5e026e48...) -> 3327 keys | 5452 slots | 1 slaves.
10.0.0.139:6379 (1184c1f6...) -> 3337 keys | 5461 slots | 1 slaves.
10.0.0.140:6379 (250cbf9b...) -> 3336 keys | 5471 slots | 2 slaves.
[OK] 10000 keys in 3 masters.
0.61 keys per slot on average.
Performing Cluster Check (using node 10.0.0.138:6379)
M: 5e026e48110a3e6d4ab857e1401c7ac4acdeec66 10.0.0.138:6379
slots:[0-1355],[1365-5460] (5452 slots) master
1 additional replica(s)
S: 402845a7c82716fa4dff01105888be1976a97161 10.0.0.145:6379
slots: (0 slots) slave
replicates 250cbf9b6bf83fd8cdfd61732d98449fc4eed005
M: 1184c1f6e224bdd408e31dc66653db39317911cc 10.0.0.139:6379
slots:[1356-1364],[5461-6816],[6827-10922] (5461 slots) master
1 additional replica(s)
S: aa6576bb77b65399cb612fe0cd8c656c4091b6bb 10.0.0.141:6379
slots: (0 slots) slave
replicates 5e026e48110a3e6d4ab857e1401c7ac4acdeec66
S: 93a65f9e2bc09febacb473e4cf9489bb36091c96 10.0.0.142:6379
slots: (0 slots) slave
replicates 1184c1f6e224bdd408e31dc66653db39317911cc
S: fa141734962e0e15e2e2bbd7a79a930f4225c9ba 10.0.0.143:6379
slots: (0 slots) slave
replicates 250cbf9b6bf83fd8cdfd61732d98449fc4eed005
M: 250cbf9b6bf83fd8cdfd61732d98449fc4eed005 10.0.0.140:6379
slots:[6817-6826],[10923-16383] (5471 slots) master
2 additional replica(s)
[OK] All nodes agree about slots configuration.
Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.
#删除多余的slave从节点
[root@redis-3 ~]#redis-cli -a 123456 --cluster del-node 10.0.0.145:6379 402845a7c82716fa4dff01105888be1976a97161
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
Removing node 402845a7c82716fa4dff01105888be1976a97161 from cluster 10.0.0.145:6379
Sending CLUSTER FORGET messages to the cluster...
SHUTDOWN the node.
#删除集群文件
[root@redis-10 ~]#rm -f /var/lib/redis/nodes-6379.conf
[root@redis-10 ~]#redis-cli -a 123456 --cluster check 10.0.0.138:6379
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
10.0.0.138:6379 (5e026e48...) -> 3327 keys | 5452 slots | 1 slaves.
10.0.0.139:6379 (1184c1f6...) -> 3337 keys | 5461 slots | 1 slaves.
10.0.0.140:6379 (250cbf9b...) -> 3336 keys | 5471 slots | 1 slaves.
[OK] 10000 keys in 3 masters.
0.61 keys per slot on average.
Performing Cluster Check (using node 10.0.0.138:6379)
M: 5e026e48110a3e6d4ab857e1401c7ac4acdeec66 10.0.0.138:6379
slots:[0-1355],[1365-5460] (5452 slots) master
1 additional replica(s)
M: 1184c1f6e224bdd408e31dc66653db39317911cc 10.0.0.139:6379
slots:[1356-1364],[5461-6816],[6827-10922] (5461 slots) master
1 additional replica(s)
S: aa6576bb77b65399cb612fe0cd8c656c4091b6bb 10.0.0.141:6379
slots: (0 slots) slave
replicates 5e026e48110a3e6d4ab857e1401c7ac4acdeec66
S: 93a65f9e2bc09febacb473e4cf9489bb36091c96 10.0.0.142:6379
slots: (0 slots) slave
replicates 1184c1f6e224bdd408e31dc66653db39317911cc
S: fa141734962e0e15e2e2bbd7a79a930f4225c9ba 10.0.0.143:6379
slots: (0 slots) slave
replicates 250cbf9b6bf83fd8cdfd61732d98449fc4eed005
M: 250cbf9b6bf83fd8cdfd61732d98449fc4eed005 10.0.0.140:6379
slots:[6817-6826],[10923-16383] (5471 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
Check for open slots...
Check slots coverage...
[OK] All 16384 slots covered.
[root@redis-3 ~]#redis-cli -a 123456 --cluster info 10.0.0.138:6379
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
10.0.0.138:6379 (5e026e48...) -> 3327 keys | 5452 slots | 1 slaves.
10.0.0.139:6379 (1184c1f6...) -> 3337 keys | 5461 slots | 1 slaves.
10.0.0.140:6379 (250cbf9b...) -> 3336 keys | 5471 slots | 1 slaves.
[OK] 10000 keys in 3 masters.
0.61 keys per slot on average.
#查看集群信息
[root@redis-3 ~]#redis-cli -a 123456 -h 10.0.0.138 CLUSTER INFO
Warning: Using a password with ‘-a‘ or ‘-u‘ option on the command line interface may not be safe.
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:15
cluster_my_epoch:13
cluster_stats_messages_ping_sent:9137
cluster_stats_messages_pong_sent:5294
cluster_stats_messages_auth-req_sent:10
cluster_stats_messages_update_sent:26
cluster_stats_messages_sent:14467
cluster_stats_messages_ping_received:5288
cluster_stats_messages_pong_received:5426
cluster_stats_messages_meet_received:2
cluster_stats_messages_fail_received:6
cluster_stats_messages_auth-req_received:2
cluster_stats_messages_auth-ack_received:2
cluster_stats_messages_received:10726