1. 程式人生 > >Prometheus Operator自定義監控項

Prometheus Operator自定義監控項

Prometheus Operator預設的監控指標並不能完全滿足實際的監控需求,這時候就需要我們自己根據業務新增自定義監控。新增一個自定義監控的步驟如下: 1、建立一個ServiceMonitor物件,用於Prometheus新增監控項 2、為ServiceMonitor物件關聯metrics資料介面的Service物件 3、確保Services物件可以正確獲取到metrics資料 下面本文將以如何新增redis監控為例 ### 部署redis k8s-redis-and-exporter-deployment.yaml ```yaml --- apiVersion: v1 kind: Namespace metadata: name: redis --- apiVersion: apps/v1 kind: Deployment metadata: namespace: redis name: redis spec: replicas: 1 selector: matchLabels: app: redis template: metadata: annotations: prometheus.io/scrape: "true" prometheus.io/port: "9121" labels: app: redis spec: containers: - name: redis image: redis resources: requests: cpu: 100m memory: 100Mi ports: - containerPort: 6379 - name: redis-exporter image: oliver006/redis_exporter:latest resources: requests: cpu: 100m memory: 100Mi ports: - containerPort: 9121 ``` 部署redis的同時,我們把redis_exporter以sidecar的形式和redis服務部署在用一個Pod 另外注意,我們添加了`annotations:prometheus.io/scrape: "true" 和 prometheus.io/port: "9121"` ### 建立 Redis Service ```yaml apiVersion: v1 kind: Service metadata: name: redis-svc namespace: redis labels: app: redis spec: type: NodePort ports: - name: redis port: 6379 targetPort: 6379 - name: redis-exporter port: 9121 targetPort: 9121 selector: app: redis ``` 檢查下部署好的服務並驗證metrics能夠獲取到資料 ```shell [root@]# kubectl get po,ep,svc -n redis NAME READY STATUS RESTARTS AGE pod/redis-78446485d8-sp57x 2/2 Running 0 116m NAME ENDPOINTS AGE endpoints/redis-svc 100.102.126.3:9121,100.102.126.3:6379 6m5s NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/redis-svc NodePort 10.105.11