一、Prometheus+Grafana普罗米修斯,搭建和使用

1. Prometheus普罗米修斯,搭建和使用

1.1 prometheus监控框架工具介绍

prometheus是由谷歌研发的一款开源的监控软件,它通过安装在远程机器上的exporter,通过HTTP协议从远程的机器收集数据并存储在本地的时序数据库上

同时Prometheus后端用 golang语言开发,前端是 Grafana

1.2 支持类型

Prometheus为了支持各种中间件以及第三方的监控提供了exporter,大家可以把它理解成监控适配器,将不同指标类型和格式的数据统一转化为Prometheus能够识别的指标类型。

例如Node exporter主要通过读取Linux的/proc以及/sys目录下的系统文件获取操作系统运行状态,reids exporter通过Reids命令行获取指标,mysql exporter通过读取数据库监控表获取MySQL的性能数据。他们将这些异构的数据转化为标准的Prometheus格式,并提供HTTP查询接口。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ovE8puOM-1615534500766)(FB2D85A532DE4EA0B6378CCE4AF0E677)]

Prometheus的流行和Kubernetes密不可分,支持对Kubernetes、容器、OpenStack的监控。

2. Prometheus 源码安装和启动配置

IP角色系统
192.168.2.45Prometheus 服务端CentOS 7
192.168.2.44node_exporter 客户端CentOS 7

普罗米修斯下载网址:https://prometheus.io/download/

监控集成器下载地址:http://www.coderdocument.com/docs/prometheus/v2.14/instrumenting/exporters_and_integrations.html

[root@VM_2-45 ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.25.0/prometheus-2.25.0.linux-amd64.tar.gz
[root@VM_2-45 ~]# tar xf  prometheus-2.25.0.linux-amd64.tar.gz -C /usr/local
[root@VM_2-45 ~]# mv prometheus-2.25.0.linux-amd64/ prometheus
  • 查看版本号
[root@VM_2-45 /usr/local/prometheus]# ./prometheus --version
prometheus, version 2.25.0 (branch: HEAD, revision: a6be548dbc17780d562a39c0e4bd0bd4c00ad6e2)
  build user:       root@615f028225c9
  build date:       20210217-14:17:24
  go version:       go1.15.8
  platform:         linux/amd64
[root@VM_2-45 /usr/local/prometheus]# 
  • 查看帮助文档
[root@VM_2-45 /usr/local/prometheus]# ./prometheus --help

2.1 prometheus.yml 配置解释

# my global config
global:
  # 默认情况下,每15s拉取一次目标采样点数据。
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  # 每15秒评估一次规则。默认值为每1分钟。
  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:
  # job名称会增加到拉取到的所有采样点上,同时还有一个instance目标服务的host:port标签也会增加到采样点上
  - job_name: 'prometheus'

    # 覆盖global的采样点,拉取时间间隔5s
    scrape_interval: 5s
    static_configs:
    - targets: ['localhost:9090']

2.2 启动服务

# 启动服务
[root@VM_2-45 /usr/local/prometheus]# ./prometheus --config.file=prometheus.yml


# 指定配置文件
--config.file="prometheus.yml"
# 默认指定监听地址端口,可修改端口
--web.listen-address="0.0.0.0:9090" 
# 最大连接数
--web.max-connections=512
# tsdb数据存储的目录,默认当前data/
--storage.tsdb.path="data/"
# premetheus 存储数据的时间,默认保存15天
--storage.tsdb.retention=15d 
# 通过命令热加载无需重启 curl -XPOST 192.168.2.45:9090/-/reload
--web.enable-lifecycle
# 可以启用 TLS 或 身份验证 的配置文件的路径
--web.config.file=""


启动选项了解:./prometheus --help

2.3 访问:http://localhost:9090

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GlBdVFxb-1615534500769)(08736E415D4E482D94C292A069393B6C)]

2.4 查看暴露指标

http://localhost:9090/metrics

2.5 将Prometheus配置为系统服务

  • 1.进入systemd目录下:cd /usr/lib/systemd/system
[root@VM_2-45 ~]# cd /usr/lib/systemd/system
  • 2.创建文件:vim prometheus.service
[Unit]
  Description=https://prometheus.io
  
  [Service]
  Restart=on-failure
  ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --web.listen-address=:9090

  [Install]                      
  WantedBy=multi-user.target
  • 3.生效系统system文件
systemctl daemon-reload
  • 4.启动服务
[root@VM_2-45 ~]# systemctl start prometheus

3. 客户端,配置服务发现监控linux主机及相关服务

IP角色系统
192.168.2.45Prometheus 服务端CentOS 7
192.168.2.44node_exporter 客户端CentOS 7

3.1 客户端安装node_exporter

3.1.1 监控Linux 安装常用node_exporter

[root@VM_2-44 ~]# wget https://github.com/prometheus/node_exporter/releases/download/v1.1.2/node_exporter-1.1.2.linux-amd64.tar.gz
[root@VM_2-44 ~]# tar xf node_exporter-1.1.2.linux-amd64.tar.gz -C /usr/local/
[root@VM_2-44 ~]# mv node_exporter-1.1.2.linux-amd64/ node_exporter

3.1.2 启动node_exporter,并添加到服务

# 直接启动
[root@VM_2-44 /usr/local/node_exporter]# ./node_exporter &

# 启动后会监听9100端口


# 添加为服务方式启动
[root@VM_2-44 ~]# vim /usr/lib/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
After=network.target 

[Service]
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure

[Install]
WantedBy=multi-user.target

# 启动node_exporter
systemctl daemon-reload
systemctl start node_exporter

3.1.3 普罗米修斯服务端配置文件添加监控项

[root@VM_2-45 /usr/local/prometheus]# cat 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:
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['192.168.2.45:9090']

  - job_name: 'linux'
    static_configs:
    - targets: ['192.168.2.44:9100','192.168.2.43:9100'] # 多个用,分开

# 添加上面三行

3.1.4 重启普罗米修斯

[root@VM_2-45 /usr/local/prometheus]# systemctl restart prometheus.service
  • 打开普罗米修斯自带的监控页面,Status -> Targets 查看:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vbIfPE38-1615534500770)(63E10467ED8E43969ED111B99F43D01C)]

3.2 监控mysql(mysqld-exporter)

Grafana使用这个模板链接: https://pan.baidu.com/s/1sVE5_oEUnak4vTU7XaT5ig 提取码: k5gi

[root@VM_2-44 ~]# wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
[root@VM_2-44 ~]# tar xf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local/
[root@VM_2-44 /usr/local]# mv mysqld_exporter-0.12.1.linux-amd64 mysqld_exporter
[root@VM_2-44 /usr/local/mysqld_exporter]# vim .my.cnf
[client]
user=root
password=123456

3.2.1 启动mysqld-exporter

[root@VM_2-44 /usr/local/mysqld_exporter]# ./mysqld_exporter --config.my-cnf="/usr/local/mysqld_exporter/.my.cnf" &
[root@VM_2-44 /usr/local/mysqld_exporter]# ps -ef |grep exporter
root       3447   3398  0 01:31 pts/1    00:00:02 ./node_exporter
root       4647   3398  0 02:13 pts/1    00:00:00 ./mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf
root       4654   3398  0 02:13 pts/1    00:00:00 grep --color=auto exporter
[root@VM_2-44 /usr/local/mysqld_exporter]# ss -lntp |grep 4647
LISTEN     0      128         :::9104                    :::*                   users:(("mysqld_exporter",pid=4647,fd=3))
[root@VM_2-44 /usr/local/mysqld_exporter]# 

# 启动后会监听9104端口

3.2.2 普罗米修斯配置文件添加监控项后重启

[root@VM_2-45 /usr/local/prometheus]# vim prometheus.yml 
  - job_name: 'mysql'
    static_configs:
    - targets: ['192.168.2.44:9104']

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ra0ZWYMI-1615534500772)(3FED6FBB52F24F15B459AD06CC9FEED9)]

  • 重启普罗米修斯
[root@VM_2-45 /usr/local/prometheus]# systemctl restart prometheus.service

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BN2kDYHD-1615534500774)(B556B74646E449CEBCF8E606AA3CA228)]

3.1.1 为了管理服务,将宿主机节点监控采集node_exporter加入到服务配置文件里

[root@VM_2-44 ~]# vi /usr/lib/systemd/system/node_exporter.service

[Unit]
Description=https://prometheus.io

[Service]
Restart=on-failure
ExecStart=/usr/local/node_exporter/node_exporter --collector.systemd --collector.systemd.unit-whitelist=(docker|sshd|nginx).service

[Install]
WantedBy=multi-user.target


# 监控客户端,docker服务,nginx服务,sshd

如果要监控节点的系统服务,需要在后面添加名单参数
–collector.systemd.unit-whitelist=".+" 从systemd中循环正则匹配单元
–collector.systemd.unit-whitelist="(docker|sshd|nginx).service" 白名单,收集目标

/usr/bin/node_exporter --collector.systemd --collector.systemd.unit-whitelist=(docker|sshd|nginx).service

4. Grafana 展示 Prometheus 数据

wget https://mirrors.tuna.tsinghua.edu.cn/grafana/yum/rpm/grafana-7.4.3-1.x86_64.rpm
[root@VM_2-45 ~]# yum install initscripts fontconfig
[root@VM_2-45 ~]# yum install -y grafana-7.4.3-1.x86_64.rpm
[root@VM_2-45 ~]# systemctl status grafana-server.service 

启动后访问地址:ip:3000
初始用户名和密码都是admin

4.1 添加Prometheus数据源

Configuration -> Data Sources ->add data source -> Prometheus

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4iVhER8e-1615534500775)(68B71080ABF54BE28FE84801894EE4DD)]

4.2 新增Dashboard Linux基础数据展示

Create -> import

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kh7Ifjdz-1615534500775)(1BA665086C004F7EAE21564B513E6542)]

4.2.2 导入模板8919

已经画好的模板链接: https://pan.baidu.com/s/1d70reg4Mro3Dh17bb-ZEVA 提取码: b6c1

更多grafana模板: https://grafana.com/grafana/dashboards 搜索 相应 dashboards的id如8919,12227

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RGCoQNaY-1615534500776)(F9E3AD6175614D828E27EEC6CCE806C5)]

4.2.3 选择数据源

在这里插入图片描述

4.2.4 查看Dashboard

Dashboards ->Manage
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oqhoPWbO-1615534500777)(8F21001E0D2947EC94582639BEDF62E5)]

4.3 新增Dashboard MySQL数据展示

  • 上面3.2的内容完成后,在继续下面操作

4.3.1 设置数据源

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-L3PWkOmo-1615534500778)(2FFDE2D5B020497C93627B9F8D3B220B)]

  • 数据库上授权远程账户
    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-clGbSZqm-1615534500778)(94B07BA1545D43DA955EC3E467B9DB27)]

4.3.2 导入已经画好的dashboard,数据源选择刚刚创建好的mysql数据源即可

链接:https://pan.baidu.com/s/12S33kZkZd9gIXF8tMHKECg 提取码9b0s

或者链接:https://pan.baidu.com/s/1GBzogDLsYS3IvwH4WbdPLw 提取码:ef6e

将下载的json打开复制进去加载
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GVErpmvl-1615534500779)(AC3BB3A19ABB43E2A8C53BE2FEA59239)]

5. 监控Redis(redis_exporter)

链接:https://pan.baidu.com/s/12y7HxtQlBjFwr_i8ClM8Kw 提取码:13v2

5.1 安装redis_exporter

或者下载:
wget https://github.com/oliver006/redis_exporter/releases/download/v0.15.0/redis_exporter-v0.15.0.linux-amd64.tar.gz
tar -C /usr/local/ -xvf redis_exporter-v0.15.0.linux-amd64.tar.gz

5.2启动redis_exporter

默认redis_exporter端口为9121

./redis_exporter redis//192.168.0.103:6379 & -web.listenaddress 192.168.0.103:9121

5.3 prometheus配置文件中加入redis监控并重启

vim /usr/local/Prometheus/prometheus.yml

# 默认redis-exporter端口为9121
- job_name: 'Redis'
    static_configs:
    - targets: ['192.168.2.44:9121']

5.4 grafana导入画好的dashboard

链接:https://pan.baidu.com/s/1LIkVC0o3lyTT59JbX0mSpg 提取码:gf1d

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-w1RRATmR-1615534500780)(BC17EC5A3FD44E9AA1B73E3730EE5A11)]

6. 监控windows机器(wmi-exporter)

https://github.com/martinlindhe/wmi_exporter/releases

wmi-exporter下载地址:https://github.com/prometheus-community/windows_exporter/releases/download/v0.16.0/windows_exporter-0.16.0-amd64.msi

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aAz7YYKf-1615534500780)(CC98A86C2AB94BC98BF014739DDF4A86)]

  • 双击即可完成安装 ,被监控windows机器安装wmi-exporter,会自动创建一个开机自启的服务

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AuxpIaR2-1615534500781)(CA81699CCF634A9BB47643197B44D703)]

  • 验证服务是否启动,默认wmi-exporter端口为9182

  • 修改Prometheus配置文件


# 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:
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['192.168.2.45:9090']

  - job_name: 'linux'
    static_configs:
    - targets: ['192.168.2.44:9100']

  - job_name: 'mysql'
    static_configs:
    - targets: ['192.168.2.44:9104']

  - job_name: 'windows'
    static_configs:
    - targets: ['192.168.2.125:9182']

# 重启普罗米修斯
[root@VM_2-45 /usr/local/prometheus]# systemctl restart prometheus.service 

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2NmNWLcY-1615534500781)(EFE928535A584785AF3B6739D9B410CB)]

  • 访问http://192.168.2.45:9090/targets 查看监控信息,

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3RkCviFC-1615534500782)(69E3BFBEFDB9499A9CE8D7187D277C71)]

grafana导入画好的dashboard,选择普罗米修斯数据源
链接: https://pan.baidu.com/s/1-9Sch1IYZvR1DeimMz7_mQ 提取码: xh6v
或者dashboard Id为:10467的模板

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-umbLes2H-1615534500782)(353B3A15DFC94A48A31B9B822E02C6A7)]

  • 54
    点赞
  • 346
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
你!要在Linux上搭建PrometheusGrafana,你可以按照以下步骤进行操作: 1. 首先,确保你已经在Linux服务器上安装了Docker和Docker Compose。如果没有安装,可以参考官方文档进行安装。 2. 创建一个新的目录来存放PrometheusGrafana的配置文件。进入该目录并创建一个名为docker-compose.yml的文件。 3. 打开docker-compose.yml文件,并添加以下内容: ```yaml version: '3' services: prometheus: image: prom/prometheus volumes: - ./prometheus:/etc/prometheus command: - --config.file=/etc/prometheus/prometheus.yml ports: - 9090:9090 grafana: image: grafana/grafana ports: - 3000:3000 ``` 上述配置将创建两个服务:PrometheusGrafanaPrometheus服务将监听9090端口,Grafana服务将监听3000端口。 4. 在同一目录下创建一个名为prometheus目录,用于存放Prometheus的配置文件。进入prometheus目录,并创建一个名为prometheus.yml的文件。 5. 打开prometheus.yml文件,并添加以下内容: ```yaml global: scrape_interval: 15s scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] ``` 上述配置将告诉Prometheus每隔15秒抓取一次本地主机的metrics信息。 6. 返回到docker-compose.yml文件所在目录,并在终端中运行以下命令启动PrometheusGrafana容器: ``` docker-compose up -d ``` 这将在后台启动容器。 7. 等待一段时间,然后在浏览器中访问http://服务器IP:3000,会看到Grafana的登录页面。 8. 使用默认用户名(admin)和密码(admin)登录Grafana。 9. 在Grafana中,点击左侧面板上的"Configuration"选项,然后选择"Data Sources"。 10. 点击"Add data source"按钮,选择"Prometheus"作为数据源类型。 11. 在"HTTP"选项卡中,输入Prometheus服务的URL,即http://localhost:9090。 12. 点击"Save & Test"按钮,确保连接正常。 现在,你已经成功搭建PrometheusGrafana。你可以开始创建仪表盘和监控指标了。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值