作业六

请同学们完成作业后,将本周博客作业提交地址:https://www.wjx.top/jq/85988108.aspx
1、RDB和AOF的优缺点

RDB 模式优点
1.RDB快照保存了某个时间点的数据,可以通过脚本执行redis指令bgsave(非阻塞,后台执行)或者
save(会阻塞写操作,不推荐)命令自定义时间点备份,可以保留多个备份,当出现问题可以恢复到不
同时间点的版本,很适合备份,并且此文件格式也支持有不少第三方工具可以进行后续的数据分析
2.RDB可以最大化Redis的性能,父进程在保存 RDB文件时唯一要做的就是fork出一个子进程,然后
这个子进程就会处理接下来的所有保存工作,父进程无须执行任何磁盘工/0操作。
3.RDB在大量数据,比如几个G的数据,恢复的速度比AOF的快
RDB 模式缺点
1.不能实时保存数据,可能会丢失自上一次执行RDB备份到当前的内存数据
2.当数据量非常大的时候,从父进程fork子进程进行保存至RDB文件时需要一点时间,可能是毫秒或
者秒,取决于磁盘IO性能

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 出现的可能性更多

2、master和slave同步过程

1.从服务发送一个sync同步命令给主服务要求全量同步
2.主服务接收到从服务的sync同步命令时,会fork一个子进程后台执行bgsave命令(非阻塞)快照保存,生成RDB文件,并将RDB文件发送给从服务
3.从服务再将接收到的RDB文件载入自己的redis内存
4.待从服务将RDB载入完成后,主服务再将缓冲区所有写命令发送给从服务
5.从服务在将主服务所有的写命令载入内存从而实现数据的完整同步
6.从服务下次在需要同步数据时只需要发送自己的offset位置(相当于mysql binlog的位置)即可,只同步新增加的数据,再不需要全量同步

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

#配置所有节点redis
[root@master ~]#yum -y install redis #所有节点均执行此命令

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

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

[root@master ~]#rsync -av /etc/redis.conf 10.0.0.18:/etc/
The authenticity of host '10.0.0.18 (10.0.0.18)' can't be established.
ECDSA key fingerprint is SHA256:E8oPeOCwQOdj6sRDLmQ9KsGivUoLOysv2Xj2/oUP0G4.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.18' (ECDSA) to the list of known hosts.
root@10.0.0.18's password: 
sending incremental file list
redis.conf

sent 2,525 bytes  received 569 bytes  687.56 bytes/sec
total size is 62,159  speedup is 20.09

[root@master ~]#rsync -av /etc/redis.conf 10.0.0.28:/etc/
The authenticity of host '10.0.0.28 (10.0.0.28)' can't be established.
ECDSA key fingerprint is SHA256:E8oPeOCwQOdj6sRDLmQ9KsGivUoLOysv2Xj2/oUP0G4.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.0.0.28' (ECDSA) to the list of known hosts.
root@10.0.0.28's password: 
sending incremental file list
redis.conf

sent 2,525 bytes  received 569 bytes  562.55 bytes/sec
total size is 62,159  speedup is 20.09

[root@master ~]#systemctl enable --now redis #所有节点均执行
Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service.

#查看master状态
[root@master ~]#redis-cli -a qwe123
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.18,port=6379,state=online,offset=0,lag=0
slave1:ip=10.0.0.28,port=6379,state=online,offset=0,lag=0
master_replid:9143e87c341728b9f828142d573ad6b7a994c031
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:0


#配置slave1
[root@slave1 ~]#redis-cli -a qwe123
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:6
master_sync_in_progress:0
slave_repl_offset:196
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:9143e87c341728b9f828142d573ad6b7a994c031
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:196
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:196

#配置slave2
[root@slave2 ~]#redis-cli -a qwe123
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 "qwe123"
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:4
master_sync_in_progress:0
slave_repl_offset:224
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:9143e87c341728b9f828142d573ad6b7a994c031
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:224
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:224

#配置三个哨兵服务
[root@master ~]#vim /etc/redis-sentinel.conf
[root@master ~]#grep -vE "^#|^$" /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.8 6379 2
sentinel auth-pass mymaster qwe123
sentinel down-after-milliseconds mymaster 3000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes

[root@master ~]#rsync -av /etc/redis-sentinel.conf 10.0.0.18:/etc/
root@10.0.0.18's password: 
sending incremental file list
redis-sentinel.conf

sent 4,268 bytes  received 119 bytes  1,754.80 bytes/sec
total size is 9,713  speedup is 2.21
[root@master ~]#rsync -av /etc/redis-sentinel.conf 10.0.0.28:/etc/
root@10.0.0.28's password: 
sending incremental file list
redis-sentinel.conf

sent 4,268 bytes  received 119 bytes  2,924.67 bytes/sec
total size is 9,713  speedup is 2.21

#启动哨兵并查看
[root@master ~]#systemctl enable --now redis-sentinel.service
Created symlink /etc/systemd/system/multi-user.target.wants/redis-sentinel.service → /usr/lib/systemd/system/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                      [::]:*        
LISTEN    0           128                        [::]:26379                   [::]:*  

[root@slave1 ~]#systemctl enable --now redis-sentinel.service
Created symlink /etc/systemd/system/multi-user.target.wants/redis-sentinel.service → /usr/lib/systemd/system/redis-sentinel.service.
[root@slave1 ~]#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                      [::]:*        
LISTEN    0           128                        [::]:26379                   [::]:* 

[root@slave2 ~]#systemctl enable --now redis-sentinel.service
Created symlink /etc/systemd/system/multi-user.target.wants/redis-sentinel.service → /usr/lib/systemd/system/redis-sentinel.service.
[root@slave2 ~]#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                      [::]:*        
LISTEN    0           128                        [::]:26379                   [::]:*    

[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

#主节点故障测试
[root@master ~]#killall redis-server

[root@master ~]#redis-cli -a qwe123 -p 26379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
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

[root@master ~]#tail -f /var/log/redis/sentinel.log
2148:X 25 Oct 2020 04:51:25.804 # +sdown master mymaster 10.0.0.8 6379
2148:X 25 Oct 2020 04:51:25.888 # +new-epoch 1
2148:X 25 Oct 2020 04:51:25.889 # +vote-for-leader 52dedaf2db8a09a09b04806a7f0cd65ebceacc75 1
2148:X 25 Oct 2020 04:51:26.947 # +odown master mymaster 10.0.0.8 6379 #quorum 3/2
2148:X 25 Oct 2020 04:51:26.947 # Next failover delay: I will not start a failover before Sun Oct 25 04:57:26 2020
2148:X 25 Oct 2020 04:51:27.016 # +config-update-from sentinel 52dedaf2db8a09a09b04806a7f0cd65ebceacc75 10.0.0.28 26379 @ mymaster 10.0.0.8 6379
2148:X 25 Oct 2020 04:51:27.016 # +switch-master mymaster 10.0.0.8 6379 10.0.0.18 6379
2148:X 25 Oct 2020 04:51:27.016 * +slave slave 10.0.0.28:6379 10.0.0.28 6379 @ mymaster 10.0.0.18 6379
2148:X 25 Oct 2020 04:51:27.016 * +slave slave 10.0.0.8:6379 10.0.0.8 6379 @ mymaster 10.0.0.18 6379
2148:X 25 Oct 2020 04:51:30.067 # +sdown slave 10.0.0.8:6379 10.0.0.8 6379 @ mymaster 10.0.0.18 6379

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

[root@slave1 ~]#redis-cli -a qwe123
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=243464,lag=0
master_replid:be0883fb8be5da70561affc86cfd6530c8cbeff2
master_replid2:9143e87c341728b9f828142d573ad6b7a994c031
master_repl_offset:243464
second_repl_offset:47303
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:243464

[root@slave2 ~]#redis-cli -a qwe123
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:2
master_sync_in_progress:0
slave_repl_offset:249078
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:be0883fb8be5da70561affc86cfd6530c8cbeff2
master_replid2:9143e87c341728b9f828142d573ad6b7a994c031
master_repl_offset:249078
second_repl_offset:47303
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:249078

[root@master ~]#redis-cli -a qwe123
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:283956
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:be0883fb8be5da70561affc86cfd6530c8cbeff2
master_replid2:9143e87c341728b9f828142d573ad6b7a994c031
master_repl_offset:283956
second_repl_offset:47303
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:47303
repl_backlog_histlen:236654

4、redis cluster集群创建和使用

#在每个节点上安装redis
[root@centos8 ~]#dnf -y install 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 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             0.0.0.0:6379         0.0.0.0:*     
LISTEN 0       128                [::]:22              [::]:*   

#注意进程有【cluster】状态
[root@centos8 ~]#ps -ef | grep redis
redis      9545      1  0 04:37 ?        00:00:00 /usr/bin/redis-server 0.0.0.0:6379 [cluster]
root       9559   1350  0 04:45 pts/0    00:00:00 grep --color=auto redis

#创建集群
[root@centos8 ~]#redis-cli -a 123456 --cluster create 10.0.0.8:6379 10.0.0.18:6379 10.0.0.28:6379 10.0.0.38:6379 10.0.0.48:6379 10.0.0.58:6379 --cluster-replicas 1 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.0.0.38:6379 to 10.0.0.8:6379
Adding replica 10.0.0.48:6379 to 10.0.0.18:6379
Adding replica 10.0.0.58:6379 to 10.0.0.28:6379
M: 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 to join the cluster
Waiting for the cluster to join
.....
>>> Performing Cluster Check (using node 10.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.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

#观察上述结果,可以看到三组master/slave
Adding replica 10.0.0.38:6379 to 10.0.0.8:6379
Adding replica 10.0.0.48:6379 to 10.0.0.18:6379
Adding replica 10.0.0.58:6379 to 10.0.0.28:6379

#查看主从状态
[root@redis-node1 ~]#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.38,port=6379,state=online,offset=798,lag=1
master_replid:c6d03ac327373545ec13a6e02d7796000c8cf519
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:798
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:798

[root@redis-node2 ~]#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.48,port=6379,state=online,offset=882,lag=0
master_replid:e38086a24e8d7babe45bb460fe1f3754e188c119
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:882
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:882

[root@redis-node3 ~]#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.58,port=6379,state=online,offset=924,lag=1
master_replid:5fa0c5b0ff68a053f890d345b37723a7995db382
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:938
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:938

[root@redis-node4 ~]#
[root@redis-node4 ~]#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.8
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:1106
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:c6d03ac327373545ec13a6e02d7796000c8cf519
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1106
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1106

[root@redis-node5 ~]#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.18
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:1106
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:e38086a24e8d7babe45bb460fe1f3754e188c119
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1106
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1106

[root@redis-node6 ~]#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.28
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:1106
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:5fa0c5b0ff68a053f890d345b37723a7995db382
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1106
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1106

#验证集群状态
[root@redis-node1 ~]#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:1
cluster_stats_messages_ping_sent:1060
cluster_stats_messages_pong_sent:961
cluster_stats_messages_sent:2021
cluster_stats_messages_ping_received:956
cluster_stats_messages_pong_received:1056
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:2017

#查看任意节点的集群状态
[root@redis-node1 ~]#redis-cli -a 123456 --cluster info 10.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...) -> 0 keys | 5462 slots | 1 slaves.
10.0.0.28:6379 (8ffc94e8...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.8:6379 (72630b08...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

#查看集群node对应关系
[root@redis-node1 ~]#redis-cli -a 123456 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@16379 slave 72630b08354c8b67d7dd7af467b5b1c21efc05e7 0 1603611694449 4 connected
36b3aa7483d2ba99e69db995c5b1e0a73b9ca937 10.0.0.48:6379@16379 slave f7bb0a1728ec381483f464ab68c16edc2041fe10 0 1603611692429 5 connected
72630b08354c8b67d7dd7af467b5b1c21efc05e7 10.0.0.8:6379@16379 myself,master - 0 1603611694000 1 connected 0-5460
8ffc94e86dbf7f3477b77db130538d2219022bba 10.0.0.28:6379@16379 master - 0 1603611696469 3 connected 10923-16383
f7bb0a1728ec381483f464ab68c16edc2041fe10 10.0.0.18:6379@16379 master - 0 1603611694000 2 connected 5461-10922
7060d1ab30b41709903bf4e901aa3a8d14749ab0 10.0.0.58:6379@16379 slave 8ffc94e86dbf7f3477b77db130538d2219022bba 0 1603611695459 6 connected

[root@redis-node1 ~]#redis-cli -a 123456 --cluster check 10.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...) -> 0 keys | 5462 slots | 1 slaves.
10.0.0.28:6379 (8ffc94e8...) -> 0 keys | 5461 slots | 1 slaves.
10.0.0.8:6379 (72630b08...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 10.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.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

#redis cluster 写入key
经算法计算,当前key的槽位需要写入指定的node
[root@redis-node1 ~]#redis-cli -a 123456 -h 10.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 9189 10.0.0.18:6379    #槽位不在当前node所以无法写入
[root@redis-node1 ~]#redis-cli -a 123456 -h 10.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 -a 123456 -h 10.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 -a 123456 -h 10.0.0.48 keys "*"
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 -a 123456 -h 10.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 9189 10.0.0.18:6379

#redis cluster 计算key所属的slot
[root@redis-node1 ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster nodes 
f67fb2a9264bdcddc3311956798e9f5c1d43fd0d 10.0.0.38:6379@16379 slave 72630b08354c8b67d7dd7af467b5b1c21efc05e7 0 1603612348000 4 connected
36b3aa7483d2ba99e69db995c5b1e0a73b9ca937 10.0.0.48:6379@16379 slave f7bb0a1728ec381483f464ab68c16edc2041fe10 0 1603612351000 5 connected
72630b08354c8b67d7dd7af467b5b1c21efc05e7 10.0.0.8:6379@16379 myself,master - 0 1603612348000 1 connected 0-5460
8ffc94e86dbf7f3477b77db130538d2219022bba 10.0.0.28:6379@16379 master - 0 1603612350482 3 connected 10923-16383
f7bb0a1728ec381483f464ab68c16edc2041fe10 10.0.0.18:6379@16379 master - 0 1603612351491 2 connected 5461-10922
7060d1ab30b41709903bf4e901aa3a8d14749ab0 10.0.0.58:6379@16379 slave 8ffc94e86dbf7f3477b77db130538d2219022bba 0 1603612350000 6 connected

#计算的发哦hello对应的slot
[root@redis-node1 ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster keyslot hello 
(integer) 866
[root@redis-node1 ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning set hello magedu 
OK
[root@redis-node1 ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning cluster keyslot name 
(integer) 5798
[root@redis-node1 ~]#redis-cli -h 10.0.0.8 -a 123456 --no-auth-warning set name wang 
(error) MOVED 5798 10.0.0.18:6379
[root@redis-node1 ~]#redis-cli -h 10.0.0.18 -a 123456 --no-auth-warning set name wang 
OK
[root@redis-node1 ~]#redis-cli -h 10.0.0.18 -a 123456 --no-auth-warning get name 
"wang"

#使用选项-c 以集群模式连接
[root@redis-node1 ~]#redis-cli -c -h 10.0.0.8 -a 123456 --no-auth-warning 
10.0.0.8:6379> cluster keyslot linux 
(integer) 12299
10.0.0.8:6379> set linux love 
-> Redirected to slot [12299] located at 10.0.0.28:6379
OK
10.0.0.28:6379> exit 
[root@redis-node1 ~]#redis-cli -h 10.0.0.28 -a 123456 --no-auth-warning get linux 
"love"

#使用python脚本实现rediscluster集群写入
[root@redis-node1 ~]#dnf -y install python3 
[root@redis-node1 ~]#pip3 install redis-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 -a 123456 -h 10.0.0.8
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.8:6379> dbsize 
(integer) 3332
10.0.0.8:6379> get key1 
(error) MOVED 9189 10.0.0.18:6379
10.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 -a 123456 -h 10.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 -a 123456 -h 10.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 -a 123456 -h 10.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 -a 123456 -h 10.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:M 25 Oct 2020 16:19:02.384 * 1 changes in 900 seconds. Saving...
9697:M 25 Oct 2020 16:19:02.386 * Background saving started by pid 9873
9873:C 25 Oct 2020 16:19:02.389 * DB saved on disk
9873:C 25 Oct 2020 16:19:02.390 * RDB: 6 MB of memory used by copy-on-write
9697:M 25 Oct 2020 16:19:02.487 * Background saving terminated with success
9697:M 25 Oct 2020 16:24:03.087 * 10 changes in 300 seconds. Saving...
9697:M 25 Oct 2020 16:24:03.089 * Background saving started by pid 9875
9875:C 25 Oct 2020 16:24:03.092 * DB saved on disk
9875:C 25 Oct 2020 16:24:03.092 * RDB: 4 MB of memory used by copy-on-write
9697:M 25 Oct 2020 16:24:03.189 * Background saving terminated with success
[root@redis-node2 ~]#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> shutdown 
not connected> exit
[root@redis-node2 ~]#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                [::]:22              [::]:*
[root@redis-node2 ~]#redis-cli -a 123456 --cluster info 10.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 refused
10.0.0.8:6379 (72630b08...) -> 3332 keys | 5461 slots | 1 slaves.
10.0.0.48:6379 (36b3aa74...) -> 3341 keys | 5462 slots | 0 slaves.  #10.0.0.48为新的master
10.0.0.28:6379 (8ffc94e8...) -> 3330 keys | 5461 slots | 1 slaves.
[OK] 10003 keys in 3 masters.
0.61 keys per slot on average.
[root@redis-node2 ~]#redis-cli -a 123456 --cluster check 10.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 refused
10.0.0.8:6379 (72630b08...) -> 3332 keys | 5461 slots | 1 slaves.
10.0.0.48:6379 (36b3aa74...) -> 3341 keys | 5462 slots | 0 slaves.
10.0.0.28:6379 (8ffc94e8...) -> 3330 keys | 5461 slots | 1 slaves.
[OK] 10003 keys in 3 masters.
0.61 keys per slot on average.
>>> Performing Cluster Check (using node 10.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.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

[root@redis-node2 ~]#redis-cli -a 123456 -h 10.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:0
master_replid:79b9fca6844610ee6a6d1dd0f2a3ef5434a9be45
master_replid2:e38086a24e8d7babe45bb460fe1f3754e188c119
master_repl_offset:141751
second_repl_offset:141752
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:141751
10.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@16379 master - 0 1603614822274 7 connected 5461-10922
7060d1ab30b41709903bf4e901aa3a8d14749ab0 10.0.0.58:6379@16379 slave 8ffc94e86dbf7f3477b77db130538d2219022bba 0 1603614822274 6 connected
72630b08354c8b67d7dd7af467b5b1c21efc05e7 10.0.0.8:6379@16379 master - 0 1603614822274 1 connected 0-5460
8ffc94e86dbf7f3477b77db130538d2219022bba 10.0.0.28:6379@16379 master - 0 1603614822274 3 connected 10923-16383
f67fb2a9264bdcddc3311956798e9f5c1d43fd0d 10.0.0.38:6379@16379 slave 72630b08354c8b67d7dd7af467b5b1c21efc05e7 0 1603614822274 4 connected
f7bb0a1728ec381483f464ab68c16edc2041fe10 10.0.0.18:6379@16379 myself,slave 36b3aa7483d2ba99e69db995c5b1e0a73b9ca937 0 1603614822266 2 connected
vars currentEpoch 7 lastVoteEpoch 0

[root@redis-node2 ~]#redis-cli -a 123456 -h 10.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:1
slave0:ip=10.0.0.18,port=6379,state=online,offset=141905,lag=0
master_replid:79b9fca6844610ee6a6d1dd0f2a3ef5434a9be45
master_replid2:e38086a24e8d7babe45bb460fe1f3754e188c119
master_repl_offset:141905
second_repl_offset:141752
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:141905
10.0.0.48:6379>
posted @ 2020-10-23 19:57  进击的子卿  阅读(197)  评论(0)    收藏  举报