当前位置: 首页 > news >正文

alertmanage调用企业微信告警(k8s内部署)

一、前言

    alertmanage调用企业微信应用告警会比直接使用钉钉告警更麻烦一点,调用企业微信应用告警需要在应用内配置企业可信ip,不然调用企业微信接口就会报错,提示ip地址有风险

二、部署

   先自行创建企业微信,再使用管理后台创建应用

 

 记录下agentid和secret值,后面配置调用企业微信告警会用到

往下找到企业可信ip,先按时提示验证企业 

 

 会提示让你输入域名,然后给出一个txt文件,需要使用nginx挂载这个txt文件到域名上,要使用该域名访问到该路径,就可以通过验证

只需在该域名的nginx代理中加入一下该配置项,匹配txt结尾的请求,到该root路径下,将txt文件放到该root路径下即可,重新加载nginx配置,就可以去验证企业可信ip了

查看企业微信的企业id

部署alertmanage

创建存放yaml配置的目录

mkdir /opt/alertmanage && cd /opt/alertmanage

编辑alertmanage配置文件,使用configmap服务挂载

vi configmap.yaml

kind: ConfigMap
apiVersion: v1
metadata:name: alertmanagernamespace: prometheus
data:alertmanager.yml: |-global:resolve_timeout: 1msmtp_smarthost: 'smtp.qq.com:25'     #配置告警邮箱服务器smtp_from: '123456@qq.com'    #告警发送到的邮箱smtp_auth_username: '123456@qq.com'  smtp_auth_password: 'ioasdgxvovclbsfb'   #授权码,在邮箱里面获取smtp_require_tls: false  wechat_api_url: "https://qyapi.weixin.qq.com/cgi-bin/"  #企业微信的api接口,统一固定的wechat_api_corp_id: "ww7490f1231244252"   #上面获取的企业微信中的企业idtemplates:- '/etc/alertmanager/template/*.tmpl'       #使用告警模板route:group_by: ['alertname_wechat']group_wait: 30s    #告警等待发送时间group_interval: 5mrepeat_interval: 15m   #重复告警的再次发送时间receiver: wechat       #调用的告警组件,下面配置的组件名称receivers:- name: 'wechat'wechat_configs:- send_resolved: truecorp_id: ww7490f1231244252  #上面获取的企业微信中的企业idto_user: '@all'   #发送给所有人agent_id: 1000002   #上面获取的企业微信应用中的agentidapi_secret: J6WJQ7QXEY5zodi12342352342  #上面获取的企业微信应用中的secret

编辑alertmanage使用的告警模板,使用configmap服务挂载

vi configmap-wechat.yaml

apiVersion: v1   
data:WeChat.tmpl: |-     #配置告警消息模板文件{{ define "wechat.default.message" }}{{- if gt (len .Alerts.Firing) 0 -}}   #判断是告警信息还是异常恢复信息,是告警则继续往下执行,不是则不往下执行{{- range $index, $alert := .Alerts -}}   #循环输出多个告警信息{{- if eq $index 0 -}}    #判断是否输出完毕{{- end }}    #对应第一个判断=========莓族环境监控报警 =========告警类型: {{ $alert.Labels.alertname }}告警级别: {{ $alert.Labels.severity }}故障主机: {{ $alert.Labels.instance }}告警主题: {{ $alert.Annotations.summary }}告警详情: {{ $alert.Annotations.description }}触发阀值:{{ $alert.Annotations.value }}故障时间: {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}{{ if gt (len $alert.Labels.instance) 0 -}}故障实例: {{ $alert.Labels.instance }}{{- end -}}{{- end }}   #对应循环输出告警信息{{- end }}   #对应最后一个判断========= = end =  ========={{- if gt (len .Alerts.Resolved) 0 -}}  #判断是告警信息还是异常恢复信息{{- range $index, $alert := .Alerts.Resolved -}}  #循环输出多个异常恢复信息{{- if eq $index 0 -}}    #判断是否输出完毕{{- end }}               #对应第一个判断=========莓族环境异常恢复 =========恢复类型: {{ $alert.Labels.alertname }}恢复级别: {{ $alert.Labels.severity }}系统环境: {{ $alert.Labels.environment }}恢复主题: {{ $alert.Annotations.summary }}告警详情: {{ $alert.Annotations.description }}故障时间: {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}恢复时间: {{ ($alert.EndsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}{{ if gt (len $alert.Labels.instance) 0 -}}故障实例: {{ $alert.Labels.instance }}{{- end -}}{{- end }}  #对应循环输出多个异常恢复信息{{- end }}  #对应最后一个判断{{- end }}   #整个流程结束
kind: ConfigMap
metadata:name: wechatnamespace: prometheus

以上的变量取值都来自于配置的prometheus告警配置和alertmanage的值

 

 编辑部署alertmanage服务的deployment的yaml

apiVersion: apps/v1
kind: Deployment
metadata:name: alertmanagernamespace: prometheuslabels:app: alertmanager
spec:replicas: 1selector:matchLabels:app: alertmanagertemplate:metadata:labels:app: alertmanagerspec:containers:- name: alertmanagerimage: "prom/alertmanager"imagePullPolicy: "IfNotPresent"args:- "--config.file=/etc/alertmanager/alertmanager.yml"ports:- containerPort: 9093readinessProbe:httpGet:path: /#/statusport: 9093initialDelaySeconds: 30timeoutSeconds: 30volumeMounts:- name: config-volume   #挂载配置文件mountPath: /etc/alertmanager- name: wechat-tmpl      #挂载告警规则模板mountPath: /etc/alertmanager/template- name: tz-configmountPath: /etc/localtimeresources:limits:cpu: 10mmemory: 50Mirequests:cpu: 10mmemory: 50Mivolumes:- name: config-volume    #使用配置文件的configmapconfigMap:name: alertmanager- name: wechat-tmpl     #使用规则模板的configmapconfigMap:name: wechat- name: tz-confighostPath:path: /usr/share/zoneinfo/Asia/Shanghai

编辑alertmanage服务对外访问的service的yaml

vi service.yaml

apiVersion: v1
kind: Service
metadata:name: svc-alertmanagernamespace: prometheus
spec:type: NodePortports:- port: 9093protocol: TCPtargetPort: 9093nodePort: 30011selector:app: alertmanager

创建命名空间

kubectl create namespace prometheus

创建所有yaml服务

kubectl apply -f configmap-wechat.yaml

kubectl apply -f configmap.yaml

kubectl apply -f deployment.yaml 

kubectl apply -f service.yaml

查看服务是否正常

kubectl get pod -n prometheus

可以看到alertmanage服务正常运行,但是查看日志就会发现调用企业微信的应用接口报错,这是因为还没有在企业微信应用里配置企业可信ip

kubectl logs -f --tail=10 alertmanager-6b89d8cd4-mnx5m -n prometheus

 上面给出的报错ip地址就是你现在所用的公网地址,将这个地址复制下来,配置到企业微信应用的企业可信ip中

配置完成后,测试告警,企业微信就会收到应用发送的告警信息了

 

以上的企业微信白名单,可以使用自己企业中的固定公网地址,配置策略路由,将走向 qyapi.weixin.qq.com该域名的所有流量都走固定公网地址,注意是调用企业微信api接口的域名,而不是报错给出的那个域名

至此alertmanage服务使用企业微信告警配置完成

相关文章:

  • Python 爬虫之下载歌曲(二)
  • 每日一题——LeetCode206.反转链表
  • 【微服务核心】Spring Boot
  • aws-sdk-cpp通过bazel构建的S3_client轮子
  • sql_lab之sqli中的堆叠型注入(less-38)
  • 【C++】STL 容器 - list 双向链表容器 ① ( 容器特点 | 容器操作时间复杂度 | 构造函数 )
  • nginx-proxy-manager初次登录502 bad gateway
  • TensorFlow是什么
  • 单例模式你了解嘛?
  • Pytest框架 —— 用例标记和测试执行篇!
  • k8s搭建(一、环境配置与docker安装)
  • 【Python常见数据结构操作-持续更新】
  • 利用Jmeter做接口测试(功能测试)全流程分析!
  • Go在Win10上接收UDP组播数据
  • Y9000P + ubuntu22.04 配置Anaconda+pycharm +pytorch
  • php的引用
  • [ 一起学React系列 -- 8 ] React中的文件上传
  • Android Studio:GIT提交项目到远程仓库
  • android 一些 utils
  • Angular2开发踩坑系列-生产环境编译
  • C++回声服务器_9-epoll边缘触发模式版本服务器
  • canvas 五子棋游戏
  •  D - 粉碎叛乱F - 其他起义
  • JS学习笔记——闭包
  • Python进阶细节
  • QQ浏览器x5内核的兼容性问题
  • select2 取值 遍历 设置默认值
  • webpack4 一点通
  • 漫谈开发设计中的一些“原则”及“设计哲学”
  • 如何学习JavaEE,项目又该如何做?
  • 使用Tinker来调试Laravel应用程序的数据以及使用Tinker一些总结
  • 算法---两个栈实现一个队列
  • 学习ES6 变量的解构赋值
  • 转载:[译] 内容加速黑科技趣谈
  • 自定义函数
  • ​决定德拉瓦州地区版图的关键历史事件
  • ​水经微图Web1.5.0版即将上线
  • #LLM入门|Prompt#3.3_存储_Memory
  • #NOIP 2014# day.2 T2 寻找道路
  • %check_box% in rails :coditions={:has_many , :through}
  • (12)目标检测_SSD基于pytorch搭建代码
  • (C语言)球球大作战
  • (免费领源码)python#django#mysql公交线路查询系统85021- 计算机毕业设计项目选题推荐
  • (转)Windows2003安全设置/维护
  • .NET Core 中插件式开发实现
  • .NET 反射的使用
  • .NET 中选择合适的文件打开模式(CreateNew, Create, Open, OpenOrCreate, Truncate, Append)
  • .NET/C# 获取一个正在运行的进程的命令行参数
  • .NET:自动将请求参数绑定到ASPX、ASHX和MVC(菜鸟必看)
  • .NET简谈设计模式之(单件模式)
  • [ element-ui:table ] 设置table中某些行数据禁止被选中,通过selectable 定义方法解决
  • [ 攻防演练演示篇 ] 利用通达OA 文件上传漏洞上传webshell获取主机权限
  • [2013][note]通过石墨烯调谐用于开关、传感的动态可重构Fano超——
  • [BZOJ5125]小Q的书架(决策单调性+分治DP+树状数组)
  • [C++] 默认构造函数、参数化构造函数、拷贝构造函数、移动构造函数及其使用案例