prometheus+influxdb+grafana

使用influxdb 作为prometheus持久化存储和使用mysql 作为grafana 持久化存储的安装方法
注:
1、influxdb要求1.4以上版本
2、prometheus用默认的存储方式在grafana上可方便计算空间使用率等的信息(涉及计算),切到influxdb后目前还不知道如何转换.
系统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环境

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数据库

yum install -y influxdb-1.8.1.x86_64.rpm

修改influxdb配置文件,默认启动端口8086

vim /etc/influxdb/influxdb.conf

influxdb的 启动、停止、查看状态、设置开机启动。

启动
systemctl start influxdb
停止
systemctl stop influxdb
查看状态
systemctl status influxdb
设为开机启动
systemctl enable influxdb

创建http接口用于prometheus

curl -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE prometheus"

返回结果
{"results":[{"statement_id":0}]}

查看数据库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

查看数据库prometheus中的保留策略
show retention policies on prometheus;
修改数据库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数据库

# 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        数据库转换工具(将数据库从b1或bz1格式转换为tsm1格式)

在 /var/lib/influxdb/下面会有如下文件夹

ll /var/lib/influxdb/
/var/lib/influxdb/data     存放最终存储的数据,文件以.tsm结尾
/var/lib/influxdb/meta     存放数据库元数据
/var/lib/influxdb/wal      存放预写日志文件

/var/lib/influxdb/influxd.pid PID文件

2.2、安装Prometheus

备注

1.Grafana官网https://grafana.com/
2.Grafana软件包下载页面和安装方法https://grafana.com/grafana/download
3.Grafana默认的端口号:3000
4.Prometheus官网https://prometheus.io
5.Prometheus和node_exporter的下载地址https://prometheus.io/download/

prometheus-2.24.1
grafana-7.5.7
prometheus默认端口9090,node_exporter默认端口9100
系统:

# cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 

主机名 ip 主要安装的软件 角色(作用)
web1 192.168.100.245 prometheus、grafana prometheus和grafana服务端(收集数据并展示)
web3 192.168.100.246 node_exporter prometheus客户端(被收集数据的客户端)

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

备注:现在的Prometheus都是编译过的,无需安装go语言环境,直接解压即可使用。

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

创建Prometheus数据存储目录,并给Prometheus主目录赋用户Prometheus权限

mkdir -p /var/lib/prometheus
chown -R prometheus /var/lib/prometheus
chown -R prometheus:prometheus /usr/local/prometheus/

将Prometheus加入到系统管理程序中

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是可选项,默认数据目录在运行目录的./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监控中

注意:将配置文件中的ip地址改成你的被监控客户端的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
image.png

启动Prometheus并设置其开机自启

systemctl start prometheus.service
systemctl enable prometheus.service

验证prometheus的Web页面,prometheus默认的端口号是9090,浏览器输入http://192.168.100.245:9090/

image.png

在prometheus的web页面上查看主机监控状态

在prometheus的web页面上依次点击 “Status”---“Targets”,你会发现只监控到prometheus服务器自己。这就对了


image.png

使用prometheus的web方式查看主机的监控值

就如上图中的那个地址所写的一样,格式:http://prometheus服务器ip:9090/metrics 。看到的一堆数据,存在形式是【key {数量} value】

image.png

image.png

拿一个监控的key去用图形方式查看

在上图中找个已经产生数值的key,等会便于查看数据的变化情况。如【go_goroutines】
在最上面的文本框中输入key,如【go_goroutines】,然后点【Execute】,然后点【Graph】就能看到key[go_goroutines]被绘制出的图形了。


image.png

2.3在客户端安装部署node_exporter

以下安装步骤在192.168.100.246上执行

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,创建软连接

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

创建用于运行node_exporter的用户

groupadd prometheus
useradd -g prometheus -s /sbin/nologin prometheus

给node_exporter主目录赋权限

chown -R prometheus:prometheus /usr/local/node_exporter/

将node_exporter加入到系统服务当中

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
image.png

image.png

在prometheus的web上检查是否监控到了本机

中已经修改过配置文件,将本客户端加入到了prometheus服务器的配置文件中了。
登录prometheus的web,依次点击【Status---Targets】,正常的话就会看得到被监控端246主机了。


image.png

2.2.3 安装部署grafana-7.5.7,及如何在命令行下搜索、安装插件。

备注:
1.Grafana官网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
image.png

2.2.4给Grafana在线安装插件

以zabbix插件为例:在线搜索Grafana插件列表

grafana-cli plugins list-remote
image.png

在线搜索Zabbix插件:

grafana-cli plugins list-remote |grep zabbix
image.png

安装Grafana的zabbix插件

grafana-cli plugins install alexanderzobnin-zabbix-app
image.png

重启Grafana服务,让安装的插件生效

systemctl restart grafana-server

查看Grafana已安装的插件列表

grafana-cli plugins ls
image.png

Grafana命令帮助

grafana-cli后跟--help,或在任何二级、三级命令后都可以跟--help,帮助非常有用。

3.使用web方式打开Grafana

浏览器打开服务器的3000端口,如:http://192.168.100.245:3000/


image.png

3.1登录Grafana,Grafana默认用户名和默认密码都是admin

3.1.1在grafana的web上查看都安装了哪些插件

image.png

3.1.2在Grafana网页上启用刚才安装的zabbix插件

点一下看到的目标插件,然后点【Enable】按钮,只有手工点过启用的grafana插件才能被正常使用。


image.png

3.1.3 在Grafana网页上添加数据源

依次点开左侧的齿轮状图标【Configuration】---【Data Source】,再新页面中点【Add data source】


image.png

点一下数据源类型Prometheus

image.png

为Grafana数据源prometheus添加数据源参数

Name:随便输入
Default:设置为开启状态
URL:http://192.168.73.34:9090/ ,写你的prometheus主页地址。
Access:Server(default)
Scrape interval:15s ,因为我们这是测试环境,尽量把刷新数据的时间写小点。


image.png

点【Save & Test】 后,能弹出绿色的【Data source is working】就说明我们的prometheus数据源添加成功了。


image.png

3.1.4在Grafana上查看默认的prometheus仪表盘。

image.png

然后点一下【Prometheus 2.0 Stats】就能看到默认的仪表盘了。


image.png

image.png

3.1.5从官方下载支持中文的模板显示prometheus监控的node节点

下载地址 https://grafana.com/grafana/dashboards/

image.png

image.png
image.png

image.png
image.png
image.png
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,519评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,842评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,544评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,742评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,646评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,027评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,513评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,169评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,324评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,268评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,299评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,996评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,591评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,667评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,911评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,288评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,871评论 2 341

推荐阅读更多精彩内容