Prometheus是一套開源的監控&報警&時間序列數據庫的組合,起始是由SoundCloud公司開發的。隨着發展,愈來愈多公司和組織接受採用Prometheus,社區也十分活躍,他們便將它獨立成開源項目,而且有公司來運做。google SRE的書內也曾提到跟他們BorgMon監控系統類似的實現是Prometheus。如今最多見的Kubernetes容器管理系統中,一般會搭配Prometheus進行監控。相似的產品還有influxdb, 若是用於監控用途是一般會配合grafana進行數據展現和報警。node
更詳細的說明和幫助,能夠參考prometheus官網: https://prometheus.io/mysql
如官網描述的同樣,prometheus主要是在當咱們須要單純記錄以時間進行分片的數據時使用,不管是系統級別的監控,或是咱們的高度動態的服務架構。針對微服務場景,prometheus也能很好的支持多爲數據的收集和查詢。linux
固然,prometheus也有侷限性,若是咱們的監控或系統,須要百分百的準確性就不太適合。git
須要瞭解更多關於prometheus的知識,強烈建議好好閱讀官網的文檔。github
本文的重點是如何在CentOS 7中安裝和配置Prometheus,咱們要按生產級的要求來部署Prometheus。如下就開始咱們的部署工做, 按步驟進行操做。web
以root或具備sudo權限的用戶登陸CentOS系統,執行如下命令升級系統(非必要):sql
yum update -y
vim /etc/sysconfig/selinux
將SELINUX=enforcing
改成ELINUX=disabled
, 而後重啓系統reboot
。shell
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
cd /data/software/ wget https://github.com/prometheus/prometheus/releases/download/v2.11.1/prometheus-2.11.1.linux-amd64.tar.gz tar -zxvf prometheus-2.11.1.linux-amd64.tar.gz
useradd --no-create-home --shell /bin/false prometheus
mkdir /etc/prometheus mkdir /var/lib/prometheus
chown prometheus:prometheus /etc/prometheus chown prometheus:prometheus /var/lib/prometheus
cd /data/software/prometheus-2.11.1.linux-amd64 cp prometheus /usr/local/bin/ cp promtool /usr/local/bin/ # 設置權限(更改全部者) chown prometheus:prometheus /usr/local/bin/prometheus chown prometheus:prometheus /usr/local/bin/promtool cp -r consoles /etc/prometheus cp -r console_libraries /etc/prometheus chown -R prometheus:prometheus /etc/prometheus/consoles chown -R prometheus:prometheus /etc/prometheus/console_libraries
mkdir /etc/prometheus/ cp prometheus.xml /etc/prometheus/ or vim /etc/systemd/system/prometheus.service chown prometheus:prometheus /etc/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=<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']
vi /etc/systemd/system/prometheus.service
[Unit] Description=Prometheus Wants=network-online.target After=network-online.target [Service] User=prometheus Group=prometheus Type=simple ExecStart=/usr/local/bin/prometheus \ --config.file /etc/prometheus/prometheus.yml \ --storage.tsdb.path /var/lib/prometheus/ \ --web.console.templates=/etc/prometheus/consoles \ --web.console.libraries=/etc/prometheus/console_libraries [Install] WantedBy=multi-user.target
# reload服務配置 systemctl daemon-reload # 啓動服務 systemctl start prometheus # 查看服務狀態 systemctl status prometheus
若是一切正常應該能夠看到以下輸出, 說明服務以及正常啓動。vim
[root@ ~]# systemctl status prometheus ● prometheus.service - Prometheus Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; vendor prese t: disabled) Active: active (running) since Mon 2019-07-15 17:43:53 CST; 1h 34min ago Main PID: 22807 (prometheus) CGroup: /system.slice/prometheus.service └─22807 /usr/local/bin/prometheus --config.file /etc/prometheus/pr... Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.660Z...)" Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.660Z...)" Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.661Z...)" Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.663Z...." Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.674Z...IC Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.674Z...d" Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.674Z...ml Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.695Z...90 Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.698Z...ml Jul 15 17:43:53 . prometheus[22807]: level=info ts=2019-07-15T09:43:53.698Z...." Hint: Some lines were ellipsized, use -l to show in full.
systemctl enable prometheus
也能夠試一下重啓系統,看服務會不會自動開啓。
由於咱們的配置文件中配置的端口爲9090
, 因此若是你的系統有設置防火牆,這須要將這個端口開放出來。
firewall-cmd --zone=public --add-port=9090/tcp --permanent # 重啓防火牆服務 systemctl reload firewalld
由於我是在虛擬機裏安裝的,直接在宿主機瀏覽器中輸入: http://192.168.56.2:9090
,若是出現如下頁面這表示服務正常開啓。
exporter是prometheus用來收集數據的客戶端工具,在prometheus的官網上https://prometheus.io/download/ 能夠下載到不少服務的exporter採集器。如下是咱們常常用到的一些採集器:
Name | Description | URL |
---|---|---|
Node Exporter | 針對服務器進行監控的探測器(探針) | https://github.com/prometheus/node_exporter |
Alert Manager | 報警相關 | https://github.com/prometheus/alertmanager |
Mysql Exporter | MySQL服務監控相關的探測器 | https://github.com/prometheus/mysqld_exporter |
Blackbox Exporter | 黑盒監控解決方案,其容許用戶經過:HTTP、HTTPS、DNS、TCP以及ICMP的方式對網絡進行探測 | https://github.com/prometheus/blackbox_exporter |
jmx_exporter | JMX服務的探測器 | https://github.com/prometheus/jmx_exporter |
具體的能夠到咱們使用時再行在https://prometheus.io/download/或github中尋找須要的插件進行安裝配置,這裏咱們就簡單介紹幾個經常使用的插件的安裝和使用。
node_exporter是一個服務器端agent,負責採集服務器基礎監控項。下面咱們就來一步一步安裝node_exporter收集所在服務器的性能。
wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz tar -zvxf node_exporter-0.18.1.linux-amd64.tar.gz
useradd -rs /bin/false nodeusr
cp node_exporter-0.18.1.linux-amd64/node_exporter /usr/local/bin/
vim /etc/systemd/system/node_exporter.service
內容以下:
[Unit] Description=Node Exporter After=network.target [Service] User=nodeusr Group=nodeusr Type=simple ExecStart=/usr/local/bin/node_exporter [Install] WantedBy=multi-user.target
systemctl daemon-reload systemctl start node_exporter
查看服務狀態,若是正常的話會顯示以下:
[root@ software]# systemctl status node_exporter ● node_exporter.service - Node Exporter Loaded: loaded (/etc/systemd/system/node_exporter.service; disabled; vendor preset: disabled) Active: active (running) since Mon 2019-07-15 20:10:48 CST; 20min ago Main PID: 30530 (node_exporter) CGroup: /system.slice/node_exporter.service └─30530 /usr/local/bin/node_exporter Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - sockstat" source="node_exporter.go:104" Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - stat" source="node_exporter.go:104" Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - textfile" source="node_exporter.go:104" Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - time" source="node_exporter.go:104" Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - timex" source="node_exporter.go:104" Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - uname" source="node_exporter.go:104" Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - vmstat" source="node_exporter.go:104" Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - xfs" source="node_exporter.go:104" Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg=" - zfs" source="node_exporter.go:104" Jul 15 20:10:48 . node_exporter[30530]: time="2019-07-15T20:10:48+08:00" level=info msg="Listening on :9100" source="node_exporter.go:170"
systemctl enable node_exporter
若是防火牆服務有開啓的話,就須要開通端口: 9100
firewall-cmd --zone=public --add-port=9100/tcp --permanent systemctl restart firewalld
在宿主機瀏覽器輸入http://192.168.56.2:9100/metrics
vim /etc/prometheus/prometheus.yml
在prometheus.xml中的scrape配置項中加入如下內容:
- job_name: 'node_exporter_centos' scrape_interval: 5s static_configs: - targets: ['192.168.56.2:9100']
最終的配置文件以下:
而後從新啓動prometheus服務。
systemctl restart prometheus
在宿主機的瀏覽器中輸入prometheus的地址, http://192.168.56.2:9090/graph
export VERSION=0.18.0 curl -LO https://github.com/prometheus/alertmanager/releases/download/v$VERSION/alertmanager-$VERSION.darwin-amd64.tar.gz tar xvf alertmanager-$VERSION.darwin-amd64.tar.gz