基于docker搭建 JVM-微服务 监控(Prometheus+Grafana)

基于docker搭建 JVM-微服务 监控(Prometheus+Grafana)

一、JVM监控

基于docker安装node_exporter+prometheus+grafana

下载镜像包:

docker pull prom/node_exporter
docker pull prom/prometheus
docker pull grafana/grafana
docker pull timonwong/prometheus-webhook-dingtalk
1、启动node-exporter
docker run -d --name node_exporter -p 9100:9100 \
  -v "/proc:/host/proc:ro" \
  -v "/sys:/host/sys:ro" \
  -v "/:/rootfs:ro" \
  --net="host" \
  prom/node-exporter
2、启动prometheus

新建目录prometheus,编辑配置文件prometheus.yml

mkdir /opt/prometheus
cd /opt/prometheus/
vim prometheus.yml

内容如下:
##数据采集Job配置

scrape_configs:
  ###以下内容为prometheus自身配置
  - job_name: 'prometheus'
    static_configs:
      - targets: [ '192.168.1.4:9090' ]
        labels:
          appname: 'prometheus'
  ###以下内容为SpringBoot应用配置
  #- job_name: 'springboot_prometheus'
  #  scrape_interval: 5s
  #  metrics_path: '/actuator/prometheus'
  #  static_configs:
  #    - targets: [ '192.168.164.xx:9999' ]
  #      labels:
  #        appname: 'springboot_prometheus'
  ###以下内容为jmx-exporter应用配置
  - job_name: 'jmx-exporter'
    scrape_interval: 10s
    metrics_path: '/metrics'
    static_configs:
      - targets: [ '192.168.1.4:3010','192.168.1.4:3011','192.168.1.4:3012']
        labels:
          appname: 'jmx-exporter'

注意:修改IP地址,这里的192.168.1.4就是本机地址
启动prometheus

docker run  -d  --name prometheus \
  -p 9090:9090 \
  -v /Users/software/docker/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml  \
  prom/prometheus
 
docker cp node_alived.yml prometheus:/etc/prometheus/

等待几秒钟,查看端口状态

root@ubuntu:/opt/prometheus# netstat -anpt
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1147/sshd       
tcp        0     36 192.168.91.132:22       192.168.91.1:63648      ESTABLISHED 2969/0          
tcp        0      0 192.168.91.132:22       192.168.91.1:63340      ESTABLISHED 1321/1          
tcp6       0      0 :::9100                 :::*                    LISTEN      3070/node_exporter
tcp6       0      0 :::22                   :::*                    LISTEN      1147/sshd       
tcp6       0      0 :::9090                 :::*                    LISTEN      3336/docker-proxy

访问url:
http://192.168.1.4:9090/graph
效果如下:

访问targets,url如下:
http://192.168.1.4:9090/targets
效果如下:
在这里插入图片描述

如果状态没有UP起来,等待一会,就会UP了

3、启动grafana

新建空文件夹grafana-storage,用来存储数据

mkdir /grafana-storage

设置权限

chmod 777 -R /grafana-storage

因为grafana用户会在这个目录写入文件,直接设置777,比较简单粗暴!

启动grafana
docker run -d --name grafana \
  -p 3000:3000 \
  --name=grafana \
  -v /Users/software/docker/grafana-storage:/var/lib/grafana \
  grafana/grafana

等待几秒钟,查看端口状态:

root@ubuntu:/opt/prometheus# netstat -anpt
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1147/sshd       
tcp        0     36 192.168.91.132:22       192.168.91.1:63648      ESTABLISHED 2969/0          
tcp        0      0 192.168.91.132:22       192.168.91.1:63340      ESTABLISHED 1321/1          
tcp6       0      0 :::9100                 :::*                    LISTEN      3070/node_exporter
tcp6       0      0 :::22                   :::*                    LISTEN      1147/sshd       
tcp6       0      0 :::3000                 :::*                    LISTEN      3494/docker-proxy
tcp6       0      0 :::9090                 :::*                    LISTEN      3336/docker-proxy
tcp6       0      0 192.168.91.132:9100     172.17.0.2:55108        ESTABLISHED 3070/node_exporter

访问url:
http://192.168.1.4:3000/

默认会先跳转到登录页面,默认的用户名和密码都是admin
基于docker搭建 JVM-微服务 监控(Prometheus+Grafana)
登录之后,它会要求你重置密码。你还可以再输次admin密码!
基于docker搭建 JVM-微服务 监控(Prometheus+Grafana)
密码设置完成之后,就会跳转到首页基于docker搭建 JVM-微服务 监控(Prometheus+Grafana)

点击Add data source,由于使用的是镜像方式,所以版本比较新。和网络上的文章展示的图片不一样!
基于docker搭建 JVM-微服务 监控(Prometheus+Grafana)name名字写Prometheus
type 选择Prometheus,因为数据都从它那里获取
url 输入Prometheus的ip+端口

点击下面的Save & Test,如果出现绿色的,说明ok了
基于docker搭建 JVM-微服务 监控(Prometheus+Grafana)

import 导入选择模版:
基于docker搭建 JVM-微服务 监控(Prometheus+Grafana)

4、配置启动alertmanager
docker run -d --name alertmanager -p 9093:9093 -v /Users/software/docker/alertmanager.yml:/etc/alertmanager/alertmanager.yml prom/alertmanager

docker run -d --name alertmanager -p 9093:9093 -v /Users/software/docker/alertmanager.yml:/etc/alertmanager/alertmanager.yml prom/alertmanager
5、配置启动dingtalk
docker run --name webhook -p 8060:8060 -v /Users/software/docker/dingtalk/config.yml:/etc/prometheus-webhook-dingtalk/config.xml timonwong/prometheus-webhook-dingtalk
--ding.profile="webhook=https://oapi.dingtalk.com/robot/send?access_token=token"

1、Jmx_exporter + Prometheus +Grafana

1.1 下载Jmx_exporter架包

https://github.com/prometheus/jmx_exporter

1.2 在配置Jmx_exporter的config.yaml:
---
lowercaseOutputLabelNames: true
lowercaseOutputName: true
whitelistObjectNames: ["java.lang:type=OperatingSystem"]
blacklistObjectNames: []
rules:
  - pattern: 'java.lang<type=OperatingSystem><>(committed_virtual_memory|free_physical_memory|free_swap_space|total_physical_memory|total_swap_space)_size:'
    name: os_$1_bytes
    type: GAUGE
    attrNameSnakeCase: true
  - pattern: 'java.lang<type=OperatingSystem><>((?!process_cpu_time)\w+):'
    name: os_$1
    type: GAUGE
    attrNameSnakeCase: true
1.3 启动应用配置agent
java -javaagent:./jmx_prometheus_javaagent-0.12.0.jar=8698:config.yaml -jar yourJar.jar
1.4 配置Promethus的config:
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: ['localhost:9090']
  - job_name: 'monitor-demo'

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

    static_configs:
    - targets: ['localhost:8698']
1.5 在Import之前应首先在Grafana创建Promethus的数据源
1.6 Grafana使用的是模板是8563,直接Import就好
        https://grafana.com/grafana/dashboards/8563

2、Micrometer + Prometheus +Grafana

使用Grafana模板是4701
https://grafana.com/grafana/dashboards/4701

2.1 maven所需要的到依赖
<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.2.2</version>
</dependency>

<dependency>
  <groupId>io.github.mweirauch</groupId>
  <artifactId>micrometer-jvm-extras</artifactId>
  <version>0.2.0</version>
</dependency>
2.2 application.properties配置

(注意,这里没有配置management.endpoints.web.base-path,所以默认的请求路径是/actuator,而Promethus的路径是/actuator/prometheus)

server.port=8699
#spring.application.name=JVM-micrometer
management.metrics.tags.application=JVM-micrometer

#prometheus配置

management.metrics.export.prometheus.enabled=true
management.metrics.export.prometheus.step=1ms
management.metrics.export.prometheus.descriptions=true

management.endpoint.prometheus.enabled=true
management.endpoints.web.exposure.include=health,info,env,prometheus,metrics,httptrace,threaddump,heapdump,springmetrics
2.3 配置Prometheus的config:
- job_name: 'actuator-demo'
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
    metrics_path: '/actuator/prometheus'
    static_configs:
    - targets: ['localhost:8699']
2.4 在Grafana上Import4701即可

基于docker搭建 JVM-微服务 监控(Prometheus+Grafana)

3、接口调用监控

基于docker搭建 JVM-微服务 监控(Prometheus+Grafana)

服务链路追踪:
Zipkin或SkyWalking

4、Apache SkyWalking

官网文档:https://skywalking.apache.org/docs/main/latest/readme/

传送门:基于docker 搭建skywalking8.6.0-es7 链路追踪和JVM监控平台,钉钉告警

参考:
prometheus官网
SkyWalking官网
参考1
参考2
参考3

-------------欢迎各位留言交流,如有不正确的地方,请予以指正。【Q:981233589】

上一篇:K8s部署kube-state-metrics 并使用Prometheus监控(章节五)


下一篇:一个Service/Functional/Persistence的简单框架