redis 通过aof恢复数据

前提:redis 开启aof持久化存储。

在redis中写入一些测试数据

for line in {1..300};do kubectl exec -it redis-cluster-3 -n  redis-cluster -- redis-cli -c -p 6379  -a gNY7VRyxeIZqLLao -c set wacky_${line} ${line}; done

查看集群及pod信息

[root@master-1 ~]#  kubectl exec -it  -n redis-cluster redis-cluster-2 -- redis-cli -a gNY7VRyxeIZqLLao  cluster nodes                              Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
4f43d802b3148b9341fbde552c1c7b8697d51672 10.244.39.27:6379@16379 slave 1faf758436472c460bee7781315cb88f1030a20b 0 1733206308505 21 connected
4c3c3ab66f585613532c2069c27c5c981dcccf89 10.244.39.30:6379@16379 slave 416bd92abe5beedce2a709bd088d133f8c32f6e7 0 1733206308505 20 connected
ad2aefcd7e5002dcb6b7387fdb977323ecba528e 10.244.247.24:6379@16379 myself,slave 257df4e4446069fa7ca1b198d031c5f438c9c6f0 0 0 22 connected
416bd92abe5beedce2a709bd088d133f8c32f6e7 10.244.84.166:6379@16379 master - 0 1733206308505 20 connected 0-5460
257df4e4446069fa7ca1b198d031c5f438c9c6f0 10.244.84.165:6379@16379 master - 0 1733206309518 22 connected 10923-16383
1faf758436472c460bee7781315cb88f1030a20b 10.244.247.23:6379@16379 master - 0 1733206308000 21 connected 5461-10922

[root@master-1 ~]# kubectl  get pod -n redis-cluster -owide                                                                                         NAME              READY   STATUS    RESTARTS   AGE     IP              NODE       NOMINATED NODE   READINESS GATES
redis-cluster-0   1/1     Running   0          3h19m   10.244.84.166   node-1     <none>           <none>
redis-cluster-1   1/1     Running   0          3h19m   10.244.39.27    master-1   <none>           <none>
redis-cluster-2   1/1     Running   0          26m     10.244.247.24   node-2     <none>           <none>
redis-cluster-3   1/1     Running   0          9m10s   10.244.84.165   node-1     <none>           <none>
redis-cluster-4   1/1     Running   0          3h19m   10.244.39.30    master-1   <none>           <none>
redis-cluster-5   1/1     Running   0          3h20m   10.244.247.23   node-2     <none>           <none> 

可以看到集群主从关系,然后去查看 主从节点的aof文件是否一致

[root@master-1 ~]# tail -100 /opt/loki/redis-cluster-pvc-data-redis-cluster-3-pvc-28d941a9-cede-4792-85bd-e67b16bda2da/appendonlydir/appendonly.aof.4.incr.aof  |grep wacky
wacky_319
wacky_320
wacky_324
wacky_325
wacky_328
wacky_329
wacky_333
wacky_337
wacky_342
wacky_346
wacky_351
wacky_355
wacky_359
wacky_360

[root@master-1 ~]# tail -100 /opt/loki/redis-cluster-pvc-data-redis-cluster-2-pvc-e7cfbe18-6984-4055-afdf-aca76854973b/appendonlydir/appendonly.aof.6.incr.aof  |grep wacky
wacky_319
wacky_320
wacky_324
wacky_325
wacky_328
wacky_329
wacky_333
wacky_337
wacky_342
wacky_346
wacky_351
wacky_355
wacky_359
wacky_360 

然后在其中一个master节点执行flushdb

[root@master-1 ~]#  kubectl exec -it  -n redis-cluster redis-cluster-3 -- redis-cli -a gNY7VRyxeIZqLLao  flushdb                                    Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK 

再次查询

[root@master-1 ~]# for line in {1..350};do kubectl exec -it redis-cluster-1 -n  redis-cluster -- redis-cli -c -p 6379  -a gNY7VRyxeIZqLLao -c get wacky_${line}; done |grep nil
(nil)
(nil)
(nil)
...

查看主从节点的aof文件

[root@master-1 ~]# tail -20 /opt/loki/redis-cluster-pvc-data-redis-cluster-2-pvc-e7cfbe18-6984-4055-afdf-aca76854973b/appendonlydir/appendonly.aof.6.incr.aof
wacky_355
$3
355
*3
$3
set
$9
wacky_359
$3
359
*3
$3
set
$9
wacky_360
$3
360
*1
$7
flushdb
[root@master-1 ~]# tail -20 /opt/loki/redis-cluster-pvc-data-redis-cluster-3-pvc-28d941a9-cede-4792-85bd-e67b16bda2da/appendonlydir/appendonly.aof.4.incr.aof
wacky_355
$3
355
*3
$3
set
$9
wacky_359
$3
359
*3
$3
set
$9
wacky_360
$3
360
*1
$7
flushdb

逐行解析

Redis AOF 文件遵循的是 RESP(REdis Serialization Protocol)协议格式。这个协议是基于行的,适用于客户端与 Redis 服务器之间的通信。我们逐行解析:

  1. *2

    • 含义:这是一个数组,表示接下来有 2 个元素
    • 在 Redis 命令中,命令本身和参数都作为数组元素。
  2. $6

    • 含义:表示接下来的字符串长度为 6 个字符。
    • 在此例中,接下来的内容是 SELECT,正好是 6 个字符。
  3. SELECT

    • 含义:这是 Redis 的 SELECT 命令,用于切换数据库。
    • Redis 默认有 16 个逻辑数据库,编号从 0 到 15。
  4. $1

    • 含义:表示接下来的字符串长度为 1 个字符。
    • 在此例中,接下来的内容是 0
  5. 0

    • 含义:这是 SELECT 命令的参数,表示选择第 0 号数据库。
  6. *1

    • 含义:这是另一个数组,表示接下来有 1 个元素
  7. $7

    • 含义:表示接下来的字符串长度为 7 个字符。
    • 在此例中,接下来的内容是 flushdb,正好是 7 个字符。
  8. flushdb

    • 含义:这是 Redis 的 FLUSHDB 命令,用于清空当前数据库中的所有数据。

编辑aof文件删除掉最后三行

[root@master-1 ~]# tail -20 /opt/loki/redis-cluster-pvc-data-redis-cluster-2-pvc-e7cfbe18-6984-4055-afdf-aca76854973b/appendonlydir/appendonly.aof.6.incr.aof
...
wacky_360
$3
360
[root@master-1 ~]# tail -20 /opt/loki/redis-cluster-pvc-data-redis-cluster-3-pvc-28d941a9-cede-4792-85bd-e67b16bda2da/appendonlydir/appendonly.aof.4.incr.aof
...
*3
$3
set
$9
wacky_360
$3
360

重启redis

[root@master-1 ~]# kubectl  delete pod -n redis-cluster redis-cluster-3
pod "redis-cluster-3" deleted 

再次查看集群对应关系

[root@master-1 ~]# kubectl  get pod -n redis-cluster -owide
NAME              READY   STATUS    RESTARTS   AGE     IP              NODE       NOMINATED NODE   READINESS GATES
redis-cluster-0   1/1     Running   0          3h20m   10.244.84.166   node-1     <none>           <none>
redis-cluster-1   1/1     Running   0          3h20m   10.244.39.27    master-1   <none>           <none>
redis-cluster-2   1/1     Running   0          27m     10.244.247.24   node-2     <none>           <none>
redis-cluster-3   1/1     Running   0          17s     10.244.84.167   node-1     <none>           <none>
redis-cluster-4   1/1     Running   0          3h20m   10.244.39.30    master-1   <none>           <none>
redis-cluster-5   1/1     Running   0          3h21m   10.244.247.23   node-2     <none>           <none>

[root@master-1 ~]#  kubectl exec -it  -n redis-cluster redis-cluster-2 -- redis-cli -a gNY7VRyxeIZqLLao  cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
4f43d802b3148b9341fbde552c1c7b8697d51672 10.244.39.27:6379@16379 slave 1faf758436472c460bee7781315cb88f1030a20b 0 1733206381000 21 connected
4c3c3ab66f585613532c2069c27c5c981dcccf89 10.244.39.30:6379@16379 slave 416bd92abe5beedce2a709bd088d133f8c32f6e7 0 1733206382735 20 connected
ad2aefcd7e5002dcb6b7387fdb977323ecba528e 10.244.247.24:6379@16379 myself,slave 257df4e4446069fa7ca1b198d031c5f438c9c6f0 0 0 22 connected
416bd92abe5beedce2a709bd088d133f8c32f6e7 10.244.84.166:6379@16379 master - 0 1733206381000 20 connected 0-5460
257df4e4446069fa7ca1b198d031c5f438c9c6f0 10.244.84.167:6379@16379 master - 0 1733206382000 22 connected 10923-16383
1faf758436472c460bee7781315cb88f1030a20b 10.244.247.23:6379@16379 master - 0 1733206382000 21 connected 5461-10922 

再次获取key,已经可以全部获取到了

[root@master-1 ~]# for line in {1..350};do kubectl exec -it redis-cluster-1 -n  redis-cluster -- redis-cli -c -p 6379  -a gNY7VRyxeIZqLLao -c get wacky_${line}; done |grep nil

 

posted @ 2024-12-03 14:40  不会跳舞的胖子  阅读(22)  评论(0编辑  收藏  举报