本地部署Grafana+Prometheus

Prometheus 简述

Prometheus 是一个开源的系统监控和告警工具包,最初由 SoundCloud 开发,并在 2012 年作为开源项目发布。它被设计用于记录实时指标数据并提供强大的查询功能,用于分析和告警。Prometheus 主要用于云原生环境,并且可以轻松地与 Kubernetes 集成。它在 2016 年加入了 Cloud Native Computing Foundation (CNCF) 项目。

Prometheus 的特点

  1. 多维数据模型:Prometheus 使用标签(Labels)对时间序列数据进行标识,支持对这些标签进行灵活的查询。

  2. 强大的查询语言:Prometheus 提供了一个强大的查询语言 PromQL,用于实时查询和分析数据。

  3. 独立存储:Prometheus 自带时间序列数据库 (TSDB),支持本地存储,并且可以通过远程写入和读取扩展存储能力。

  4. 灵活的告警管理:Prometheus 内置告警管理器,可以基于数据查询结果触发告警,并通过多种途径(如邮件、Slack、Webhook 等)发送通知。

  5. 可扩展的服务发现:Prometheus 支持多种服务发现机制,包括 Kubernetes、Consul、DNS 等,便于自动发现目标服务。

  6. Pull 模型:Prometheus 使用 Pull 模型从监控目标处定期拉取数据,而不是依赖于被监控对象主动推送数据。

  7. 高可靠性:即使部分 Prometheus 服务器失效,监控系统仍能保持部分功能,且不依赖外部存储系统。

Prometheus 生态组件

  1. Alertmanager:用于处理 Prometheus 发送的告警通知,可以进行去重、分组、路由,并通过各种方式发送告警。

  2. Pushgateway:允许短暂存在的作业推送指标到 Prometheus,例如 batch jobs。

  3. Grafana:虽然不是 Prometheus 的原生组件,但通常与 Prometheus 一起使用,提供强大的数据可视化功能。

  4. Exporter:Prometheus 使用 Exporter 来采集不同系统、服务的指标数据。例如,Node Exporter 用于监控主机操作系统层面的指标。

  5. Prometheus Operator:Kubernetes 环境中常用的管理工具,简化了 Prometheus 的部署和管理。

  6. Thanos:一个扩展 Prometheus 存储的项目,用于在多 Prometheus 实例间实现高可用和长期存储。

  7. Cortex:用于水平扩展 Prometheus 的分布式系统,通过多租户架构提供长期存储和查询服务。

Prometheus 的工作原理

  1. 数据采集:Prometheus 定期从监控的目标(如应用程序、服务、操作系统)拉取(scrape)指标数据。数据以时间序列的形式存储,时间序列由度量名称及其相关标签(如 http_requests_total{method="GET",status="200"})标识。

  2. 存储:拉取到的数据存储在 Prometheus 自带的时间序列数据库(TSDB)中。时间序列数据按照时间戳进行存储,可以通过 PromQL 查询。

  3. 数据查询:用户可以通过 PromQL 查询存储的数据,用于实时分析、生成图表或告警条件。PromQL 支持复杂的数据处理和聚合操作。

  4. 告警:当某个查询的结果触发预定义的告警规则时,Prometheus 会将告警发送给 Alertmanager。Alertmanager 负责处理告警,执行去重、分组、路由,并通过配置好的通知渠道发送告警。

  5. 数据可视化:Prometheus 的查询结果可以通过如 Grafana 这样的工具进行可视化展示,方便监控和分析系统的运行状态。

Prometheus 通过其灵活的查询和告警功能,结合强大的生态组件,为现代云原生环境提供了全面的监控和告警解决方案。

本地部署Prometheus

在本地 Linux 系统上部署 Prometheus 主要包括以下几个步骤:

1. 下载 Prometheus

首先,访问 Prometheus 的 官方发布页面 以获取最新版本的 Prometheus。

# 下载 Prometheus 压缩包(以 2.54.0 版本为例,请替换为最新版本)
wget https://github.com/prometheus/prometheus/releases/download/v2.54.0/prometheus-2.54.0.linux-amd64.tar.gz
​
# 解压缩
tar -xzf prometheus-2.54.0.linux-amd64.tar.gz
​
# 进入解压后的目录
cd prometheus-2.54.0.linux-amd64

我直接在官网上下载的

我新建了一个linux啥也没有的,新建一个data文件把下载好的gz文件放在里面

解压文件 到 prometheus文件中

把文件移动到 /usr/local/prometheus

2. 配置 Prometheus

Prometheus 配置文件为 prometheus.yml,在解压后的目录中已包含一个默认配置文件。你可以根据需要进行修改。以下是一个基本的配置文件示例:

global:
  scrape_interval: 15s # 全局抓取间隔
  evaluation_interval: 15s # 全局评估间隔
​
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090'] # 监控自身

在这个配置中,Prometheus 会每 15 秒抓取一次监控目标的数据。在默认情况下,它会监控自身(localhost:9090)。

一定要注意格式问题!!!!

3.配置开机自启(注意格式问题)

#进入这个文件,默认是没有的,直接进入就行 vi

 /usr/lib/systemd/system/prometheus.service

[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/prometheus/prometheus \
  --config.file=/usr/local/prometheus/prometheus.yml \
  --storage.tsdb.path=/usr/local/prometheus/data/ \
  --storage.tsdb.retention=15d \
  --web.enable-lifecycle
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target
  • 重新加载 systemd 配置

  • sudo systemctl daemon-reload
  • 启动 Prometheus 服务

    sudo systemctl start prometheus
  • 检查服务状态

    sudo systemctl status prometheus

ps:一定要记得把防火墙关掉systemctl stop firewalld.service

3. 启动 Prometheus

sudo systemctl start prometheus

4.访问http://localhost:9090则可以进入页面

默认只监控本机一台

如果访问不到 就访问 Linux的IP  地址 我的是  http://192.168.186.49:9090

选择 Status->Targets

4. 配置系统服务(可选)---可选

为了方便管理,可以将 Prometheus 配置为系统服务。以下是在基于 systemd 的系统(如 Ubuntu、CentOS 等)中配置的示例:

# 创建 Prometheus 用户和组(可选)
sudo useradd --no-create-home --shell /bin/false prometheus
​
# 将 Prometheus 二进制文件移动到 /usr/local/bin
sudo mv prometheus /usr/local/bin/
sudo mv promtool /usr/local/bin/
​
# 创建 Prometheus 目录并移动配置文件
sudo mkdir /etc/prometheus
sudo mv prometheus.yml /etc/prometheus/
​
# 设置权限
sudo chown -R prometheus:prometheus /etc/prometheus
sudo chown prometheus:prometheus /usr/local/bin/prometheus
sudo chown prometheus:prometheus /usr/local/bin/promtool
​
# 创建 systemd 服务文件
sudo tee /etc/systemd/system/prometheus.service <<EOF
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
​
[Service]
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
​
[Install]
WantedBy=multi-user.target
EOF
​
# 重新加载 systemd 服务文件
sudo systemctl daemon-reload
​
# 启动 Prometheus 服务
sudo systemctl start prometheus
​
# 设置开机自启动
sudo systemctl enable prometheus

5. 验证 Prometheus 部署

启动服务后,访问 Prometheus 的 Web UI (http://localhost:9090) 验证其是否正常工作。你可以在 "Status" -> "Targets" 页面查看 Prometheus 正在监控的目标。

通过 http:// 服务器 IP:9090/metrics 可以查看到监控的数据:

6. 添加更多的监控目标和 Exporter

Prometheus 部署完成后,你可以开始添加更多的监控目标。例如,使用 node_exporter 来监控服务器的 CPU、内存、磁盘等资源。下载并启动 node_exporter,然后将其配置添加到 prometheus.yml 中。

scrape_configs:
  - job_name: 'node_exporter'
    static_configs:
      - targets: ['localhost:9100'] # 假设 node_exporter 运行在 localhost

prometheus.yml 文件中添加这个配置后,重新启动 Prometheus 服务以使更改生效。

通过以上步骤,Prometheus 就成功部署并运行在你的 Linux 服务器上了。

7.‌Node Exporter‌ 是什么

Node Exporter‌是一个由Prometheus官方提供的数据采集组件,专门用于收集服务器层面的运行指标。它能够提取所有格式为Prometheus指标的字符串,并将其暴露以便抓取,从而提供关于机器的负载、内存使用、磁盘IO、网络等基础监控信息。Node Exporter通过扫描指定目录中的文件,将这些信息转化为Prometheus支持的格式,等待中央服务器主动前来抓取。这种设计使得Node Exporter能够提供类似于传统主机监控维度的数据,同时避免了向中央服务器发送数据的需求,提高了效率和灵活性。

Node Exporter的主要功能包括但不限于收集CPU负载、内存使用情况、磁盘IO、网络状态等关键系统资源的使用情况,这些信息对于监控系统的运行状态和性能至关重要。通过与Prometheus和Grafana等工具的结合,Node Exporter能够为用户提供多维度的监控服务,包括节点资源状态和服务状态监控功能,从而帮助用户更好地理解和优化系统的性能。

总的来说,Node Exporter是Prometheus生态系统中的一个关键组件,它通过收集服务器层面的各种运行指标,为监控系统的健康状况和性能提供了重要的数据支持‌

部署 Node Exporter 监控系统级指标

1.下载 node_exporter 

监控远程主机(被监控端上安装node_Export)组件

2.解压

[root@node data]# ls
mysqld_exporter-0.15.1.linux-amd64.tar.gz  node_exporter-1.8.2.linux-amd64.tar.gz  prometheus-2.54.0.linux-amd64.tar.gz
[root@node data]# tar xf node_exporter-1.8.2.linux-amd64.tar.gz 
[root@node data]# ls
mysqld_exporter-0.15.1.linux-amd64.tar.gz  node_exporter-1.8.2.linux-amd64  node_exporter-1.8.2.linux-amd64.tar.gz  prometheus-2.54.0.linux-amd64.tar.gz
[root@node data]# cd node_exporter-1.8.2.linux-amd64
[root@node node_exporter-1.8.2.linux-amd64]# ls
LICENSE  node_exporter  NOTICE
[root@node node_exporter-1.8.2.linux-amd64]# mv node_exporter  /usr/local/bin/
[root@node node_exporter-1.8.2.linux-amd64]# 

3.配置开机自启

[Unit]
Description=mysqld_exporter
Documentation=https://prometheus.io/
After=network.target
 
[Service]
Type=simple
ExecStart=/usr/local/bin/node_exporter \
--collector.ntp \
--collector.mountstats \
--collector.systemd \
--collector.tcpstat
 
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
 
[Install]
WantedBy=multi-user.target

4.启动

通过浏览http://192.168.186.49:9100/9100/metrics 可以查看到node_Exporter被监控端收集的监控信息(我是在本机安装的)

添加到Prometheus yml中

5.页面展示

部署mysqld_exporter 远程监控数据库

1.官网下载

2.解压

Node Exporter

剩下的是同上,前提是机子安装这mysql  记得授权一下

1. 配置 MariaDB 用户权限

首先,需要在 MariaDB 中创建一个用户并授予其访问数据库的权限,这样 mysqld_exporter 才能访问数据库以收集指标。

3. 在 Prometheus 中配置远程 MariaDB 的监控

  1. 登录 MariaDB:

    mysql -u root -p
    
  2. 创建一个用户并授予权限: 假设你想创建一个名为 exporter 的用户,密码为 exporter_password,并只授予读取权限:

    CREATE USER 'exporter'@'%' IDENTIFIED BY 'exporter_password'; GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'%'; FLUSH PRIVILEGES;

  3. 退出 MariaDB:

    EXIT;

  4. 2. 部署 mysqld_exporter

  5. mysqld_exporter 是 Prometheus 用来监控 MySQL/MariaDB 数据库的一个工具。

  6. 下载并解压 mysqld_exporter

    wget https://github.com/prometheus/mysqld_exporter/releases/latest/download/mysqld_exporter-<version>.linux-amd64.tar.gz tar xvf mysqld_exporter-<version>.linux-amd64.tar.gz cd mysqld_exporter-<version>.linux-amd64

  7. 配置环境变量: 为了让 mysqld_exporter 连接 MariaDB,需要设置一些环境变量。 在 /etc/default/ 或者 /etc/sysconfig/ 目录下创建一个文件,假设命名为 mysqld_exporter,并设置以下内容:

    DATA_SOURCE_NAME='exporter:exporter_password@(remote-mysql-host:3306)/'

  8. 编辑 Prometheus 配置文件 prometheus.yml 添加以下内容到 scrape_configs 部分:

    - job_name: 'mariadb' static_configs: - targets: ['remote-mysql-host:9104']

  9. 启动 mysqld_exporter

  10. ./mysqld_exporter --config.my-cnf="/path/to/.my.cnf"

同 node_exporter

部署Grafana可视化页面

1.Grafana 简介

Grafana 是一个开源的数据可视化和监控平台,广泛用于分析和展示时序数据(Time Series Data)。它允许用户从多个数据源中提取数据,创建动态仪表板,进行监控和告警,并且具有强大的自定义能力。Grafana 的主要用途是通过美观且易于理解的方式,展示和分析监控数据。

2.Grafana 的特点

  1. 多数据源支持: Grafana 支持多种数据源,包括 Prometheus、Elasticsearch、Graphite、InfluxDB、MySQL、PostgreSQL 等,用户可以从不同的数据源中提取数据并在同一个仪表板中进行展示。

  2. 动态仪表板: Grafana 提供了强大的仪表板功能,用户可以通过拖放的方式创建和编辑可视化图表,支持图表的实时更新和多维度的数据显示。

  3. 丰富的可视化选项: Grafana 支持多种可视化类型,包括折线图、柱状图、热力图、饼图、单值显示、表格等,能够满足各种数据展示需求。

  4. 强大的查询编辑器: 每种数据源都有对应的查询编辑器,用户可以通过简单的语法或 SQL 语言编写复杂的查询来提取数据并进行可视化。

  5. 用户权限管理: Grafana 提供细粒度的用户和团队管理功能,可以根据需要为不同用户设置不同的访问权限。

  6. 告警功能: Grafana 支持基于图表数据的告警配置。当监控数据达到某个阈值时,Grafana 可以通过多种渠道(如邮件、Slack、Webhook 等)发送告警通知。

  7. 插件扩展: Grafana 有丰富的插件生态系统,用户可以通过插件扩展其功能,如增加新的数据源支持、新的可视化类型、报警通道等。

  8. 开放 API: Grafana 提供 REST API 接口,用户可以通过 API 对 Grafana 进行自动化管理和集成,如创建仪表板、查询数据、管理用户等。

3.Grafana 的应用场景

  • 监控与告警: 与 Prometheus 结合使用,Grafana 可以对服务器、应用程序和网络设备进行监控,并设置告警规则。

  • 日志分析: 与 Elasticsearch 结合,Grafana 可以用于展示和分析系统日志。

  • 业务数据展示: Grafana 也可用于展示业务指标,如销售数据、网站访问量等,通过自定义图表帮助业务人员做出决策

4.官网下载下载 Grafana |Grafana 实验室

建议下载rpm  我的机子有点问题,我就先用gz  就是下载比较慢(我这里显示2个小时  真的是漫长的等待)

5.解压配置开机自启(这里我还没发现问题,到后面发现问题 ,觉得啥也展示不出来,grafana没有正确安装,重新安装了一下,但是linux  用的centos7-2009   yum  下载依赖 但是 报错,一直说解析不了那个源,一定药用rpm进行安装)

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"


 One of the configured repositories failed (Unknown),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:

     1. Contact the upstream for the repository and get them to fix the problem.

     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).

     3. Run the command with the repository temporarily disabled
            yum --disablerepo=<repoid> ...

     4. Disable the repository permanently, so yum won't use it by default. Yum
        will then just ignore the repository until you permanently enable it
        again or use --enablerepo for temporary usage:

            yum-config-manager --disable <repoid>
        or
            subscription-manager repos --disable=<repoid>

     5. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:

            yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true

Cannot find a valid baseurl for repo: base/7/x86_64

解决:

更换成国内源,更换的时候记得先把原先的备份一下!!!

阿里云源:
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache

yum update

然后下载grafana需要的依赖 fontconfig

yum install fontconfig

然后用rpm   来安装grafana rpm的安装包

安装完以后,grafana  在 /usr/share/grafana    默认配置文件 在  /etc/grafana  

配置文件 :

[Unit]
Description=Grafana instance
After=network.target


[Service]
Type=simple
WorkingDirectory=/etc/grafana/
ExecStart=/usr/share/grafana/bin/grafana-server --config=/etc/grafana/grafana.ini
Restart=always


[Install]
WantedBy=multi-user.target

6.访问地址  http://服务器IP:3000  默认账号密码:admin  admin

第一次登录他会提醒你修改密码  我修改成admin  123456

7.配置数据源

选择Prometheus

这里改成get

这里查看我们刚刚配置的数据源

这里导入模板 选择的是第二个 随便点了 

这里只是展示个模板 

现在做数据配置

8.导入监控模板

Grafana dashboards | Grafana Labs这是官网上的模板有很多

复制好模板id,下载好的json,复制到(二选一,要么是id 要么是json)

点击添加

参考:Linux部署Prometheus+Grafana_普罗米修斯部署-CSDN博客

  • 22
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Docker部署PrometheusGrafana的步骤: 1. 安装Docker和Docker Compose 如果你还没有安装Docker和Docker Compose,可以参考官方文档进行安装。 2. 创建Docker Compose文件 在本地创建一个文件夹(例如:`prometheus-grafana`),并在其中创建一个`docker-compose.yml`文件,用于定义PrometheusGrafana服务。 在文件中,添加以下内容: ``` version: '3' services: prometheus: image: prom/prometheus container_name: prometheus ports: - "9090:9090" volumes: - ./prometheus:/etc/prometheus command: - --config.file=/etc/prometheus/prometheus.yml restart: always grafana: image: grafana/grafana container_name: grafana ports: - "3000:3000" volumes: - ./grafana:/var/lib/grafana restart: always ``` 这个文件定义了两个服务:`prometheus`和`grafana`。`prometheus`服务将使用`prom/prometheus`镜像,并将本地`./prometheus`目录挂载到容器中`/etc/prometheus`目录,`grafana`服务将使用`grafana/grafana`镜像,并将本地`./grafana`目录挂载到容器中`/var/lib/grafana`目录。 3. 创建Prometheus配置文件 在本地创建`prometheus`文件夹,在其中创建一个`prometheus.yml`文件,用于定义Prometheus监控的目标和规则。 在文件中,添加以下内容: ``` global: scrape_interval: 15s evaluation_interval: 15s scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] ``` 这个文件定义了一个`prometheus`作业,将使用Prometheus默认的`localhost:9090`目标进行监控。 4. 运行Docker Compose 在终端中,进入`prometheus-grafana`目录,并运行以下命令: ``` docker-compose up -d ``` 这将启动PrometheusGrafana服务,并将它们置于后台运行。你可以使用以下命令检查服务是否正在运行: ``` docker-compose ps ``` 5. 访问Grafana 在浏览器中,访问`http://localhost:3000`,使用默认的用户名和密码(admin/admin)登录Grafana。 现在,你可以在Grafana中添加一个数据源,选择Prometheus,并将URL设置为`http://prometheus:9090`(因为Prometheus服务的名称是`prometheus`,而不是`localhost`)。 完成后,你可以创建一个新的仪表板并添加一个面板,从而开始使用Grafana可视化Prometheus监控数据。 以上就是使用Docker部署PrometheusGrafana的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值