2020-10-26

1、RDB和AOF的优缺点

RDB模式的优点:

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

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

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

比如: 可以在最近的24小时内,每小时备份一次RDB文件,并且在每个月的每一天,也备份一个

ROB文件。这样的话,即使遇上问题,也可以随时将数据集还原到不同的版本。

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

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

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

RDB模式的缺点:

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

如果你需要尽量避免在服务器故障时丢失数据,那么RDB不适合你。虽然Redis允许你设置不同的

保存点(save point)来控制保存RDB文件的频率,但是,因为ROB文件需要保存整个数据集的状

态,所以它并不是一个轻松的操作。因此你可能会至少5分钟才保存一次RDB文件。在这种情况

下,一旦发生故障停机,你就可能会丢失好几分钟的数据。

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

者秒,取决于磁盘IO性能

在数据集比较庞大时,fork()可能会非常耗时,造成服务器在一定时间内停止处理客户端﹔如果数

据集非常巨大,并且CPU时间非常紧张的话,那么这种停止时间甚至可能会长达整整一秒或更久。

虽然 AOF重写也需要进行fork(),但无论AOF重写的执行间隔有多长,数据的持久性都不会有任何

损失。

AOF模式的优点:

1. 数据安全性相对较高,根据所使用的fsync策略(fsync是同步内存中redis所有已经修改的文件到存

储设备),默认是appendfsync everysec,即每秒执行一次 fsync,在这种配置下,Redis 仍然可以

保持良好的性能,并且就算发生故障停机,也最多只会丢失一秒钟的数据( fsync会在后台线程执

行,所以主线程可以继续努力地处理命令请求)

2. 由于该机制对日志文件的写入操作采用的是append模式,因此在写入过程中不需要seek, 即使出

现宕机现象,也不会破坏日志文件中已经存在的内容。然而如果本次操作只是写入了一半数据就出

现了系统崩溃问题,不用担心,在Redis下一次启动之前,可以通过 redis-check-aof 工具来解决

数据一致性的问题

3.Redis可以在 AOF文件体积变得过大时,自动地在后台对AOF进行重写,重写后的新AOF文件包含了

恢复当前数据集所需的最小命令集合。整个重写操作是绝对安全的,因为Redis在创建新 AOF文件

的过程中,append模式不断的将修改数据追加到现有的 AOF文件里面,即使重写过程中发生停

机,现有的 AOF文件也不会丢失。而一旦新AOF文件创建完毕,Redis就会从旧AOF文件切换到新

AOF文件,并开始对新AOF文件进行追加操作。

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

件完成数据的重建

AOF的缺点

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

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

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

4. bug 出现的可能性更多

RDB和AOF的区别:

RDB持久化是指在指定的时间间隔内将内存中的数据集快照写入磁盘,实际操作过程是fork一个子进程,先将数据集写入临时文件,写入成功后,在替换之前的文件,用二进制压缩存储

AOF持久化日志的形式记录服务器所处理的每一个写、删除操作,查询操作不会记录,以文本的方式记录,可以打开文件看到详细的操作记录。

RDB和AOF的选择

如果主要充当缓存功能,或者可以承受数分钟的丢失,通常生产环境一般只需要启用RDB即可,此也是默认值

如果数据需要持久保存,一点不能丢失,可以选择同时开启RDB和AOF,一般不建议只开启AOF。

2、master和slave同步过程

1、slave启动后向master发送同步指令SYNC,master接收到SYNC指令后将调用该命令的处理函数syncCommand()进行同步处理

2、在函数syncCommand中,将调用函数rdbSaveBackground启动以个备份进程用于同步数据,如果已经有一个备份进程在运行,将不会在重新启动了

3、备份进程将执行函数rdbSave()完成将redis的全部数据保存为rdb文件

4、在redis的时间事件函数serverCron中,将对备份后的数据进行处理,在serverCron函数将会检查备份,进程是否已经执行完毕,如果备份进程已经完成备份,则调用函数backgroundSaveDoneHandler完成后续处理

5、在函数backgroundSaveDoneHandler中,首先更新master的各种状态

6、在函数updateSlavesWaitingBgsave中,将遍历所有的等待此次备份的slave,另外,这里并不是立即就把数据发送过去,而是将为每个等待的slave注册写事件,并注册写事件的响应函数sendBulkToSlave,即当slave对应的socket能够发送数据时就调用函数sendBulkToSlave(),实际发送rdb文件的操作都在函数sendBulkToSlave中完成的

7、sendBulkToSlave函数将把备份的rdb文件发送给slave。

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

#在所有主从节点上执行

[root@master ~]#dnf -y install redis

[root@slave1 ~]#dnf -y install redis

[root@slave2 ~]#dnf -y install redis

#在所有主从节点上执行,修改redis的配置文件 交互式修改

[root@master ~]#sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e 's/^# masterauth .*/masterauth 1234567/' -e 's/^# requirepass .*/requirepass 1234567/' /etc/redis.conf

[root@slave1 ~]#sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e 's/^# masterauth .*/masterauth 1234567/' -e 's/^# requirepass .*/requirepass 1234567/' /etc/redis.conf

[root@slave2 ~]#sed -i -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e 's/^# masterauth .*/masterauth 1234567/' -e 's/^# requirepass .*/requirepass 1234567/' /etc/redis.conf

#在所有从节点上执行

[root@slave1 ~]#echo "replicaof 10.0.0.8 6379" >> /etc/redis.conf

[root@slave2 ~]#echo "replicaof 10.0.0.8 6379" >> /etc/redis.conf

#在所有主从节点上执行

![](https://img2020.cnblogs.com/blog/1979780/202010/1979780-20201024144439773-574411739.png)

#查看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.28,port=6379,state=online,offset=126,lag=0

slave1:ip=10.0.0.18,port=6379,state=online,offset=126,lag=0

master_replid:458f6d2c11e76b3be7747a027dc0b481b3794faa

master_replid2:0000000000000000000000000000000000000000

master_repl_offset:126

second_repl_offset:-1

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:1

repl_backlog_histlen:126

#配置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> REPLICAOF 10.0.0.8 6379

OK Already connected to specified master

127.0.0.1:6379> CONFIG SET masterauth "123456"

OK

127.0.0.1:6379> INFO relication

127.0.0.1:6379> INFO replication

# Replication

role:slave

master_host:10.0.0.8

master_port:6379

master_link_status:up

master_last_io_seconds_ago:8

master_sync_in_progress:0

slave_repl_offset:588

slave_priority:100

slave_read_only:1

connected_slaves:0

master_replid:458f6d2c11e76b3be7747a027dc0b481b3794faa

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

127.0.0.1:6379>

#配置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> REPLICAOF 10.0.0.8 6379

OK Already connected to specified master

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

master_port:6379

master_link_status:up

master_last_io_seconds_ago:3

master_sync_in_progress:0

slave_repl_offset:826

slave_priority:100

slave_read_only:1

connected_slaves:0

master_replid:458f6d2c11e76b3be7747a027dc0b481b3794faa

master_replid2:0000000000000000000000000000000000000000

master_repl_offset:826

second_repl_offset:-1

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:1

repl_backlog_histlen:826

127.0.0.1:6379>

#三个哨兵服务器的配置都如下

[root@master ~]#grep -vE "^#|^$" /etc/redis-sentinel.conf

bind 0.0.0.0

port 26379

daemonize no

pidfile "/var/run/redis-sentinel.pid"

logfile "/var/log/redis/sentinel.log"

dir "/tmp"

sentinel myid 91d1527420e293e7d3f0ab726ff1f62c71239e39

sentinel deny-scripts-reconfig yes

sentinel monitor mymaster 10.0.0.8 6379 2

sentinel down-after-milliseconds mymaster 3000

sentinel auth-pass mymaster 123456

sentinel config-epoch mymaster 0

protected-mode no

supervised systemd

sentinel leader-epoch mymaster 0

sentinel known-replica mymaster 10.0.0.18 6379

sentinel known-replica mymaster 10.0.0.28 6379

sentinel current-epoch 0

[root@master ~]#scp /etc/redis-sentinel.conf 10.0.0.18:/etc/

[root@master ~]#scp /etc/redis-sentinel.conf 10.0.0.28:/etc/

#启动哨兵(三台哨兵都要启动)

#确保每个哨兵主机myid不同

[root@master ~]#vim /etc/redis-sentinel.conf

sentinel myid 91d1527420e293e7d3f0ab726ff1f62c71239e39

[root@slave1 ~]#vim /etc/redis-sentinel.conf

sentinel myid 91d1527420e293e7d3f0ab726ff1f62c71239e3a

[root@slave2 ~]#vim /etc/redis-sentinel.conf

sentinel myid 91d1527420e293e7d3f0ab726ff1f62c71239e3b

#重新启动服务

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

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

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

#验证哨兵端口

[root@master ~]#ss -ntl

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                0.0.0.0:26379            0.0.0.0:*     

LISTEN  0        128                0.0.0.0:6379            0.0.0.0:*     

LISTEN  0        128                    [::]:22                  [::]:*     

#master的哨兵日志

[root@master ~]#tail -f /var/log/redis/sentinel.log

2720:X 25 Oct 2020 00:23:23.267 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo

2720:X 25 Oct 2020 00:23:23.267 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=2720, just started

2720:X 25 Oct 2020 00:23:23.267 # Configuration loaded

2720:X 25 Oct 2020 00:23:23.267 * supervised by systemd, will signal readiness

2720:X 25 Oct 2020 00:23:23.268 * Running mode=sentinel, port=26379.

2720:X 25 Oct 2020 00:23:23.268 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

2720:X 25 Oct 2020 00:23:23.270 # Sentinel ID is 91d1527420e293e7d3f0ab726ff1f62c71239e39

2720:X 25 Oct 2020 00:23:23.270 # +monitor master mymaster 10.0.0.8 6379 quorum 2

2720:X 25 Oct 2020 00:23:23.270 * +slave slave 10.0.0.28:6379 10.0.0.28 6379 @ mymaster 10.0.0.8 6379

2720:X 25 Oct 2020 00:23:23.272 * +slave slave 10.0.0.18:6379 10.0.0.18 6379 @ mymaster 10.0.0.8 6379

#slave的哨兵日志

[root@slave1 ~]#tail -f /var/log/redis/sentinel.log

2306:X 25 Oct 2020 00:24:32.202 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo

2306:X 25 Oct 2020 00:24:32.202 # Redis version=5.0.3, bits=64, commit=00000000, modified=0, pid=2306, just started

2306:X 25 Oct 2020 00:24:32.202 # Configuration loaded

2306:X 25 Oct 2020 00:24:32.202 * supervised by systemd, will signal readiness

2306:X 25 Oct 2020 00:24:32.203 * Running mode=sentinel, port=26379.

2306:X 25 Oct 2020 00:24:32.203 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

2306:X 25 Oct 2020 00:24:32.203 # Sentinel ID is 91d1527420e293e7d3f0ab726ff1f62c71239e39

2306:X 25 Oct 2020 00:24:32.203 # +monitor master mymaster 10.0.0.8 6379 quorum 2

#当前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.8:6379,slaves=2,sentinels=3

127.0.0.1:26379>

#停止Redis Master测试故障转移

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.18:6379,slaves=2,sentinels=3

故障转移时sentinel的信息

[root@master ~]#tail -f /var/log/redis/sentinel.log

2720:X 25 Oct 2020 01:12:10.286 # +vote-for-leader 91d1527420e293e7d3f0ab726ff1f62c71239f39 1

2720:X 25 Oct 2020 01:12:10.288 # +odown master mymaster 10.0.0.8 6379 #quorum 3/2

2720:X 25 Oct 2020 01:12:10.288 # Next failover delay: I will not start a failover before Sun Oct 25 01:18:10 2020

2720:X 25 Oct 2020 01:12:11.423 # +config-update-from sentinel 91d1527420e293e7d3f0ab726ff1f62c71239f39 10.0.0.18 26379 @ mymaster 10.0.0.8 6379

2720:X 25 Oct 2020 01:12:11.423 # +switch-master mymaster 10.0.0.8 6379 10.0.0.18 6379

2720:X 25 Oct 2020 01:12:11.423 * +slave slave 10.0.0.28:6379 10.0.0.28 6379 @ mymaster 10.0.0.18 6379

2720:X 25 Oct 2020 01:12:11.423 * +slave slave 10.0.0.8:6379 10.0.0.8 6379 @ mymaster 10.0.0.18 6379

2720:X 25 Oct 2020 01:12:14.430 # +sdown slave 10.0.0.8:6379 10.0.0.8 6379 @ mymaster 10.0.0.18 6379

2720:X 25 Oct 2020 01:28:13.050 # -sdown slave 10.0.0.8:6379 10.0.0.8 6379 @ mymaster 10.0.0.18 6379

2720:X 25 Oct 2020 01:30:05.586 # +sdown slave 10.0.0.8:6379 10.0.0.8 6379 @ mymaster 10.0.0.18 6379

#故障转移后redis配置文件会被自动修改

[root@slave2 ~]#grep ^replicaof /etc/redis.conf

replicaof 10.0.0.18 6379

#哨兵配置文件sentinel monitor IP 同样也会被修改

[root@slave1 ~]#grep "^[a-Z]" /etc/redis-sentinel.conf

bind 0.0.0.0

port 26379

daemonize no

pidfile "/var/run/redis-sentinel.pid"

logfile "/var/log/redis/sentinel.log"

dir "/tmp"

sentinel myid 91d1527420e293e7d3f0ab726ff1f62c71239e3a

sentinel deny-scripts-reconfig yes

sentinel monitor mymaster 10.0.0.18 6379 2        #此行被修改了

sentinel down-after-milliseconds mymaster 3000

sentinel auth-pass mymaster 123456

sentinel config-epoch mymaster 1

protected-mode no

supervised systemd

sentinel leader-epoch mymaster 1

sentinel known-replica mymaster 10.0.0.8 6379

sentinel known-replica mymaster 10.0.0.28 6379

sentinel known-sentinel mymaster 10.0.0.8 26379 91d1527420e293e7d3f0ab726ff1f62c71239e39

sentinel known-sentinel mymaster 10.0.0.28 26379 91d1527420e293e7d3f0ab726ff1f62c71239e3b

sentinel current-epoch 1

[root@slave2 ~]#grep "^[a-Z]" /etc/redis-sentinel.conf

bind 0.0.0.0

port 26379

daemonize no

pidfile "/var/run/redis-sentinel.pid"

logfile "/var/log/redis/sentinel.log"

dir "/tmp"

sentinel myid 91d1527420e293e7d3f0ab726ff1f62c71239e3b

sentinel deny-scripts-reconfig yes

sentinel monitor mymaster 10.0.0.18 6379 2    # 此行被修改

sentinel down-after-milliseconds mymaster 3000

sentinel auth-pass mymaster 123456

sentinel config-epoch mymaster 1

protected-mode no

supervised systemd

sentinel leader-epoch mymaster 1

sentinel known-replica mymaster 10.0.0.8 6379

sentinel known-replica mymaster 10.0.0.28 6379

sentinel known-sentinel mymaster 10.0.0.8 26379 91d1527420e293e7d3f0ab726ff1f62c71239e39

sentinel known-sentinel mymaster 10.0.0.18 26379 91d1527420e293e7d3f0ab726ff1f62c71239f39

sentinel current-epoch 1

#新的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> info replication

# Replication

role:master

connected_slaves:1

slave0:ip=10.0.0.28,port=6379,state=online,offset=839313,lag=1

master_replid:04d719e05ec3aaf8cf4d1c6b8857c0a95e373b44

master_replid2:458f6d2c11e76b3be7747a027dc0b481b3794faa

master_repl_offset:839460

second_repl_offset:548810

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:1

repl_backlog_histlen:839460

#另一个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.18

master_port:6379

master_link_status:up

master_last_io_seconds_ago:1

master_sync_in_progress:0

slave_repl_offset:925499

slave_priority:100

slave_read_only:1

connected_slaves:0

master_replid:04d719e05ec3aaf8cf4d1c6b8857c0a95e373b44

master_replid2:458f6d2c11e76b3be7747a027dc0b481b3794faa

master_repl_offset:925499

second_repl_offset:548810

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:1

repl_backlog_histlen:925499

#恢复故障的原master重新加入redis集群

#sentinel会自动修改下面行指向新的master

[root@master ~]#cat /etc/redis.conf

# Generated by CONFIG REWRITE

replicaof 10.0.0.18 6379

#在原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:slave

master_host:10.0.0.18

master_port:6379

master_link_status:up

master_last_io_seconds_ago:1

master_sync_in_progress:0

slave_repl_offset:2091286

slave_priority:100

slave_read_only:1

connected_slaves:0

master_replid:04d719e05ec3aaf8cf4d1c6b8857c0a95e373b44

master_replid2:0000000000000000000000000000000000000000

master_repl_offset:2091286

second_repl_offset:-1

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:1978239

repl_backlog_histlen:113048

[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.18:6379,slaves=2,sentinels=3

#观察新的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> info replication

# Replication

role:master

connected_slaves:2

slave0:ip=10.0.0.28,port=6379,state=online,offset=2126150,lag=0

slave1:ip=10.0.0.8,port=6379,state=online,offset=2126150,lag=1

master_replid:04d719e05ec3aaf8cf4d1c6b8857c0a95e373b44

master_replid2:458f6d2c11e76b3be7747a027dc0b481b3794faa

master_repl_offset:2126283

second_repl_offset:548810

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:1077708

repl_backlog_histlen:1048576

127.0.0.1:6379>

[root@slave1 ~]#tail /var/log/redis/sentinel.log

2469:X 25 Oct 2020 01:12:15.924 # +sdown slave 10.0.0.8:6379 10.0.0.8 6379 @ mymaster 10.0.0.18 6379

2469:X 25 Oct 2020 01:28:13.251 # -sdown slave 10.0.0.8:6379 10.0.0.8 6379 @ mymaster 10.0.0.18 6379

4、redis cluster集群创建和使用

#在每个节点上安装redis

[root@centos8 ~]#dnf -yinstall redis

#每个节点修改redis配置,必须开启cluster功能的参数,批量修改

[root@centos8 ~]#sed-i.bak -e's/bind 127.0.0.1/bind 0.0.0.0/'-e's/masterauth/masterauth 123456'-e's/^#requirepass/requirepass 123456'-e's/^# cluster-enabled yes/cluster-enabled yes'-e's/^# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf'-e's/clusterrequirefull-coverage yes/c cluster-require-full-coverage no'/etc/redis.conf

#重启服务

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

#验证当前redis服务状态

开启了16379的cluster的端口,实际的端口=redis prot +10000[root@centos8 ~]#ss -ntl

State  Recv-Q  Send-Q    Local Address:Port    Peer Address:Port 

LISTEN 01280.0.0.0:220.0.0.0:*   

LISTEN 01280.0.0.0:163790.0.0.0:*   

LISTEN 01280.0.0.0:63790.0.0.0:*   

LISTEN 0128[::]:22[::]:* 

#注意进程有【cluster】状态

[root@centos8 ~]#ps-ef |grep redis

redis      95451004:37?00:00:00/usr/bin/redis-server0.0.0.0:6379 [cluster]

root      95591350004:45pts/000:00:00grep--color=auto redis

#创建集群

[root@centos8 ~]#redis-cli -a123456--cluster create10.0.0.8:637910.0.0.18:637910.0.0.28:637910.0.0.38:637910.0.0.48:637910.0.0.58:6379--cluster-replicas1

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.>>> Performing hash slots allocation on6 nodes...

Master[0] -> Slots0-5460Master[1] -> Slots5461-10922Master[2] -> Slots10923-16383Adding replica 10.0.0.38:6379to10.0.0.8:6379Adding replica 10.0.0.48:6379to10.0.0.18:6379Adding replica 10.0.0.58:6379to10.0.0.28:6379M: 72630b08354c8b67d7dd7af467b5b1c21efc05e7 10.0.0.8:6379  slots:[0-5460] (5461 slots) master

M: f7bb0a1728ec381483f464ab68c16edc2041fe10 10.0.0.18:6379  slots:[5461-10922] (5462 slots) master

M: 8ffc94e86dbf7f3477b77db130538d2219022bba 10.0.0.28:6379  slots:[10923-16383] (5461 slots) master

S: f67fb2a9264bdcddc3311956798e9f5c1d43fd0d 10.0.0.38:6379  replicates 72630b08354c8b67d7dd7af467b5b1c21efc05e7

S: 36b3aa7483d2ba99e69db995c5b1e0a73b9ca937 10.0.0.48:6379  replicates f7bb0a1728ec381483f464ab68c16edc2041fe10

S: 7060d1ab30b41709903bf4e901aa3a8d14749ab0 10.0.0.58:6379  replicates 8ffc94e86dbf7f3477b77db130538d2219022bba

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 tojoin the cluster

Waiting forthe cluster tojoin.....>>> Performing Cluster Check (using node10.0.0.8:6379)

M: 72630b08354c8b67d7dd7af467b5b1c21efc05e7 10.0.0.8:6379  slots:[0-5460] (5461 slots) master

  1 additional replica(s)

S: f67fb2a9264bdcddc3311956798e9f5c1d43fd0d 10.0.0.38:6379  slots: (0 slots) slave

  replicates 72630b08354c8b67d7dd7af467b5b1c21efc05e7

S: 36b3aa7483d2ba99e69db995c5b1e0a73b9ca937 10.0.0.48:6379  slots: (0 slots) slave

  replicates f7bb0a1728ec381483f464ab68c16edc2041fe10

M: 8ffc94e86dbf7f3477b77db130538d2219022bba 10.0.0.28:6379  slots:[10923-16383] (5461 slots) master

  1 additional replica(s)

M: f7bb0a1728ec381483f464ab68c16edc2041fe10 10.0.0.18:6379  slots:[5461-10922] (5462 slots) master

  1 additional replica(s)

S: 7060d1ab30b41709903bf4e901aa3a8d14749ab0 10.0.0.58:6379  slots: (0 slots) slave

  replicates 8ffc94e86dbf7f3477b77db130538d2219022bba

[OK] All nodes agree about slots configuration.>>> Checkfor open slots...>>> Check slots coverage...

[OK] All 16384 slots covered.

#观察上述结果,可以看到三组master/slave

Adding replica 10.0.0.38:6379to10.0.0.8:6379Adding replica 10.0.0.48:6379to10.0.0.18:6379Adding replica 10.0.0.58:6379to10.0.0.28:6379#查看主从状态

[root@redis-node1 ~]#redis-cli -a123456-cinfo replication

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.

# Replication

role:master

connected_slaves:1slave0:ip=10.0.0.38,port=6379,state=online,offset=798,lag=1master_replid:c6d03ac327373545ec13a6e02d7796000c8cf519

master_replid2:0000000000000000000000000000000000000000master_repl_offset:798second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:798[root@redis-node2 ~]#redis-cli -a123456info replication

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.

# Replication

role:master

connected_slaves:1slave0:ip=10.0.0.48,port=6379,state=online,offset=882,lag=0master_replid:e38086a24e8d7babe45bb460fe1f3754e188c119

master_replid2:0000000000000000000000000000000000000000master_repl_offset:882second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:882[root@redis-node3 ~]#redis-cli -a123456info replication

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.

# Replication

role:master

connected_slaves:1slave0:ip=10.0.0.58,port=6379,state=online,offset=924,lag=1master_replid:5fa0c5b0ff68a053f890d345b37723a7995db382

master_replid2:0000000000000000000000000000000000000000master_repl_offset:938second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:938[root@redis-node4 ~]#

[root@redis-node4 ~]#redis-cli -a123456info 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.8master_port:6379master_link_status:up

master_last_io_seconds_ago:4master_sync_in_progress:0slave_repl_offset:1106slave_priority:100slave_read_only:1connected_slaves:0master_replid:c6d03ac327373545ec13a6e02d7796000c8cf519

master_replid2:0000000000000000000000000000000000000000master_repl_offset:1106second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:1106[root@redis-node5 ~]#redis-cli -a123456info 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.18master_port:6379master_link_status:up

master_last_io_seconds_ago:2master_sync_in_progress:0slave_repl_offset:1106slave_priority:100slave_read_only:1connected_slaves:0master_replid:e38086a24e8d7babe45bb460fe1f3754e188c119

master_replid2:0000000000000000000000000000000000000000master_repl_offset:1106second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:1106[root@redis-node6 ~]#redis-cli -a123456info 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.28master_port:6379master_link_status:up

master_last_io_seconds_ago:1master_sync_in_progress:0slave_repl_offset:1106slave_priority:100slave_read_only:1connected_slaves:0master_replid:5fa0c5b0ff68a053f890d345b37723a7995db382

master_replid2:0000000000000000000000000000000000000000master_repl_offset:1106second_repl_offset:-1repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:1106#验证集群状态

[root@redis-node1 ~]#redis-cli -a123456clusterinfo

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.

cluster_state:ok

cluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:6cluster_size:3cluster_current_epoch:6cluster_my_epoch:1cluster_stats_messages_ping_sent:1060cluster_stats_messages_pong_sent:961cluster_stats_messages_sent:2021cluster_stats_messages_ping_received:956cluster_stats_messages_pong_received:1056cluster_stats_messages_meet_received:5cluster_stats_messages_received:2017#查看任意节点的集群状态

[root@redis-node1 ~]#redis-cli -a123456--clusterinfo10.0.0.38:6379Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.10.0.0.18:6379(f7bb0a17...) ->0keys |5462slots |1 slaves.10.0.0.28:6379(8ffc94e8...) ->0keys |5461slots |1 slaves.10.0.0.8:6379(72630b08...) ->0keys |5461slots |1 slaves.

[OK] 0keysin3 masters.0.00 keys per slot on average.

#查看集群node对应关系

[root@redis-node1 ~]#redis-cli -a123456 cluster nodes

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.

f67fb2a9264bdcddc3311956798e9f5c1d43fd0d 10.0.0.38:6379@16379slave 72630b08354c8b67d7dd7af467b5b1c21efc05e7016036116944494 connected

36b3aa7483d2ba99e69db995c5b1e0a73b9ca937 10.0.0.48:6379@16379slave f7bb0a1728ec381483f464ab68c16edc2041fe10016036116924295 connected

72630b08354c8b67d7dd7af467b5b1c21efc05e7 10.0.0.8:6379@16379myself,master -016036116940001connected0-54608ffc94e86dbf7f3477b77db130538d2219022bba 10.0.0.28:6379@16379master -016036116964693connected10923-16383f7bb0a1728ec381483f464ab68c16edc2041fe10 10.0.0.18:6379@16379master -016036116940002connected5461-109227060d1ab30b41709903bf4e901aa3a8d14749ab0 10.0.0.58:6379@16379slave 8ffc94e86dbf7f3477b77db130538d2219022bba016036116954596 connected

[root@redis-node1 ~]#redis-cli -a123456--cluster check10.0.0.38:6379

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.10.0.0.18:6379(f7bb0a17...) ->0keys |5462slots |1 slaves.10.0.0.28:6379(8ffc94e8...) ->0keys |5461slots |1 slaves.10.0.0.8:6379(72630b08...) ->0keys |5461slots |1 slaves.

[OK] 0keysin3 masters.0.00 keys per slot on average.>>> Performing Cluster Check (using node10.0.0.38:6379)

S: f67fb2a9264bdcddc3311956798e9f5c1d43fd0d 10.0.0.38:6379  slots: (0 slots) slave

  replicates 72630b08354c8b67d7dd7af467b5b1c21efc05e7

M: f7bb0a1728ec381483f464ab68c16edc2041fe10 10.0.0.18:6379  slots:[5461-10922] (5462 slots) master

  1 additional replica(s)

S: 7060d1ab30b41709903bf4e901aa3a8d14749ab0 10.0.0.58:6379  slots: (0 slots) slave

  replicates 8ffc94e86dbf7f3477b77db130538d2219022bba

M: 8ffc94e86dbf7f3477b77db130538d2219022bba 10.0.0.28:6379  slots:[10923-16383] (5461 slots) master

  1 additional replica(s)

S: 36b3aa7483d2ba99e69db995c5b1e0a73b9ca937 10.0.0.48:6379  slots: (0 slots) slave

  replicates f7bb0a1728ec381483f464ab68c16edc2041fe10

M: 72630b08354c8b67d7dd7af467b5b1c21efc05e7 10.0.0.8:6379  slots:[0-5460] (5461 slots) master

  1 additional replica(s)

[OK] All nodes agree about slots configuration.>>> Checkfor open slots...>>> Check slots coverage...

[OK] All 16384 slots covered.

#redis cluster 写入key

经算法计算,当前key的槽位需要写入指定的node

[root@redis-node1 ~]#redis-cli -a123456-h10.0.0.8 set key1 values1

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.

(error) MOVED 918910.0.0.18:6379    #槽位不在当前node所以无法写入

[root@redis-node1 ~]#redis-cli -a123456-h10.0.0.18 set key1 values1

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.

OK

#指定node可以写入

[root@redis-node1 ~]#redis-cli -a123456-h10.0.0.18 get key1

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe."values1"[root@redis-node1 ~]#

#对应的slave节点可以keys*,但get key1失败,可以到master上执行get key1

[root@redis-node1 ~]#redis-cli -a123456-h10.0.0.48keys"*"Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.1)"key1"[root@redis-node1 ~]#redis-cli -a123456-h10.0.0.48 get key1

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.

(error) MOVED 918910.0.0.18:6379#redis cluster 计算key所属的slot

[root@redis-node1 ~]#redis-cli -h10.0.0.8-a123456--no-auth-warning cluster nodes

f67fb2a9264bdcddc3311956798e9f5c1d43fd0d 10.0.0.38:6379@16379slave 72630b08354c8b67d7dd7af467b5b1c21efc05e7016036123480004 connected

36b3aa7483d2ba99e69db995c5b1e0a73b9ca937 10.0.0.48:6379@16379slave f7bb0a1728ec381483f464ab68c16edc2041fe10016036123510005 connected

72630b08354c8b67d7dd7af467b5b1c21efc05e7 10.0.0.8:6379@16379myself,master -016036123480001connected0-54608ffc94e86dbf7f3477b77db130538d2219022bba 10.0.0.28:6379@16379master -016036123504823connected10923-16383f7bb0a1728ec381483f464ab68c16edc2041fe10 10.0.0.18:6379@16379master -016036123514912connected5461-109227060d1ab30b41709903bf4e901aa3a8d14749ab0 10.0.0.58:6379@16379slave 8ffc94e86dbf7f3477b77db130538d2219022bba016036123500006 connected

#计算的发哦hello对应的slot

[root@redis-node1 ~]#redis-cli -h10.0.0.8-a123456--no-auth-warning cluster keyslot hello

(integer) 866[root@redis-node1 ~]#redis-cli -h10.0.0.8-a123456--no-auth-warning set hello magedu

OK

[root@redis-node1 ~]#redis-cli -h10.0.0.8-a123456--no-auth-warning cluster keyslot name

(integer) 5798[root@redis-node1 ~]#redis-cli -h10.0.0.8-a123456--no-auth-warning set name wang

(error) MOVED 579810.0.0.18:6379[root@redis-node1 ~]#redis-cli -h10.0.0.18-a123456--no-auth-warning set name wang

OK

[root@redis-node1 ~]#redis-cli -h10.0.0.18-a123456--no-auth-warning get name "wang"#使用选项-c 以集群模式连接

[root@redis-node1 ~]#redis-cli -c -h10.0.0.8-a123456--no-auth-warning 10.0.0.8:6379> cluster keyslot linux

(integer) 1229910.0.0.8:6379> set linux love -> Redirected to slot [12299] located at10.0.0.28:6379OK10.0.0.28:6379> exit

[root@redis-node1 ~]#redis-cli -h10.0.0.28-a123456--no-auth-warning get linux "love"#使用python脚本实现rediscluster集群写入

[root@redis-node1 ~]#dnf -yinstall python3

[root@redis-node1 ~]#pip3installredis-py-cluster

[root@redis-node1 ~]#vim redis_cluster_test.py![](https://img2020.cnblogs.com/blog/1979780/202010/1979780-20201025161938978-1854678095.png)[root@redis-node1 ~]#./redis_cluster_test.py![](https://img2020.cnblogs.com/blog/1979780/202010/1979780-20201025162023921-1143351531.png)[root@redis-node1 ~]#redis-cli -a123456-h10.0.0.8Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.10.0.0.8:6379> dbsize

(integer) 333210.0.0.8:6379> get key1

(error) MOVED 918910.0.0.18:637910.0.0.8:6379> get key2"value2"10.0.0.8:6379> get key3"value3"10.0.0.8:6379>keys * ![](https://img2020.cnblogs.com/blog/1979780/202010/1979780-20201025162320897-563304046.png)[root@redis-node1 ~]#redis-cli -a123456-h10.0.0.18 dbsize

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.

(integer) 3341[root@redis-node1 ~]#redis-cli -a123456-h10.0.0.18 get key1

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe."value1"[root@redis-node1 ~]#redis-cli -a123456-h10.0.0.28 dbsize

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.

(integer) 3330[root@redis-node1 ~]#redis-cli -a123456-h10.0.0.18 get key5

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe."value5"#模拟master故障,对应的slave节点自动提升为新master

#模拟node2节点出故障,需要对应的数秒故障转移时间

[root@redis-node2 ~]#tail-f /var/log/redis/redis.log 9697:M25Oct202016:19:02.384*1changesin900 seconds. Saving...9697:M25Oct202016:19:02.386* Background saving started by pid98739873:C25Oct202016:19:02.389* DB saved on disk9873:C25Oct202016:19:02.390* RDB:6MB of memory used by copy-on-write9697:M25Oct202016:19:02.487* Background saving terminated with success9697:M25Oct202016:24:03.087*10changesin300 seconds. Saving...9697:M25Oct202016:24:03.089* Background saving started by pid98759875:C25Oct202016:24:03.092* DB saved on disk9875:C25Oct202016:24:03.092* RDB:4MB of memory used by copy-on-write9697:M25Oct202016:24:03.189* Background saving terminated with success

[root@redis-node2 ~]#redis-cli -a123456

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.127.0.0.1:6379> shutdown

not connected> exit

[root@redis-node2 ~]#ss -ntl

State  Recv-Q  Send-Q    Local Address:Port    Peer Address:Port 

LISTEN 01280.0.0.0:220.0.0.0:*   

LISTEN 0128[::]:22[::]:*[root@redis-node2 ~]#redis-cli -a123456--clusterinfo10.0.0.8:6379

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.

Could not connect to Redis at 10.0.0.18:6379: Connection refused10.0.0.8:6379(72630b08...) ->3332keys |5461slots |1 slaves.10.0.0.48:6379(36b3aa74...) ->3341keys |5462slots |0slaves.  #10.0.0.48为新的master10.0.0.28:6379(8ffc94e8...) ->3330keys |5461slots |1 slaves.

[OK] 10003keysin3 masters.0.61 keys per slot on average.

[root@redis-node2 ~]#redis-cli -a123456--cluster check10.0.0.8:6379

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.

Could not connect to Redis at 10.0.0.18:6379: Connection refused10.0.0.8:6379(72630b08...) ->3332keys |5461slots |1 slaves.10.0.0.48:6379(36b3aa74...) ->3341keys |5462slots |0 slaves.10.0.0.28:6379(8ffc94e8...) ->3330keys |5461slots |1 slaves.

[OK] 10003keysin3 masters.0.61 keys per slot on average.>>> Performing Cluster Check (using node10.0.0.8:6379)

M: 72630b08354c8b67d7dd7af467b5b1c21efc05e7 10.0.0.8:6379  slots:[0-5460] (5461 slots) master

  1 additional replica(s)

S: f67fb2a9264bdcddc3311956798e9f5c1d43fd0d 10.0.0.38:6379  slots: (0 slots) slave

  replicates 72630b08354c8b67d7dd7af467b5b1c21efc05e7

M: 36b3aa7483d2ba99e69db995c5b1e0a73b9ca937 10.0.0.48:6379  slots:[5461-10922] (5462 slots) master

M: 8ffc94e86dbf7f3477b77db130538d2219022bba 10.0.0.28:6379  slots:[10923-16383] (5461 slots) master

  1 additional replica(s)

S: 7060d1ab30b41709903bf4e901aa3a8d14749ab0 10.0.0.58:6379  slots: (0 slots) slave

  replicates 8ffc94e86dbf7f3477b77db130538d2219022bba

[OK] All nodes agree about slots configuration.>>> Checkfor open slots...>>> Check slots coverage...

[OK] All 16384 slots covered.

[root@redis-node2 ~]#redis-cli -a123456-h10.0.0.48

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.10.0.0.48:6379>info replication

# Replication

role:master

connected_slaves:0master_replid:79b9fca6844610ee6a6d1dd0f2a3ef5434a9be45

master_replid2:e38086a24e8d7babe45bb460fe1f3754e188c119

master_repl_offset:141751second_repl_offset:141752repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:14175110.0.0.48:6379>

#故障恢复节点node2

[root@redis-node2 ~]#systemctl start redis

#查看自动生成的配置文件,可以查看node2自动成为slave节点

[root@redis-node2 ~]#cat/var/lib/redis/nodes-6379.conf

36b3aa7483d2ba99e69db995c5b1e0a73b9ca937 10.0.0.48:6379@16379master -016036148222747connected5461-109227060d1ab30b41709903bf4e901aa3a8d14749ab0 10.0.0.58:6379@16379slave 8ffc94e86dbf7f3477b77db130538d2219022bba016036148222746 connected

72630b08354c8b67d7dd7af467b5b1c21efc05e7 10.0.0.8:6379@16379master -016036148222741connected0-54608ffc94e86dbf7f3477b77db130538d2219022bba 10.0.0.28:6379@16379master -016036148222743connected10923-16383f67fb2a9264bdcddc3311956798e9f5c1d43fd0d 10.0.0.38:6379@16379slave 72630b08354c8b67d7dd7af467b5b1c21efc05e7016036148222744 connected

f7bb0a1728ec381483f464ab68c16edc2041fe10 10.0.0.18:6379@16379myself,slave 36b3aa7483d2ba99e69db995c5b1e0a73b9ca937016036148222662 connected

vars currentEpoch 7lastVoteEpoch0[root@redis-node2 ~]#redis-cli -a123456-h10.0.0.48

Warning: Using a password with '-a'or'-u' option on the command line interface may not be safe.10.0.0.48:6379>info replication

# Replication

role:master

connected_slaves:1slave0:ip=10.0.0.18,port=6379,state=online,offset=141905,lag=0master_replid:79b9fca6844610ee6a6d1dd0f2a3ef5434a9be45

master_replid2:e38086a24e8d7babe45bb460fe1f3754e188c119

master_repl_offset:141905second_repl_offset:141752repl_backlog_active:1repl_backlog_size:1048576repl_backlog_first_byte_offset:1repl_backlog_histlen:14190510.0.0.48:6379>

你可能感兴趣的:(2020-10-26)