Redis系列之监控

勤奋不是嘴上说说而已,而是实际的行动,在勤奋的苦度中持之以恒,永不退却。业精于勤,荒于嬉;行成于思,毁于随。在人生的仕途上,我们毫不迟疑地选择勤奋,她是几乎于世界上一切成就的催产婆。只要我们拥着勤奋去思考,拥着勤奋的手去耕耘,用抱勤奋的心去对待工作,浪迹红尘而坚韧不拔,那么,我们的生命就会绽放火花,让人生的时光更加的闪亮而精彩。

导读:本篇文章讲解 Redis系列之监控,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

概述

在大范围使用Redis,即集群化部署后,需要知道Redis服务器的运行状况,保证高可用,诞生监控自身状态的需求。选项包括命令和工具。

命令

监控工具是基于命令,而这些命令绝大多数都是Redis官方推出的,即Redis(redis-cli)提供的一系列命令:

https://github.com/xetorthio/jedis/issues/1959

info

在RDM console输入info,即可得到一系列信息:

Server: General information about the Redis server
Stats: General statistics
Memory: Memory consumption information
Clients: Client connections section
Persistence: RDB and AOF information
Replication: Master/slave replication information
CPU: CPU consumption statistics
Commandstats: Redis command statistics
Cluster: Redis Cluster information
Keyspace: Database related statistics

对应的在代码里面:

String host = "10.114.31.113";
int port = 6379;
Jedis jedis = new Jedis(host, port);
String stats = jedis.info("stats");
Properties props = new Properties();
props.load(new StringReader(stats));
Long hit = Long.parseLong(props.get("keyspace_hits").toString());
Long miss = Long.parseLong(props.get("keyspace_misses").toString());
Double hitPercent = (double) hit / (hit + miss);

monitor

相对于info,更重量级,对Redis Server的影响更大。

memory

Redis 4版本新增:

  1. memory doctor: similar to the latency doctor tool, a feature that outputs memory consumption issues and provides possible solutions.
  2. memory usage <key> [samples <count>]: an estimate of the amount of memory used by the given key. The optional samples argument specifies how many elements of an aggregate datatype to sample to approximate the total size. The default is 5.
  3. memory stats: a detailed report of your instance’s memory usage; similar to the memory section of info, it pulls in other client- and replication-related metrics.
  4. memory malloc-stats: a detailed breakdown of the allocator’s internal statistics.

slowlog

slowlog是redis用于记录慢查询执行时间的日志系统,slowlog只保存在内存中,因此效率很高,完全不用担心会影响到redis的性能;从2.2.12版本引入的一条命令。

# 设置
CONFIG SET slowlog-log-slower-than 6000
CONFIG SET slowlog-max-len 25
# 使用
slowlog get 2

Latency monitor

Redis 2.8.13新增的命令:

config set latency-monitor-threshold <time in milliseconds>
latency latest
latency graph <event-type>
latency graph command
latency history <event-name>
latency history command

工具

下面的诸多工具,大多基于上面的命令。

RDM

大名鼎鼎的Redis desktop manager,

自版本后付费使用,破解还挺难的,下载之前版本使用。

DPM

DPM

redis-benchmark

Redis自带的性能检测工具,可以模拟 N 个客户端同时发出k个请求。可使用redis-benchmark -h来查看基准参数。
命令格式:redis-benchmark [-h ] [-p ] [-c ] [-n <requests]> [-k ]
参数介绍:
-h 指定服务器主机名,如:127.0.0.1
-p 指定服务器端口,一般6379
-s 指定服务器socket
-c 指定并发连接数
-n 指定请求数
-d 以字节的形式指定 SET/GET 值的数据大小
-k 1=keep alive 0=reconnect 1
-r SET/GET/INCR 使用随机 key,SADD 使用随机值
-P:通过管道传输<numreq>请求
-q 强制退出 redis。仅显示 query/sec 值
–csv:以 CSV 格式输出
-l 生成循环,永久执行测试
-t 仅运行以逗号分隔的测试命令列表。
-I Idle 模式。仅打开 N 个 idle 连接并等待。

实例

  1. 同时执行1000个请求来检测性能:redis-benchmark -n 1000 -q
  2. 50个并发请求,10000个请求,检测Redis性能:redis-benchmark -h localhost -p 6379 -c 50 -n 10000

RedisLive

GitHub
python开发的redis的可视化及查询分析工具,docker运行:docker run --name redis-live -p 8888:8888 -d snakeliwei/redislive
监控信息可以使用redis存储和SQLite持久化存储。docker运行:docker run --name redis-live -p 8888:8888 -d snakeliwei/redislive
访问:http://<host>:8888/index.html
优势:支持同时监控多个Redis实例
劣势:基于info、memory命令,其中memory命令对服务器的性能影响较大?Redis新版本依旧有这个问题?

redis-stat

注意,不是Redis作者antirez写的redis-tools工具集中的那个redis-stat,这里的工具,久未更新,功能简单。

GitHub开源的redis-stat,采用Ruby开发,redis指标可视化的监控工具,原理:基于info命令来统计,而不是monitor,不影响redis性能

安装

  1. docker运行
    docker run --name redis-stat -p 8080:63790 -d insready/redis-stat --server 192.168.1.12
  2. Ruby gem
    # 安装ruby
    sudo yum install ruby
    sudo yum install ruby-devel
    gem update --system
    # 更换国内镜像地址
    gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/
    # 验证
    gem sources -l
    # 安装
    gem install redis-stat
    

    运行redis-stat --server --daemon --auth=<passwd>,即可在后台启动redis-stat,浏览器输入:http://<host>:63790访问。

  3. Jar包
    需要本地有Java环境,GitHub release下载jar包,然后上传到要监控的服务器,java -jar redis-stat-0.4.14.jar --server --auth=<passwd>,浏览器输入:http://<host>:63790。服务器需要开放防火墙端口。

劣势:对于Redis cluster,即同时监控多个redis实例,不能单独显示每一个实例的数据信息,而是总和?

redmon

GitHub
提供cli、admin的web界面,能够实时监控redis。
docker运行:docker run -p 4567:4567 -d vieux/redmon -r redis://192.168.99.100:6379

redis-faina

GitHub
由Instagram开源的一个 Redis 查询分析小工具。Instagram团队曾经使用 PGFouine 来作为其PostgreSQL的查询分析工具。基于monitor命令。

redis_exporter

GitHub-redis_exporter
redis_exporter为Prometheus提供redis指标的exporter,支持Redis 2.x,3.x and 4.x,配合Prometheus以及Grafana的Prometheus Redis插件,可以在Grafana进行可视化及监控。

Grafana

提到可视化工具,必定要提到Grafana,对于Redis监控。

Prometheus

监控新秀,当然也支持对Redis的监控。

对比

工具 语言 GitHub star GitHub fork
RedisLive python
redis-stat Ruby
redmon
redis-faina

参考

how-to-monitor-redis-performance-metrics
how-to-collect-redis-metrics
monitor-redis-using-datadog
Redis图形化监控方案RedisLive介绍

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/142271.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!