Redis Exporter 快速入门

在本指南中,您将学习如何设置和配置 Redis Exporter 以收集 Redis 指标,例如 Redis 正常运行时间、每秒执行的命令和内存利用率,并将它们公开为 Prometheus 风格的指标。然后,您将配置Prometheus以抓取 Redis 指标,并可选择将它们发送到 Grafana Cloud。最后,您将设置一组预先配置和策划的记录规则、Grafana仪表板和警报规则。在本指南的最后,您将拥有可用于可视化 Redis 指标的仪表板,以及一组预配置的警报。

https://grafana.com/oss/prometheus/exporters/redis-exporter/?tab=installation

先决条件

在开始之前,您应该准备好以下内容:

  • Redis 服务器和 Redis Exporter 可以连接到的端口 6379,如果您没有安装 Redis,请参阅他们的下载页面。
  • 与 Redis Exporter 版本兼容的 Linux 机器。要查看可用版本列表,请参阅Releases。
  • 在您的环境中或直接在机器上运行的 Prometheus。要了解如何安装 Prometheus,请参阅Prometheus 文档中的安装。
  • 在您的环境中或直接在机器上运行的 Grafana。要了解如何安装 Grafana,请参阅Grafana 文档中的安装 Grafana 。
  • (可选)Grafana Cloud 帐户。Grafana Cloud 托管 Grafana 和基于Cortex的 Prometheus 指标端点。您仍然需要使用环境中安装的 Prometheus 或Grafana Cloud Agent来抓取指标。要了解有关 Grafana Cloud 的更多信息,请参阅Grafana Cloud。

第 1 步:设置 Redis 导出器

在此步骤中,您将在计算机上设置 Redis 导出器,以收集和公开 Prometheus 格式的 Redis 指标。本指南使用带有 Redis Server v6.2.1 的 Ubuntu 20.04 系统。根据您的操作系统和 Redis 版本,步骤可能略有不同。

首先,登录到您的机器并下载相关的 Redis Exporter 二进制文件。本指南使用linux-amd64二进制文件,但您应该选择与您系统的操作系统和体系结构相对应的二进制文件:

https://github.com/oliver006/redis_exporter/releases

cd /usr/local/src && wget https://github.com/oliver006/redis_exporter/releases/download/v1.48.0/redis_exporter-v1.48.0.linux-amd64.tar.gz

替换v1.48.0为您要安装的版本。本指南可能会过时,因此最好查看 Redis Exporter Releases 页面以获取最新的稳定版本。

解压并进入目录:

cd /usr/local/src && tar -zxvf redis_exporter-v1.48.0.linux-amd64.tar.gz
mv redis_exporter-v1.48.0.linux-amd64 /usr/local/redis_exporter;
cd /usr/local/redis_exporter;

查看使用帮助:

[root@192 redis_exporter]# ./redis_exporter -h
Usage of ./redis_exporter:
  -check-key-groups string
        Comma separated list of lua regex for grouping keys
  -check-keys string
        Comma separated list of key-patterns to export value and length/size, searched for with SCAN
  -check-keys-batch-size int
        Approximate number of keys to process in each execution, larger value speeds up scanning.
        WARNING: Still Redis is a single-threaded app, huge COUNT can affect production environment. (default 1000)
  -check-single-keys string
        Comma separated list of single keys to export value and length/size
  -check-single-streams string
        Comma separated list of single streams to export info about streams, groups and consumers
  -check-streams string
        Comma separated list of stream-patterns to export info about streams, groups and consumers, searched for with SCAN
  -config-command string
        What to use for the CONFIG command (default "CONFIG")
  -connection-timeout string
        Timeout for connection to Redis instance (default "15s")
  -count-keys string
        Comma separated list of patterns to count (eg: 'db0=production_*,db3=sessions:*'), searched for with SCAN
  -debug
        Output verbose debug information
  -export-client-list
        Whether to scrape Client List specific metrics
  -export-client-port
        Whether to include the client's port when exporting the client list. Warning: including the port increases the number of metrics generated and will make your Prometheus server take up more memory
  -include-config-metrics
        Whether to include all config settings as metrics
  -include-system-metrics
        Whether to include system metrics like e.g. redis_total_system_memory_bytes
  -is-cluster
        Whether this is a redis cluster (Enable this if you need to fetch key level data on a Redis Cluster).
  -is-tile38
        Whether to scrape Tile38 specific metrics
  -log-format string
        Log format, valid options are txt and json (default "txt")
  -max-distinct-key-groups int
        The maximum number of distinct key groups with the most memory utilization to present as distinct metrics per database, the leftover key groups will be aggregated in the 'overflow' bucket (default 100)
  -namespace string
        Namespace for metrics (default "redis")
  -ping-on-connect
        Whether to ping the redis instance after connecting
  -redact-config-metrics
        Whether to redact config settings that include potentially sensitive information like passwords (default true)
  -redis-only-metrics
        Whether to also export go runtime metrics
  -redis.addr string
        Address of the Redis instance to scrape (default "redis://localhost:6379")
  -redis.password string
        Password of the Redis instance to scrape
  -redis.password-file string
        Password file of the Redis instance to scrape
  -redis.user string
        User name to use for authentication (Redis ACL for Redis 6.0 and newer)
  -script string
        Path to Lua Redis scripts for collecting extra metrics, separated by ,
  -set-client-name
        Whether to set client name to redis_exporter (default true)
  -skip-tls-verification
        Whether to to skip TLS verification
  -tls-ca-cert-file string
        Name of the CA certificate file (including full path) if the server requires TLS client authentication
  -tls-client-cert-file string
        Name of the client certificate file (including full path) if the server requires TLS client authentication
  -tls-client-key-file string
        Name of the client key file (including full path) if the server requires TLS client authentication
  -tls-server-ca-cert-file string
        Name of the CA certificate file (including full path) if the web interface and telemetry should require TLS client authentication
  -tls-server-cert-file string
        Name of the server certificate file (including full path) if the web interface and telemetry should use TLS
  -tls-server-key-file string
        Name of the server key file (including full path) if the web interface and telemetry should use TLS
  -tls-server-min-version string
        Minimum TLS version that is acceptable by the web interface and telemetry when using TLS (default "TLS1.2")
  -version
        Show version information and exit
  -web.listen-address string
        Address to listen on for web interface and telemetry. (default ":9121")
  -web.telemetry-path string
        Path under which to expose metrics. (default "/metrics")

运行 Redis Exporter 时,需要指定 Redis Server 运行的地址。这可能与您的设置不同,默认情况下 Redis Exporter 将连接到本地主机,但可以使用标志设置地址-redis.addr。

最后,运行导出器:

./redis_exporter -redis.addr redis://localhost:16379
INFO[0000] Redis Metrics Exporter v1.18.0    build date: 2021-03-11-03:26:58    sha1: d0597c841d2c9fa30ce8b6ded6251d1994822e27    Go: go1.16.1    GOOS: linux    GOARCH: amd64
INFO[0000] Providing metrics at :9121/metrics

如果您看到以上输出,则说明您已成功运行 Redis Exporter。

Redis Exporter 在端口 9121 上以 Prometheus 格式发布 Redis 指标。您可以使用curl. 您将需要打开一个新的 SSH 会话或后台 Redis Exporter 进程才能使用curl。

curl http://localhost:9121/metrics

. . .
redis_cluster_connections 0
# HELP redis_cluster_enabled cluster_enabled metric
# TYPE redis_cluster_enabled gauge
redis_cluster_enabled 0
# HELP redis_commands_duration_seconds_total Total amount of time in seconds spent per command
# TYPE redis_commands_duration_seconds_total counter
redis_commands_duration_seconds_total{cmd="client"} 1.7e-05
redis_commands_duration_seconds_total{cmd="command"} 0.000443
redis_commands_duration_seconds_total{cmd="config"} 0.000178
redis_commands_duration_seconds_total{cmd="info"} 8.1e-05
redis_commands_duration_seconds_total{cmd="latency"} 8e-06
redis_commands_duration_seconds_total{cmd="set"} 1.7e-05
redis_commands_duration_seconds_total{cmd="slowlog"} 1.4e-05
# HELP redis_commands_processed_total commands_processed_total metric
# TYPE redis_commands_processed_total counter
redis_commands_processed_total 10

如果您看到以上输出,则您已准备好开始使用 Prometheus 抓取 Redis 指标。

为避免从命令行运行和管理 Redis Exporter,您可以创建一个systemd服务。要了解如何执行此操作,请参阅创建系统服务来管理代理。将代理二进制文件的路径替换为 Redis Exporter 的路径。

使用 systemd方式启动
# cat >/usr/lib/systemd/system/redis_exporter.service <<EOF

[Unit]
Description=Prometheus exporter for Redis server metrics.
Documentation=https://github.com/oliver006/redis_exporter
After=network.target

[Service]
ExecStart=/usr/local/redis_exporter/redis_exporter -redis.addr redis://localhost:16379 -redis.password hpkRzkC7pcDnYp3e
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

# systemctl enable redis_exporter

# systemctl restart redis_exporter

设置随机启动:

[root@local ~]# systemctl daemon-reload
[root@local ~]# systemctl disable redis_exporter.service
[root@local ~]# systemctl enable redis_exporter.service
[root@local ~]# systemctl start redis_exporter.service
[root@local ~]# systemctl status redis_exporter.service
[root@local ~]# systemctl stop redis_exporter.service
[root@local ~]# systemctl restart redis_exporter.service
Redis Exporter 提供以下功能:
  • 针对 Redis 进行身份验证
  • TLS 配置
  • 仅限 Redis 的指标

以及更多。要了解有关这些功能的更多信息,请参阅Redis Exporter GitHub 存储库。

第 2 步:使用 Prometheus 抓取 Redis Exporter

现在 Redis Exporter 已在您的计算机上启动并运行,您可以配置 Prometheus 抓取作业来收集和存储 Redis Exporter 指标。

将以下抓取作业配置添加到配置文件scrape_configs的部分prometheus.yml:

- job_name: redis
  static_configs:
  - targets: ['redis_exporter_machine_IP_address:9121']

替换redis_exporter_machine_IP_address为运行 Redis Exporter 的机器的 IP 地址。如果您在同一台机器上运行 Prometheus,这将是localhost. 要了解有关配置 Prometheus 的更多信息,请参阅Prometheus 文档中的配置。

如果您没有prometheus.yml配置文件,请使用您最喜欢的文本编辑器创建一个简单的配置文件。打开您喜欢的文本编辑器并粘贴以下 Prometheus 配置:

global:
  scrape_interval: 15s

scrape_configs:
- job_name: redis
  static_configs:
  - targets: ['redis_exporter_machine_IP_address:9121']

此配置告诉 Prometheus 每 15 秒抓取一次所有作业。唯一配置的抓取作业被调用redis-exporter并定义了一个redis_exporter_machine_IP_address:9121目标。默认情况下,Prometheus 将使用 HTTP 抓取/metrics端点。

保存并关闭文件。然后,您可以使用以下命令使用该文件运行 Prometheus:

./prometheus --config.file=./prometheus.yml

第 3 步:配置录制规则

第 4 步:配置仪表板

本快速入门包括以下仪表板:

Prometheus Redis Exporter 的 Redis 仪表板

要了解如何将这些仪表板导入 Grafana,请参阅从 Grafana 文档导入仪表板。

注意:这些仪表板查询可能取决于上一步中定义的记录规则。确保在导入仪表板之前导入这些。

您可以在此处获取仪表板。 https://grafana.com/oss/prometheus/exporters/redis-exporter/?tab=dashboards

此仪表板包括以下指标的面板:

  • 每秒命令
  • 每秒命令延迟
  • 每个实例的命中率
  • 总内存使用量
  • 每个实例的内存碎片率
  • 每个实例每秒的键逐出
  • 已连接/已阻止的客户端
  • 每个数据库的项目总数
  • 过期与非过期密钥
  • 每个实例连接的从站
  • 自上次主连接以来的时间

您可以使用以下仪表板 ID 直接导入仪表板:763

作者:Jeebiz  创建时间:2023-02-21 22:44
最后编辑:Jeebiz  更新时间:2024-01-24 21:48