Redis系列之监控

概述

在大范围使用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 starGitHub fork
RedisLivepython
redis-statRuby
redmon
redis-faina

参考

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

johnny233

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值