Prometheus Operator User-defined service monitoring

The whole process

  1. Java Agent was installed and the Metrics interface was added

  2. Add a tag and port to the Deployment template such as metrics: jmx-metrics

  3. Metrics :jmx-metrics

  4. Create servicemonitor association with JMx-metrics

  5. Authorize users to access resources

configuration

2 See Prometheus -operator xupd- Openapi-platform. yaml

apiVersion: apps/v1 kind: Deployment metadata: name: xupd-openapi-platform namespace: xupd labels: app: xupd-openapi-platform metrics: jmx-metrics spec: replicas: 1 selector: matchLabels: app: xupd-openapi-platform template: metadata: labels: app: xupd-openapi-platform metrics: jmx-metrics spec: containers: - Name: xupd-Openapi-platform image: 172.16.12.43/xupd/ xupd-Openapi-platform/xupd-Openapi-platform :93 imagePullPolicy: Always env: - name: LC_ALL value: "C.UTF-8" - name: MY_NODE_NAME valueFrom: fieldRef: fieldPath: spec.nodeName - name: MY_NODE_IP valueFrom: fieldRef: fieldPath: status.hostIP ports: - containerPort: 7145 - containerPort: 6060 name: http-metrics readinessProbe: tcpSocket: port: 7145 volumeMounts: - name: app-logs mountPath: /home/project/xupd-openapi-platform/log/ - name: xupd-openapi-platform-log image: 172.16.12.43 / vv - base/beats/filebeat: 6.5.4 args: [" - c ", "/ etc/filebeat/filebeat. Yml", "- e,"] volumeMounts: - name: app-logs mountPath: /home/project/xupd-openapi-platform/log - name: xupd-openapi-platform-config mountPath: /etc/filebeat/ volumes: - name: app-logs emptyDir: {} - name: xupd-openapi-platform-config configMap: name: Xupd-openapi-platform-config added metrics: jmx-metrics-containerport: 6060 Name: http-metricsCopy the code
  1. service_plat.yaml

    ApiVersion: v1 kind: Service metadata: labels: metrics: jmx-metrics # ServiceMonitor Jmx-metrics namespace: xupd spec: ports: -name: http-metrics 6060 # Jmx-exporter exposed service port targetPort: http-metrics # pod YamL exposed port name Selector: metrics: Jmx-metrics # service itself tag selectorCopy the code
  2. servicemoniotr_plat.yaml

    ApiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor # prometry-operator CRD metadata: name: jmx-metrics namespace: monitoring labels: k8s-apps: jmx-metrics spec: jobLabel: Metrics # job=jmx-metrics selector: matchLabels: metrics: matchLabels =jmx-metrics selector: matchLabels: metrics Jmx-metrics: jmx-metrics service namespaceSelector: matchNames -xupd endpoints: -port: Http-metrics metrics metrics metrics metrics metrics metrics metrics metrics metrics metricsCopy the code
    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: prometheus-k8s
    rules:
    - apiGroups:
      - ""
      resources:
      - nodes
      - services
      - endpoints
      - pods
      - nodes/proxy
      verbs:
      - get
      - list
      - watch
    - apiGroups:
      - ""
      resources:
      - configmaps
      - nodes/metrics
      verbs:
      - get
    - nonResourceURLs:
      - /metrics
      verbs:
      - get
    
    Copy the code

misarrangement

kubectl delete pod prometheus-k8s-1 -n monitoring

kubectl logs prometheus-k8s-1 -n monitoring -c prometheus

Failed to list *v1.Endpoints: Endpoints is forbidden: User \"system:ser viceaccount:monitoring:prometheus-k8s\" cannot list resource \"endpoints\" in API group \"\" in the Namespace \"xupd\"" level=error TS = 2021-02-03t07:21:01.417Z Caller =klog.go:94 Component = k8s_client_Runtime func=ErrorDepth msg="/app/discovery/kubernetes/kubernetes.go:265: Failed to list *v1.Pod: pods is forbidden: User \"system:serviceaccount :monitoring:prometheus-k8s\" cannot list resource \"pods\" in API group \"\" in the Namespace \"xupd\"" level=error TS = 2021-02-03t07:21:01.423Z Caller =klog.go:94 Component = k8s_client_Runtime func=ErrorDepth msg="/app/discovery/kubernetes/kubernetes.go:264: Failed to list *v1.Service: services is forbidden: User \"system:servic eaccount:monitoring:prometheus-k8s\" cannot list resource \"services\" in API group \"\" in the namespace \"xupd\""Copy the code

Reference Documents:

Chanjarster. Making. IO/post/PROM – g… (java agent)

www.jianshu.com/p/7fb9e68a5,…).

Q.cnblogs.com/q/125431/ (grant…

www.cnblogs.com/scofield666…