Prometheus + Grafana 部署說明之「安裝」
說明
在前面的Prometheus學習系列文章裡,大致介紹說明了Prometheus和Grafana的一些使用,現在開始介紹如何從頭開始部署Prometheus+Grafana,來監控各個相關的指標資料來進行展示。
部署
Prometheus基於Golang編寫(需要安裝),編譯後的軟體包,不依賴於任何的第三方依賴。使用者只需要下載對應平臺的二進位制包,解壓並且新增基本的配置即可正常啟動Prometheus Server。
環境:
系統: centos-release-7-4.1708.el7.centos.x86_64 IP:10.47.97.47 Prometheus: prometheus-2.14.0 Grafana: grafana-6.5.1-1
下載:
1:Prometheus wget https://github.com/prometheus/prometheus/releases/download/v2.14.0/prometheus-2.14.0.linux-amd64.tar.gz 2:Grafana: wget https://dl.grafana.com/oss/release/grafana-6.5.1-1.x86_64.rpm
安裝
1: Prometheus 安裝
- 新增使用者prometheus
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
- 解壓 & 移動到相應目錄
tar xvf prometheus-2.14.0.linux-amd64.tar.gz cp prometheus-2.14.0.linux-amd64/prometheus /usr/local/bin/ cp prometheus-2.14.0.linux-amd64/promtool /usr/local/bin/ cp -r prometheus-2.14.0.linux-amd64/consoles/ /etc/prometheus/ cp -r prometheus-2.14.0.linux-amd64/console_libraries/ /etc/prometheus/ cp prometheus-2.14.0.linux-amd64/prometheus.yml /etc/prometheus/ chown prometheus:prometheus /usr/local/bin/prometheus chown prometheus:prometheus /usr/local/bin/promtool chown -R prometheus:prometheus /etc/prometheus/consoles chown -R prometheus:prometheus /etc/prometheus/console_libraries chown -R prometheus:prometheus /etc/prometheus/prometheus.yml
- 配置Prometheus service,埠為9099
vim /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 \ --web.listen-address=:9099 [Install] WantedBy=multi-user.target
- 載入systemd服務 & 啟動
# 載入 systemctl daemon-reload
# 開機啟動
systemctl enable prometheus.service # 啟動 systemctl start prometheus # 檢視狀態 systemctl status prometheus - 檢視狀態:systemctl status prometheus
# systemctl status prometheus ● prometheus.service - Prometheus Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2019-12-09 18:35:00 CST; 1min 8s ago Main PID: 18875 (prometheus) CGroup: /system.slice/prometheus.service └─18875 /usr/local/bin/prometheus --config.file /etc/prometheus/prometheus.yml --storage.tsdb.path /var/lib/prometheus/ --web.console.templates=/etc/prometheus/consoles --web.console.librari... Dec 09 18:35:00 test26-mysql03 prometheus[18875]: level=info ts=2019-12-09T10:35:00.175Z caller=head.go:583 component=tsdb msg="WAL segment loaded" segment=4 maxSegment=8 Dec 09 18:35:00 test26-mysql03 prometheus[18875]: level=info ts=2019-12-09T10:35:00.175Z caller=head.go:583 component=tsdb msg="WAL segment loaded" segment=5 maxSegment=8 Dec 09 18:35:00 test26-mysql03 prometheus[18875]: level=info ts=2019-12-09T10:35:00.176Z caller=head.go:583 component=tsdb msg="WAL segment loaded" segment=6 maxSegment=8 Dec 09 18:35:00 test26-mysql03 prometheus[18875]: level=info ts=2019-12-09T10:35:00.178Z caller=head.go:583 component=tsdb msg="WAL segment loaded" segment=7 maxSegment=8 Dec 09 18:35:00 test26-mysql03 prometheus[18875]: level=info ts=2019-12-09T10:35:00.178Z caller=head.go:583 component=tsdb msg="WAL segment loaded" segment=8 maxSegment=8 Dec 09 18:35:00 test26-mysql03 prometheus[18875]: level=info ts=2019-12-09T10:35:00.180Z caller=main.go:672 fs_type=EXT4_SUPER_MAGIC Dec 09 18:35:00 test26-mysql03 prometheus[18875]: level=info ts=2019-12-09T10:35:00.180Z caller=main.go:673 msg="TSDB started" Dec 09 18:35:00 test26-mysql03 prometheus[18875]: level=info ts=2019-12-09T10:35:00.180Z caller=main.go:743 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml Dec 09 18:35:00 test26-mysql03 prometheus[18875]: level=info ts=2019-12-09T10:35:00.182Z caller=main.go:771 msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml Dec 09 18:35:00 test26-mysql03 prometheus[18875]: level=info ts=2019-12-09T10:35:00.182Z caller=main.go:626 msg="Server is ready to receive web requests."
View Code - 命令列啟動:
sudo -u prometheus /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 --web.listen-address=:9099
- 訪問URL:
http://10.47.97.47:9099/graph
- 配置檔案:
# 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:9099'] - job_name: 'node_exporter' scrape_interval: 1s static_configs: - targets: ['a:9199','b:9199','c:9199','d:9199'] relabel_configs: - action: replace source_labels: ['__address__'] ##源標籤 regex: (.*):(.*) ##正則,會匹配到__address__值 replacement: $1 ##引用正則匹配到的內容 target_label: HOSTNAME ##賦予新的標籤,名為HOSTNAME
之後可以訪問到Prometheus的Web介面。到此Prometheus的安裝結束。更多的引數可以看Prometheus學習系列(二)之Prometheus FIRST STEPS和Prometheus學習系列(四)之Prometheus 配置說明
2: Grafana 安裝
- 下載完rpm包之後,直接執行:
sudo yum localinstall grafana-6.5.1-1.x86_64.rpm
- 安裝完之後進行Grafana配置的修改:修改埠 vim /etc/grafana/grafana.ini ,關於配置說明請見:官方文件、Grafana配置和Grafana 配置檔案詳解
http_port = 3333
- 載入systemd服務 & 啟動
直接啟動 $ sudo service grafana-server start
#配置為在引導時啟動 $ sudo /sbin/chkconfig --add grafana-server #載入 $ systemctl daemon-reload
#開啟 $ systemctl start grafana-server
#檢視狀態 $ systemctl status grafana-server
#開啟啟動 sudo systemctl enable grafana-server.service - 日誌&資料目錄
日誌目錄: 配置為在引導時啟動 資料目錄: /var/lib/grafana/grafana.db
- 訪問URL:
http://10.47.97.47:3333
在1、2操作中,已經把Prometheus和Grafana安裝完畢,現在開始安裝Exporter來採集資料。
Exporter是Prometheus的一類資料採集元件的總稱。它負責從目標處蒐集資料,並將其轉化為Prometheus支援的格式。與傳統的資料採集元件不同的是,它並不向中央伺服器傳送資料,而是等待中央伺服器主動前來抓取,預設的抓取地址為http://CURRENT_IP:9199/metrics。3. Exporter 安裝
① node_exporter 安裝
node_exporter的作用是用於機器系統資料收集,監控伺服器CPU、記憶體、磁碟、I/O、網路等等資訊。
- 下載
wget https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter-0.18.1.linux-amd64.tar.gz
- 安裝
# 解壓 tar xvf node_exporter-0.18.1.linux-amd64.tar.gz cp node_exporter /usr/sbin/ #幫助 node_exporter --help #開啟,埠:9199 node_exporter --web.listen-address=:9199
- 載入systemd服務 & 啟動 vim /etc/systemd/system/node_exporter.service
[Unit] Description=node_exporter Documentation=https://prometheus.io/ After=network.target [Service] Type=simple User=prometheus ExecStart=/usr/sbin/node_exporter --web.listen-address=:9199 Restart=on-failure [Install] WantedBy=multi-user.target
systemctl 啟動
# 載入 systemctl daemon-reload # 開機啟動 systemctl enable node_exporter.service # 啟動 systemctl start node_exporter # 檢視狀態 systemctl status node_exporter
此時,node_exporter啟動後已經開始採集指標資訊,可以通過地址訪問:http://10.47.97.47:9199/metrics
② mysqld_exporter 安裝
mysqld_exporter的作用是用於MySQL資料庫資料收集。
- 下載
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
- 安裝
# 解壓 tar xvf mysqld_exporter-0.12.1.linux-amd64.tar.gz cp mysqld_exporter /usr/sbin/ #幫助 mysqld_exporter --help #連線被監控資料庫,資料庫資訊儲存在以下的任意一個地方 1:環境變數 export DATA_SOURCE_NAME='user:password@(hostname:3306)/' ./mysqld_exporter <flags> 2:配置檔案 vim ~/.my.cnf [client] host=10.27.27.27 port=5381 user=root password=root
- 啟動
可以放到systemctl裡啟動,也可以直接執行:
./mysqld_exporter INFO[0000] Starting mysqld_exporter (version=0.12.1, branch=HEAD, revision=48667bf7c3b438b5e93b259f3d17b70a7c9aff96) source="mysqld_exporter.go:257" INFO[0000] Build context (go=go1.12.7, user=root@0b3e56a7bc0a, date=20190729-12:35:58) source="mysqld_exporter.go:258" INFO[0000] Enabled scrapers: source="mysqld_exporter.go:269" INFO[0000] --collect.global_status source="mysqld_exporter.go:273" INFO[0000] --collect.global_variables source="mysqld_exporter.go:273" INFO[0000] --collect.slave_status source="mysqld_exporter.go:273" INFO[0000] --collect.info_schema.innodb_cmp source="mysqld_exporter.go:273" INFO[0000] --collect.info_schema.innodb_cmpmem source="mysqld_exporter.go:273" INFO[0000] --collect.info_schema.query_response_time source="mysqld_exporter.go:273" INFO[0000] Listening on :9104 source="mysqld_exporter.go:283"
其他的exporter可以見官網。
視覺化監控
上面已經安裝部署好了Prometheus、Grafana和Exporter,現在需要把這3者聯絡起來進行資料展示。如已經安裝了node_exporter,需要把node_exporter採集的資訊配置到Prometheus上。即修改Prometheus配置檔案:
在scrape_configs選項組下面新增:
- job_name: 'node_exporter' scrape_interval: 1s static_configs: - targets: ['localhost:9199']
重啟Prometheus:
systemctl restart prometheus
此時可以在Prometheus上進行出圖了:通過相關的PromSQL得到各種資料指標,如node_load5。Prometheus上面比較適合做一些除錯,不適合看監控指標,需要把結果放到Grafana上進行展示。
在Grafana上(http://10.47.97.47:3333)進行配置資料來源。把Prometheus(http://10.47.97.47:9099)做為資料來源新增進去。如:
根據實際情況設定好各個值,儲存完成。 然後去官網找個模板匯入:比如匯入的ID是8919:
完成之後,可以看到相關的監控圖示了。現在根據表示式自定義一個圖:如定義一個CPU使用率:
1-avg(irate(node_cpu_seconds_total{HOSTNAME=~"$hostname",mode='idle'}[1m])) by (HOSTNAME)
HOSTNAME:node_cpu_seconds_total metrice裡的一個label。
$hostname:在設定裡設定的一個變數。最終展示圖如:
關於更多的圖可以自行定義,關於PromSQL可以見Prometheus PromQL說明
&n