Rancher中可以很方便的开启监控功能,其使用的是Prometheus Operator + Grafana,那么我们也可以利用它来采集JVM数据。
首先,开启集群的监控:
然后,开启项目的监控:
你的Java应用的镜像得配置JMX Exporter,配置方法见 使用Prometheus+Grafana监控JVM ,我在这里选择将JMX Exporter端口设置为6060。
然后在你的Deployment/StatefulSets 中配置这个端口:
apiVersion: apps/v1 kind: Deployment metadata: name: ... namespace: ... spec: selector: matchLabels: app: ... replicas: 1 template: metadata: labels: app: ... spec: containers: - name: ... image: ... ports: - containerPort: 6060 name: http-metrics - ...
和 Service 也一样:
apiVersion: v1 kind: Service metadata: namespace: ... name: ... labels: app: ... needMonitor: 'true' spec: ports: - port: 6060 targetPort: http-metrics protocol: TCP name: http-metrics - ... selector: app: ...
可以看到,我把端口取了个名字叫做http-metrics,同时Service添加了Label needMonitor: 'true'
ServiceMonitor是Prometheus Operator定义的CRD:
apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: ... namespace: ... spec: selector: matchLabels: needMonitor: 'true' endpoints: - port: http-metrics path: /metrics
这样Prometheus就能把同namespace下的所有 needMonitor: 'true'
的Service的JMX Exporter都采集到。
你需要给Grafana添加JVM Dashboard,在这之前你需要设置Grafana的admin密码,进入项目找到Grafana,进入其Shell:
执行:
grafana-cli admin reset-admin-password <新密码>
然后随便进入一个Deployment/StatefulSets,进入Grafana:
用admin账号和你刚才设置的密码登录进去,进入管理页面导入Dashboard:
到 https://grafana.com/orgs/chanjarster/dashboards 找到 JVM dashboard (for Prometheus Operator) ,看到它的编号是8878。把这个编号填到导入页面:
然后大功告成: