(一)Prometheus監控--安裝和配置

(一)、概述
一、什麼是prometheus
Prometheus是由SoundCloud開發的開源監控報警系統和時序列數據庫(TSDB)。Prometheus使用Go語言開發,是Google BorgMon監控系統的開源版本。
2016年由Google發起Linux基金會旗下的原生雲基金會(Cloud Native Computing Foundation), 將Prometheus歸入其下第二大開源項目。
Prometheus目前在開源社區至關活躍。
Prometheus和Heapster(Heapster是K8S的一個子項目,用於獲取集羣的性能數據。)相比功能更完善、更全面。Prometheus性能也足夠支撐上萬臺規模的集羣。
二、Prometheus的特色node

  • 多維度數據模型。
  • 靈活的查詢語言。
  • 不依賴分佈式存儲,單個服務器節點是自主的。
  • 經過基於HTTP的pull方式採集時序數據。
  • 能夠經過中間網關進行時序列數據推送。
  • 經過服務發現或者靜態配置來發現目標服務對象。
  • 支持多種多樣的圖表和界面展現,好比Grafana等。
    三、基本組件
    Prometheus生態包含多個組件,其中許多的組件都是可選的.
  • Prometheus Server: 用於收集和存儲時間序列數據。
  • Client Library: 客戶端庫,爲須要監控的服務生成相應的 metrics 並暴露給 Prometheus server。當 Prometheus server 來 pull 時,直接返回實時狀態的 metrics。
  • Push Gateway: 主要用於短時間的 jobs。因爲這類 jobs 存在時間較短,可能在 Prometheus 來 pull 以前就消失了。爲此,此次 jobs 能夠直接向 Prometheus server 端推送它們的 metrics。這種方式主要用於服務層面的 metrics,對於機器層面的 metrices,須要使用 node exporter。
  • Exporters: 用於暴露已有的第三方服務的 metrics 給 Prometheus。
  • Alertmanager: 從 Prometheus server 端接收到 alerts 後,會進行去除重複數據,分組,並路由到對收的接受方式,發出報警。常見的接收方式有:電子郵件,pagerduty,OpsGenie, webhook 等。
    四、基本原理
    Prometheus的基本原理是經過HTTP協議週期性抓取被監控組件的狀態,任意組件只要提供對應的HTTP接口就能夠接入監控。不須要任何SDK或者其餘的集成過程。這樣作很是適合作虛擬化環境監控系統,好比VM、Docker、Kubernetes等。輸出被監控組件信息的HTTP接口被叫作exporter 。目前互聯網公司經常使用的組件大部分都有exporter能夠直接使用,好比Varnish、Haproxy、Nginx、MySQL、Linux系統信息(包括磁盤、內存、CPU、網絡等等)。
    五、架構圖
    (一)Prometheus監控--安裝和配置
    六、服務過程
    服務過程
  • Prometheus Daemon負責定時去目標上抓取metrics(指標)數據,每一個抓取目標須要暴露一個http服務的接口給它定時抓取。Prometheus支持經過配置文件、文本文件、Zookeeper、Consul、DNS SRV Lookup等方式指定抓取目標。Prometheus採用PULL的方式進行監控,即服務器能夠直接經過目標PULL數據或者間接地經過中間網關來Push數據。
  • Prometheus在本地存儲抓取的全部數據,並經過必定規則進行清理和整理數據,並把獲得的結果存儲到新的時間序列中。
  • Prometheus經過PromQL和其餘API可視化地展現收集的數據。Prometheus支持不少方式的圖表可視化,例如Grafana、自帶的Promdash以及自身提供的模版引擎等等。Prometheus還提供HTTP API的查詢方式,自定義所須要的輸出。
  • PushGateway支持Client主動推送metrics到PushGateway,而Prometheus只是定時去Gateway上抓取數據。
  • Alertmanager是獨立於Prometheus的一個組件,能夠支持Prometheus的查詢語句,提供十分靈活的報警方式。

(二)、安裝配置
1、Prometheust Server端安裝和相關配置
1.一、二進制包安裝python

1.一、官網地址https://prometheus.io/download/
1.二、下載和安裝
wget https://github.com/prometheus/prometheus/releases/download/v2.22.0-rc.0/prometheus-2.22.0-rc.0.linux-386.tar.gz
tar xf prometheus-2.22.0-rc.0.linux-386.tar.gz 
mv prometheus-2.22.0-rc.0.linux-386/prometheus /usr/local/prometheus
1.三、配置系統啓動文件
 cat  /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus Server
After=network.target
Documentation=https://prometheus.io/docs/introduction/overview/

[Service]
Type=simple
WorkingDirectory=/data/prometheus/
ExecStart=/usr/local/prometheus/prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --web.read-timeout=5m \
  --web.max-connections=512 \
  --storage.tsdb.retention=15d \
  --storage.tsdb.path=/data/prometheus \
  --query.timeout=2m

Restart=on-failure

[Install]
WantedBy=multi-user.target

1.四、把配置文件轉移到標準目錄/etc/prometheus/
mkdir -p /etc/prometheus/
cp /usr/local/prometheus/prometheus.yml /etc/prometheus/

cat /etc/prometheus/prometheus.yml
global:
  scrape_interval:     15s 
  evaluation_interval: 15s

alerting:
  alertmanagers:
  - static_configs:
    - targets:

rule_files:

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']
1.四、啓動
systemctl daemon-reload
systemctl start prometheus
systemctl enable prometheus
systemctl status prometheus
1.五、查看經過 ip:9090進行查看。二進制安裝很是方便,沒有依賴,自動查詢的web界面。配置了9090端口,默認prometheus會抓取本身的/metrics接口

1.二、docker安裝linux

一、安裝
docker run \
    -p 9090:9090 \
    -v /etc/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
    prom/prometheus

二、查看prometheus服務和狀態
docker start prometheus
docker stop prometheus
docker stats prometheus

2、安裝node_exporter提供metricsgit

一、下載、解壓和移動
wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
tar xf node_exporter-1.0.1.linux-amd64.tar.gz 
mv node_exporter-1.0.1.linux-amd64/node_exporter /usr/local/prometheus/
二、配置啓動文件
cat /etc/systemd/system/node_export.service
[Unit]
Description=Node Export
After=network.target
Documentation=https://prometheus.io/docs/guides/node-exporter/

[Service]
Type=simple
WorkingDirectory=/tmp/
ExecStart=/usr/local/prometheus/node_exporter 

Restart=on-failure

[Install]
WantedBy=multi-user.target

三、自啓動
[root@ES-Master2 opt]# systemctl start node_export
[root@ES-Master2 opt]# systemctl enable node_export
Created symlink from /etc/systemd/system/multi-user.target.wants/node_export.service to /etc/systemd/system/node_export.service.
[root@ES-Master2 opt]# systemctl status node_export

四、加入監控
手動加入 prometheus 監控,修改其配置文件,再尾部增長以下內容:
  - job_name: elasticsearch
    scrape_interval: 60s
    scrape_timeout:  30s
    metrics_path: "/metrics"
    static_configs:
    - targets: [172.20.3.201:9100]
      labels:
        service: elasticsearch
而後從新載入配置curl -X POST http://localhost:9090/-/reload

五、查看生效的接口
(一)Prometheus監控--安裝和配置github

六、docker安裝更簡單web

docker run -d \
  --net="host" \
  --pid="host" \
  -v "/:/host:ro,rslave" \
  quay.io/prometheus/node-exporter \
  --path.rootfs=/host

3、安裝grafana進行展現
Grafana是用於可視化大型測量數據的開源程序,它提供了強大和優雅的方式去建立、共享、瀏覽數據。
Dashboard中顯示了你不一樣metric數據源中的數據。
Grafana最經常使用於因特網基礎設施和應用分析,但在其餘領域也有用到,好比:工業傳感器、家庭自動化、過程控制等等。
Grafana支持熱插拔控制面板和可擴展的數據源,目前已經支持Graphite、InfluxDB、OpenTSDB、Elasticsearch、Prometheus等。docker

一、下載和安裝
一、下載地址 https://grafana.com/grafana/download
二、Red Hat, CentOS, RHEL, and Fedora(64 Bit)
wget https://dl.grafana.com/oss/release/grafana-7.2.1-1.x86_64.rpm
sudo yum install grafana-7.2.1-1.x86_64.rpm -y
三、自啓動
systemctl enable grafana-server
systemctl start grafana-server
四、訪問ip:3000端口,默認帳號和密碼都是admin:admin
二、配置數據源。設置-配置-數據源-選擇prometheus

(一)Prometheus監控--安裝和配置
(一)Prometheus監控--安裝和配置

三、下載相應的模板https://grafana.com/grafana/dashboards
四、導入模板。+--import,把下載的模板導進去

(一)Prometheus監控--安裝和配置

相關文章
相關標籤/搜索