redis动态开启aof
背景:在开启了RDB持久化后,想切换为AOF存储,但是由于Redis启动后AOF文件读取优先级高于RDB文件,当修改配置文件后,重启redis,导致key丢失。
动态开启aof
操作步骤:
在redis运行前提下。使redis加载到rdb文件,然后在节点上执行以下命令
config set appendonly yes
这样redis会动态开启aof功能,并且加载key到aof文件。
2. 修改配置文件,打开aof持久化,因为上面的命令是临时开启,重启会失效。
appendonly yes
当下次重启后,依然可以去加载aof文件。
实践操作
1. 开启aof功能
1 2 3 4 5 6 7 | kubectl exec -n redis-cluster redis-cluster-0 -- config set appendonly yes kubectl exec -n redis-cluster redis-cluster-0 -- redis-cli -a gNY7VRyxeIZqLLao config set appendonly yes kubectl exec -n redis-cluster redis-cluster-1 -- redis-cli -a gNY7VRyxeIZqLLao config set appendonly yes kubectl exec -n redis-cluster redis-cluster-2 -- redis-cli -a gNY7VRyxeIZqLLao config set appendonly yes kubectl exec -n redis-cluster redis-cluster-3 -- redis-cli -a gNY7VRyxeIZqLLao config set appendonly yes kubectl exec -n redis-cluster redis-cluster-4 -- redis-cli -a gNY7VRyxeIZqLLao config set appendonly yes kubectl exec -n redis-cluster redis-cluster-5 -- redis-cli -a gNY7VRyxeIZqLLao config set appendonly yes |
2. 查看存储目录
[root@master-1 ~]# ll /opt/loki/redis-cluster-pvc-data-redis-cluster-*/a* /opt/loki/redis-cluster-pvc-data-redis-cluster-0-pvc-7b8891ea-ff8f-405c-a214-f93924aa918f/appendonlydir: 总用量 12 -rw-r--r-- 1 root root 1350 12月 3 10:42 appendonly.aof.4.base.rdb # 第一次全量备份文件 -rw-r--r-- 1 root root 1429 12月 3 10:47 appendonly.aof.4.incr.aof # 增量文件 -rw-r--r-- 1 root root 88 12月 3 10:42 appendonly.aof.manifest # 索引文件 /opt/loki/redis-cluster-pvc-data-redis-cluster-1-pvc-bd03ff8b-f027-4bc7-b1ae-b2c4b2c4aa7e/appendonlydir: 总用量 12 -rw-r--r-- 1 root root 1181 12月 3 10:42 appendonly.aof.4.base.rdb -rw-r--r-- 1 root root 1207 12月 3 10:47 appendonly.aof.4.incr.aof -rw-r--r-- 1 root root 88 12月 3 10:42 appendonly.aof.manifest /opt/loki/redis-cluster-pvc-data-redis-cluster-2-pvc-e7cfbe18-6984-4055-afdf-aca76854973b/appendonlydir: 总用量 12 -rw-r--r-- 1 root root 1315 12月 3 10:43 appendonly.aof.4.base.rdb -rw-r--r-- 1 root root 1207 12月 3 10:47 appendonly.aof.4.incr.aof -rw-r--r-- 1 root root 88 12月 3 10:43 appendonly.aof.manifest /opt/loki/redis-cluster-pvc-data-redis-cluster-3-pvc-28d941a9-cede-4792-85bd-e67b16bda2da/appendonlydir: 总用量 12 -rw-r--r-- 1 root root 1315 12月 3 10:43 appendonly.aof.2.base.rdb -rw-r--r-- 1 root root 1207 12月 3 10:47 appendonly.aof.2.incr.aof -rw-r--r-- 1 root root 88 12月 3 10:43 appendonly.aof.manifest /opt/loki/redis-cluster-pvc-data-redis-cluster-4-pvc-6a616d88-1698-4e7d-bd38-e995e7047cad/appendonlydir: 总用量 12 -rw-r--r-- 1 root root 1350 12月 3 10:43 appendonly.aof.2.base.rdb -rw-r--r-- 1 root root 1429 12月 3 10:47 appendonly.aof.2.incr.aof -rw-r--r-- 1 root root 88 12月 3 10:43 appendonly.aof.manifest /opt/loki/redis-cluster-pvc-data-redis-cluster-5-pvc-9b777f8e-2787-4de3-a63e-41fc82ccc415/appendonlydir: 总用量 12 -rw-r--r-- 1 root root 1181 12月 3 10:43 appendonly.aof.2.base.rdb -rw-r--r-- 1 root root 1207 12月 3 10:47 appendonly.aof.2.incr.aof -rw-r--r-- 1 root root 88 12月 3 10:43 appendonly.aof.manifest [root@master-1 ~]# ll /opt/loki/redis-cluster-pvc-data-redis-cluster-*/a* /opt/loki/redis-cluster-pvc-data-redis-cluster-0-pvc-7b8891ea-ff8f-405c-a214-f93924aa918f/appendonlydir: 总用量 12 -rw-r--r-- 1 root root 1350 12月 3 10:42 appendonly.aof.4.base.rdb -rw-r--r-- 1 root root 2021 12月 3 10:51 appendonly.aof.4.incr.aof -rw-r--r-- 1 root root 88 12月 3 10:42 appendonly.aof.manifest /opt/loki/redis-cluster-pvc-data-redis-cluster-1-pvc-bd03ff8b-f027-4bc7-b1ae-b2c4b2c4aa7e/appendonlydir: 总用量 12 -rw-r--r-- 1 root root 1181 12月 3 10:42 appendonly.aof.4.base.rdb -rw-r--r-- 1 root root 1799 12月 3 10:51 appendonly.aof.4.incr.aof -rw-r--r-- 1 root root 88 12月 3 10:42 appendonly.aof.manifest /opt/loki/redis-cluster-pvc-data-redis-cluster-2-pvc-e7cfbe18-6984-4055-afdf-aca76854973b/appendonlydir: 总用量 12 -rw-r--r-- 1 root root 1315 12月 3 10:43 appendonly.aof.4.base.rdb -rw-r--r-- 1 root root 1910 12月 3 10:51 appendonly.aof.4.incr.aof -rw-r--r-- 1 root root 88 12月 3 10:43 appendonly.aof.manifest /opt/loki/redis-cluster-pvc-data-redis-cluster-3-pvc-28d941a9-cede-4792-85bd-e67b16bda2da/appendonlydir: 总用量 12 -rw-r--r-- 1 root root 1315 12月 3 10:43 appendonly.aof.2.base.rdb -rw-r--r-- 1 root root 1910 12月 3 10:51 appendonly.aof.2.incr.aof -rw-r--r-- 1 root root 88 12月 3 10:43 appendonly.aof.manifest /opt/loki/redis-cluster-pvc-data-redis-cluster-4-pvc-6a616d88-1698-4e7d-bd38-e995e7047cad/appendonlydir: 总用量 12 -rw-r--r-- 1 root root 1350 12月 3 10:43 appendonly.aof.2.base.rdb -rw-r--r-- 1 root root 2021 12月 3 10:51 appendonly.aof.2.incr.aof -rw-r--r-- 1 root root 88 12月 3 10:43 appendonly.aof.manifest /opt/loki/redis-cluster-pvc-data-redis-cluster-5-pvc-9b777f8e-2787-4de3-a63e-41fc82ccc415/appendonlydir: 总用量 12 -rw-r--r-- 1 root root 1181 12月 3 10:43 appendonly.aof.2.base.rdb -rw-r--r-- 1 root root 1799 12月 3 10:51 appendonly.aof.2.incr.aof -rw-r--r-- 1 root root 88 12月 3 10:43 appendonly.aof.manifest
3. 修改configmap文件
vim configmap.yaml apiVersion: v1 kind: ConfigMap metadata: name: redis-cluster-config namespace: redis-cluster data: redis-cluster.conf: | daemonize no supervised no protected-mode no bind 0.0.0.0 port 6379 cluster-announce-bus-port 16379 cluster-enabled yes appendonly yes ...
kubectl apply -f configmap.yaml
4. 重启redis
1 | kubectl rollout restart statefulset -n redis-cluster redis-cluster |
5. 测试,再次读取,保证数据没丢失
1 2 3 4 5 6 7 | [root@master-1 redis]# for line in {1..350}; do kubectl exec -it redis-cluster-1 -n redis-cluster -- redis-cli -c -p 6379 -a gNY7VRyxeIZqLLao -c g et wacky_${line}; done Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. "1" Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. "2" Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. "3" |
越学越感到自己的无知
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现