乐闻世界logo
搜索文章和话题

Redis 如何进行监控和运维?有哪些关键指标和工具?

2月19日 19:37

Redis 的监控和运维是保证 Redis 稳定运行的重要环节,需要从多个维度进行监控和管理。

1. Redis 监控指标

基础指标

内存使用情况

bash
# 查看内存使用情况 INFO memory # 关键指标 used_memory:1024000 # 已使用内存 used_memory_human:1.00M # 已使用内存(人类可读) used_memory_rss:2048000 # 操作系统分配的内存 used_memory_rss_human:2.00M # 操作系统分配的内存(人类可读) used_memory_peak:2048000 # 历史内存使用峰值 used_memory_peak_human:2.00M # 历史内存使用峰值(人类可读) maxmemory:1073741824 # 最大内存限制 maxmemory_human:1.00G # 最大内存限制(人类可读) mem_fragmentation_ratio:2.00 # 内存碎片率

连接情况

bash
# 查看连接情况 INFO clients # 关键指标 connected_clients:10 # 已连接客户端数 blocked_clients:0 # 被阻塞客户端数 client_longest_output_list:0 # 最长输出列表 client_biggest_input_buf:0 # 最大输入缓冲区

命令执行情况

bash
# 查看命令执行情况 INFO commandstats # 关键指标 cmdstat_get:calls=1000,usec=5000,usec_per_call=5.00 cmdstat_set:calls=500,usec=2500,usec_per_call=5.00

持久化情况

bash
# 查看持久化情况 INFO persistence # 关键指标 rdb_last_save_time:1234567890 # 最后一次 RDB 保存时间 rdb_changes_since_last_save:100 # 自上次保存以来的变更次数 aof_enabled:1 # AOF 是否启用 aof_rewrite_in_progress:0 # AOF 重写是否进行中

性能指标

QPS(每秒查询数)

bash
# 计算 QPS INFO stats # 关键指标 instantaneous_ops_per_sec:1000 # 当前每秒操作数 total_commands_processed:1000000 # 总处理命令数 total_connections_received:10000 # 总连接数

延迟

bash
# 查看延迟 INFO stats # 关键指标 instantaneous_input_kbps:100 # 当前输入带宽 instantaneous_output_kbps:200 # 当前输出带宽

命中率

bash
# 计算命中率 keyspace_hits:10000 # 命中次数 keyspace_misses:1000 # 未命中次数 hit_rate = keyspace_hits / (keyspace_hits + keyspace_misses)

2. Redis 监控工具

Redis 自带命令

INFO 命令

bash
# 查看所有信息 INFO # 查看特定信息 INFO memory INFO stats INFO replication INFO persistence

SLOWLOG 命令

bash
# 查看慢查询 SLOWLOG GET 10 # 查看慢查询数量 SLOWLOG LEN # 清空慢查询 SLOWLOG RESET

MONITOR 命令

bash
# 实时监控 Redis 命令 MONITOR

第三方监控工具

Redis Exporter

bash
# 安装 Redis Exporter docker run -d --name redis-exporter \ -e REDIS_ADDR=redis://localhost:6379 \ prom/redis-exporter # 配置 Prometheus 抓取数据 scrape_configs: - job_name: 'redis' static_configs: - targets: ['localhost:9121']

Grafana + Prometheus

bash
# 使用 Grafana 展示 Redis 监控数据 # 导入 Redis Dashboard https://grafana.com/grafana/dashboards/11835-redis-dashboard/

Redis Insight

bash
# Redis 官方可视化工具 # 下载地址 https://redis.com/redis-enterprise/redis-insight/

3. Redis 运维操作

备份与恢复

RDB 备份

bash
# 手动触发 RDB 备份 SAVE # 或 BGSAVE # 备份文件位置 /var/lib/redis/dump.rdb # 恢复 RDB 备份 # 停止 Redis redis-cli shutdown # 复制备份文件 cp dump.rdb /var/lib/redis/dump.rdb # 启动 Redis redis-server /etc/redis/redis.conf

AOF 备份

bash
# 备份 AOF 文件 cp /var/lib/redis/appendonly.aof /backup/appendonly.aof # 恢复 AOF 备份 # 停止 Redis redis-cli shutdown # 复制备份文件 cp /backup/appendonly.aof /var/lib/redis/appendonly.aof # 启动 Redis redis-server /etc/redis/redis.conf

数据迁移

主从迁移

bash
# 在从节点配置主节点 redis-cli slaveof <master-ip> <master-port> # 等待同步完成 redis-cli info replication # 取消主从关系 redis-cli slaveof no one

使用 MIGRATE 命令

bash
# 迁移单个 key MIGRATE <target-host> <target-port> <key> <target-database> <timeout> # 迁移多个 key MIGRATE <target-host> <target-port> "" <target-database> <timeout> KEYS key1 key2 key3

使用 Redis-Shake

bash
# 安装 Redis-Shake wget https://github.com/alibaba/RedisShake/releases/download/v2.0.3/redis-shake-v2.0.3.tar.gz tar -xzf redis-shake-v2.0.3.tar.gz # 配置文件 cat > shake.conf << EOF source.type: standalone source.address: source.redis.com:6379 source.password: source_password target.type: standalone target.address: target.redis.com:6379 target.password: target_password EOF # 启动迁移 ./redis-shake.linux -type=sync -conf=shake.conf

集群扩容

添加节点

bash
# 添加新节点 redis-cli --cluster add-node <new-node-ip>:<new-node-port> <existing-node-ip>:<existing-node-port> # 分配哈希槽 redis-cli --cluster reshard <existing-node-ip>:<existing-node-port> \ --cluster-from <node-id> \ --cluster-to <new-node-id> \ --cluster-slots 1000

删除节点

bash
# 迁移哈希槽 redis-cli --cluster reshard <existing-node-ip>:<existing-node-port> \ --cluster-from <node-id> \ --cluster-to <other-node-id> \ --cluster-slots 1000 # 删除节点 redis-cli --cluster del-node <existing-node-ip>:<existing-node-port> <node-id>

4. Redis 故障排查

内存不足

问题现象

  • Redis 报错:OOM command not allowed when used memory > 'maxmemory'
  • Redis 性能下降

排查步骤

bash
# 查看内存使用情况 INFO memory # 查看大 Key redis-cli --bigkeys # 查看内存碎片率 INFO memory | grep mem_fragmentation_ratio

解决方案

bash
# 调整最大内存限制 CONFIG SET maxmemory 2gb # 开启内存淘汰策略 CONFIG SET maxmemory-policy allkeys-lru # 清理内存碎片 MEMORY PURGE

连接数过多

问题现象

  • Redis 报错:max number of clients reached
  • 客户端连接失败

排查步骤

bash
# 查看连接数 INFO clients # 查看最大连接数 CONFIG GET maxclients

解决方案

bash
# 调整最大连接数 CONFIG SET maxclients 10000 # 关闭空闲连接 CONFIG SET timeout 300

慢查询

问题现象

  • Redis 响应慢
  • 慢查询日志增多

排查步骤

bash
# 查看慢查询 SLOWLOG GET 10 # 查看慢查询配置 CONFIG GET slowlog-log-slower-than CONFIG GET slowlog-max-len

解决方案

bash
# 调整慢查询阈值 CONFIG SET slowlog-log-slower-than 10000 # 优化慢查询命令 # 避免 KEYS 命令,使用 SCAN # 避免 O(n) 复杂度的命令 # 使用 Pipeline 减少网络往返

主从同步延迟

问题现象

  • 从节点数据滞后
  • 读取到旧数据

排查步骤

bash
# 查看主从同步状态 INFO replication # 查看同步延迟 master_repl_offset:1000000 slave_repl_offset:999000

解决方案

bash
# 调整复制缓冲区大小 CONFIG SET repl-backlog-size 10mb # 调整复制超时时间 CONFIG SET repl-timeout 60 # 优化网络延迟 # 使用更快的网络 # 减少主从节点之间的距离

5. Redis 性能优化

配置优化

内存优化

bash
# 设置最大内存 maxmemory 2gb # 设置内存淘汰策略 maxmemory-policy allkeys-lru # 关闭 THP echo never > /sys/kernel/mm/transparent_hugepage/enabled

持久化优化

bash
# RDB 配置 save 900 1 save 300 10 save 60 10000 # AOF 配置 appendonly yes appendfsync everysec auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb

网络优化

bash
# 设置 TCP backlog tcp-backlog 511 # 设置 TCP keepalive tcp-keepalive 300 # 设置超时时间 timeout 300

运维优化

定期备份

bash
# 定期备份 RDB 文件 0 2 * * * cp /var/lib/redis/dump.rdb /backup/dump_$(date +\%Y\%m\%d).rdb # 定期备份 AOF 文件 0 3 * * * cp /var/lib/redis/appendonly.aof /backup/appendonly_$(date +\%Y\%m\%d).aof

监控告警

bash
# 配置监控告警 # 内存使用率超过 80% 告警 # QPS 下降超过 50% 告警 # 延迟超过 100ms 告警 # 连接数超过 80% 告警

总结

Redis 的监控和运维是保证 Redis 稳定运行的重要环节。需要从基础指标、性能指标、监控工具、运维操作、故障排查、性能优化等多个维度进行监控和管理。在实际应用中,需要建立完善的监控体系,及时发现和解决问题,确保 Redis 的稳定性和性能。

标签:Redis