成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Prometheus Operator自定義監控項

運維 系統運維
Prometheus Operator默認的監控指標并不能完全滿足實際的監控需求,這時候就需要我們自己根據業務添加自定義監控

[[357469]]

 Prometheus Operator默認的監控指標并不能完全滿足實際的監控需求,這時候就需要我們自己根據業務添加自定義監控。添加一個自定義監控的步驟如下:

1、創建一個ServiceMonitor對象,用于Prometheus添加監控項

2、為ServiceMonitor對象關聯metrics數據接口的Service對象

3、確保Services對象可以正確獲取到metrics數據

下面本文將以如何添加redis監控為例

部署redis

k8s-redis-and-exporter-deployment.yaml

  1. --- 
  2. apiVersion: v1 
  3. kind: Namespace 
  4. metadata: 
  5.   name: redis 
  6. --- 
  7. apiVersion: apps/v1 
  8. kind: Deployment 
  9. metadata: 
  10.   namespace: redis 
  11.   name: redis 
  12. spec: 
  13.   replicas: 1 
  14.   selector: 
  15.     matchLabels: 
  16.       app: redis 
  17.   template: 
  18.     metadata: 
  19.       annotations: 
  20.         prometheus.io/scrape: "true" 
  21.         prometheus.io/port: "9121" 
  22.       labels: 
  23.         app: redis 
  24.     spec: 
  25.       containers: 
  26.       - name: redis 
  27.         image: redis 
  28.         resources: 
  29.           requests: 
  30.             cpu: 100m 
  31.             memory: 100Mi 
  32.         ports: 
  33.         - containerPort: 6379 
  34.       - name: redis-exporter 
  35.         image: oliver006/redis_exporter:latest 
  36.         resources: 
  37.           requests: 
  38.             cpu: 100m 
  39.             memory: 100Mi 
  40.         ports: 
  41.         - containerPort: 9121 

 部署redis的同時,我們把redis_exporter以sidecar的形式和redis服務部署在用一個Pod

另外注意,我們添加了annotations:prometheus.io/scrape: "true" 和 prometheus.io/port: "9121"

創建 Redis Service

  1. apiVersion: v1 
  2. kind: Service 
  3. metadata: 
  4.   name: redis-svc 
  5.   namespace: redis 
  6.   labels: 
  7.     app: redis 
  8. spec: 
  9.   type: NodePort 
  10.   ports: 
  11.   - name: redis 
  12.     port: 6379 
  13.     targetPort: 6379 
  14.   - name: redis-exporter 
  15.     port: 9121 
  16.     targetPort: 9121 
  17.   selector: 
  18.     app: redis 

 檢查下部署好的服務并驗證metrics能夠獲取到數據

  1. [root@]# kubectl get po,ep,svc -n redis 
  2. NAME                         READY   STATUS    RESTARTS   AGE 
  3. pod/redis-78446485d8-sp57x   2/2     Running   0          116m 
  4.  
  5. NAME                  ENDPOINTS                               AGE 
  6. endpoints/redis-svc   100.102.126.3:9121,100.102.126.3:6379   6m5s 
  7.  
  8. NAME                TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)                         AGE 
  9. service/redis-svc   NodePort   10.105.111.177   <none>        6379:32357/TCP,9121:31019/TCP   6m5s 
  10.  
  11. 驗證metrics 
  12. [root@qd01-stop-k8s-master001 MyDefine]# curl 10.105.111.177:9121/metrics 
  13. # HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles. 
  14. # TYPE go_gc_duration_seconds summary 
  15. go_gc_duration_seconds{quantile="0"} 0 
  16. go_gc_duration_seconds{quantile="0.25"} 0 
  17. go_gc_duration_seconds{quantile="0.5"} 0 
  18. go_gc_duration_seconds{quantile="0.75"} 0 
  19. go_gc_duration_seconds{quantile="1"} 0 
  20. go_gc_duration_seconds_sum 0 
  21. go_gc_duration_seconds_count 0 
  22. # HELP go_goroutines Number of goroutines that currently exist. 
  23. # TYPE go_goroutines gauge 
  24. go_goroutines 8 
  25. # HELP go_info Information about the Go environment. 
  26. # TYPE go_info gauge 
  27. ............ 

 創建 ServiceMonitor

現在 Prometheus 訪問redis,接下來創建 ServiceMonitor 對象即可

  1. apiVersion: monitoring.coreos.com/v1 
  2. kind: ServiceMonitor 
  3. metadata: 
  4.   name: redis-k8s 
  5.   namespace: monitoring 
  6.   labels: 
  7.     app: redis 
  8. spec: 
  9.   jobLabel: redis 
  10.   endpoints: 
  11.   - port: redis-exporter 
  12.     interval: 30s 
  13.     scheme: http 
  14.   selector: 
  15.     matchLabels: 
  16.       app: redis 
  17.   namespaceSelector: 
  18.     matchNames: 
  19.     - redis 

 執行創建并查看-serviceMonitor

  1. [root@]# kubectl apply -f prometheus-serviceMonitorRedis.yaml 
  2. servicemonitor.monitoring.coreos.com/redis-k8s created 
  3.  
  4. [root@]# kubectl get serviceMonitor -n monitoring 
  5. NAME                      AGE 
  6. redis-k8s                 11s 

 現在切換到PrometheusUI界面查看targets,會發現多了剛才創建的redis-k8s監控項

 現在就可以查詢redis-exporter收集到的redis監控指標了


配置 PrometheusRule

我們現在能收集到redis的監控指標了,但是現在并沒有配置監控報警規則。需要我們自己根據實際關心的指標添加報警規則

首先我們看下Prometheus默認的規則,大概如下。


現在我們就來為redis添加一條規則,在 Prometheus的 Config 頁面下面查看關于 AlertManager 的配置:


上面 alertmanagers 實例的配置我們可以看到是通過角色為 endpoints 的 kubernetes 的服務發現機制獲取的,匹配的是服務名為 alertmanager-main,端口名為 web 的 Service 服務,我們查看下 alertmanager-main 這個 Service:

  1. [root@]# kubectl describe svc alertmanager-main -n monitoring 
  2. Name:              alertmanager-main 
  3. Namespace:         monitoring 
  4. Labels:            alertmanager=main 
  5. Annotations:       <none> 
  6. Selector:          alertmanager=main,app=alertmanager 
  7. Type:              ClusterIP 
  8. IP:                10.111.141.65 
  9. Port:              web  9093/TCP 
  10. TargetPort:        web/TCP 
  11. Endpoints:         100.118.246.1:9093,100.64.147.129:9093,100.98.81.194:9093 
  12. Session Affinity:  ClientIP 
  13. Events:            <none> 

可以看到服務名就是 alertmanager-main,Port 定義的名稱也是 web,符合上面的規則,所以 Prometheus 和 AlertManager 組件就正確關聯上了。而對應的報警規則文件位于:/etc/prometheus/rules/prometheus-k8s-rulefiles-0/目錄下面所有的 YAML 文件。可以進入 Prometheus 的 Pod 中驗證下該目錄下面是否有 YAML 文件:


這個YAML文件實際上就是我們之前創建的一個 PrometheusRule 文件包含的:

這里的 PrometheusRule 的 name 為 prometheus-k8s-rules,namespace 為 monitoring,我們可以猜想到我們創建一個 PrometheusRule 資源對象后,會自動在上面的 prometheus-k8s-rulefiles-0 目錄下面生成一個對應的-.yaml文件,所以如果以后我們需要自定義一個報警選項的話,只需要定義一個 PrometheusRule 資源對象即可。至于為什么 Prometheus 能夠識別這個 PrometheusRule 資源對象呢?這就查看我們創建的 prometheus( prometheus-prometheus.yaml) 這個資源對象了,里面有非常重要的一個屬性 ruleSelector,用來匹配 rule 規則的過濾器,要求匹配具有 prometheus=k8s 和 role=alert-rules 標簽的 PrometheusRule 資源對象,現在明白了吧?

  1. ruleSelector: 
  2.    matchLabels: 
  3.      prometheus: k8s 
  4.      role: alert-rules 

 所以要想自定義一個報警規則,只需要創建一個具有 prometheus=k8s 和 role=alert-rules 標簽的 PrometheusRule 對象就行了,比如現在我們添加一個redis是否可用的報警,我們可以通過redis_up這個指標檢查redis是否啟動,創建文件 prometheus-redisRules.yaml:

  1. apiVersion: monitoring.coreos.com/v1 
  2. kind: PrometheusRule 
  3. metadata: 
  4.   labels: 
  5.     prometheus: k8s 
  6.     role: alert-rules 
  7.   name: redis-rules 
  8.   namespace: monitoring 
  9. spec: 
  10.   groups: 
  11.   - name: redis 
  12.     rules: 
  13.     - alert: RedisUnavailable 
  14.       annotations: 
  15.         summary: redis instance info 
  16.         description: If redis_up == 0, redis will be unavailable 
  17.       expr: | 
  18.         redis_up == 0 
  19.       for: 3m 
  20.       labels: 
  21.         severity: critical 

 創建prometheusrule后,可以看到我們自己創建的redis-rules

  1. kubectl apply -f prometheus-redisRules.yaml 
  2.  
  3. kubectl get prometheusrule -n monitoring 
  4. NAME                   AGE 
  5. etcd-rules             4d18h 
  6. prometheus-k8s-rules   17d 
  7. redis-rules            15s 

注意 label 標簽一定至少要有 prometheus=k8s 或 role=alert-rules,創建完成后,隔一會兒再去容器中查看下 rules 文件夾:


現在看到我們創建的 rule 文件已經被注入到了對應的 rulefiles 文件夾下面了。然后再去 Prometheus的 Alert 頁面下面就可以查看到上面我們新建的報警規則了:


配置報警

現在我們知道了怎么去添加一個報警規則配置項,但是這些報警信息用怎樣的方式去發送呢?

這個就需要我們配置alertmanager

這里我以郵件和微信為例

alertmanager的配置文件alertmanager.yaml使用 alertmanager-secret.yaml 文件創建,這里看下默認的配置

cat alertmanager-secret.yaml

  1. apiVersion: v1 
  2. kind: Secret 
  3. metadata: 
  4.   name: alertmanager-main 
  5.   namespace: monitoring 
  6. stringData: 
  7.   alertmanager.yaml: |- 
  8.     "global"
  9.       "resolve_timeout""5m" 
  10.     "inhibit_rules"
  11.     - "equal"
  12.       - "namespace" 
  13.       - "alertname" 
  14.       "source_match"
  15.         "severity""critical" 
  16.       "target_match_re"
  17.         "severity""warning|info" 
  18.     - "equal"
  19.       - "namespace" 
  20.       - "alertname" 
  21.       "source_match"
  22.         "severity""warning" 
  23.       "target_match_re"
  24.         "severity""info" 
  25.     "receivers"
  26.     - "name""Default" 
  27.     - "name""Watchdog" 
  28.     - "name""Critical" 
  29.     "route"
  30.       "group_by"
  31.       - "namespace" 
  32.       "group_interval""5m" 
  33.       "group_wait""30s" 
  34.       "receiver""Default" 
  35.       "repeat_interval""12h" 
  36.       "routes"
  37.       - "match"
  38.           "alertname""Watchdog" 
  39.         "receiver""Watchdog" 
  40.       - "match"
  41.           "severity""critical" 
  42.         "receiver""Critical" 
  43. type: Opaque 

 現在我們需要修改這個文件,配置微信和郵件相關信息,前提你需要自行準備好企業微信相關信息,可以自行網上搜相關教程。

首先創建alertmanager.yaml文件

  1. global
  2.   resolve_timeout: 5m 
  3.   smtp_smarthost: 'smtp.51os.club:25' 
  4.   smtp_from: 'amos' 
  5.   smtp_auth_username: 'amos@51os.club' 
  6.   smtp_auth_password: 'Mypassword' 
  7.   smtp_hello: '51os.club' 
  8.   smtp_require_tls: false 
  9.   wechat_api_url: 'https://qyapi.weixin.qq.com/cgi-bin/' 
  10.   wechat_api_secret: 'SGGc4x-RDcVD_ptvVhYrxxxxxxxxxxOhWVWIITRxM' 
  11.   wechat_api_corp_id: 'ww419xxxxxxxx735e1c0' 
  12.  
  13. templates: 
  14. '*.tmpl' 
  15.  
  16. route: 
  17.   group_by: ['job''severity'
  18.   group_wait: 30s 
  19.   group_interval: 5m 
  20.   repeat_interval: 12h 
  21.   receiver: default 
  22.   routes: 
  23.   - receiver: wechat 
  24.     continuetrue 
  25.     match: 
  26.       alertname: Watchdog 
  27.  
  28. receivers: 
  29. name'default' 
  30.   email_configs: 
  31.   - to'10xxxx1648@qq.com' 
  32.     send_resolved: true 
  33. name'wechat' 
  34.   wechat_configs: 
  35.   - send_resolved: false 
  36.     corp_id: 'ww419xxxxxxxx35e1c0' 
  37.     to_party: '13' 
  38.     message: '{{ template "wechat.default.message" . }}' 
  39.     agent_id: '1000003' 
  40.     api_secret: 'SGGc4x-RDcxxxxxxxxY6YwfZFsO9OhWVWIITRxM' 

 我這里添加了兩個接收器,默認的通過郵箱進行發送,對于 Watchdog 這個報警我們通過 webhook 來進行發送,這個 webhook 就是wechat。

說明我這里偷懶,因為現在系統剛好有一個報警Watchdog,所以我這里匹配了 Watchdog 這個報警,當然您可以換成我們自定義的redis的監控RedisUnavailable

 然后使用在創建一個templates文件,這個文件是發微信消息的模板wechat.tmpl:

  1. {{ define "wechat.default.message" }} 
  2. {{- if gt (len .Alerts.Firing) 0 -}} 
  3. {{- range $index, $alert := .Alerts -}} 
  4. {{- if eq $index 0 -}} 
  5. AlertTpye: {{ $alert.Labels.alertname }} 
  6. AlertLevel: {{ $alert.Labels.severity }} 
  7.  
  8. ===================== 
  9. {{- end }} 
  10. ===Alert Info=== 
  11. Alert Info: {{ $alert.Annotations.message }} 
  12. Alert Time: {{ $alert.StartsAt.Format "2006-01-02 15:04:05" }} 
  13. ===More Info=== 
  14. {{ if gt (len $alert.Labels.instance) 0 -}}InstanceIp: {{ $alert.Labels.instance }};{{- end -}} 
  15. {{- if gt (len $alert.Labels.namespace) 0 -}}InstanceNamespace: {{ $alert.Labels.namespace }};{{- end -}} 
  16. {{- if gt (len $alert.Labels.node) 0 -}}NodeIP: {{ $alert.Labels.node }};{{- end -}} 
  17. {{- if gt (len $alert.Labels.pod_name) 0 -}}PodName: {{ $alert.Labels.pod_name }}{{- end }} 
  18. ===================== 
  19. {{- end }} 
  20. {{- end }} 
  21.  
  22. {{- if gt (len .Alerts.Resolved) 0 -}} 
  23. {{- range $index, $alert := .Alerts -}} 
  24. {{- if eq $index 0 -}} 
  25. AlertTpye: {{ $alert.Labels.alertname }} 
  26. AlertLevel: {{ $alert.Labels.severity }} 
  27.  
  28. ===================== 
  29. {{- end }} 
  30. ===Alert Info=== 
  31. Alert Info: {{ $alert.Annotations.message }} 
  32. Alert Start Time: {{ $alert.StartsAt.Format "2006-01-02 15:04:05" }} 
  33. Alert Fix Time: {{ $alert.EndsAt.Format "2006-01-02 15:04:05" }} 
  34. ===More Info=== 
  35. {{ if gt (len $alert.Labels.instance) 0 -}}InstanceIp: {{ $alert.Labels.instance }};{{- end -}} 
  36. {{- if gt (len $alert.Labels.namespace) 0 -}}InstanceNamespace: {{ $alert.Labels.namespace }};{{- end -}} 
  37. {{- if gt (len $alert.Labels.node) 0 -}}NodeIP: {{ $alert.Labels.node }};{{- end -}} 
  38. {{- if gt (len $alert.Labels.pod_name) 0 -}}PodName: {{ $alert.Labels.pod_name }};{{- end }} 
  39. ===================== 
  40. {{- end }} 
  41. {{- end }} 
  42. {{- end }} 

 現在我們先刪除原來的 alertmanager-main secret,然后再基于alertmanager.yaml和wechat.tmpl創建alertmanager-main secret 

  1. kubectl delete secret alertmanager-main -n monitoring 
  2. kubectl create secret generic alertmanager-main --from-file=alertmanager.yaml --from-file=wechat.tmpl -n monitoring 

 上面的步驟創建完成后,很快我們就會收到一條wechat消息,同樣郵箱中也會收到報警信息:


再次查看 AlertManager 的配置信息可以看到已經變成上面我們的配置信息了

 

責任編輯:姜華 來源: 今日頭條
相關推薦

2021-03-26 20:37:14

Prometheus監控指標

2021-11-08 09:00:00

PrometheusKubernetes集群

2023-12-29 08:01:52

自定義指標模板

2021-05-28 08:58:41

Golang網卡metrics

2023-03-26 08:41:37

2021-10-28 08:39:22

Node Export自定義 監控

2013-01-10 09:36:19

NagiosNagios插件

2016-02-26 14:57:50

飛象網

2015-02-12 15:33:43

微信SDK

2011-04-06 15:05:58

nagios監控Linux

2015-02-12 15:38:26

微信SDK

2016-12-26 15:25:59

Android自定義View

2016-11-16 21:55:55

源碼分析自定義view androi

2011-06-23 10:49:13

Qt 自定義信號

2009-07-06 16:59:26

JSP自定義標簽

2022-04-24 15:17:56

鴻蒙操作系統

2013-04-01 14:35:10

Android開發Android自定義x

2009-06-08 20:13:36

Eclipse自定義控

2015-01-14 15:06:48

定義相機

2021-11-23 15:06:42

Kubernetes 運維開源
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美极品少妇xxxxⅹ免费视频 | 91精品国产一区二区 | 国产一区2区 | 日韩综合在线 | 欧美不卡视频 | 久久欧美精品 | 日韩在线观看精品 | 正在播放国产精品 | www.v888av.com| 日韩视频国产 | 黑人精品| 99影视 | 99精品国产一区二区青青牛奶 | 精品综合视频 | 精品综合久久 | 国产一区二 | 亚洲一区中文字幕 | 人人干免费 | 99精品一级欧美片免费播放 | 精产国产伦理一二三区 | 夜夜骑av| 精品视频在线观看 | 91在线看 | 爱综合| 国产精品无码久久久久 | 亚洲国产精品美女 | 日韩精品一区二区三区视频播放 | 中文字幕乱码一区二区三区 | 草b视频 | 亚洲一区欧美一区 | 久久综合久久久 | 一区二区视频在线 | 999久久久久久久久6666 | 日韩精品久久一区二区三区 | 亚洲444kkkk在线观看最新 | 欧美三区视频 | 麻豆视频国产在线观看 | 欧美影院久久 | 日本亚洲一区二区 | 欧美精品久久久久久 | 久久激情视频 |