title: 2.6.监控redis
order: 11
icon: lightbulb
一、环境
主机名 | IP地址 | 系统 | 说明 |
localhost | 192.168.11.61 | Ubuntu 20.04 | docker安装的prometheus |
test | 192.168.11.62 | Ubuntu 20.04 | redis版本5.0,Docker 版本 23.0.1 |
1、环境搭建
docker安装
略
docker-compose安装
略
redis
已经在监控nginx那节课安装好了
docker-compose.yaml
version: '3'
services:
redis:
image: redis:5
container_name: redis
command: redis-server --requirepass 123456 --maxmemory 512mb
restart: always
volumes:
- /data/redis/data:/data
ports:
- 6379:6379
```
docker-compose up -d
二、监控redis
1、二进制安装(二选一)
redis_exporter下载地址:https://github.com/oliver006/redis_exporter/releases
下载二进制包解压并放入/opt目录
wget https://github.com/oliver006/redis_exporter/releases/download/v1.48.0/redis_exporter-v1.48.0.linux-amd64.tar.gz
tar xvf redis_exporter-v1.48.0.linux-amd64.tar.gz
mkdir /opt/prometheus/
mv redis_exporter-v1.48.0.linux-amd64 /opt/prometheus/redis_exporter
创建用户
useradd -M -s /usr/sbin/nologin prometheus
更改exporter文件夹权限
chown prometheus:prometheus -R /opt/prometheus
创建 systemd 服务
redis_exporter.service
cat > /etc/systemd/system/redis_exporter.service <<"EOF"
[Unit]
Description=Prometheus Redis Exporter
After=network.target
[Service]
Type=simple
User=prometheus
Group=prometheus
Restart=always
ExecStart=/opt/prometheus/redis_exporter/redis_exporter \
-redis.addr localhost:6379 \
-redis.password 123456
[Install]
WantedBy=multi-user.target
EOF
启动 redis_exporter
systemctl daemon-reload
systemctl start redis_exporter
加入到开机自启动
systemctl enable redis_exporter
检查
systemctl status redis_exporter
启动不了检查日志
journalctl -u redis_exporter -f
2、docker安装(二选一)
docker直接运行
docker run -d --restart=always --name redis_exporter -p 9121:9121 oliver006/redis_exporter --redis.addr redis://192.168.11.62:6379 --redis.password '123456'
docker-compose方式
cat >docker-compose.yaml <<EOF
version: '3.3'
services:
redis_exporter:
image: oliver006/redis_exporter
container_name: redis_exporter
restart: always
environment:
REDIS_ADDR: "192.168.11.62:6379"
REDIS_PASSWORD: 123456
ports:
- "9121:9121"
EOF
启动
docker-compose up -d
检查
docker ps
或:
docker logs -f redis_exporter
3、参数解释
Environment variable | 值 | description |
REDIS_ADDR | 192.168.11.62:6379 | redis服务器地址,如:ip:6379 |
REDIS_PASSWORD | 123456 | redis服务器管理密码 |
4、metrics地址
注:安装好Exporter后会暴露一个http://ip:端口/metrics
的HTTP服务
名称 | 地址 | 备注 |
redis_exporter |
5、Prometheus配置
配置prometheus去采集(拉取)redis_exporter的监控样本数据
cd /data/docker-prometheus
#在scrape_configs(搜刮配置):下面增加如下配置:
cat >> prometheus/prometheus.yml << "EOF"
- job_name: 'redis_exporter'
static_configs:
- targets: ['192.168.11.62:9121']
labels:
instance: test服务器
EOF
重新加载配置
curl -X POST http://localhost:9090/-/reload
检查
6、常用的监控指标
redis_up # 服务器是否在线
redis_uptime_in_seconds # 运行时长,单位 s
rate(redis_cpu_sys_seconds_total[1m]) + rate(redis_cpu_user_seconds_total[1m]) # 占用 CPU 核数
redis_memory_used_bytes # 占用内存量
redis_memory_max_bytes # 限制的最大内存,如果没限制则为 0
delta(redis_net_input_bytes_total[1m]) # 网络接收的 bytes
delta(redis_net_output_bytes_total[1m]) # 网络发送的 bytes
redis_connected_clients # 客户端连接数
redis_connected_clients / redis_config_maxclients # 连接数使用率
redis_rejected_connections_total # 拒绝的客户端连接数
redis_connected_slaves # slave 连接数
7、触发器配置
Prometheus配置
# 报警(触发器)配置
rule_files:
- "alert.yml"
- "rules/*.yml"
redis触发器(告警规则)
因为是单机,所以未配置集群的触发器
cat >> prometheus/rules/redis.yml <<"EOF"
groups:
- name: redis
rules:
- alert: RedisDown
expr: redis_up == 0
for: 0m
labels:
severity: critical
annotations:
summary: 'Redis Down,实例:{{ $labels.instance }}'
description: "Redis实例 is down"
- alert: RedisMissingBackup
expr: time() - redis_rdb_last_save_timestamp_seconds > 60 * 60 * 24
for: 0m
labels:
severity: critical
annotations:
summary: "Redis备份丢失,实例:{{ $labels.instance }}"
description: "Redis 24小时未备份"
- alert: RedisOutOfConfiguredMaxmemory
expr: redis_memory_used_bytes / redis_memory_max_bytes * 100 > 90
for: 2m
labels:
severity: warning
annotations:
summary: "Redis超出配置的最大内存,实例:{{ $labels.instance }}"
description: "Redis内存使用超过配置最大内存的90%"
- alert: RedisTooManyConnections
expr: redis_connected_clients > 100
for: 2m
labels:
severity: warning
annotations:
summary: "Redis连接数过多,实例:{{ $labels.instance }}"
description: "Redis当前连接数为: {{ $value }}"
- alert: RedisNotEnoughConnections
expr: redis_connected_clients < 1
for: 2m
labels:
severity: warning
annotations:
summary: "Redis没有足够的连接,实例:{{ $labels.instance }}"
description: "Redis当前连接数为: {{ $value }}"
- alert: RedisRejectedConnections
expr: increase(redis_rejected_connections_total[1m]) > 0
for: 0m
labels:
severity: critical
annotations:
summary: "Redis有拒绝连接,实例:{{ $labels.instance }}"
description: "与Redis 的某些连接被拒绝{{ $value }}"
EOF
检查:
vim prometheus/alert.yml
检查配置
docker exec -it prometheus promtool check config /etc/prometheus/prometheus.yml
重新加载配置
curl -X POST http://localhost:9090/-/reload
检查
http://192.168.11.61:9090/alerts?search=
或:
http://192.168.11.61:9090/rules
8、dashboard
grafana展示prometheus从redis_exporter收集到的的数据
Redis:https://grafana.com/grafana/dashboards/11835
或:
https://grafana.com/grafana/dashboards/17507
图行展示问题
redis dashbord 内存使用图行展示有问题,如下图
http://192.168.11.61:9090/graph
查询到redis_memory_max_bytes
为0,因为redis配置参数maxmemory
未设置,默认0
。
解决
redis增加配置maxmemory
的值
三、我的微信
如果碰到问题,可以随时加我微信,谢谢
评论区