重慶分公司,新征程啟航
為企業提供網站建設、域名注冊、服務器等服務
為企業提供網站建設、域名注冊、服務器等服務
本篇文章為大家展示了如何入門Prometheus,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
成都創新互聯服務項目包括射陽網站建設、射陽網站制作、射陽網頁制作以及射陽網絡營銷策劃等。多年來,我們專注于互聯網行業,利用自身積累的技術優勢、行業經驗、深度合作伙伴關系等,向廣大中小型企業、政府機構等提供互聯網行業的解決方案,射陽網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到射陽省份的部分城市,未來相信會繼續擴大服務區域并繼續獲得客戶的支持與信任!
1、Prometheus簡介
Prometheus最開始是由SoundCloud
公司開源的一款監控
軟件,自從2012年成立
以來許多的公司和組織都在使用Prometheus
官方GitHub地址為:https://github.com/prometheus/prometheus
官方地址:https://prometheus.io/
2、Prometheus特點
一個多維數據模型,其中包含通過度量標準名稱和key/value
對標識的時間序列數據
PromQL
,一種靈活的查詢語言
,可以利用此維度
不依賴分布式存儲,單個服務器節點可以直接工作
基于HTTP的pull方式采集時間序列
推送時間序列
通過PushGateway
組件支持
通過服務發現
或靜態配置
發現目標
多種圖形和儀表板支持模式(grafana)
組件
Prometheus Server,主要用于抓取數據和存儲時序數據,另 外還提供查詢和 Alert Rule 配置管理。
Alertmanager,主要是負責實現報警功能。
Push Gateway ,用于批量,短期的監控數據的匯總節點,實現接收由Client push過來的各種指標數據,在指定的時間間隔,由主程序訪問抓取。
*_exporter,各種匯報數據的。
基礎架構
從這個架構圖,也可以看出 Prometheus 的主要模塊包含, Server, Exporters, Pushgateway, PromQL, Alertmanager, WebUI 等。
它大致使用邏輯是這樣:
Prometheus server 定期從靜態配置的targets
或者服務發現
的targets
拉取數據。
當新拉取的數據大于配置內存緩存區的時候,Prometheus會將數據持久化到磁盤
(如果使用Remote Storage將持久化到云端)。
Prometheus可以配置rules,然后定時查詢數據,當條件觸發的時候,會將alert 推送到配置的Alertmanager。
Alertmanager收到警告的時候,可以根據配置,聚合,去重,降噪,最后發送警告。
可以使用API
, Prometheus Console 或者Grafana查詢和聚合數據。
Prometheus vs Zabbix
Zabbix 使用的是 C 和 PHP, Prometheus 使用 Golang, 整體而言 Prometheus 運行速度更快一點
Zabbix 屬于傳統主機監控,主要用于物理主機,交換機,網絡等 監控,Prometheus 不僅適用主機監控,還適用于 Cloud, SaaS, Openstack,Container 監控
Zabbix 在傳統主機監控方面,有更豐富的插件
Zabbix 可以在 WebGui 中配置很多事情,但是 Prometheus 需要手動修改文件配置
3、Prometheus部署
1.下載安裝包prometheus-2.4.0.linux-amd64.tar.gz
github下載地址:https://github.com/prometheus/prometheus/releases/tag/v2.4.0
2.解壓
$ tar xf prometheus-2.14.0.linux-amd64.tar.gz -C /usr/local/ $ ln -s /usr/local/prometheus-2.14.0.linux-amd64/ /usr/local/prometheus
創建system
$ vim /etc/systemd/system/prometheus.service [root@linux-node1 ~]# vim /etc/systemd/system/prometheus.service [Unit] Description=Prometheus Wants=network-online.target After=network-online.target [Service] User=root Group=root Type=simple ExecStart=/usr/local/prometheus/prometheus \ --config.file=/usr/local/prometheus/prometheus.yml [Install] WantedBy=multi-user.target
3.配置prometheus.yml (保持默認配置)
$ vim /usr/local/prometheus/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=` to any timeseries scraped from this config. - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9090']
4.啟動
$ systemctl daemon-reload $ systemctl start prometheus.service $ systemctl enable prometheus.service // Prometheus Server端啟動默認端口 - 9090 $ netstat -lntup | grep "9090" tcp6 0 0 :::9090 :::* LISTEN 3393/./prometheus
5.訪問http://ip:9090/
Prometheus自帶的監控界面
4、Grafana部署Grafana
是一款采用go
語言編寫的開源應用,主要用于大規模指標數據
的可視化展現
,是網絡架構和應用分析中最流行的時序數據展示工具
,目前已經支持絕大部分常用的時序數據庫
。
Grafana
支持許多不同的數據源
。每個數據源都有一個特定的查詢編輯器
,該編輯器定制的特性和功能是公開的特定數據來源。 官方支持以下數據源:Graphite,Elasticsearch,InfluxDB,Prometheus,Cloudwatch,MySQL和OpenTSDB等。
Grafana文檔:https://grafana.com/docs/grafana/latest/installation/rpm/
1.安裝Grafana
這里使用rpm包安裝
$ wget https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/grafana-6.3.7-1.x86_64.rpm $ rpm -i --nodeps grafana-6.3.7-1.x86_64.rpm
2.啟動Grafana,并加入開機自啟
$ systemctl start grafana-server.service $ systemctl enable grafana-server.service // Grafana 啟動默認端口 - 3000 $ netstat -lntup | grep "3000" tcp6 0 0 :::3000 :::* LISTEN 3585/grafana-server
3.訪問grafana
瀏覽器訪問IP:3000
端口,即可打開grafana頁面,默認用戶名密碼都是admin,初次登錄會要求修改默認的登錄密碼
4.添加數據源
(1)點擊主界面的“Add data source”
(2)選擇Prometheus
(3)Dashboards頁面選擇Prometheus 2.0 Stats
(4)Settings頁面填寫Prometheus地址并保存
(5)切換到我們剛才添加的Prometheus 2.0 Stats
即可看到整個監控頁面
那么現在Prometheus是沒有任何監控數據的,
5、Node-Exporter部署
需要監控服務器CPU、內存、磁盤、I/O等信息,首先需要安裝node_exporter
。node_exporter
的作用是用于機器系統數據收集。
下載地址:
https://github.com/prometheus/node_exporter/releases/
https://prometheus.io/download/
1.安裝node_exporter
$ tar xf node_exporter-0.18.1.linux-amd64.tar.gz -C /usr/local/ $ ln -s /usr/local/node_exporter-0.18.1.linux-amd64/ /usr/local/node_exporter
創建system服務
$ vim /etc/systemd/system/node_exporter.service [Unit] Description=node_exporter After=network.target [Service] Type=simple User=root ExecStart=/usr/local/node_exporter/node_exporter Restart=on-failure [Install] WantedBy=multi-user.target
2.啟動node-exporter
$ systemctl daemon-reload $ systemctl start node_exporter.service $ systemctl enable node_exporter.service // Node Exporter默認端口 - 9100 $ netstat -lntup | grep "9100" tcp6 0 0 :::9100 :::* LISTEN 5122/node_exporter
3.Node Exporter默認的抓取地址為http://IP:9100/metrics
$ curl http://127.0.0.1:9100/metrics
4.Prometheus配置文件添加被監控機器
// 默認node-exporter端口為9100 $ vim /usr/local/prometheus/prometheus.yml - job_name: 'prometheus' static_configs: - targets: ['localhost:9100'] - job_name: 'node_exporter_centos' scrape_interval: 5s static_configs: - targets: ['10.0.0.171:9100']
prometheus.yml中一共定義了兩個監控:一個是監控prometheus自身服務,另一個是監控Linux服務器
5.重啟prometheus
服務
$ systemctl restart prometheus
6.Grafana
安裝Node Exporter
的dashboards
下載地址:https://grafana.com/grafana/dashboards/11074/revisions
(1)將下載好的.json結尾的文件在Grafana中導入,并選擇對應的數據源
(2)通過Node Exporter
的dashboards就可以看到很多圖表
上述內容就是如何入門Prometheus,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創新互聯行業資訊頻道。