应用场景

使用Prometheus 社区开发了 JMX Exporter 用于导出 JVM 的监控指标,以便使用 Prometheus 来采集监控数据。将Java 业务容器化至 Kubernetes 后,通过Prometheus收集jvm监控数据,并在grafana面便查看

接入流程

  • step 1:

    • 下载jmx_exporter

      wget -O /data/prometheus/jmx_exporter/jmx_prometheus_javaagent-0.17.2.jar https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.15.0/jmx_prometheus_javaagent-0.17.2.jar
      
    • 编写 JMX Exporter 配置文件 prometheus-jmx-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
              labels: {}
              type: GAUGE
              attrNameSnakeCase: true
            - pattern: 'java.lang<type=OperatingSystem><>((?!process_cpu_time)\w+):'
              name: os_$1
              labels: {}
              type: GAUGE
              attrNameSnakeCase: true
      
  • step 2:

    • 添加启动参数格式:-javaagent:/$YourDirectory/jmx_prometheus_javaagent-0.17.2.jar=8088:/$YourDirectory/prometheus-jmx-config.yaml

    • 修改原来应用的k8s service.yaml文件,添加8088端口和labels,以backend3rd-service为例:

      apiVersion: v1
      kind: Service
      metadata:
        labels:
          app: xxx-api
          kind: spring-boot-application #新增,用于下一个步骤ServiceMonitor使用
        name: xxx-api-service
      spec:
        type: NodePort
        ports:
        - port: 80
          targetPort: 8080
          nodePort: 31015
          name: xxx-api
        - port: 5005
          targetPort: 5005
          nodePort: 32005
          name: xxx-debug
        - port: 8088 #新增
          targetPort: 8088 #新增
          name: jmx-port #新增
        selector:
          app: xxx-api
          kind: spring-boot-application #新增
      
      
  • step 3:

    • 添加Prometheus监控配置,创建一个ServiceMonitor对象来配置Prometheus:

      apiVersion: monitoring.coreos.com/v1
      kind: ServiceMonitor
      metadata:
        name: springboot-jvm-monitor
        namespace: xxx
        labels:
          app: springboot-jvm-monitor
      spec:
        endpoints:
        - port: jmx-port
          interval: 5s
        namespaceSelector:
          matchNames:
          - xxx
        selector:
          matchLabels:
            kind: spring-boot-application
      
  • step 4:

    • 添加 Grafana 监控面板(https://grafana.com/grafana/dashboards/8563-jvm-dashboard/)
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐