使用docker-compose搭建Prometheus+Grafana监控系统性能

Table of Contents

概念与设计总览

  • Prometheus 是一个开源的服务监控系统和时间序列数据库
  • Grafana是开源的可视化和分析软件

一、环境说明

  • Linux Centos7
  • docker-compose

二、安装node_exporter(选装,注意prometheus.yml配置)

  1. node_exporter用于采集系统信息,github地址

  2. 前往https://github.com/prometheus/node_exporter/releases列表下载对应系统的最新文件

    image.png

  3. 解压

    image.png

  4. 启动:./node_exporter –web.listen-address=”:9100” &

    image.png

  5. 打开页面:http://localhost:9100

    image.png


三、安装mysql_exporter(选装,注意prometheus.yml配置)

  1. mysql_exporter用于采集Mysql信息,github地址

  2. 前往https://github.com/prometheus/mysqld_exporter/releases列表下载对应系统的最新文件

  3. 解压。参考第二步图片

  4. 启动。[为选填参数]

    1
    2
    export DATA_SOURCE_NAME='root:yourpass@(127.0.0.1:30009)/[dbname]';
    ./mysqld_exporter-0.12.1.linux-amd64/mysqld_exporter --web.listen-address=0.0.0.0:30008 --config.my-cnf /opt/software/docker/mysql5.7/mysql/my.cnf
  5. 打开页面 http://localhost:30008


四、docker-compose安装Prometheus

  1. 编写docker-compose.yml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    version: '3.3'
    services:
    prometheus:
    image: prom/prometheus:latest
    container_name: prometheus
    restart: always
    privileged: true
    user: root
    ports:
    - 9090:9090
    volumes:
    #将刚刚准备好的defaults.ini直接映射到/usr/share/grafana/conf/defaults.ini路径
    - ./prometheus.yml:/etc/prometheus/prometheus.yml
  2. 新建prometheus.yml并配置node exporter监控数据

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    # 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:
    # - "first_rules.yml"
    # - "second_rules.yml"

    # A scrape configuration containing exactly one endpoint to scrape:
    # Here it's Prometheus itself.
    scrape_configs:
    # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
    - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['docker.for.mac.host.internal:9090'] # 服务器上使用内网ip
    # 采集node exporter监控数据
    - job_name: 'node'
    static_configs:
    - targets: ['docker.for.mac.localhost:9100'] # 服务器上使用内网ip
    # 采集mysql exporter监控数据
    - job_name: 'mysql'
    static_configs:
    - targets: ['docker.for.mac.localhost:30008'] # 服务器上使用内网ip
  3. 启动docker

    启动容器:docker-compose up -d

    查看容器:docker-compose ps

    删除容器:docker-compose rm

  4. 验证并查看node_exporter状态


五、docker-compose安装Grafana

  1. 编写docker-compose.yml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    version: '3.3'
    services:
    grafana:
    image: grafana/grafana:latest
    container_name: grafana
    restart: always
    privileged: true
    user: root
    ports:
    - 3000:3000
    environment:
    - GF_INSTALL_PLUGINS=grafana-clock-panel,grafana-simple-json-datasource
    volumes:
    #将刚刚准备好的defaults.ini直接映射到/usr/share/grafana/conf/defaults.ini路径
    - ./config/defaults.ini:/usr/share/grafana/conf/defaults.ini
    #data目录,如果使用了默认的sqlite3数据库,则文件会存在这边
    - ./data/grafana:/var/lib/grafana
    #log目录,后期会写入log文件
    - ./log:/var/log/grafana
    # 添加插件 *注意修改defaults.ini文件:server_url = http://renderer:8081/render
    # renderer:
    # image: grafana/grafana-image-renderer
    # restart: always
    # ports:
    # - 8081:8081
    # container_name: renderer
    # environment:
    # - GF_RENDERER_PLUGIN_TZ=Asia/Shanghai
    # - GF_RENDERER_PLUGIN_IGNORE_HTTPS_ERRORS=true
  2. 启动docker

    启动容器:docker-compose up -d

    查看容器:docker-compose ps

    删除容器:docker-compose rm

  3. 验证,默认用户名密码为admin/admin

  4. 新增prometheus数据源并配置服务地址

    image.png

  5. 寻找别人配置好的模板,其他模板查看https://grafana.com/grafana/dashboards

    1. 导入mysql_exporter模板,https://grafana.com/grafana/dashboards/8919

      image.png

    2. 导入mysql_exporter模板,https://grafana.com/grafana/dashboards/7362

  6. 导入模板

    image.png

  7. 查看

    image.png

    image.png

相关文章

评论系统未开启,无法评论!