在linux节点上安装redis exporter随记

来自三线的随记

Prometheus exporter for Redis metrics.

Supports Redis 2.x, 3.x, 4.x, 5.x, and 6.x


binary file github repository and documentation

https://github.com/oliver006/redis_exporter

下載redis exporter 二進制文件壓縮包

在節點上執行命令下載

wget "https://github.com/oliver006/redis_exporter/releases/download/v1.12.1/redis_exporter-v1.12.1.linux-amd64.tar.gz" 

解壓壓縮包

[root@master redis]# ls
redis_exporter-v1.12.1.linux-amd64.tar.gz
[root@master redis]# tar -xzf redis_exporter-v1.12.1.linux-amd64.tar.gz
[root@master redis]# ls
redis_exporter-v1.12.1.linux-amd64  redis_exporter-v1.12.1.linux-amd64.tar.gz

將 redis_exporter-v1.12.1.linux-amd64 目錄下的 redis_exporter 二進制文件複製到 /usr/local/bin 路徑下

注意需要給予該二進制文件nobody用戶可執行權限

[root@master redis]# cd redis_exporter-v1.12.1.linux-amd64/
[root@master redis_exporter-v1.12.1.linux-amd64]# ls
LICENSE  README.md  redis_exporter
[root@master redis_exporter-v1.12.1.linux-amd64]# cp redis_exporter /usr/local/bin/
[root@master redis_exporter-v1.12.1.linux-amd64]# ls -l /usr/local/bin/redis_exporter
-rwxr-xr-x. 1 root root 8720384 Oct 22 19:32 /usr/local/bin/redis_exporter

創建systemd service文件

根據實際情況修改其中的redis.password配置值(redis實例認證密碼)

修改後直接複製下文全部文字命令並在節點上執行即可

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

[Service]
User=nobody
Group=nobody
Type=simple
ExecStart=/usr/local/bin/redis_exporter\\
          -redis.password "dangerous"\\
          -web.listen-address=:9121

[Install]
WantedBy=multi-user.target
EOF

重載系統 systemd 配置

執行命令 systemctl daemon-reload

啟動服務並且設置服務自啟

執行命令 systemctl enable --now redis_exporter

[root@master redis_exporter-v1.12.1.linux-amd64]# systemctl enable --now redis_exporter
Created symlink from /etc/systemd/system/multi-user.target.wants/redis_exporter.service to /etc/systemd/system/redis_exporter.service.

查看服務運行狀態

執行命令systemctl status redis_exporter

能夠得到類似下文的回顯結果

主要看Active屬性值為active(running)

以及日誌中提示Providing metrics at :9121/metrics就好了

[root@master redis_exporter-v1.12.1.linux-amd64]# systemctl status redis_exporter
● redis_exporter.service - redis_exporter
   Loaded: loaded (/etc/systemd/system/redis_exporter.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2020-10-23 11:19:35 CST; 8s ago
 Main PID: 2716 (redis_exporter)
    Tasks: 7
   Memory: 1.5M
   CGroup: /system.slice/redis_exporter.service
           └─2716 /usr/local/bin/redis_exporter -redis.password dangerous -web.listen-address=:9121

Oct 23 11:19:35 master systemd[1]: Started redis_exporter.
Oct 23 11:19:35 master redis_exporter[2716]: time="2020-10-23T11:19:35+08:00" level=info msg="Redis Metrics Exporter v1.12.1    build date: 2020-10-16-02:16:15    sha1: 7d742d9fbdabb8758ad172...GOARCH: amd64"
Oct 23 11:19:35 master redis_exporter[2716]: time="2020-10-23T11:19:35+08:00" level=info msg="Providing metrics at :9121/metrics"
Hint: Some lines were ellipsized, use -l to show in full.

測試接口

在節點上使用命令curl -s {{節點IP}}:9121/metrics | grep redis_up訪問接口

能夠得到類似下文結果(redis_up 值非0)

curl  -s 192.168.150.166:9121/metrics | grep redis_up
# HELP redis_up Information about the Redis instance
# TYPE redis_up gauge
redis_up 1
# HELP redis_uptime_in_seconds uptime_in_seconds metric
# TYPE redis_uptime_in_seconds gauge
redis_uptime_in_seconds 54555

或直接使用瀏覽器訪問{{節點IP}}:9121/metrics能夠看到相關 redis 實例指標信息則為正常