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

Prometheus Operator自定義監(jiān)控項(xiàng)

運(yùn)維 系統(tǒng)運(yùn)維
Prometheus Operator默認(rèn)的監(jiān)控指標(biāo)并不能完全滿(mǎn)足實(shí)際的監(jiān)控需求,這時(shí)候就需要我們自己根據(jù)業(yè)務(wù)添加自定義監(jiān)控

[[357469]]

 Prometheus Operator默認(rèn)的監(jiān)控指標(biāo)并不能完全滿(mǎn)足實(shí)際的監(jiān)控需求,這時(shí)候就需要我們自己根據(jù)業(yè)務(wù)添加自定義監(jiān)控。添加一個(gè)自定義監(jiān)控的步驟如下:

1、創(chuàng)建一個(gè)ServiceMonitor對(duì)象,用于Prometheus添加監(jiān)控項(xiàng)

2、為ServiceMonitor對(duì)象關(guān)聯(lián)metrics數(shù)據(jù)接口的Service對(duì)象

3、確保Services對(duì)象可以正確獲取到metrics數(shù)據(jù)

下面本文將以如何添加redis監(jiān)控為例

部署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的同時(shí),我們把redis_exporter以sidecar的形式和redis服務(wù)部署在用一個(gè)Pod

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

創(chuàng)建 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 

 檢查下部署好的服務(wù)并驗(yàn)證metrics能夠獲取到數(shù)據(jù)

  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. 驗(yàn)證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. ............ 

 創(chuàng)建 ServiceMonitor

現(xiàn)在 Prometheus 訪問(wèn)redis,接下來(lái)創(chuàng)建 ServiceMonitor 對(duì)象即可

  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 

 執(zhí)行創(chuàng)建并查看-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 

 現(xiàn)在切換到PrometheusUI界面查看targets,會(huì)發(fā)現(xiàn)多了剛才創(chuàng)建的redis-k8s監(jiān)控項(xiàng)

 現(xiàn)在就可以查詢(xún)r(jià)edis-exporter收集到的redis監(jiān)控指標(biāo)了


配置 PrometheusRule

我們現(xiàn)在能收集到redis的監(jiān)控指標(biāo)了,但是現(xiàn)在并沒(méi)有配置監(jiān)控報(bào)警規(guī)則。需要我們自己根據(jù)實(shí)際關(guān)心的指標(biāo)添加報(bào)警規(guī)則

首先我們看下Prometheus默認(rèn)的規(guī)則,大概如下。


現(xiàn)在我們就來(lái)為redis添加一條規(guī)則,在 Prometheus的 Config 頁(yè)面下面查看關(guān)于 AlertManager 的配置:


上面 alertmanagers 實(shí)例的配置我們可以看到是通過(guò)角色為 endpoints 的 kubernetes 的服務(wù)發(fā)現(xiàn)機(jī)制獲取的,匹配的是服務(wù)名為 alertmanager-main,端口名為 web 的 Service 服務(wù),我們查看下 alertmanager-main 這個(gè) 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> 

可以看到服務(wù)名就是 alertmanager-main,Port 定義的名稱(chēng)也是 web,符合上面的規(guī)則,所以 Prometheus 和 AlertManager 組件就正確關(guān)聯(lián)上了。而對(duì)應(yīng)的報(bào)警規(guī)則文件位于:/etc/prometheus/rules/prometheus-k8s-rulefiles-0/目錄下面所有的 YAML 文件??梢赃M(jìn)入 Prometheus 的 Pod 中驗(yàn)證下該目錄下面是否有 YAML 文件:


這個(gè)YAML文件實(shí)際上就是我們之前創(chuàng)建的一個(gè) PrometheusRule 文件包含的:

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

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

 所以要想自定義一個(gè)報(bào)警規(guī)則,只需要?jiǎng)?chuàng)建一個(gè)具有 prometheus=k8s 和 role=alert-rules 標(biāo)簽的 PrometheusRule 對(duì)象就行了,比如現(xiàn)在我們添加一個(gè)redis是否可用的報(bào)警,我們可以通過(guò)redis_up這個(gè)指標(biāo)檢查redis是否啟動(dòng),創(chuàng)建文件 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 

 創(chuàng)建prometheusrule后,可以看到我們自己創(chuàng)建的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 標(biāo)簽一定至少要有 prometheus=k8s 或 role=alert-rules,創(chuàng)建完成后,隔一會(huì)兒再去容器中查看下 rules 文件夾:


現(xiàn)在看到我們創(chuàng)建的 rule 文件已經(jīng)被注入到了對(duì)應(yīng)的 rulefiles 文件夾下面了。然后再去 Prometheus的 Alert 頁(yè)面下面就可以查看到上面我們新建的報(bào)警規(guī)則了:


配置報(bào)警

現(xiàn)在我們知道了怎么去添加一個(gè)報(bào)警規(guī)則配置項(xiàng),但是這些報(bào)警信息用怎樣的方式去發(fā)送呢?

這個(gè)就需要我們配置alertmanager

這里我以郵件和微信為例

alertmanager的配置文件alertmanager.yaml使用 alertmanager-secret.yaml 文件創(chuàng)建,這里看下默認(rèn)的配置

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 

 現(xiàn)在我們需要修改這個(gè)文件,配置微信和郵件相關(guān)信息,前提你需要自行準(zhǔn)備好企業(yè)微信相關(guān)信息,可以自行網(wǎng)上搜相關(guān)教程。

首先創(chuàng)建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' 

 我這里添加了兩個(gè)接收器,默認(rèn)的通過(guò)郵箱進(jìn)行發(fā)送,對(duì)于 Watchdog 這個(gè)報(bào)警我們通過(guò) webhook 來(lái)進(jìn)行發(fā)送,這個(gè) webhook 就是wechat。

說(shuō)明我這里偷懶,因?yàn)楝F(xiàn)在系統(tǒng)剛好有一個(gè)報(bào)警Watchdog,所以我這里匹配了 Watchdog 這個(gè)報(bào)警,當(dāng)然您可以換成我們自定義的redis的監(jiān)控RedisUnavailable

 然后使用在創(chuàng)建一個(gè)templates文件,這個(gè)文件是發(fā)微信消息的模板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 }} 

 現(xiàn)在我們先刪除原來(lái)的 alertmanager-main secret,然后再基于alertmanager.yaml和wechat.tmpl創(chuàng)建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 

 上面的步驟創(chuàng)建完成后,很快我們就會(huì)收到一條wechat消息,同樣郵箱中也會(huì)收到報(bào)警信息:


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

 

責(zé)任編輯:姜華 來(lái)源: 今日頭條
相關(guān)推薦

2021-03-26 20:37:14

Prometheus監(jiān)控指標(biāo)

2021-11-08 09:00:00

PrometheusKubernetes集群

2023-12-29 08:01:52

自定義指標(biāo)模板

2021-05-28 08:58:41

Golang網(wǎng)卡metrics

2023-03-26 08:41:37

2021-10-28 08:39:22

Node Export自定義 監(jiān)控

2013-01-10 09:36:19

NagiosNagios插件

2016-02-26 14:57:50

飛象網(wǎng)

2015-02-12 15:33:43

微信SDK

2011-04-06 15:05:58

nagios監(jiān)控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 自定義信號(hào)

2009-07-06 16:59:26

JSP自定義標(biāo)簽

2022-04-24 15:17:56

鴻蒙操作系統(tǒng)

2013-04-01 14:35:10

Android開(kāi)發(fā)Android自定義x

2021-11-23 15:06:42

Kubernetes 運(yùn)維開(kāi)源

2009-06-08 20:13:36

Eclipse自定義控

2011-12-16 14:23:51

Java
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 日本小电影网站 | 91免费在线看 | 欧美精品久久 | 色婷婷综合久久久中文字幕 | 国产成人免费在线观看 | 日本不卡高清视频 | 国产精品视频一区二区三区 | 日本啊v在线 | 午夜影院普通用户体验区 | 久久久女女女女999久久 | 国产黄色av网站 | www日本在线观看 | 亚洲激情第一页 | 日本人做爰大片免费观看一老师 | 欧美极品一区二区 | 国产欧美精品区一区二区三区 | 在线视频亚洲 | 午夜天堂精品久久久久 | 久久香焦 | 伊人久久精品一区二区三区 | 国产精品99久久久久久大便 | 精品久久久久久亚洲综合网 | 91精品国产综合久久精品 | 免费一区二区三区 | 亚洲网站在线观看 | 精产国产伦理一二三区 | 亚洲一区久久久 | 久久久久久久久久久久久久av | 男女激情网站免费 | 中文字幕在线免费观看 | 人成久久| 欧美一区二区三区 | 日韩亚洲一区二区 | 日本免费一区二区三区 | 亚洲日本激情 | 国产在线一区二区三区 | 精品久久香蕉国产线看观看亚洲 | 麻豆视频国产在线观看 | 中文字幕乱码亚洲精品一区 | 中文字幕亚洲精品 | 亚洲视频一区二区三区 |