Prometheus安裝(采用 influxdb)


一、安裝Prometheus

# 獲取軟件
$ wget https://github.com/prometheus/prometheus/releases/download/v2.20.1/prometheus-2.20.1.linux-amd64.tar.gz
$ groupadd prometheus
$ useradd -g prometheus -m -d /usr/local/prometheus -s /sbin/nologin prometheus
$ tar zxf prometheus-2.24.1.linux-amd64.tar.gz
$ mv prometheus-2.24.1.linux-amd64/* /usr/local/prometheus
$ sed -i 's/localhost/10.4.7.100/g' /usr/local/prometheus/prometheus.yml 

# 創建systemd服務
$ cat <<EOF > /usr/lib/systemd/system/prometheus.service
[Unit]
Description=prometheus
After=network.target
[Service]
Type=simple
User=prometheus
WorkingDirectory=/usr/local/prometheus          # 如果添加這一行,可以不指定配置文件(--config.file=/usr/local/prometheus/prometheus.yml)
ExecStart=/usr/local/prometheus/prometheus   --web.enable-lifecycle 
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
# --web.enable-lifecycle: 支持通過http請求重載配置
# 啟動服務
$ systemctl daemon-reload
$ systemctl start prometheus
$ systemctl status prometheus && systemctl enable prometheus

# 確認端口已經被監聽
$ ss -lnput | grep 9090
tcp    LISTEN     0      1024     :::9090                 :::*                   users:(("prometheus",pid=8257,fd=7))

二、后端存儲配置

默認情況下prometheus會將采集的數據防止到本機的data目錄的, 存儲數據的大小受限和擴展不便,這是使用influxdb作為后端的數據庫來存儲數據。

三、 influxdb安裝配置

influxdb的官方文檔地址為: https://docs.influxdata.com/influxdb/v1.7/introduction/downloading/ 根據不同系統進行下載,這里使用官方提供的rpm進行安裝。

# 獲取軟件包
$ wget https://dl.influxdata.com/influxdb/releases/influxdb-1.7.8.x86_64.rpm
# 本地安裝
$ yum localinstall influxdb-1.7.8.x86_64.rpm

# 備份默認的默認的配置文件,這里可以對influxdb的數據存放位置做些設置
$ cp /etc/influxdb/influxdb.conf  /etc/influxdb/influxdb.conf.default

# 啟動
$ systemctl start influxdb && systemctl enable influxdb
# 查看狀態
$ systemctl status influxdb
# 查看端口是否監聽
$ ss -lnput | grep 8086
tcp    LISTEN     0      1024     :::8086                 :::*                   users:(("influxd",pid=9405,fd=5))

創建一個Prometheus數據庫

$ influx                                     # 登錄influxdb數據庫
> create database prometheus;                # 創建prometheus數據庫
> show databases;                            # 查看數據庫
name: databases 
name
----
_internal
prometheus                        
> exit                                          # 退出

四、 配置prometheus集成infludb

官方的幫助文檔在這里: https://docs.influxdata.com/influxdb/v1.7/supported_protocols/prometheus/

$ vim /usr/local/prometheus/prometheus.yml
# 添加如下幾行
remote_write:
  - url: "http://10.4.7.100:8086/api/v1/prom/write?db=prometheus"

remote_read:
  - url: "http://10.4.7.100:8086/api/v1/prom/read?db=prometheus"

$ systemctl restart prometheus 
$ systemctl status prometheus

注意: 如果influxdb配置有密碼, 請參考上面的官方文檔地址進行配置。

五、測試數據是否存儲到influxdb中

$ influx
> show databases;
name: databases
name
----
_internal
prometheus
> use prometheus
Using database prometheus
> show measures;
ERR: error parsing query: found measures, expected CONTINUOUS, DATABASES, DIAGNOSTICS, FIELD, GRANTS, MEASUREMENT, MEASUREMENTS, QUERIES, RETENTION, SERIES, SHARD, SHARDS, STATS, SUBSCRIPTIONS, TAG, USERS at line 1, char 6
> show MEASUREMENTS;
name: measurements
name
----
go_gc_duration_seconds
go_gc_duration_seconds_count
go_gc_duration_seconds_sum
go_goroutines
go_info
go_memstats_alloc_bytes
# 后面還是有很多,這里不粘貼了。

# 做個簡單查詢
> select * from prometheus_http_requests_total limit 10 ; 
name: prometheus_http_requests_total
time                __name__                       code handler  instance       job        value
----                --------                       ---- -------  --------       ---        -----
1568975686217000000 prometheus_http_requests_total 200  /metrics localhost:9090 prometheus 1
1568975701216000000 prometheus_http_requests_total 200  /metrics localhost:9090 prometheus 2
1568975716218000000 prometheus_http_requests_total 200  /metrics localhost:9090 prometheus 3
1568975731217000000 prometheus_http_requests_total 200  /metrics localhost:9090 prometheus 4
1568975746216000000 prometheus_http_requests_total 200  /metrics localhost:9090 prometheus 5
1568975761217000000 prometheus_http_requests_total 200  /metrics localhost:9090 prometheus 6
1568975776217000000 prometheus_http_requests_total 200  /metrics localhost:9090 prometheus 7
1568975791217000000 prometheus_http_requests_total 200  /metrics localhost:9090 prometheus 8
1568975806217000000 prometheus_http_requests_total 200  /metrics localhost:9090 prometheus 9
1568975821216000000 prometheus_http_requests_total 200  /metrics localhost:9090 prometheus 10


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM