Tomcat部署监控

构建镜像

监控Tomcat需添加JMX,重新构建docker镜像

$ mkdir tomcat && cd tomcat
$ vim prometheus-jmx-config.yaml

ssl: false
lowercaseOutputName: false
lowercaseOutputLabelNames: false

$ vim Dockerfile

FROM tomcat:8.5.41-alpine
RUN wget -P /usr/local/tomcat/ https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.13.0/jmx_prometheus_javaagent-0.13.0.jar
ADD prometheus-jmx-config.yaml /usr/local/tomcat/prometheus-jmx-config.yaml
CMD [ "catalina.sh", "run" ]

$ docker build -t tomcat-jmx:8.5.41-alpine .

创建deployment

$ vim tomcat-jmx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-jmx
  namespace: tomcat
spec:
  selector:
    matchLabels:
      app: tomcat-jmx
  replicas: 1
  template:
    metadata:
      labels:
        app: tomcat-jmx
    spec:
      containers:
      - name: tomcat
        image: tomcat-jmx:8.5.41-alpine
        env:
        - name: JAVA_OPTS
          value: -javaagent:/usr/local/tomcat/jmx_prometheus_javaagent-0.13.0.jar=8088:/usr/local/tomcat/prometheus-jmx-config.yaml
$ kubectl cerate -f tomcat-jmx-deployment.yaml

测试监控端口是否能通

$ kubectl get pod -n tomcat -owide

NAME                          READY   STATUS    RESTARTS   AGE   IP            
tomcat-jmx-7cbd8f5897-bc7k2   1/1     Running   0          10m   172.31.0.181  

$ curl 172.31.0.181:8088/metrics

创建service与servicemonitor

$ vim tomcat-monitor.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app: tomcat-jmx                 #配置标签
  name: tomcat-jmx         #service名称
  namespace: tomcat                      #命名空间,与nginx-exporter相同
spec:
  ports:
  - name: metrics
    port: 8088
    protocol: TCP
    targetPort: 8088
  type: ClusterIP
  selector:
    app: tomcat-jmx                         #与nginx-exporter标签一致
status:
  loadBalancer: {}
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  generation: 1
  name: tomcat                           #target名称
  namespace: kubesphere-monitoring-system
spec:
  endpoints:
  - interval: 30s
    path: /metrics
    port: metrics
  namespaceSelector:
    matchNames:
    - tomcat                             #与service命名空间一致
  selector:
    matchLabels:
      app: tomcat-jmx               #与service标签一致
$ kubectl create -f tomcat-monitor.yaml