prometheus监控redis

redis_exporter prometheus官方开源的专门监控Redis的插件工具,我们直接使用就可以。

笔记配套视频效果更佳哦,视频地址:https://edu.51cto.com/lecturer/14390454.html

1、初始环境配置之Redis安装
#1、安装Redis
[root@localhost ~]# yum install redis -y
[root@localhost ~]# vi /etc/redis.conf 
bind 192.168.1.101 127.0.0.1
[root@localhost ~]# systemctl restart redis
2、安装配置redis_exporter

https://github.com/oliver006/redis_exporter/releases/tag/v1.33.0

[root@localhost ~]# wget https://github.com/oliver006/redis_exporter/releases/download/v1.33.0/redis_exporter-v1.33.0.linux-amd64.tar.gz
[root@localhost ~]# tar zxf redis_exporter-v1.33.0.linux-amd64.tar.gz 
[root@localhost ~]# mv redis_exporter-v1.33.0.linux-amd64 /usr/local/redis_exporter
[root@localhost ~]# cd /usr/local/redis_exporter/
[root@localhost redis_exporter]# 
#启动(本课程未使用)
# 有密码启动
nohup ./redis_exporter -redis.addr redis://localhost:6379  -redis.password 'Redis密码' &

# 无密码启动
nohup ./redis_exporter -redis.addr redis://localhost:6379 &
3、加入系统

笔记配套视频效果更佳哦,视频地址:https://edu.51cto.com/lecturer/14390454.html

[root@localhost ~]# cat > /etc/systemd/system/redis_exporter.service << "EOF"
[Unit]
Description=redis_exporter
After=local-fs.target network-online.target network.target
Wants=local-fs.target network-online.target network.target
 
[Service]
ExecStart=/usr/local/redis_exporter/redis_exporter -redis.addr 127.0.0.1:6379
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start redis_exporter
[root@localhost ~]# systemctl status redis_exporter
[root@localhost ~]# systemctl enable redis_exporter
4、将服务加入prometheus
# Prometheus添加job如下:
..........
  - job_name: redis_since
    static_configs:
    - targets: ['192.168.1.101:9121']     # 指定redis_exporter的监听地址
    
#重启prometheus
[root@prometheus ]# curl -X POST http://192.168.1.120:9090/-/reload
#或者重启(在生产环境中我们建议使用上面热加载的方式启动)
[root@prometheus alertmanager]# systemctl restart prometheus
5、登录prometheus查看---->确保Redis已经采集到数据

6、配置grafana大屏展示 763
redis_memory_used_bytes /1024

7、编写告警规则与告警
编写告警规则和告警

告警规则模板:https://github.com/oliver006/redis_exporter/blob/v1.23.0/contrib/redis-mixin/alerts/redis.yaml

告警模板:https://github.com/oliver006/redis_exporter/blob/v1.23.0/contrib/redis-mixin/rules/redis.yaml
[root@prometheus prometheus]# cat rules/redis_alerts.yml 
groups:
  - name: redis
    rules:
      - alert: RedisDown
        expr: redis_up == 0
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "Redis down (instance {{ $labels.instance }})"
          description: "Redis instance is down\n  VALUE = {{ $value }}\n  LABELS: {{ $labels }}"
      - alert: RedisOutOfMemory
        expr: redis_memory_used_bytes / redis_total_system_memory_bytes * 100 > 90
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "Redis out of memory (instance {{ $labels.instance }})"
          description: "Redis is running out of memory (> 90%)\n  VALUE = {{ $value }}\n  LABELS: {{ $labels }}"
      - alert: RedisTooManyConnections
        expr: redis_connected_clients > 100
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "Redis too many connections (instance {{ $labels.instance }})"
          description: "Redis instance has too many connections\n  VALUE = {{ $value }}\n  LABELS: {{ $labels }}"
[root@prometheus rules]# vi redis_rule.yml
[root@prometheus rules]# cat redis_rule.yml 
groups:
  - name: redis_rules
    rules:
    - record: redis_memory_fragmentation_ratio
      expr: redis_memory_used_rss_bytes / redis_memory_used_bytes
[root@prometheus prometheus]# vi prometheus.yml 
...............
rule_files:
  - './rules/rule.yml'
  - './rules/mysql_rules.yml'
  - './rules/mysql_alerts.yml'
  - './rules/redis_alerts.yml'
  - './rules/redis_rules.yml'
#重启prometheus
[root@prometheus ]# curl -X POST http://192.168.1.120:9090/-/reload
#或者重启(在生产环境中我们建议使用上面热加载的方式启动)
[root@prometheus alertmanager]# systemctl restart prometheus
8、测试告警
[root@localhost redis_exporter]# systemctl stop redis

笔记配套视频效果更佳哦,视频地址:https://edu.51cto.com/lecturer/14390454.html

posted @ 2022-01-19 17:41  老王教你学Linux  阅读(717)  评论(0编辑  收藏  举报