温馨提示×

centos redis配置怎么监控

小樊
39
2026-09-21 03:47:39
栏目: 云计算

在 CentOS 上监控 Redis,可以从系统层、Redis 自身、可视化工具、告警几个层面来做。下面按“由浅入深”的方式给你一套可落地的方案。


一、最基础:Redis 自带命令

1. redis-cli ping

redis-cli ping

返回 PONG 表示存活。

2. redis-cli info

最重要、最常用:

redis-cli info

或分类查看:

redis-cli info memory
redis-cli info stats
redis-cli info replication
redis-cli info persistence

重点关注指标:

  • used_memory:内存使用
  • connected_clients:客户端连接数
  • evicted_keys:被淘汰的 key
  • keyspace_hits / misses:命中率
  • rdb_last_bgsave_status:持久化状态

二、进程 & 系统级监控(CentOS)

1. 看 Redis 进程

ps -ef | grep redis

2. 端口监听

ss -lntp | grep 6379

3. 系统资源

top
free -h
df -h

三、日志监控(很重要)

Redis 日志位置在配置文件中:

grep logfile /etc/redis.conf

实时看日志:

tail -f /var/log/redis/redis.log

重点看:

  • Background saving started/failed
  • Out Of Memory
  • Connection refused

四、自动化监控(推荐)

方案 1:Redis + systemd(最简单)

CentOS 7/8 一般用 systemd:

systemctl status redis

设置开机自启:

systemctl enable redis

方案 2:Shell 脚本 + 告警(轻量)

示例监控脚本:

#!/bin/bash
if ! redis-cli ping > /dev/null; then
    echo "Redis is down!" | mail -s "Redis Alert" admin@example.com
fi

加进 crontab:

* * * * * /opt/monitor_redis.sh

五、专业监控方案(生产推荐)

1. Redis Exporter + Prometheus + Grafana(最主流)

安装 redis_exporter

wget https://github.com/oliver006/redis_exporter/releases/download/v1.54.0/redis_exporter-v1.54.0.linux-amd64.tar.gz
tar -xzf redis_exporter-*.tar.gz
./redis_exporter &

Prometheus 配置

scrape_configs:
  - job_name: redis
    static_configs:
      - targets: ['localhost:9121']

Grafana

  • 导入官方 Dashboard(ID:763)
  • 可看:内存、QPS、连接数、命中率

2. Redis Insight(官方 GUI)

适合开发和排查:

docker run -d -p 8001:8001 redis/redisinsight

浏览器访问:http://IP:8001


六、关键监控指标建议

✅ 必须监控:

  • Redis 是否存活
  • 内存使用率(>80% 告警)
  • 连接数异常增长
  • 持久化是否失败
  • 命中率 < 90%

七、常见坑提醒

  • maxmemory 没配 → 容易 OOM
  • 没开 AOF / RDB → 数据丢失无感知
  • 公网暴露 6379 → 被挖矿

如果你愿意,我可以:

  • 给你一套 CentOS Redis 监控脚本
  • 或直接帮你 写 Prometheus + Grafana 完整配置
  • 或针对 单机 / 集群 给方案

你现在是 测试环境还是生产环境?

0 踩