使用influxdb 作為prometheus持久化存儲和使用mysql 作為grafana 持久化存儲的安裝方法
注:
1、influxdb要求1.4以上版本
2、prometheus用默認的存儲方式在grafana上可方便計算空間使用率等的信息(涉及計算),切到influxdb后目前還不知道如何轉換.
系統(tǒng)centos7.3
go1.15.14
influxdb-1.8.1.x86_64.rpm
prometheus-2.24.1.linux-amd64.tar.gz
grafana-7.5.7-1.x86_64.rpm
mysql-5.7.27
1、準備工作
1.1檢查是否安裝有influxdb
rpm -qa | grep influxdb
influxdb2-2.1.1-1.x86_64
1.2刪除已安裝的版本
rpm -e influxdb2-2.1.1-1.x86_64
1.3yunm安裝go環(huán)境
yum install go
1.4下載influxdb 安裝文件
wget https://dl.influxdata.com/influxdb/releases/influxdb-1.8.1.x86_64.rpm
版本信息可以從這里查看 https://portal.influxdata.com/downloads/,要下載不同的版本只需修改上面的版本信息即可。
2安裝
2.1、安裝influxdb數(shù)據(jù)庫
yum install -y influxdb-1.8.1.x86_64.rpm
修改influxdb配置文件,默認啟動端口8086
vim /etc/influxdb/influxdb.conf
influxdb的 啟動、停止、查看狀態(tài)、設置開機啟動。
啟動
systemctl start influxdb
停止
systemctl stop influxdb
查看狀態(tài)
systemctl status influxdb
設為開機啟動
systemctl enable influxdb
創(chuàng)建http接口用于prometheus
curl -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE prometheus"
返回結果
{"results":[{"statement_id":0}]}
查看數(shù)據(jù)庫prometheus中的保留策略,并修改為默認保留365天。
# influx
Connected to http://localhost:8086 version 1.8.1
InfluxDB shell version: 1.8.1
> show databases;
name: databases
name
----
_internal
prometheus
> use prometheus;
Using database prometheus
> show retention policies on prometheus;
name duration shardGroupDuration replicaN default
---- -------- ------------------ -------- -------
autogen 0s 168h0m0s 1 true
> alter retention policy "autogen" on "prometheus" duration 365d default;
> show retention policies on prometheus;
name duration shardGroupDuration replicaN default
---- -------- ------------------ -------- -------
autogen 8760h0m0s 168h0m0s 1 true
> exit
查看數(shù)據(jù)庫prometheus中的保留策略
show retention policies on prometheus;
修改數(shù)據(jù)庫prometheus中的默認保留策略為365天。
alter retention policy "autogen" on "prometheus" duration 365d default;
修改prometheus配置
查看prometheus啟動進程
ps -ef|grep prometheus
返回結果
prometh+ 25574 1 4 11月15 ? 00:43:28 /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus --web.enable-admin-api --web.enable-lifecycle
修改找到的起效配置文件
vim /usr/local/prometheus/prometheus.yml
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# 遠程讀寫特性相關的配置,Remote write configuration (for Graphite, OpenTSDB, or InfluxDB).
#
remote_write:
- url: "http://localhost:8086/api/v1/prom/write?db=prometheus"
remote_read:
- url: "http://localhost:8086/api/v1/prom/read?db=prometheus"
保存退出后重啟Prometheus
systemctl restart prometheus.service
查看Influxdb數(shù)據(jù)庫
# influx
Connected to http://localhost:8086 version 1.8.1
InfluxDB shell version: 1.8.1
> use prometheus;
Using database prometheus
> show measurements;
name: measurements
name
----
go_gc_duration_seconds
go_gc_duration_seconds_count
go_gc_duration_seconds_sum
go_goroutines
go_info
go_memstats_alloc_bytes
go_memstats_alloc_bytes_total
go_memstats_buck_hash_sys_bytes
go_memstats_frees_total
go_memstats_gc_cpu_fraction
go_memstats_gc_sys_bytes
go_memstats_heap_alloc_bytes
go_memstats_heap_idle_bytes
.
.
.
promhttp_metric_handler_requests_in_flight
promhttp_metric_handler_requests_total
scrape_duration_seconds
scrape_samples_post_metric_relabeling
scrape_samples_scraped
scrape_series_added
up
> exit
查看influxdb相關配置
ll /usr/bin/influx*
/usr/bin/influxd influxdb服務器
/usr/bin/influx influxdb命令行客戶端
/usr/bin/influx_inspect 查看工具
/usr/bin/influx_stress 壓力測試工具
/usr/bin/influx_tsm 數(shù)據(jù)庫轉換工具(將數(shù)據(jù)庫從b1或bz1格式轉換為tsm1格式)
在 /var/lib/influxdb/下面會有如下文件夾
ll /var/lib/influxdb/
/var/lib/influxdb/data 存放最終存儲的數(shù)據(jù),文件以.tsm結尾
/var/lib/influxdb/meta 存放數(shù)據(jù)庫元數(shù)據(jù)
/var/lib/influxdb/wal 存放預寫日志文件
/var/lib/influxdb/influxd.pid PID文件
2.2、安裝Prometheus
備注
1.Grafana官網(wǎng)https://grafana.com/
2.Grafana軟件包下載頁面和安裝方法https://grafana.com/grafana/download
3.Grafana默認的端口號:3000
4.Prometheus官網(wǎng)https://prometheus.io
5.Prometheus和node_exporter的下載地址https://prometheus.io/download/
prometheus-2.24.1
grafana-7.5.7
prometheus默認端口9090,node_exporter默認端口9100
系統(tǒng):
# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
主機名 ip 主要安裝的軟件 角色(作用)
web1 192.168.100.245 prometheus、grafana prometheus和grafana服務端(收集數(shù)據(jù)并展示)
web3 192.168.100.246 node_exporter prometheus客戶端(被收集數(shù)據(jù)的客戶端)
2.2.1在centos7.3上安裝Prometheus 2.24.1
記得切換yum源為阿里源,安裝epel源,安裝ntp服務,啟動定時時間校準。
關閉centos防火墻和selinux
systemctl stop firwalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
下載prometheus-2.24.1
wget -P /usr/local/src https://github.com/prometheus/prometheus/releases/download/v2.24.1/prometheus-2.24.1.linux-amd64.tar.gz
解壓Prometheus
備注:現(xiàn)在的Prometheus都是編譯過的,無需安裝go語言環(huán)境,直接解壓即可使用。
tar -zxvf /usr/local/src/prometheus-2.24.1.linux-amd64.tar.gz -C /usr/local
將Prometheus做成軟連接的形式
ln -s /usr/local/src/prometheus-2.24.1.linux-amd64 /usr/local/prometheus
創(chuàng)建Prometheus數(shù)據(jù)存儲目錄,并給Prometheus主目錄賦用戶Prometheus權限
mkdir -p /var/lib/prometheus
chown -R prometheus /var/lib/prometheus
chown -R prometheus:prometheus /usr/local/prometheus/
將Prometheus加入到系統(tǒng)管理程序中
cat >/etc/systemd/system/prometheus.service <<EOF
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
# --storage.tsdb.path是可選項,默認數(shù)據(jù)目錄在運行目錄的./dada目錄中
ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
2.2.2將客戶端加入到Prometheus監(jiān)控中
注意:將配置文件中的ip地址改成你的被監(jiān)控客戶端的ip,(node_exporter的)端口號默認是9100
cp /usr/local/prometheus/prometheus.yml{,.bak}
cat >> /usr/local/prometheus/prometheus.yml <<EOF
- job_name: '246'
scrape_interval: 10s
static_configs:
- targets: ['192.168.100.246:9100']
labels:
instance: test-246
EOF

啟動Prometheus并設置其開機自啟
systemctl start prometheus.service
systemctl enable prometheus.service
驗證prometheus的Web頁面,prometheus默認的端口號是9090,瀏覽器輸入http://192.168.100.245:9090/

在prometheus的web頁面上查看主機監(jiān)控狀態(tài)
在prometheus的web頁面上依次點擊 “Status”---“Targets”,你會發(fā)現(xiàn)只監(jiān)控到prometheus服務器自己。這就對了

使用prometheus的web方式查看主機的監(jiān)控值
就如上圖中的那個地址所寫的一樣,格式:http://prometheus服務器ip:9090/metrics 。看到的一堆數(shù)據(jù),存在形式是【key {數(shù)量} value】


拿一個監(jiān)控的key去用圖形方式查看
在上圖中找個已經(jīng)產(chǎn)生數(shù)值的key,等會便于查看數(shù)據(jù)的變化情況。如【go_goroutines】
在最上面的文本框中輸入key,如【go_goroutines】,然后點【Execute】,然后點【Graph】就能看到key[go_goroutines]被繪制出的圖形了。

2.3在客戶端安裝部署node_exporter
以下安裝步驟在192.168.100.246上執(zhí)行
2.3.1下載node_exporter
wget -P /usr/local/src https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz
版本選擇地址 https://github.com/prometheus/node_exporter/releases
解壓node_exporter,創(chuàng)建軟連接
tar -zxvf node_exporter-1.0.1.linux-amd64.tar.gz -C /usr/local
ln -s /usr/local/node_exporter-1.0.1.linux-amd64 /usr/local/node_exporter
創(chuàng)建用于運行node_exporter的用戶
groupadd prometheus
useradd -g prometheus -s /sbin/nologin prometheus
給node_exporter主目錄賦權限
chown -R prometheus:prometheus /usr/local/node_exporter/
將node_exporter加入到系統(tǒng)服務當中
cat >/usr/lib/systemd/system/node_exporter.service <<EOF
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
啟動node_exporter并將其設置開機自啟
systemctl start node_exporter
systemctl enable node_exporter
檢查node_exporter是否已啟動,node_exporter默認的端口是9100
systemctl status node_exporter
ss -ntl |grep 9100


在prometheus的web上檢查是否監(jiān)控到了本機
中已經(jīng)修改過配置文件,將本客戶端加入到了prometheus服務器的配置文件中了。
登錄prometheus的web,依次點擊【Status---Targets】,正常的話就會看得到被監(jiān)控端246主機了。

2.2.3 安裝部署grafana-7.5.7,及如何在命令行下搜索、安裝插件。
備注:
1.Grafana官網(wǎng)https://grafana.com/
2.Grafana軟件包下載頁面和安裝方法https://grafana.com/grafana/download
3.Grafana默認的端口號:3000
下載Grafana的rpm包,及安裝
wget https://dl.grafana.com/oss/release/grafana-7.5.7-1.x86_64.rpm
yum install grafana-7.5.7-1.x86_64.rpm
啟動grafana,并設置其開機自啟
systemctl restart grafana-server
systemctl enable grafana-server
查看已安裝的grafana版本號
grafana-cli -version

2.2.4給Grafana在線安裝插件
以zabbix插件為例:在線搜索Grafana插件列表
grafana-cli plugins list-remote

在線搜索Zabbix插件:
grafana-cli plugins list-remote |grep zabbix

安裝Grafana的zabbix插件
grafana-cli plugins install alexanderzobnin-zabbix-app

重啟Grafana服務,讓安裝的插件生效
systemctl restart grafana-server
查看Grafana已安裝的插件列表
grafana-cli plugins ls

Grafana命令幫助
grafana-cli后跟--help,或在任何二級、三級命令后都可以跟--help,幫助非常有用。
3.使用web方式打開Grafana
瀏覽器打開服務器的3000端口,如:http://192.168.100.245:3000/

3.1登錄Grafana,Grafana默認用戶名和默認密碼都是admin
3.1.1在grafana的web上查看都安裝了哪些插件

3.1.2在Grafana網(wǎng)頁上啟用剛才安裝的zabbix插件
點一下看到的目標插件,然后點【Enable】按鈕,只有手工點過啟用的grafana插件才能被正常使用。

3.1.3 在Grafana網(wǎng)頁上添加數(shù)據(jù)源
依次點開左側的齒輪狀圖標【Configuration】---【Data Source】,再新頁面中點【Add data source】

點一下數(shù)據(jù)源類型Prometheus

為Grafana數(shù)據(jù)源prometheus添加數(shù)據(jù)源參數(shù)
Name:隨便輸入
Default:設置為開啟狀態(tài)
URL:http://192.168.73.34:9090/ ,寫你的prometheus主頁地址。
Access:Server(default)
Scrape interval:15s ,因為我們這是測試環(huán)境,盡量把刷新數(shù)據(jù)的時間寫小點。

點【Save & Test】 后,能彈出綠色的【Data source is working】就說明我們的prometheus數(shù)據(jù)源添加成功了。

3.1.4在Grafana上查看默認的prometheus儀表盤。

然后點一下【Prometheus 2.0 Stats】就能看到默認的儀表盤了。


3.1.5從官方下載支持中文的模板顯示prometheus監(jiān)控的node節(jié)點
下載地址 https://grafana.com/grafana/dashboards/






