Ubuntu搭建prometheus+grafana

一. 搭建prometheus

  1. 在springboot项目中:
    在 pom.xml 中添加如下配置:
<!-- 开启 Spring Boot Actuator -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 将 Actuator 指标转换为 Prometheus 格式 -->
<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-registry-prometheus</artifactId>
  <version>${micrometer.version}</version>
</dependency>
  1. 然后修改 application.yaml 中添加 Spring Boot Actuator 相关配置:
spring:
  application:
    name: spring-boot-demo
management:
  endpoints:
    web:
      exposure:
        include: "*"
    health:
      show-details: always
  metrics:
    export:
      prometheus:
        enable: true
    tags:
      application: spring-boot-demo
  1. 可以通过 /actuator/prometheus 接口查看配置是否正确
curl 'http://localhost:8080/actuator/prometheus' -i -X GET
  1. 下载对应版本prometheusDownload | Prometheus并解压
tar xvfz prometheus-2.37.0.linux-arm64.tar.gz
  1. 在 prometheus.yml 中添加上 Service 对应的任务
scrape_configs:
  # ...
  -  job_name: 'springboot-app-smart_hospital' # Prometheus 任务名称,自定义
     metrics_path: '/actuator/prometheus' # 指标获取路径
     scrape_interval: 5s # 抓取指标的间隔时间
     static_configs:
       - targets: ['localhost:30090'] # 指标访问入口,即 k8s 集群的 Service
  1. 运行./prometheus,测试访问9090端口
  2. 编辑服务
vim /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus Time Series Collection and Processing Server
Wants=network-online.target
After=network-online.target

[Service]
User=root
Group=root
Type=simple
ExecStart=/root/prometheus/prometheus-2.37.0.linux-arm64/prometheus \
    --config.file /root/prometheus/prometheus-2.37.0.linux-arm64/prometheus.yml \
    --storage.tsdb.path /var/lib/prometheus/ \
    --web.console.templates=/root/prometheus/prometheus-2.37.0.linux-arm64/consoles \
    --web.console.libraries=/root/prometheus/prometheus-2.37.0.linux-arm64/console_libraries

[Install]
WantedBy=multi-user.target
  1. 依次执行
systemctl daemon-reload
systemctl restart prometheus.service
systemctl status prometheus.service
systemctl enable prometheus

二. 搭建node_exporter

  1. 下载对应版本prometheus-node_exporterDownload | Prometheus并解压
tar xvfz node_exporter-1.3.1.linux-arm64.tar.gz 
  1. 编辑服务
vim /etc/systemd/system/node_exporter.service
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/docs/introduction/overview
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=simple
PIDFile==/var/run/node_exporter.pid
ExecStart=/root/node_exporter/node_exporter-1.3.1.linux-arm64/node_exporter
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target
  1. 启动服务
systemctl daemon-reload
systemctl restart node_exporter
systemctl status node_exporter
systemctl enable node_exporter
  1. 配置prometheus
vim prometheus.yml
# 添加
- job_name: "node_exporter"
    static_configs:
      - targets: ["localhost:9100"]
  1. 重启prometheus
systemctl daemon-reload
systemctl restart prometheus.service
systemctl status prometheus.service

三. 搭建grafana

  1. 依次执行
sudo apt-get install -y adduser libfontconfig1
wget https://dl.grafana.com/enterprise/release/grafana-enterprise_9.0.5_arm64.deb
sudo dpkg -i grafana-enterprise_9.0.5_arm64.deb
systemctl daemon-reload
systemctl restart grafana-server
systemctl status grafana-server
systemctl enable grafana-server
  1. import模板8919(node_exporter),4701(JVM)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你!要在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个

红包金额最低5元

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

抵扣说明:

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

余额充值