Prometheus是以operator方式部署 这里仅仅提供一个思路,万变不离其宗。 使用短信告警之前需要自己购买短信服务,然后定义好短信模板,一般都有现成的sdk,自己简单包装一下就可以使用了。

思路:通过自定义webhook的方式进行发送。
我简单写了一个webhook,项目地址:https://github.com/cool-ops/prometheus-alert-sms.git

部署

1、下载代码

  1. git clone https://github.com/cool-ops/prometheus-alert-sms.git

2、编译代码

  1. cd prometheus-alert-sms/
  2. sh build.sh

3、打包镜像

  1. docker build -t registry.cn-hangzhou.aliyuncs.com/rookieops/prometheus-alert-sms:v0.0.7 .

注:镜像地址更换成自己的仓库地址
4、推送镜像到镜像仓库

  1. docker push registry.cn-hangzhou.aliyuncs.com/rookieops/prometheus-alert-sms:v0.0.7

5、修改项目目录下的prometheus-alert-sms.yaml

  1. apiVersion: v1
  2. kind: ConfigMap
  3. metadata:
  4. name: sms-conf
  5. namespace: monitoring
  6. data:
  7. sms.yaml: |
  8. adapter:
  9. adapter_name: "RongLianYun"
  10. RongLianYun:
  11. baseUrl : "https://app.cloopen.com:8883"
  12. accountSid : "xxxxxx"
  13. appToken : "xxxxxx"
  14. appId : "xxxxx"
  15. templateId : "xxx"
  16. phones : ["11111111111","22222222222"]
  17. AliYun:
  18. aliRegion: "cn-hangzhou"
  19. accessKeyId: "xxxx"
  20. accessSecret: "xxxx"
  21. phoneNumbers: "11111111111,22222222222"
  22. signName: "xxxx"
  23. templateCode: "xxxx"
  24. ---
  25. apiVersion: apps/v1
  26. kind: Deployment
  27. metadata:
  28. name: prometheus-alert-sms
  29. namespace: monitoring
  30. spec:
  31. replicas: 1
  32. selector:
  33. matchLabels:
  34. app: prometheus-alert-sms
  35. template:
  36. metadata:
  37. labels:
  38. app: prometheus-alert-sms
  39. spec:
  40. containers:
  41. - name: prometheus-alert-sms
  42. image: registry.cn-hangzhou.aliyuncs.com/rookieops/prometheus-alert-sms:v0.0.7
  43. imagePullPolicy: IfNotPresent
  44. livenessProbe:
  45. failureThreshold: 3
  46. httpGet:
  47. path: /healthCheck
  48. port: tcp-9000
  49. scheme: HTTP
  50. initialDelaySeconds: 30
  51. periodSeconds: 10
  52. successThreshold: 1
  53. timeoutSeconds: 2
  54. readinessProbe:
  55. failureThreshold: 3
  56. httpGet:
  57. path: /healthCheck
  58. port: tcp-9000
  59. scheme: HTTP
  60. initialDelaySeconds: 30
  61. periodSeconds: 10
  62. successThreshold: 1
  63. timeoutSeconds: 2
  64. env:
  65. - name: CONFIG_PATH
  66. value: /app/conf/sms.yaml
  67. ports:
  68. - name: app-port
  69. containerPort: 9000
  70. protocol: TCP
  71. resources:
  72. limits:
  73. cpu: 500m
  74. memory: 1Gi
  75. requests:
  76. cpu: 500m
  77. memory: 1Gi
  78. volumeMounts:
  79. - name: sms-conf
  80. mountPath: /app/conf/sms.yaml
  81. subPath: sms.yaml
  82. volumes:
  83. - name: sms-conf
  84. configMap:
  85. name: sms-conf
  86. ---
  87. apiVersion: v1
  88. kind: Service
  89. metadata:
  90. name: prometheus-alter-sms
  91. namespace: monitoring
  92. spec:
  93. selector:
  94. app: prometheus-alert-sms
  95. ports:
  96. - name: app-port
  97. port: 9000
  98. targetPort: 9000
  99. protocol: TCP

到自己购买的短信服务获取对应的信息。
7、部署yaml文件

  1. kubectl apply -f prometheus-alert-sms.yaml

8、修改alertmanager的报警媒介

  1. ......
  2. - receiver: sms
  3. group_wait: 10s
  4. match:
  5. filesystem: node
  6. receivers:
  7. - name: 'sms'
  8. webhook_configs:
  9. - url: "http://prometheus-alter-sms.monitoring.svc:9000"
  10. send_resolved: true
  11. ......

然后如果有报警就可以正常接受到报警了。
image.png