# Prometheus 监控 Redis

## prometheus 监控 redis 需要用到 redis\_exporter。

### 安装

```bash
cd /usr/local/src
wget https://github.com/oliver006/redis_exporter/releases/download/v0.21.2/redis_exporter-v0.21.2.linux-amd64.tar.gz
mkdir /usr/local/redis_exporter
tar xf redis_exporter-v0.21.2.linux-amd64.tar.gz -C /usr/local/redis_exporter/



# redis_exporter -h
-redis.addr：指明一个或多个 Redis 节点的地址，多个节点使用逗号分隔，默认为 redis://localhost:6379
-redis.password：验证 Redis 时使用的密码；
-redis.file：包含一个或多个redis 节点的文件路径，每行一个节点，此选项与 -redis.addr 互斥。
-web.listen-address：监听的地址和端口，默认为 0.0.0.0:9121
```

### 运行

```bash
## 无密码
./redis_exporter redis//192.168.111.11:6379 &

## 有密码
redis_exporter  -redis.addr 192.168.111.11:6379  -redis.password 123456
```

### 系统管理

```bash
$ vim /usr/lib/systemd/system/redis_exporter.service
[Unit]
Description=redis_exporter
Documentation=https://github.com/oliver006/redis_exporter
After=network.target

[Service]
Type=simple
# User=prometheus
ExecStart=/usr/local/redis_exporter/redis_exporter -redis.addr 192.168.111.11:6379  -redis.password 123456
Restart=on-failure
[Install]
WantedBy=multi-user.target
```

\`\`bash

## 启动用户，不指定 User 不用创建

groupadd prometheus useradd -g prometheus -m -d /var/lib/prometheus -s /sbin/nologin prometheus

````
```bash
systemctl daemon-reload
systemctl start redis_exporter
systemctl status redis_exporter
ss -tnl|grep 9121
````

### 配置 Grafana 的模板

* redis\_exporter 在 Grafana 上为我们提供好了 Dashboard 模板：<https://grafana.com/dashboards/763>

### docker 方式

* 如果是 Docker 部署的 Redis 集群节点，只要能连接到一个集群的一个节点，自然就能查询其他节点的指标了

> docker run -d --name redis\_exporter -p 9121:9121 oliver006/redis\_exporter --redis.addr redis\://192.168.111.11:6379 --redis.password '123456'

### 监控单节点

```yaml
  - job_name: "redis-sit"
    static_configs:
    - targets: ['192.168.111.11:9121']
```

### 监控集群

```yaml
  - job_name: 'redis_exporter_targets'
    static_configs:
      - targets:
        - redis://192.168.111.11:7000
        - redis://192.168.111.11:7001
        - redis://192.168.111.12:7002
        - redis://192.168.111.12:7003
        - redis://192.168.111.13:7004
        - redis://192.168.111.13:7005
    metrics_path: /scrape
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 192.168.111.11:9121
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/centos/prometheus/prometheus-jian-kong-redis.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
