Centos8搭建服務監控prometheus+node_export+grafana
- 簡單介紹
- prometheus
- 什么是prometheus?
- prometheus的特點
- 基本原理
- 服務程序
- 三大套件
- Grafana
- 什么是Grafana
- Grafana的特點
- node_export
- node_export是什么
- 搭建程序
- prometheus的安裝使用
- 用戶和組
- 創建目錄
- 下載
- 解壓
- 為prometheus創建組態檔目錄和存放資料目錄并賦權限
- 將prometheus組態檔拷貝到/etc/prometheus目錄下
- 將prometheus下二進制檔案拷貝到/usr/local/bin目錄下
- 修改組態檔
- 啟動prometheus
- 防火墻配置
- 將prometheus加入systemctl管理
- 創建prometheus.service服務檔案
- 重新加載服務串列
- 啟動服務
- 將prometheus設定為開機自啟動
- node_exporter的安裝和使用
- 用戶和組
- 創建目錄
- 下載
- 解壓
- 將node_exporter下二進制檔案拷貝到/usr/local/bin目錄下并設定權限
- 啟動
- 將node_exporter加入systemctl管理
- 創建node_exporter.service服務檔案
- 重新加載服務串列
- 啟動服務
- 將node_exporter設定為開機自啟動
- 加入prometheus監控
- Grafana的安裝使用
- 用戶和組
- 創建目錄
- 下載
- 解壓
- 將grafana下二進制檔案拷貝到/usr/local/bin目錄下并設定權限
- 配置grafana
- 拷貝ini組態檔檔案至/etc/grafana組態檔目錄下
- 創建grafana組態檔所需目錄
- /etc/grafana組態檔目錄下創建custom.ini的組態檔并覆寫conf/defaults.ini中定義的任何設定
- 啟動
- 配置資料源
- 匯入dashboard
- 將grafana加入systemctl管理
- 創建grafana-server.service服務檔案
- 重新加載服務串列
- 啟動服務
- 將grafana設定為開機自啟動
簡單介紹
prometheus
什么是prometheus?
Prometheus是由SoundCloud開發的開源監控報警系統和時序列資料庫(TSDB),Prometheus使用Go語言開發,是Google BorgMon監控系統的開源版本, 2016年由Google發起Linux基金會旗下的原生云基金會(Cloud Native Computing Foundation), 將Prometheus納入其下第二大開源專案, Prometheus目前在開源社區相當活躍, Prometheus和Heapster(Heapster是K8S的一個子專案,用于獲取集群的性能資料,)相比功能更完善、更全面,Prometheus性能也足夠支撐上萬臺規模的集群,簡單來說prometheus就是用來資料收集和存盤的,使用的pull模式,
prometheus的特點
- 多維度資料模型
- 靈活的查詢語言
- 不依賴分布式存盤,單個服務器節點是自主的
- 通過基于HTTP的pull方式采集時序資料
- 可以通過中間網關進行時序列資料推送
- 通過服務發現或者靜態配置來發現目標服務物件
- 支持多種多樣的圖表和界面展示,比如Grafana等
基本原理
通過HTTP協議周期性抓取被監控組件的狀態,任意組件只要提供對應的HTTP介面就可以接入監控,不需要任何SDK或者其他的集成程序,這樣做非常適合做虛擬化環境監控系統,比如VM、Docker、Kubernetes等,輸出被監控組件資訊的HTTP介面被叫做exporter ,目前互聯網公司常用的組件大部分都有exporter可以直接使用,比如Varnish、Haproxy、Nginx、MySQL、Linux系統資訊(包括磁盤、記憶體、CPU、網路等等),
服務程序
- 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的查詢陳述句,提供十分靈活的報警方式
三大套件
- Server 主要負責資料采集和存盤,提供PromQL查詢語言的支持
- Alertmanager 警告管理器,用來進行報警
- Push Gateway 支持臨時性Job主動推送指標的中間網關
Grafana
什么是Grafana
Grafana是一個跨平臺的開源的度量分析和可視化工具,可以通過將采集的資料查詢然后可視化的展示,并及時通知,說白了,grafana就是prometheus的資料展示工具,
Grafana的特點
- 展示方式:快速靈活的客戶端圖表,面板插件有許多不同方式的可視化指標和日志,官方庫中具有豐富的儀表盤插件,比如熱圖、折線圖、圖表等多種展示方式
- 資料源:Graphite,InfluxDB,OpenTSDB,Prometheus,Elasticsearch,CloudWatch和KairosDB等
- 通知提醒:以可視方式定義最重要指標的警報規則,Grafana將不斷計算并發送通知,在資料達到閾值時通過Slack、PagerDuty等獲得通知
- 混合展示:在同一圖表中混合使用不同的資料源,可以基于每個查詢指定資料源,甚至自定義資料源
- 注釋:使用來自不同資料源的豐富事件注釋圖表,將滑鼠懸停在事件上會顯示完整的事件元資料和標記
- 過濾器:Ad-hoc過濾器允許動態創建新的鍵/值過濾器,這些過濾器會自動應用于使用該資料源的所有查詢
node_export
node_export是什么
node-export 主要主要是監控kubernetes 集群node 物理主機:cpu、memory、network、disk 等基礎監控資源, node_export是一個監控服務會暴露一個介面,prometheus通過這個介面獲取資料,
搭建程序
prometheus的安裝使用
用戶和組
#先檢查用戶prometheus和組prometheus是否存在
#檢查組是否存在
cat /etc/group | grep prometheus
#如果不存在,創建組
groupadd prometheus
#檢查用戶prometheus是否存在
cat /etc/passwd | grep prometheus
或者
id prometheus
#如果不存在,創建用戶
useradd -g prometheus prometheus
創建目錄
# 創建目錄并切換
[root@dxm22 ~]# mkdir -p /data/prometheus
[root@dxm22 ~]# cd /data/prometheus/
下載
[root@dxm22 prometheus]# wget https://github.com/prometheus/prometheus/releases/download/v2.23.0/prometheus-2.23.0.linux-amd64.tar.gz
# 下載完查看
[root@dxm22 prometheus]# ll -h
總用量 62M
-rw-r--r-- 1 root root 62M 11月 26 20:52 prometheus-2.23.0.linux-amd64.tar.gz
解壓
[root@dxm22 prometheus]# tar -zxvf prometheus-2.23.0.linux-amd64.tar.gz
prometheus-2.23.0.linux-amd64/
prometheus-2.23.0.linux-amd64/LICENSE
prometheus-2.23.0.linux-amd64/consoles/
prometheus-2.23.0.linux-amd64/consoles/node-cpu.html
prometheus-2.23.0.linux-amd64/consoles/prometheus-overview.html
prometheus-2.23.0.linux-amd64/consoles/node-overview.html
prometheus-2.23.0.linux-amd64/consoles/node.html
prometheus-2.23.0.linux-amd64/consoles/node-disk.html
prometheus-2.23.0.linux-amd64/consoles/index.html.example
prometheus-2.23.0.linux-amd64/consoles/prometheus.html
prometheus-2.23.0.linux-amd64/prometheus
prometheus-2.23.0.linux-amd64/console_libraries/
prometheus-2.23.0.linux-amd64/console_libraries/prom.lib
prometheus-2.23.0.linux-amd64/console_libraries/menu.lib
prometheus-2.23.0.linux-amd64/prometheus.yml
prometheus-2.23.0.linux-amd64/promtool
prometheus-2.23.0.linux-amd64/NOTICE
[root@dxm22 prometheus]# ll -h
總用量 62M
drwxr-xr-x 4 3434 3434 132 11月 26 20:49 prometheus-2.23.0.linux-amd64
-rw-r--r-- 1 root root 62M 11月 26 20:52 prometheus-2.23.0.linux-amd64.tar.gz
為prometheus創建組態檔目錄和存放資料目錄并賦權限
# 為prometheus創建存放組態檔的目錄
[root@dxm22 data]# mkdir /etc/prometheus
# 為prometheus創建存放資料的目錄
[root@dxm22 data]# mkdir /var/lib/prometheus
# 為/var/lib/prometheus目錄設定權限
[root@dxm22 data]# chown prometheus.prometheus /var/lib/prometheus
[root@dxm22 data]# ll /var/lib | grep prometheus
drwxr-xr-x 2 prometheus prometheus 6 12月 8 22:20 prometheus
將prometheus組態檔拷貝到/etc/prometheus目錄下
[root@dxm22 prometheus-2.23.0.linux-amd64]# cp prometheus.yml /etc/prometheus/
將prometheus下二進制檔案拷貝到/usr/local/bin目錄下
[root@dxm22 prometheus-2.23.0.linux-amd64]# cp prometheus /usr/local/bin/
[root@dxm22 prometheus-2.23.0.linux-amd64]# cp promtool /usr/local/bin/
[root@dxm22 prometheus-2.23.0.linux-amd64]# ll /usr/local/bin/ | grep prom
-rwxr-xr-x 1 root root 88153522 12月 8 22:25 prometheus
-rwxr-xr-x 1 root root 78172790 12月 8 22:26 promtool
修改組態檔
[root@dxm22 prometheus]# cat /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']
因為默認的組態檔中已經有prometheus自身的配置資訊了,所以這里我們無需修改,
啟動prometheus
# 可直接啟動(非后臺啟動),進入prometheus安裝目錄
./prometheus
# 后臺啟動,在安裝目錄下:
nohup ./prometheus --config.file=prometheus.yml --web.enable-admin-api --web.listen-address=:9090 >/dev/null 2>&1 &
# 后臺啟動,在任意目錄下:
nohup /usr/local/prometheus/prometheus --config.file=/etc/prometheus/prometheus.yml --web.enable-admin-api --web.listen-address=:9090 >/dev/null 2>&1 &
我們這里使用后臺啟動,
[root@dxm22 prometheus-2.23.0.linux-amd64]# nohup /usr/local/prometheus/prometheus --config.file=/etc/prometheus/prometheus.yml --web.enable-admin-api --web.listen-address=:9090 >/dev/null 2>&1 &
[1] 20203
# 查看是否存在prometheus行程
[root@dxm22 prometheus-2.23.0.linux-amd64]# ps aux | grep prometheus
root 20203 4.5 3.3 769484 61424 pts/0 Sl 22:43 0:00 /usr/local/prometheus/prometheus --config.file=/etc/prometheus/prometheus.yml --web.enable-admin-api --web.listen-address=:9090
root 20213 0.0 0.0 12320 1044 pts/0 S+ 22:43 0:00 grep --color=auto prometheus
# 查看9090埠是否被監聽
[root@dxm22 prometheus-2.23.0.linux-amd64]# netstat -tunlp | grep :9090
tcp6 0 0 :::9090 :::* LISTEN 20203/prometheus
啟動成功后,瀏覽器訪問ip:9090,我們這里是http://192.168.0.22:9090/targets

防火墻配置
因為我這里是本機測驗,所以防火墻是關閉狀態,正常情況下服務器的防火墻都是開啟的,所以需要做這一步配置,把9090埠加入防火墻,
# 把9090埠加入防火墻
[root@dxm22 prometheus-2.23.0.linux-amd64]# firewall-cmd --add-port=9090/tcp --permanent
success
# 多載防火墻的修改
[root@dxm22 prometheus-2.23.0.linux-amd64]# firewall-cmd --reload
success
將prometheus加入systemctl管理
創建prometheus.service服務檔案
[Unit]
Description=Prometheus Time Series Collection and Processing Server
Wants=network-online.target
After=network-online.target
[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/prometheus/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
LimitNOFILE=65536
PrivateTmp=true
RestartSec=2
StartLimitInterval=0
Restart=always
[Install]
WantedBy=multi-user.target
重新加載服務串列
[root@dxm22 prometheus-2.23.0.linux-amd64]# systemctl daemon-reload
啟動服務
# 停止上面手動后臺啟動的prometheus服務
[root@dxm22 prometheus-2.23.0.linux-amd64]# ps aux | grep prometheus
root 20203 0.0 4.0 769484 75316 pts/0 Sl 22:43 0:00 /usr/local/prometheus/prometheus --config.file=/etc/prometheus/prometheus.yml --web.enable-admin-api --web.listen-address=:9090
root 20881 0.0 0.1 12320 2376 pts/0 S+ 23:05 0:00 grep --color=auto prometheus
[root@dxm22 prometheus-2.23.0.linux-amd64]# kill -9 20203
# 檢查是否還存在prometheus行程
[root@dxm22 prometheus-2.23.0.linux-amd64]# ps aux | grep prometheus
root 20885 0.0 0.0 12320 1084 pts/0 S+ 23:05 0:00 grep --color=auto prometheus
# 查詢所有服務單元是否有prometheus
[root@dxm22 prometheus-2.23.0.linux-amd64]# systemctl list-unit-files | grep prometheus
prometheus.service disabled
# 存在,且非開啟自啟動,接著使用systemctl啟動prometheus服務
[root@dxm22 prometheus-2.23.0.linux-amd64]# systemctl start prometheus.service
# 查看prometheus服務狀態
[root@dxm22 prometheus-2.23.0.linux-amd64]# systemctl status prometheus.service
● prometheus.service - Prometheus Time Series Collection and Processing Server
Loaded: loaded (/etc/systemd/system/prometheus.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2020-12-08 23:07:57 CST; 11s ago
Main PID: 20927 (prometheus)
Tasks: 9 (limit: 11154)
Memory: 24.2M
CGroup: /system.slice/prometheus.service
└─20927 /usr/local/prometheus/prometheus --config.file /etc/prometheus/prometheus.yml --storage.tsdb.path /var/lib/pro>
12月 08 23:07:58 dxm22.host prometheus[20927]: level=info ts=2020-12-08T15:07:58.085Z caller=head.go:645 component=tsdb msg="Repl>
12月 08 23:07:58 dxm22.host prometheus[20927]: level=info ts=2020-12-08T15:07:58.085Z caller=head.go:659 component=tsdb msg="On-d>
12月 08 23:07:58 dxm22.host prometheus[20927]: level=info ts=2020-12-08T15:07:58.085Z caller=head.go:665 component=tsdb msg="Repl>
12月 08 23:07:58 dxm22.host prometheus[20927]: level=info ts=2020-12-08T15:07:58.086Z caller=head.go:717 component=tsdb msg="WAL >
12月 08 23:07:58 dxm22.host prometheus[20927]: level=info ts=2020-12-08T15:07:58.086Z caller=head.go:722 component=tsdb msg="WAL >
12月 08 23:07:58 dxm22.host prometheus[20927]: level=info ts=2020-12-08T15:07:58.086Z caller=main.go:742 fs_type=XFS_SUPER_MAGIC
12月 08 23:07:58 dxm22.host prometheus[20927]: level=info ts=2020-12-08T15:07:58.086Z caller=main.go:745 msg="TSDB started"
12月 08 23:07:58 dxm22.host prometheus[20927]: level=info ts=2020-12-08T15:07:58.086Z caller=main.go:871 msg="Loading configurati>
12月 08 23:08:00 dxm22.host prometheus[20927]: level=info ts=2020-12-08T15:08:00.619Z caller=main.go:902 msg="Completed loading o>
12月 08 23:08:00 dxm22.host prometheus[20927]: level=info ts=2020-12-08T15:08:00.619Z caller=main.go:694 msg="Server is ready to >

Active: active (running) 可以看到prometheus服務已經啟動成功,

將prometheus設定為開機自啟動
[root@dxm22 prometheus-2.23.0.linux-amd64]# systemctl list-unit-files | grep prometheus
prometheus.service disabled
# disabled表示非開機自啟動
# 設定為開機自啟動
[root@dxm22 prometheus-2.23.0.linux-amd64]# systemctl enable prometheus.service
Created symlink /etc/systemd/system/multi-user.target.wants/prometheus.service → /etc/systemd/system/prometheus.service.
# 再次查看
[root@dxm22 prometheus-2.23.0.linux-amd64]# systemctl list-unit-files | grep prometheus
prometheus.service enabled
# enabled表示是開機自啟動,執行重啟命令
[root@dxm22 prometheus-2.23.0.linux-amd64]# reboot
#重啟后再次查看prometheus服務已啟動完成,
node_exporter的安裝和使用
用戶和組
#先檢查用戶node_exporter和組node_exporter是否存在
#檢查組是否存在
cat /etc/group | grep node_exporter
#如果不存在,創建組
groupadd node_exporter
#檢查用戶node_exporter是否存在
cat /etc/passwd | grep node_exporter
或者
id node_exporter
#如果不存在,創建用戶
useradd -g node_exporter node_exporter
創建目錄
# 創建目錄并切換
[root@dxm22 ~]# mkdir -p /data/node_exporter
[root@dxm22 ~]# cd /data/node_exporter
下載
[root@dxm22 node_exporter]# wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
# 下載完查看
[root@dxm22 node_exporter]# ll -h
總用量 9.1M
-rw-r--r-- 1 root root 9.1M 6月 16 21:19 node_exporter-1.0.1.linux-amd64.tar.gz
解壓
[root@dxm22 node_exporter]# tar -zxvf node_exporter-1.0.1.linux-amd64.tar.gz
node_exporter-1.0.1.linux-amd64/
node_exporter-1.0.1.linux-amd64/NOTICE
node_exporter-1.0.1.linux-amd64/node_exporter
node_exporter-1.0.1.linux-amd64/LICENSE
將node_exporter下二進制檔案拷貝到/usr/local/bin目錄下并設定權限
# 拷貝檔案
[root@dxm22 node_exporter-1.0.1.linux-amd64]# cp node_exporter /usr/local/bin/
[root@dxm22 node_exporter-1.0.1.linux-amd64]# ll /usr/local/bin/ | grep node
-rwxr-xr-x 1 root root 19657731 12月 8 23:27 node_exporter
# 設定權限
[root@dxm22 node_exporter-1.0.1.linux-amd64]# chown node_exporter.node_exporter /usr/local/bin/node_exporter
# 查看權限設定是否成功
[root@dxm22 node_exporter-1.0.1.linux-amd64]# ll /usr/local/bin/node_exporter
-rwxr-xr-x 1 node_exporter node_exporter 19657731 12月 8 23:27 /usr/local/bin/node_exporter
啟動
# 可直接啟動(非后臺啟動),進入node_exporter安裝目錄
./node_exporter
# 后臺啟動,在安裝目錄下:
nohup ./node_exporter &
# 后臺啟動,在任意目錄下:
nohup /usr/local/bin/node_exporter &
我們這里使用后臺啟動,
[root@dxm22 node_exporter-1.0.1.linux-amd64]# nohup /usr/local/bin/node_exporter &
[1] 21296
[root@dxm22 node_exporter-1.0.1.linux-amd64]# nohup: 忽略輸入并把輸出追加到'nohup.out'
# 查看是否存在node_exporter行程
[root@dxm22 node_exporter-1.0.1.linux-amd64]# ps aux | grep node_exporter
root 21296 0.0 0.7 717504 13380 pts/0 Sl 23:35 0:00 /usr/local/bin/node_exporter
root 21315 0.0 0.0 12320 1068 pts/0 S+ 23:36 0:00 grep --color=auto node_exporter
# 查看9100埠是否被監聽
[root@dxm22 node_exporter-1.0.1.linux-amd64]# netstat -ntlp | grep :9100
tcp6 0 0 :::9100 :::* LISTEN 21296/node_exporter
說明啟動成功,在瀏覽器上輸入ip + 9100埠可訪問,(如果有防火墻,需要設定)

將node_exporter加入systemctl管理
創建node_exporter.service服務檔案
[root@dxm22 node_exporter-1.0.1.linux-amd64]# cat /etc/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter
PrivateTmp=true
RestartSec=5
StartLimitInterval=0
Restart=always
[Install]
WantedBy=multi-user.target
重新加載服務串列
[root@dxm22 node_exporter-1.0.1.linux-amd64]# systemctl daemon-reload
啟動服務
# 停止上面手動后臺啟動的node_exporter行程
[root@dxm22 node_exporter-1.0.1.linux-amd64]# ps aux | grep node_exporter
root 21464 0.0 0.8 717760 14884 pts/0 Sl 23:44 0:00 /usr/local/bin/node_exporter
root 21513 0.0 0.0 12320 1112 pts/0 S+ 23:45 0:00 grep --color=auto node_exporter
[root@dxm22 node_exporter-1.0.1.linux-amd64]# kill -9 21464
# 檢查是否還存在node_exporter行程
[root@dxm22 node_exporter-1.0.1.linux-amd64]# ps aux | grep node_exporter
root 21516 0.0 0.0 12320 1060 pts/0 S+ 23:45 0:00 grep --color=auto node_exporter
# 查詢所有服務單元是否有node_exporter
[root@dxm22 node_exporter-1.0.1.linux-amd64]# systemctl list-unit-files | grep node_exporter
node_exporter.service disabled
# 存在,且非開啟自啟動,接著使用systemctl啟動node_exporter服務
[root@dxm22 node_exporter-1.0.1.linux-amd64]# systemctl start node_exporter
# 查看node_exporter服務狀態
[root@dxm22 node_exporter-1.0.1.linux-amd64]# systemctl status node_exporter.service

Active: active (running) 可以看到node_exporter服務已經啟動成功,在瀏覽器上輸入ip + 9100埠可訪問,
將node_exporter設定為開機自啟動
[root@dxm22 node_exporter-1.0.1.linux-amd64]# systemctl list-unit-files | grep node_exporter
node_exporter.service disabled
# disabled表示非開機自啟動
# 設定為開機自啟動
[root@dxm22 node_exporter-1.0.1.linux-amd64]# systemctl enable node_exporter.service
Created symlink /etc/systemd/system/multi-user.target.wants/node_exporter.service → /etc/systemd/system/node_exporter.service.
# 再次查看
[root@dxm22 node_exporter-1.0.1.linux-amd64]# systemctl list-unit-files | grep node_exporter
node_exporter.service enabled
# enabled表示是開機自啟動,執行重啟命令
[root@dxm22 node_exporter-1.0.1.linux-amd64]# reboot
#重啟后再次查看node_exporter服務已啟動完成,
加入prometheus監控
[root@dxm22 node_exporter-1.0.1.linux-amd64]# vim /etc/prometheus/prometheus.yml
# 增加如下內容:
# node_exporter監控 -新增node_exporter相關job
- job_name: 'node_exporter'
static_configs:
- targets: ['localhost:9100']

重啟prometheus
[root@dxm22 node_exporter-1.0.1.linux-amd64]# systemctl restart prometheus.service
再次訪問http://192.168.0.22:9090/targets,
Grafana的安裝使用
用戶和組
#先檢查用戶grafana和組grafana是否存在
#檢查組是否存在
cat /etc/group | grep grafana
#如果不存在,創建組
groupadd grafana
#檢查用戶grafana是否存在
cat /etc/passwd | grep grafana
或者
id grafana
#如果不存在,創建用戶
useradd -g grafana grafana
創建目錄
# 創建目錄并切換
[root@dxm22 ~]# mkdir -p /data/grafana
[root@dxm22 ~]# cd /data/grafana
下載
# 去官網下載grafana包,官網地址:https://grafana.com/grafana/download
# 或者 wget https://dl.grafana.com/oss/release/grafana-7.3.4-1.x86_64.rpm
# 或者到華為鏡像網站下載wget https://mirrors.huaweicloud.com/grafana/7.3.4/grafana-7.3.4.linux-amd64.tar.gz
# 這里我們去華為鏡像下載
[root@dxm22 grafana]# wget https://mirrors.huaweicloud.com/grafana/7.3.4/grafana-7.3.4.linux-amd64.tar.gz
# 下載完查看
[root@dxm22 grafana]# ll -h
總用量 52M
-rw-r--r-- 1 root root 52M 11月 24 14:35 grafana-7.3.4.linux-amd64.tar.gz
解壓
[root@dxm22 grafana]# tar -zxvf grafana-7.3.4.linux-amd64.tar.gz
將grafana下二進制檔案拷貝到/usr/local/bin目錄下并設定權限
# 拷貝檔案
[root@dxm22 grafana-7.3.4]# cp /data/grafana/grafana-7.3.4/bin/grafana-server /usr/local/bin/
[root@dxm22 grafana-7.3.4]# ll /usr/local/bin/ | grep grafana
-rwxr-xr-x 1 root root 58938592 12月 9 11:56 grafana-server
# 設定權限
[root@dxm22 grafana-7.3.4]# chown grafana.grafana /usr/local/bin/grafana-server
# 查看權限設定是否成功
[root@dxm22 grafana-7.3.4]# ll /usr/local/bin/grafana-server
-rwxr-xr-x 1 grafana grafana 58938592 12月 9 11:56 /usr/local/bin/grafana-server
配置grafana
拷貝ini組態檔檔案至/etc/grafana組態檔目錄下
# 將解壓檔案下conf檔案夾下ini檔案均拷貝至/etc/grafana目錄下
[root@dxm22 grafana-7.3.4]# cp -r /data/grafana/grafana-7.3.4/conf/ /etc/grafana/
[root@dxm22 grafana-7.3.4]# ll /etc/grafana/
總用量 72
-rw-r--r-- 1 root root 30381 12月 9 12:05 defaults.ini
-rw-r--r-- 1 root root 1045 12月 9 12:05 ldap_multiple.toml
-rw-r--r-- 1 root root 2289 12月 9 12:05 ldap.toml
drwxr-xr-x 6 root root 75 12月 9 12:05 provisioning
-rw-r--r-- 1 root root 31400 12月 9 12:05 sample.ini
創建grafana組態檔所需目錄
# 創建用于存放temp files, sessions, and the sqlite3 db等的資料目錄
[root@dxm22 grafana-7.3.4]# mkdir /var/lib/grafana
# 創建grafana日志檔案目錄
[root@dxm22 grafana-7.3.4]# mkdir /var/lib/grafana/log
# 創建plugins目錄
[root@dxm22 grafana-7.3.4]# mkdir /var/lib/grafana/plugins
/etc/grafana組態檔目錄下創建custom.ini的組態檔并覆寫conf/defaults.ini中定義的任何設定
# 復制defaults.ini檔案并重命名為custom.ini
[root@dxm22 grafana-7.3.4]# cp /etc/grafana/defaults.ini /etc/grafana/custom.ini
[root@dxm22 grafana-7.3.4]# ll /etc/grafana/ | grep ini
-rw-r--r-- 1 root root 30381 12月 9 12:08 custom.ini
-rw-r--r-- 1 root root 30381 12月 9 12:05 defaults.ini
-rw-r--r-- 1 root root 31400 12月 9 12:05 sample.ini
# 修改custom.ini檔案內容,主要有以下幾項,未列出的默認即可,
data = /var/lib/grafana
logs = /var/lib/grafana/log
plugins = /var/lib/grafana/plugins
provisioning = /etc/grafana/provisioning
啟動
# 可直接啟動(非后臺啟動),進入grafana安裝目錄bin下
./grafana-server
# 后臺啟動,在安裝目錄下:
nohup ./grafana-server &
# 后臺啟動,在任意目錄下:
nohup /usr/local/bin/grafana-server &
我們這里使用后臺啟動,
[root@dxm22 grafana-7.3.4]# nohup /usr/local/bin/grafana-server &
[1] 28811
[root@dxm22 grafana-7.3.4]# nohup: 忽略輸入并把輸出追加到'nohup.out'
# 查看是否存在grafana行程
[root@dxm22 grafana-7.3.4]# ps aux | grep grafana
root 28811 4.1 2.9 1427932 54568 pts/0 Sl 12:25 0:00 /usr/local/bin/grafana-server
root 28822 0.0 0.0 12320 1076 pts/0 S+ 12:25 0:00 grep --color=auto grafana
# 查看3000埠是否被監聽
[root@dxm22 grafana-7.3.4]# netstat -ntlp | grep :3000
tcp6 0 0 :::3000 :::* LISTEN 28811/grafana-server
說明啟動成功,在瀏覽器上輸入ip + 3000埠可訪問,我們這里是http://192.168.0.22:3000/,(如果有防火墻,需要設定),默認的用戶名/密碼都使用 admin,

我們登錄之后再看

配置資料源
左側選單->Configuration->Data Sources->點 add data source 按鈕

選擇prometheus,如圖:

設定url,我們這里是http://192.168.0.22:9090,大家注意要修改為自己的IP+埠:

然后點 save & test 按鈕,看到 “Data source is working” 表示資料源可以正常作業,如圖:

匯入dashboard
可在官方模板https://grafana.com/grafana/dashboards,查看模板編號,也可以下載后匯入,比如我這里選擇import,輸入模板編號為8919





將grafana加入systemctl管理
創建grafana-server.service服務檔案
[root@dxm22 grafana-7.3.4]# vim /etc/systemd/system/grafana-server.service
[Unit]
Description=Grafana instance
Documentation=http://docs.grafana.org
Wants=network-online.target
After=network-online.target
After=postgresql.service mariadb.service mysqld.service
[Service]
User=grafana
Group=grafana
Type=notify
Restart=on-failure
WorkingDirectory=/usr/share/grafana
RuntimeDirectory=grafana
RuntimeDirectoryMode=0750
ExecStart=/usr/local/bin/grafana-server \
--config=/etc/grafana/custom.ini \
--homepath=/data/grafana/grafana-7.3.4 \
--pidfile=/var/lib/grafana/pid/grafana-server.pid \
cfg:default.paths.logs=/var/lib/grafana/log \
cfg:default.paths.data=/var/lib/grafana \
cfg:default.paths.plugins=/var/lib/grafana/plugins \
cfg:default.paths.provisioning=/etc/grafana/provisioning &
LimitNOFILE=10000
TimeoutStopSec=20
[Install]
WantedBy=multi-user.target
# 目錄賦權限
chown -R grafana.grafana /usr/share/grafana
chown -R grafana.grafana /etc/grafana/
chown -R grafana.grafana /var/lib/grafana/
重新加載服務串列
[root@dxm22 grafana-7.3.4]# systemctl daemon-reload
啟動服務
# 停止上面手動后臺啟動的grafana行程
[root@dxm22 grafana-7.3.4]# ps aux | grep grafana
root 28811 0.1 1.8 1649640 33700 pts/0 Sl 12:25 0:03 /usr/local/bin/grafana-server
root 29742 0.0 0.0 12320 1088 pts/0 S+ 13:11 0:00 grep --color=auto grafana
[root@dxm22 grafana-7.3.4]# kill -9 28811
# 檢查是否還存在grafana行程
[root@dxm22 grafana-7.3.4]# ps aux | grep grafana
root 29748 0.0 0.0 12320 972 pts/0 S+ 13:12 0:00 grep --color=auto grafana
# 查詢所有服務單元是否有grafana
[root@dxm22 grafana-7.3.4]# systemctl list-unit-files | grep grafana
grafana-server.service disabled
# 存在,且非開啟自啟動,接著使用systemctl啟動grafana服務
[root@dxm22 grafana-7.3.4]# systemctl start grafana-server.service
# 查看grafana服務狀態
[root@dxm22 grafana-7.3.4]# systemctl status grafana-server.service

Active: active (running) 可以看到grafana服務已經啟動成功,在瀏覽器上輸入ip + 3000埠可訪問,
將grafana設定為開機自啟動
[[root@dxm22 grafana-7.3.4]# systemctl list-unit-files | grep grafana
grafana-server.service disabled
# disabled表示非開機自啟動
# 設定為開機自啟動
[root@dxm22 grafana-7.3.4]# systemctl enable grafana-server
Created symlink /etc/systemd/system/multi-user.target.wants/grafana-server.service → /etc/systemd/system/grafana-server.service.
# 再次查看
[root@dxm22 grafana-7.3.4]# systemctl list-unit-files | grep grafana
grafana-server.service enabled
# enabled表示是開機自啟動,執行重啟命令
[root@dxm22 grafana-7.3.4]# reboot
#重啟后再次查看grafana服務已啟動完成,
至此我們就完成了在centos8服務器上安裝prometheus、node_exporter和grafana的監控環境,下一步我們將接入mysqld_exporter加入prometheus來監控,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/232539.html
標籤:其他
