Prometheus+Grafana监控redis
1、简述
使用 Prometheus 和 Grafana 监控 Redis 的方案与监控 MySQL 类似,需要通过 Redis Exporter 来暴露 Redis 的监控指标,再由 Prometheus 采集并通过 Grafana 可视化。
2、整体架构
- Redis Exporter:部署在可访问 Redis 的节点上,负责收集 Redis 的运行指标(如内存使用、连接数、命中率等)并提供 HTTP 接口。
- Prometheus:定时从 Redis Exporter 抓取指标并存储。
- Grafana:连接 Prometheus 数据源,通过仪表盘可视化 Redis 指标。
3、部署组件
3.1 部署 Redis Exporter
下载安装包
从官方仓库下载对应版本(以v1.75.0
为例):
wget https://github.com/oliver006/redis_exporter/releases/download/v1.75.0/redis_exporter-v1.75.0.linux-amd64.tar.gz
tar -zxvf redis_exporter-v1.75.0.linux-amd64.tar.gz
mv redis_exporter-v1.75.0.linux-amd64 /usr/local/redis_exporter
启动 Exporter-系统服务方式(推荐):
创建/etc/systemd/system/redis-exporter.service
:
[Unit]
Description=Redis Exporter
After=network.target[Service]
User=root
ExecStart=/usr/local/redis_exporter/redis_exporter --redis.addr=redis://localhost:6379 --redis.password=your_redis_password
Restart=always[Install]
WantedBy=multi-user.target
启动并设置开机自启:
systemctl daemon-reload
systemctl start redis-exporter
systemctl enable redis-exporter
验证 Exporter
访问http://服务器IP:9121/metrics
,若能看到 Redis 相关指标(如redis_up
、redis_connected_clients
),说明启动成功。
3.2 Prometheus+Grafana
安装
参考文章:docker compose安装Prometheus、Grafana_grafana docker-compose-CSDN博客
配置 Prometheus 抓取 Exporter 指标
编辑 Prometheus 配置文件(/usr/local/prometheus/prometheus.yml
),在scrape_configs
中添加 Redis 任务:
scrape_configs:- job_name: 'redis'static_configs:- targets: ['localhost:9121'] # Redis Exporter的地址
重启Prometheus。
4、配置 Grafana 可视化 Redis指标
添加 Prometheus 数据源
进入 Grafana(http://IP:3000
),依次点击 “Configuration -> Data Sources -> Add data source”,选择 “Prometheus”,填写 URL(如http://localhost:9090
),保存测试。
导入 Redis 监控仪表盘
- 点击 “Dashboards -> Import”,输入仪表盘 ID(推荐
763
或11835
,可在Grafana Dashboards搜索 “Redis” 获取)。 - 选择 Prometheus 数据源,点击 “Import”,即可看到 Redis 监控面板,包含以下核心指标:
- 可用性:
redis_up
(1 为正常) - 连接数:
redis_connected_clients
(当前连接数)、redis_client_longest_output_list
(最长输出列表) - 内存指标:
redis_used_memory
(已用内存)、redis_used_memory_peak
(内存峰值) - 命中率:
redis_keyspace_hits
(命中数)、redis_keyspace_misses
(未命中数),命中率 = hits/(hits+misses) - 持久化:
redis_rdb_last_save_time
(最近 RDB 保存时间)、redis_aof_last_rewrite_time_sec
(AOF 重写时间)
- 可用性: