k8s部署jvm exporter监控java应用

步骤

1.下载jvm exporter jar包

file

wget https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.18.0/jmx_prometheus_javaagent-0.18.0.jar

2.创建Dockerfile

FROM ubuntu:16.04 as jar
WORKDIR /
RUN apt-get update -y
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y wget
RUN wget https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.18.0/jmx_prometheus_javaagent-0.18.0.jar
FROM tomcat:jdk8-openjdk-slim
ADD prometheus-jmx-config.yaml /prometheus-jmx-config.yaml
COPY --from=jar /jmx_prometheus_javaagent-0.18.0.jar /jmx_prometheus_javaagent-0.18.0.jar

3.创建jvm export配置文件

cat 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

4.构建镜像

docker build -t jvm-exporter:1.0 .

file

file

5.创建deployment,svc

部署到监控的namespace,也可以部署到其他namespace,但是要注意有没有权限获取对应namespace中的资源。

cat jvm-exporter-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: jvm-exporter
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jvm-exporter
      name: jvm-exporter
  template:
    metadata:
      labels:
        app: jvm-exporter
        name: jvm-exporter
    spec:
      containers:
      - name: jvm-exporter
        image: registry-hz.rubikstack.com/library/jvm-exporter:1.0
        env:
        - name: JAVA_OPTS
          value: "-javaagent:/jmx_prometheus_javaagent-0.18.0.jar=8088:/prometheus-jmx-config.yaml"
---
apiVersion: v1
kind: Service
metadata:
  name: jvm-exporter
  labels:
    app: jvm-exporter
    name: jvm-exporter
spec:
  type: ClusterIP
  ports:
  - port: 8080
    protocol: TCP
    name: http
  - port: 8088
    protocol: TCP
    name: jmx-metrics
  selector:
    app: jvm-exporter
    name: jvm-exporter

file

6.创建ServiceMonitor

根据实际环境中修改。

cat servicemonitor.yaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: jvm-exporter
  namespace: cattle-monitoring-system
  labels:
    app: jvm-exporter
    name: jvm-exporter
spec:
  endpoints:
  - port: jmx-metrics
    interval: 5s
    path: /metrics
  namespaceSelector:
    matchNames:
    - cattle-monitoring-system
  selector:
    matchLabels:
      app: jvm-exporter
      name: jvm-exporter

file

file

7.grafana导入模板

使用的模板id是8563。输入job name:jvm-exporter。

file

也可以用12856。

file

0 0 投票数
文章评分
订阅评论
提醒
guest

0 评论
最旧
最新 最多投票
内联反馈
查看所有评论

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部
0
希望看到您的想法,请您发表评论x