> For the complete documentation index, see [llms.txt](https://close.gitbook.io/yun-wei-bi-ji/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://close.gitbook.io/yun-wei-bi-ji/centos/prometheus/prometheus-cai-ji-redis-zhi-biao.md).

# Prometheus-采集Redis指标

> Github: <https://github.com/oliver006/redis_exporter/releases/>

* 云厂商地址: 1.1.1.1
* 服务器地址: 2.2.2.2

### **1. 下载** `redis_exporter` <a href="#id-1.-e4-b8-8b-e8-bd-bd-redis_exporter" id="id-1.-e4-b8-8b-e8-bd-bd-redis_exporter"></a>

```
cd /usr/local/src
wget https://github.com/oliver006/redis_exporter/releases/download/v1.55.0/redis_exporter-v1.55.0.linux-amd64.tar.gz
mv redis_exporter-v1.55.0.linux-amd64 /data/redis_exporter
```

* redis\_exporter -h

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

### **2.运行 redis\_exporter 服务** <a href="#id-2.-e8-bf-90-e8-a1-8c-redis_exporter-e6-9c-8d-e5-8a-a1" id="id-2.-e8-bf-90-e8-a1-8c-redis_exporter-e6-9c-8d-e5-8a-a1"></a>

* 以 saas 为例, 新建用户: exporter， 权限: 只读， 密码: Qwe123456
* 1\. 方式 A 直接启动。

```
## 无密码
nohup  ./redis_exporter redis//r-3nsfvqew8gr0wrr6ko.redis.rds.aliyuncs.com:6379 &
## 有密码
nohup redis_exporter  -redis.addr 1.1.1.1:6379 -redis.user exporter  -redis.password Qwe123456 -redis-only-metrics &
```

* 2.方式 B 通过 system 管理。

```
cat >> /usr/lib/systemd/system/redis_exporter.service << EOF
[Unit]
Description=redis_exporter
After=network.target

[Service]
Type=simple
ExecStart=/data/redis_exporter/redis_exporter -redis.addr 1.1.1.1:6379 -redis.user exporter  -redis.password Qwe123456 -redis-only-metrics
Restart=on-failure

[Install]
WantedBy=multi-user.target

EOF
```

```
systemctl daemon-reload
systemctl enable redis_exporter
systemctl start redis_exporter
systemctl status redis_exporter

ss -antpl |grep 9121

LISTEN     0      128       [::]:9121                  [::]:*                   users:(("redis_exporter",pid=3995,fd=3))
```

### **3. Prometheus 配置** <a href="#id-3.-prometheus-e9-85-8d-e7-bd-ae" id="id-3.-prometheus-e9-85-8d-e7-bd-ae"></a>

```
root@monitor-1:/data/monitor/prometheus cat prometheus.yml
  #- job_name: 'mysql'
  #  static_configs:
  #    - targets: ['x.x.x.x:9104']
  #      labels:
  #        instance: db1
  ... 略
  # MySQL数据库相关
  - job_name: 'Redis数据库'
    static_configs:
    file_sd_configs:
    - refresh_interval: 30s
      files:
      - "vhosts/redis/*.json" 
```

```
root@monitor-1:/data/monitor/prometheus# cat vhosts/mysql/saas.json 
[
  {
    "targets": ["2.2.2.2:9121"],
    "labels": {
         "label": "xxxx使用",
         "account": "account@xxx.com",
         "cloud": "xxxxx",
         "instance": "1.1.1.1:6379"          # Redis不在本地，覆盖 instance
    }
  }
]
```

```
- Prometheus 命令热加载, 我这里端口: 8090
```

bash\
./promtool check config prometheus.yml\
curl -XPOST [localhost:8090/-/reload](http://localhost:8090/-/reload) # 主配置文件热加载，修改子配置会自动加载

```
```

### **4. Grafana 大屏** <a href="#id-4.-grafana-e5-a4-a7-e5-b1-8f" id="id-4.-grafana-e5-a4-a7-e5-b1-8f"></a>

> 根据喜好自行选择: <https://grafana.com/grafana/dashboards>
