一、使用docker-compose快速部署Prometheus+grafana环境

一、使用docker-compose快速部署Prometheus+grafana环境

老阳
2024-06-18 / 0 评论 / 1 阅读 / 正在检测是否收录...
由于最近公司服务频繁出问题,老板很生气,下面的人都很不好过,于是老大让加一下业务监控,来观察线上数据状态。
但是由于 qa 环境数据量太少,所以自己搭建了一套环境做相关监控,并且写了个脚本模仿生产上的数据,并且做了很多指标,写了一些 Grafana 的 PromQL 语法,网上的资料也很少,我就来补充一下,这个下一章再讲。

注意:

  • docker 和 docker-compose 请自行下载安装
  1. 创建相关目录并给予权限,持久化目录需要给 777 权限,否则容器启动失败
cd ~ && mkdir prometheus && chmod 777 prometheus
cd prometheus && mkdir grafana_data prometheus_data && chmod 777 grafana_data prometheus_data

./docker-compose.yml

version: "3.7"
services:
  node-exporter:
    image: prom/node-exporter:latest
    container_name: "node-exporter0"
    ports:
      - "9100:9100"
    restart: always
  prometheus:
    image: prom/prometheus:latest
    container_name: "prometheus0"
    restart: always
    ports:
      - "9090:9090"
    volumes:
      - "./prometheus.yml:/etc/prometheus/prometheus.yml"
      - "./prometheus_data:/prometheus"
  grafana:
    image: grafana/grafana
    container_name: "grafana0"
    ports:
      - "3000:3000"
    restart: always
    volumes:
      - "./grafana_data:/var/lib/grafana"

注:prometheus 主机需要部署以上容器,其他被监控主机只需要部署 node-exporter

  1. prometheus 配置文件(根据自己的服务器 ip 进行修改 targets 内容)

./prometheus.yml

global:
  scrape_interval:     15s # 默认抓取周期
  external_labels:
    monitor: 'codelab-monitor'
scrape_configs:
  - job_name: 'node-exporter' #服务的名称
    scrape_interval: 5s
    metrics_path: /metrics  #获取指标的url
    static_configs:
      - targets: ['139.224.224.229:9100'] # 这个为监听指定服务服务的ip和port,需要修改为自己的ip,貌似云服务必须用公网ip
  1. docker-compose 启动
docker-compose up -d         # 挂载目录给权限,不然会启动失败

如果启动报如下的"没有权限"错误:

grafana0         | You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migrate-to-v51-or-later
grafana0         | mkdir: can't create directory '/var/lib/grafana/plugins': Permission denied
grafana0         | GF_PATHS_DATA='/var/lib/grafana' is not writable.
grafana0         | You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migrate-to-v51-or-later
grafana0         | mkdir: can't create directory '/var/lib/grafana/plugins': Permission denied
grafana0         | GF_PATHS_DATA='/var/lib/grafana' is not writable.
grafana0         | You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migrate-to-v51-or-later
grafana0         | mkdir: can't create directory '/var/lib/grafana/plugins': Permission denied
grafana0         | GF_PATHS_DATA='/var/lib/grafana' is not writable.
grafana0         | You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migrate-to-v51-or-later
grafana0         | mkdir: can't create directory '/var/lib/grafana/plugins': Permission denied
grafana0         | GF_PATHS_DATA='/var/lib/grafana' is not writable.
grafana0         | You may have issues with file permissions, more information here: http://docs.grafana.org/installation/docker/#migrate-to-v51-or-later
grafana0         | mkdir: can't create directory '/var/lib/grafana/plugins': Permission denied

则需要将挂载目录赋予权限,即:

chmod 777 grafana_data/ prometheus_data/ #个人根据配置文件中具体的挂在目录进行权限修改
  1. 访问主机的 9090 端口可以查到 Prometheus 监控到的数据,访问 3000 端口是 grafana 的界面
账密admin/admin
  1. 配置监控,点击齿轮,选择 Data Sources
    image.png
  2. 点击 Add data source
    image.png
  3. 填写 prometheus 地址 ip+端口或者主机名+端口(需要配置 hosts)
    image.png
  4. 点击 save&test
    image.png
  5. 点击+号,选择 import
    image.png
  6. 输入模板号 9276,点击 load。    8919
    image.png
    image.png
  7. 导入模板完成
    image.png
0

评论 (0)

取消