linux下Prometheus+Grafana搭建

官方下載地址:https://prometheus.io/download/
在這裏插入圖片描述
1、下載Prometheus的tar包

wget https://github.com/prometheus/prometheus/releases/download/v2.17.1/prometheus-2.17.1.linux-amd64.tar.gz
tar -C /usr/local/ -xvf prometheus-2.17.1.linux-amd64.tar.gz  #解壓
ln -sv /usr/local/prometheus-2.17.1.linux-amd64/ /usr/local/prometheus   #創造軟連接
/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml & #啓動Prometheus

2、測試

ss -anpt|grep 9090

在這裏插入圖片描述
http://服務IP:9090
在這裏插入圖片描述
3、配置prometheus.yml的介紹

# my global config
global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
 - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']
  • global:全局配置,其中scrape_interval表示抓取一次數據的間隔時間,
    evaluation_interval表示進行告警規則檢測的間隔時間;

  • alerting:告警管理器(Alertmanager)的配置,目前還沒有安裝Alertmanager;

  • rule_files:告警規則有哪些;

  • scrape_configs:抓取監控信息的目標。一個job_name就是一個目標,其targets就是採集信息的IP和端口。這裏默認監控了Prometheus自己,可以通過修改這裏來修改Prometheus的監控端口。Prometheus的每個exporter都會是一個目標,它們可以上報不同的監控信息,比如機器狀態,或者mysql性能等等,不同語言sdk也會是一個目標,它們會上報你自定義的業務監控信息。

4、添加機器狀態監控
我們嘗試添加第一個監控exporter——監控當前機器自身的狀態,包括硬盤、CPU、流量等。因爲Prometheus已經有了很多現成的常用exporter,所以我們直接用其中的node_exporter。注意了,這裏名字雖然叫node_exporter,但跟nodejs沒有任何關係,在Prometheus看來,一臺機器或者說一個節點就是一個node,所以該exporter是在上報當前節點的狀態。

node_exporter本身也是一個http服務,可以供prometheus server調用(pull)來獲取監控的信息,安裝方法同樣是下載安裝包後解壓直接運行:

#下載最新版本,可以在github的release中對最新版本右鍵獲取下載鏈接 
wget https://github.com/prometheus/node_exporter/releases/download/v1.0.0-rc.0/node_exporter-1.0.0-rc.0.linux-amd64.tar.gz
#解壓 
tar xvf node_exporter-1.0.0-rc.0.linux-amd64.tar.gz -C /usr/local/ 
#進入解壓出的目錄 
cd /usr/local/node_exporter-1.0.0-rc.0.linux-amd64/ 
#運行監控採集服務 
nohup ./node_exporter &

運行後可以看到在監聽9100端口。這樣就可以採集了,現在先訪問試試能不能有沒有成功運行:

curl http://localhost:9100/metrics

在這裏插入圖片描述
這裏也可以看出其實每個exporter本身都是一個http服務,server端會定時來訪問獲取監控信息。
訪問成功的話,我們去prometheus的配置文件(prometheus.yml)中,加上這個target:

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
  - job_name: 'server'   #主要是以下三行
    static_configs:
      - targets: ['localhost:9100']

可以看到,就是在scrape_configs模塊中加一個job,命好名,配置好監聽的IP和端口即可,然後重新運行prometheus,在標籤欄的 Status --> Targets 中可以看到多了一個:
在這裏插入圖片描述

5、安裝Grafana
Grafana是一個可視化面板(Dashboard),有着非常漂亮的圖表和佈局展示,功能齊全的度量儀表盤和圖形編輯器,支持Graphite、zabbix、InfluxDB、Prometheus等數據源。

5.1 下載並安裝

下載地址:https://grafana.com/grafana/download
在這裏插入圖片描述
運行如下腳本

wget https://dl.grafana.com/oss/release/grafana-6.7.2-1.x86_64.rpm
sudo yum install grafana-6.7.2-1.x86_64.rpm

5.2 安裝完成後,grafana服務默認已安裝,配置文件爲/etc/grafana/grafana.ini,如需修改路徑及端口,可在該文件中修改
在這裏插入圖片描述
啓動grafana
/etc/init.d/grafana-server start
5.3 登錄grafana
在這裏插入圖片描述
5.4 導入數據源
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
5.5 添加監控模板
還記得我們上面還運行了一個node exporter吧,現在我們展現一下這個監控信息,左邊豎排點擊加號圖標中的Import,來導入其他別人寫好的面板。在Grafana的官方面板頁面其實可以看到很多別人配置好的面板,我們找到自己想要的面板,比如這個node exporter的:
官方模板:https://grafana.com/grafana/dashboards?orderBy=name&direction=asc
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
複製右邊那個面板ID,然後在Import界面輸入ID,Load後配置好數據源爲我們的Prometheus,就可以出現我們自己機器的狀態監控面板了,很炫酷吧。
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
成功

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章