Promethues监控搭建

1. 前言

Promethues用于全方面的监控系统,前边已经整理过相关的内容了:基础指标

本篇主要关注于搭建,包括mysql、redis、springboot的监控。

原则:各exporter可以访问当对应的目标,而Promethues可以访问到 各个exporter

2. 监控mysql

步骤:

  1. 启动mysqld_exporter

      diego-mysql-exporter:
        image: prom/mysqld-exporter:v0.13.0
        hostname: diego-mysql-exporter
        environment:
           DATA_SOURCE_NAME: "exporter:123456@(diego-mysql:3306)/"
        networks:
          - diego-network
        expose:
          - 9104
        deploy:
          replicas: 1
    
  2. mysql中创建专门的账号

    create user 'exporter'@'%' identified by '123456' with MAX_USER_CONNECTIONS 3 ;
    grant process,replication client,select on *.* to 'exporter'@'%';
    
  3. 配置Promethues

      - job_name: "mysql"
        static_configs:
          - targets: ["diego-mysql-exporter:9104"]
    
  4. 配置grafana

参考:

3. 监控redis

步骤:

  1. 启动redis_exporter

      diego-redis-exporter:
        image: oliver006/redis_exporter:v1.37.0
        hostname: diego-redis-exporter
        command:
          - '--redis.addr=redis://diego-cache:6379'
          # - '--redis.password=123456'
        networks:
          - diego-network
        expose:
          - 9121
        deploy:
          replicas: 1
    
  2. 配置Promethues

      - job_name: "redis"
        static_configs:
          - targets: ["diego-redis-exporter:9121"]
    
  3. 查看grafana

    采用763的面板id进行配置 面板链接

参考:

4. 监控springboot

Prometheus 监控服务基于 Spring Actuator 机制采集 JVM 等数据,结合配套提供的 Grafana Dashboard 可以方便的监控 Spring Boot 应用的状态。

步骤:

  1. 配置springboot

    除了actuator之外,增加一个micrometer-registry-prometheus

     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
        <version>1.5.1</version>
    </dependency>
    
  2. 通过配置中心来访问所有服务

    对于SpringBoot以上方式已经满足需要,但对于SpringCloud如果没有服务都需要手动去配置到Promethues就会很麻烦,有没有可能通过配置中心来自动进行配置?

    这系列3篇文章给出思路,通过服务发现的方式来进行监控,文中给出的是Promethues与consul,由于我们用的是nacos所以只能借鉴原理;

    具体思路是各个服务都开actuator,Promethues通过服务中心去获取各个服务,再去监控各个服务,这样避免了手动配置。

    参考:主参辅参,在gateway上追加一个adapter,有它实现对Promethues访问的代理,返回给Promethues各服务的监控地址

    <dependency>
        <groupId>io.github.chen-gliu</groupId>
        <artifactId>nacos-consul-adapter</artifactId>
        <version>0.0.4.M</version>
    </dependency>
    

    这一部分需要再去下下功夫,后续维护跟不上,会产生问题。

  3. 配置Promethues

      - job_name: "springcloud-gateway"
        metrics_path: "/actuator/prometheus"
        consul_sd_configs:
          - server: 192.168.31.170:7878
    
  4. 配置grafana

参考:

5. Grafana的使用

步骤:

  1. 安装

      diego-grafana:
        image: grafana/grafana:8.5.0
        hostname: diego-grafana
        privileged: true
        user: root
        environment:
          - 'GF_INSTALL_PLUGINS=grafana-clock-panel,grafana-simple-json-datasource,alexanderzobnin-zabbix-app'
          - 'GF_SECURITY_ADMIN_PASSWORD=123456'
        volumes:
          - /home/sun/database/grafana:/var/lib/grafana
        networks:
          - diego-network
        ports:
          - 3000:3000
        deploy:
          replicas: 1
    

    通过:http://127.0.0.1:9090/targets、http://127.0.0.1:9090可以访问Promethues,查看各个export的内容

    访问:http://127.0.0.1:3000/

  2. 设置Promethues数据源

  3. import各自的dashboard

参考:

6. 附录

  • docker-compose

    version: '3.4'
    
    services:
      diego-prometheus:
        image: prom/prometheus:v2.32.1
        user: "1000:1000"
        hostname: diego-prometheus
        volumes:
          - ./configs/prometheus.yml:/etc/prometheus/prometheus.yml
          -  /home/sun/database/prometheus_data:/prometheus
        command:
          - '--config.file=/etc/prometheus/prometheus.yml'
          - '--storage.tsdb.path=/prometheus'
          - '--web.console.libraries=/etc/prometheus/console_libraries'
          - '--web.console.templates=/etc/prometheus/consoles'
          - '--web.enable-lifecycle'
        ports:
          - "9090:9090"
        networks:
          - diego-network
        deploy:
          replicas: 1
    
      diego-node-exporter:
        image: prom/node-exporter:v1.3.1
        hostname: diego-node-exporter
        volumes:
          - /proc:/host/proc:ro
          - /sys:/host/sys:ro
          - /:/rootfs:ro
        command:
          - '--path.procfs=/host/proc'
          - '--path.rootfs=/rootfs'
          - '--path.sysfs=/host/sys'
          - '--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc)($$|/)'
        networks:
          - diego-network
        expose:
          - 9100
        deploy:
          replicas: 1
    
      diego-mysql-exporter:
        image: prom/mysqld-exporter:v0.13.0
        hostname: diego-mysql-exporter
        environment:
           DATA_SOURCE_NAME: "exporter:123456@(diego-mysql:3306)/"
        networks:
          - diego-network
        expose:
          - 9104
        deploy:
          replicas: 1
    
      diego-redis-exporter:
        image: oliver006/redis_exporter:v1.37.0
        hostname: diego-redis-exporter
        command:
          - '--redis.addr=redis://diego-cache:6379'
          # - '--redis.password=123456'
        networks:
          - diego-network
        expose:
          - 9121
        deploy:
          replicas: 1
    
    networks:
      diego-network: 
        external: true
    
    
  • prometheus.yml

    global:
      scrape_interval: 1m
    
    scrape_configs:
      - job_name: "prometheus"
        scrape_interval: 1m
        static_configs:
          - targets: ["localhost:9090"]
    
      - job_name: "node"
        static_configs:
          - targets: ["diego-node-exporter:9100"]
    
      - job_name: "mysql"
        static_configs:
          - targets: ["diego-mysql-exporter:9104"]
    
      - job_name: "redis"
        static_configs:
          - targets: ["diego-redis-exporter:9121"]
    
      - job_name: "springcloud-gateway"
        metrics_path: "/actuator/prometheus"
        consul_sd_configs:
          - server: 192.168.31.170:7878
    
    
# 微服务 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×