美文网首页
DockerCompose运行Grafana集成Promethe

DockerCompose运行Grafana集成Promethe

作者: 码农笔录 | 来源:发表于2021-10-08 13:13 被阅读0次
    image.png

    使用docker的方式运行Grafana集成Prometheus+node-exporter+cadvisor监控多个节点。

    node里一个是本机,另外一个是我的另外一台服务器,Prometheus只需要启动一个,另外一个服务器只需要运行一个node-exporter。

    Prometheus 官方和一些第三方,已经把一些常用数据库、系统、中间件等的指标数据的采集做成了一个个 exporter,在生产环境中,直接导入使用就可以。 这一节,我们就用 Prometheus 官方提供的 Node Exporter 来完成对Linux系统运行数据的采集 。cAdvisor可以对节点机器上的资源及容器进行实时监控和性能数据采集,包括CPU使用情况、内存使用情况、网络吞吐量及文件系统使用情况。

    171504120201129181943262234489244.png

    本文全程基于docker-compse,没有docker环境的请先准备docker环境

    docker-compose文件准备

    1.编写grafana.yml的文件,一定要记得挂在卷,否则重启以后就要重新配置。

    version: '3.1'
    services:
     grafana:
       image: grafana/grafana
       container_name: grafana
       restart: always
       ports:
        - "3000:3000"
       volumes:
        - /opt/grafana:/var/lib/grafana
    
    

    2.编写prometheus.yml包含Prometheus+node-exporter+cadvisor

    version: "3"
    services:
        prometheus:
            image: prom/prometheus
            container_name: prometheus
            hostname: prometheus
            restart: always
            volumes:
                - /opt/prometheus/config/prometheus.yml:/etc/prometheus/prometheus.yml
                - /opt/prometheus/config/node_down.yml:/etc/prometheus/node_down.yml
            ports:
                - "9090:9090"
        node-exporter:
            image: quay.io/prometheus/node-exporter
            container_name: node-exporter
            restart: always
            ports:
                - "9100:9100"
        cadvisor:
            image: google/cadvisor:latest
            container_name: cadvisor
            restart: always
            volumes:
                - /:/rootfs:ro
                - /var/run:/var/run:rw
                - /sys:/sys:ro
                - /var/lib/docker/:/var/lib/docker:ro
            ports:
                - "8080:8080"
    

    3.另外一个节点,node-exporter.yml

    version: '3.1'
    services:
      node-exporter:
         image: quay.io/prometheus/node-exporter
         container_name: node-exporter
         restart: always
         ports:
          - "9100:9100"
    

    Prometheus配置文件编辑

    上面的prometheus挂载的文件有两个prometheus.yml和node_down.yml

    1.prometheus.yml

    172.18.0.1是我docker网卡的网关地址。端口对应上面docker-compose文件里配置的地址,这里全部采用默认的端口。

    # 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.
    
    #alerting:
     # alertmanagers:
     # - static_configs:
     #   - targets: ['172.18.0.1:9093']
    
    rule_files:
      - "node_down.yml"
    
    scrape_configs:
      - job_name: 'prometheus'
        static_configs:
        - targets: ['172.18.0.1:9090']
    
      - job_name: 'cadvisor'
        static_configs:
        - targets: ['172.18.0.1:8080']
    
      - job_name: 'node'
        scrape_interval: 8s
        static_configs:
          - targets: ['172.18.0.1:9100','49.235.160.131:9100']
    
    

    2.node_down.yml

    groups:
    - name: node_down
      rules:
      - alert: InstanceDown
        expr: up == 0
        for: 1m
        labels:
          user: test
        annotations:
          summary: "Instance {{ $labels.instance }} down"
          description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 1 minutes."
    
    

    启动服务

    docker-compose -f grafana.yml up -d
    
    docker-compose -f node-exporter.yml up -d
    
    docker-compose -f prometheus.yml up -d
    

    配置界面

    启动后访问你的grafana,地址是ip:3000,第一次需要修改默认密码(admin/admin)

    登录进来后,第一步需要先加prometheus数据源

    image.png

    第二步就是找一个官方模板,模板ID:1860,当然你也可以自己设计dashboard

    image.png

    这里需要选择第一步设置好的数据源

    image.png

    实际效果

    image.png

    查看其他的服务器信息

    image.png

    监控Docker主机模板ID:193(这个模板可以直接使用来监控docker来得到仪表盘)

    image.png

    相关文章

      网友评论

          本文标题:DockerCompose运行Grafana集成Promethe

          本文链接:https://www.haomeiwen.com/subject/emkxoltx.html