Prometheus-operator自定义监控ServiceMonitor

一、ServiceMonitor

1、介绍

用于监控指定的服务状态

 

感觉ServiceMonitor和PodMonitor差不多。

我这里使用的是Prometheus-operator,所以就直接使用Prometheus-operator的Prometheus

 

 

2、创建一个用于监控的测试项目

[root@master monitor]# cat ServiceMonitor_test_dep.yaml 
kind: Service
apiVersion: v1
metadata:
  name: example-app
  labels:
    app: example-app
spec:
  selector:
    app: example-app
  ports:
  - name: web
    port: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: example-app
  template:
    metadata:
      labels:
        app: example-app
    spec:
      containers:
      - name: example-app
        image: nginx:alpine
        ports:
        - name: web
          containerPort: 80

  

3、查看

[root@master monitor]# kubectl get ep -l app=example-app
NAME          ENDPOINTS           AGE
example-app   10.244.167.179:80   60m
[root@master monitor]# curl  10.244.167.179:80 -I
HTTP/1.1 200 OK
Server: nginx/1.17.10
Date: Thu, 11 Jun 2020 02:31:14 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 14 Apr 2020 14:46:22 GMT
Connection: keep-alive
ETag: "5e95ccbe-264"
Accept-Ranges: bytes

 

4、创建ServiceMonitor

[root@master monitor]# cat   ServiceMonitor_test.yaml 
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: monitor-example-app
  namespace: default
  labels:
    release: mypro  #Prometheus所选择的标签
spec:
  namespaceSelector: #监控的pod所在名称空间
    matchNames: 
    - default 
  selector:  #选择监控endpoint的标签
    matchLabels:
      app: example-app
  endpoints:
  - port: web #service中对应的端口名称

  

5、浏览器查看prometheus的Targets监控

 

posted @ 2020-06-11 10:39  巽逸  阅读(5431)  评论(0编辑  收藏  举报