1、RDB和AOF的优缺点

RDB 模式优点

  • RDB快照保存了某个时间点的数据,可以通过脚本执行redis指令bgsave(非阻塞,后台执行)或者

save(会阻塞写操作,不推荐)命令自定义时间点备份,可以保留多个备份,当出现问题可以恢复到不

同时间点的版本,很适合备份,并且此文件格式也支持有不少第三方工具可以进行后续的数据分析

  • RDB可以最大化Redis的性能,父进程在保存 RDB文件时唯一要做的就是fork出一个子进程,然后

这个子进程就会处理接下来的所有保存工作,父进程无须执行任何磁盘I/O操作。

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

RDB 模式缺点

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

  • 当数据量非常大的时候,从父进程fork子进程进行保存至RDB文件时需要一点时间,可能是毫秒或

    者秒,取决于磁盘IO性能

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 模式缺点

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

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

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

  • bug 出现的可能性更多

2、master和slave同步过程

准备:三台主机,redis版本相同

  • 10.0.0.181 centos8 master

  • 10.0.0.182 centos8 slave1

  • 10.0.0.183 centos8 slave2

[root@master ~]#rpm -q redis
redis-5.0.3-2.module_el8.2.0+318+3d7e67ea.x86_64

[root@slave1 ~]#rpm -q redis
redis-5.0.3-2.module_el8.2.0+318+3d7e67ea.x86_64

[root@slave2 ~]#rpm -q redis
redis-5.0.3-2.module_el8.2.0+318+3d7e67ea.x86_64

#设置开机启动
[root@master ~]#systemctl enable --now redis
[root@slave1 ~]#systemctl enable --now redis
[root@slave2 ~]#systemctl enable --now redis

#查看当前角色
[root@master ~]#redis-cli

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:f748b849fdbaff714af959a72cb97e3ebd978ed1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
#修改配置文件
[root@master ~]#vim /etc/redis.conf 
requirepass 123456
bind 0.0.0.0
[root@master ~]#redis-cli
127.0.0.1:6379> config get requirepass
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "123456"

[root@slave1 ~]#redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:a785d9350ec75c09e7cda281378dfb3be47337b5
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0


[root@slave2 ~]#redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:6b9b01c8dd305127ba2733da2d41787c237d41e6
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

#修改slave节点配置文件
[root@slave1 ~]#vim /etc/redis.conf
replicaof 10.0.0.181 6379
masterauth 123456
[root@slave1 ~]#systemctl restart redis
#其它slave节点执行上面操作

#查看master和slave状态
#master查看
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.0.0.182,port=6379,state=online,offset=280,lag=0
slave1:ip=10.0.0.183,port=6379,state=online,offset=280,lag=1
master_replid:45619a966a44ad07ef6189eaa6ffc94f4fe8bc65
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:280
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:280
#slave查看
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:10.0.0.181
master_port:6379
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:308
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:45619a966a44ad07ef6189eaa6ffc94f4fe8bc65
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:308
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:308

#在master节点添加数据
127.0.0.1:6379> set k1 xiaobai
OK
127.0.0.1:6379> get k1
"xiaobai"
#在其它slave节点查看数据
127.0.0.1:6379> get k1
"xiaobai"

假设master主节点10.0.0.181故障,提升10.0.0.182为新的master

#查看当前10.0.0.182节点的状态为slave,master指向10.0.0.181
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:10.0.0.181
master_port:6379
master_link_status:up
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:869
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:45619a966a44ad07ef6189eaa6ffc94f4fe8bc65
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:869
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:869

#停止10.0.0.181主节点后,slave节点状态
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:10.0.0.181
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1149
master_link_down_since_seconds:9
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:45619a966a44ad07ef6189eaa6ffc94f4fe8bc65
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1149
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1149

#停止所有slave同步并提升10.0.0.182为新的master
127.0.0.1:6379> replicaof no one
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:0
master_replid:1c5173fb75f92b9cf97f247705cc0c45b9c0a227
master_replid2:45619a966a44ad07ef6189eaa6ffc94f4fe8bc65
master_repl_offset:1149
second_repl_offset:1150
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1149
#修改10.0.0.182主节点配置文件
[root@slave1 ~]#vim /etc/redis.conf
requirepass 123456
bind 0.0.0.0

#测试能否在10.0.0.182写入数据
127.0.0.1:6379> set k2 xiaohei
OK

#修改所有slave指向新的master节点
127.0.0.1:6379> slaveof 10.0.0.182 6379
OK

127.0.0.1:6379> info replication
# Replication
role:slave
master_host:10.0.0.182
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1149
master_link_down_since_seconds:481
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:ec8f98f66bcdcea173bbeaf0a8be714b3ea538af
master_replid2:45619a966a44ad07ef6189eaa6ffc94f4fe8bc65
master_repl_offset:1149
second_repl_offset:1150
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:267
repl_backlog_histlen:883


#查看新的master主节点状态
[root@slave1 ~]#redis-cli
127.0.0.1:6379> info replication
NOAUTH Authentication required.
127.0.0.1:6379> auth 123456
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.183,port=6379,state=online,offset=14,lag=1
master_replid:d69418fa239e654af1490488f3777f0272b3e74a
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:14
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:14

#查看slave节点数据
127.0.0.1:6379> get k2
"xiaohei"

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

准备:三台主机,redis版本相同

  • 10.0.0.181 centos8 master

  • 10.0.0.182 centos8 slave1

  • 10.0.0.183 centos8 slave2

#在所有主从节点执行
[root@master ~]#dnf -y install redis
[root@master ~]#vim /etc/redis.conf
bind 0.0.0.0
masterauth 123456
requirepass 123456

#在所有slave节点执行
[root@slave1 ~]#vim /etc/redis.conf 
replicaof 10.0.0.181 6379

[root@slave1 ~]#systemctl enable --now redis

master服务器状态

[root@master ~]#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> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=10.0.0.182,port=6379,state=online,offset=588,lag=1
slave1:ip=10.0.0.183,port=6379,state=online,offset=588,lag=1
master_replid:b7df03819c9d6b427178eba1afee06075af54765
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:588
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:588

slave服务状态

#slave1
[root@slave1 ~]#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> info replication
# Replication
role:slave
master_host:10.0.0.181
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:742
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b7df03819c9d6b427178eba1afee06075af54765
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:742
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:742

#slave2
[root@slave2 ~]#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> info replication
# Replication
role:slave
master_host:10.0.0.181
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:980
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:b7df03819c9d6b427178eba1afee06075af54765
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:980
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:561
repl_backlog_histlen:420

哨兵配置文件

Sentinel实际上是一个特殊的redis服务器,有些redis指令支持,但很多指令并不支持,默认监听在26379/tcp端口

哨兵可以不喝redis服务器部署在一起,但一般部署在一起,所有redis节点使用相同的配置文件

三个哨兵服务器配置如下

[root@master ~]#vim /etc/redis-sentinel.conf 
port 26379
daemonize no
pidfile /var/run/redis-sentinel.pid
logfile "/var/log/redis/sentinel.log"                                                   dir /tmp        
sentinel monitor mymaster 10.0.0.181 6379 2                                             sentinel auth-pass mymaster 123456        
sentinel down-after-milliseconds mymaster 3000
sentinel parallel-syncs mymaster 1                                                       sentinel deny-scripts-reconfig yes                                                                    

#复制到其他哨兵
[root@master ~]#scp /etc/redis-sentinel.conf 10.0.0.182:/etc/
[root@master ~]#scp /etc/redis-sentinel.conf 10.0.0.183:/etc/

启动三台哨兵服务器

#确保每个哨兵主机myid不同
[root@master ~]#vim /etc/redis-sentinel.conf 
sentinel myid 3967cb0ae66ab02d7857d5bf4c4935381a49f6b1                                   [root@slave1 ~]#vim /etc/redis-sentinel.conf
sentinel myid 3967cb0ae66ab02d7857d5bf4c4935381a49f6b2                                   [root@slave2 ~]#vim /etc/redis-sentinel.conf       
sentinel myid 3967cb0ae66ab02d7857d5bf4c4935381a49f6b3                                   

[root@master ~]#systemctl enable --now redis-sentinel.service

master哨兵日志

[root@master ~]#tail -f /var/log/redis/sentinel.log 
19061:X 26 Oct 2020 00:17:11.970 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
19061:X 26 Oct 2020 00:17:11.970 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=19061, just started
19061:X 26 Oct 2020 00:17:11.970 # Configuration loaded
19061:X 26 Oct 2020 00:17:11.970 * supervised by systemd, will signal readiness
19061:X 26 Oct 2020 00:17:11.971 * Running mode=sentinel, port=26379.
19061:X 26 Oct 2020 00:17:11.971 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
19061:X 26 Oct 2020 00:17:11.971 # Sentinel ID is 3967cb0ae66ab02d7857d5bf4c4935381a49f6b1
19061:X 26 Oct 2020 00:17:11.971 # +monitor master mymaster 10.0.0.181 6379 quorum 2
19061:X 26 Oct 2020 00:17:30.329 * +sentinel sentinel 3967cb0ae66ab02d7857d5bf4c4935381a49f6b2 10.0.0.182 26379 @ mymaster 10.0.0.181 6379
19061:X 26 Oct 2020 00:17:49.069 * +sentinel sentinel 3967cb0ae66ab02d7857d5bf4c4935381a49f6b3 10.0.0.183 26379 @ mymaster 10.0.0.181 6379

slave哨兵日志

[root@slave1 ~]#tail -f /var/log/redis/sentinel.log 
24821:X 26 Oct 2020 00:08:31.017 # Sentinel is now ready to exit, bye bye...
24927:X 26 Oct 2020 00:17:28.241 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
24927:X 26 Oct 2020 00:17:28.241 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=24927, just started
24927:X 26 Oct 2020 00:17:28.241 # Configuration loaded
24927:X 26 Oct 2020 00:17:28.241 * supervised by systemd, will signal readiness
24927:X 26 Oct 2020 00:17:28.241 * Running mode=sentinel, port=26379.
24927:X 26 Oct 2020 00:17:28.242 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
24927:X 26 Oct 2020 00:17:28.242 # Sentinel ID is 3967cb0ae66ab02d7857d5bf4c4935381a49f6b2
24927:X 26 Oct 2020 00:17:28.242 # +monitor master mymaster 10.0.0.181 6379 quorum 2
24927:X 26 Oct 2020 00:17:49.009 * +sentinel sentinel 3967cb0ae66ab02d7857d5bf4c4935381a49f6b3 10.0.0.183 26379 @ mymaster 10.0.0.181 6379

当前sentinel状态

[root@master ~]#redis-cli -p 26379
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=10.0.0.181:6379,slaves=2,sentinels=3

测试故障转移停止redis master

[root@master ~]#systemctl stop redis-sentinel.service

#查看各节点哨兵信息
#新的master状态
[root@slave1 ~]#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> if replication
(error) ERR unknown command `if`, with args beginning with: `replication`, 
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:10.0.0.181
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:276506
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:6b8d09bdac816bc95762622cae125ee59787e312
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:276506
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:51398
repl_backlog_histlen:225109
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.183,port=6379,state=online,offset=306844,lag=0
master_replid:7415313c8a1c737f31fe2d08c66daa1ab46f22bd
master_replid2:6b8d09bdac816bc95762622cae125ee59787e312
master_repl_offset:306844
second_repl_offset:286432
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:51398
repl_backlog_histlen:255447

#slave指向新的master
[root@slave2 ~]#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> info replication
# Replication
role:slave
master_host:10.0.0.182
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:314595
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:7415313c8a1c737f31fe2d08c66daa1ab46f22bd
master_replid2:6b8d09bdac816bc95762622cae125ee59787e312
master_repl_offset:314595
second_repl_offset:286432
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:314595

4、redis cluster集群创建和使用

准备:6台主机,redis版本相同

  • 10.0.0.181 centos8

  • 10.0.0.182 centos8

  • 10.0.0.183 centos8

  • 10.0.0.184 centos8

  • 10.0.0.185 centos8

  • 10.0.0.186 centos8

在所有主机安装redis并启动cluster功能

#在所有主机上都执行下面操作
[root@centos184 ~]#dnf -y install redis

[root@centos184 ~]#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@centos184 ~]#systemctl enable --now redis

执行meet操作实现相互通信

#在任意节点和其它所有节点meet通信
[root@centos181 ~]#redis-cli -h 10.0.0.181 -a 123456 --no-auth-warning cluster meet 10.0.0.182 6379
OK
[root@centos181 ~]#redis-cli -h 10.0.0.181 -a 123456 --no-auth-warning cluster meet 10.0.0.183 6379
OK
[root@centos181 ~]#redis-cli -h 10.0.0.181 -a 123456 --no-auth-warning cluster meet 10.0.0.184 6379
OK
[root@centos181 ~]#redis-cli -h 10.0.0.181 -a 123456 --no-auth-warning cluster meet 10.0.0.185 6379
OK
[root@centos181 ~]#redis-cli -h 10.0.0.181 -a 123456 --no-auth-warning cluster meet 10.0.0.186 6379
OK

#所有节点之间可以相互连接通信
[root@centos181 ~]#redis-cli -h 10.0.0.181 -a 123456 --no-auth-warning cluster nodes
1955b8e9e0025e42232042985beae946980d7a55 10.0.0.183:6379@16379 master - 0 1603649158000 8 connected
3f829d33da8046d1dbf69a1efa5f269059adf669 10.0.0.181:6379@16379 myself,master - 0 1603649158000 6 connected
fd6ac94c8b812ee4909b1c6b37f67e466816564a 10.0.0.184:6379@16379 master - 0 1603649157000 0 connected
705fe48685591a83c4272b7955a52b05a75ee569 10.0.0.186:6379@16379 master - 0 1603649158041 3 connected
93ce7e1941dc37e84ae89cb91c875e10ced4d1a5 10.0.0.182:6379@16379 master - 0 1603649157000 7 connected
371e66beca20dedc48ed60c6edaec4df3712586b 10.0.0.185:6379@16379 master - 0 1603649157031 2 connected

#查看当前状态
[root@centos181 ~]#redis-cli -h 10.0.0.181 -a 123456 --no-auth-warning cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:0
cluster_current_epoch:8
cluster_my_epoch:6
cluster_stats_messages_ping_sent:359
cluster_stats_messages_pong_sent:382
cluster_stats_messages_meet_sent:2
cluster_stats_messages_sent:743
cluster_stats_messages_ping_received:379
cluster_stats_messages_pong_received:361
cluster_stats_messages_meet_received:3
cluster_stats_messages_received:743

为各个master节点指派槽位范围

#添加槽位的脚本
[root@centos181 ~]#vim addslot.sh

#!/bin/bash
#**********************************************************************
#Author:                DOU
#Date:                  2020-10-26
#FileName:              addslot.sh
#Description:           The test script
#**********************************************************************
#
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

#为三个master分配槽位
[root@centos181 ~]#bash addslot.sh 10.0.0.181 6379 0 5461
[root@centos181 ~]#bash addslot.sh 10.0.0.182 6379 5461 10922
[root@centos181 ~]#bash addslot.sh 10.0.0.183 6379 10923 16383

#分配完槽位,可以看到下面信息
[root@centos181 ~]#redis-cli -a 123456 --no-auth-warning cluster nodes
1955b8e9e0025e42232042985beae946980d7a55 10.0.0.183:6379@16379 master - 0 1603651757000 12 connected 10923-16383
3f829d33da8046d1dbf69a1efa5f269059adf669 10.0.0.181:6379@16379 myself,master - 0 1603651758000 6 connected 0-5461
fd6ac94c8b812ee4909b1c6b37f67e466816564a 10.0.0.184:6379@16379 master - 0 1603651757700 0 connected
705fe48685591a83c4272b7955a52b05a75ee569 10.0.0.186:6379@16379 master - 0 1603651755687 3 connected
93ce7e1941dc37e84ae89cb91c875e10ced4d1a5 10.0.0.182:6379@16379 master - 0 1603651756692 13 connected 5462-10922
371e66beca20dedc48ed60c6edaec4df3712586b 10.0.0.185:6379@16379 master - 0 1603651757000 2 connected

指定各个节点主从关系

[root@centos181 ~]#redis-cli -h 10.0.0.184 -a 123456 --no-auth-warning cluster replicate 3f829d33da8046d1dbf69a1efa5f269059adf669
OK
[root@centos181 ~]#redis-cli -h 10.0.0.185 -a 123456 --no-auth-warning cluster replicate 93ce7e1941dc37e84ae89cb91c875e10ced4d1a5
OK
[root@centos181 ~]#redis-cli -h 10.0.0.186 -a 123456 --no-auth-warning cluster replicate 1955b8e9e0025e42232042985beae946980d7a55
OK
[root@centos181 ~]#redis-cli -a 123456 --no-auth-warning cluster nodes
1955b8e9e0025e42232042985beae946980d7a55 10.0.0.183:6379@16379 master - 0 1603652004000 12 connected 10923-16383
3f829d33da8046d1dbf69a1efa5f269059adf669 10.0.0.181:6379@16379 myself,master - 0 1603652006000 6 connected 0-5461
fd6ac94c8b812ee4909b1c6b37f67e466816564a 10.0.0.184:6379@16379 slave 3f829d33da8046d1dbf69a1efa5f269059adf669 0 1603652005000 6 connected
705fe48685591a83c4272b7955a52b05a75ee569 10.0.0.186:6379@16379 slave 1955b8e9e0025e42232042985beae946980d7a55 0 1603652005940 12 connected
93ce7e1941dc37e84ae89cb91c875e10ced4d1a5 10.0.0.182:6379@16379 master - 0 1603652003920 13 connected 5462-10922
371e66beca20dedc48ed60c6edaec4df3712586b 10.0.0.185:6379@16379 slave 93ce7e1941dc37e84ae89cb91c875e10ced4d1a5 0 1603652006000 13 connected

#查看主从状态
[root@centos181 ~]#redis-cli -h 10.0.0.181 -a 123456 --no-auth-warning info replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.184,port=6379,state=online,offset=308,lag=1
master_replid:c2467ba536c90fe4e855b218869ea607fe16d878
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:308
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:308
[root@centos181 ~]#redis-cli -h 10.0.0.184 -a 123456 --no-auth-warning info replication
# Replication
role:slave
master_host:10.0.0.181
master_port:6379
master_link_status:up
master_last_io_seconds_ago:8
master_sync_in_progress:0
slave_repl_offset:322
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:c2467ba536c90fe4e855b218869ea607fe16d878
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:322
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:322

[root@centos181 ~]#redis-cli -h 10.0.0.181 -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:13
cluster_my_epoch:6
cluster_stats_messages_ping_sent:3368
cluster_stats_messages_pong_sent:3354
cluster_stats_messages_meet_sent:2
cluster_stats_messages_update_sent:5
cluster_stats_messages_sent:6729
cluster_stats_messages_ping_received:3351
cluster_stats_messages_pong_received:3370
cluster_stats_messages_meet_received:3
cluster_stats_messages_received:6724

验证redis cluster访问

[root@centos181 ~]#redis-cli -c -h 10.0.0.181 -a 123456 --no-auth-warning set name xiaobai
OK
[root@centos184 ~]#redis-cli -c -h 10.0.0.181 -a 123456 --no-auth-warning get name
"xiaobai"