kubernetes operator安装 前提:创建企业微信,创建应用
然后配置altermanager.yaml
global:
resolve_timeout: 5m
receivers:
- name: wechat
wechat_configs:
- agent_id: "100000x"
api_secret: Nm7PRrxxxxG8EpkyWuXDAWMLmFo
corp_id: wwf9d3833cd66f34d8
send_resolved: true
to_user: Joker
route:
group_by:
- job
group_interval: 5m
group_wait: 30s
receiver: wechat
repeat_interval: 12h
routes:
- match:
alertname: Watchdog
receiver: wechat
然后删除原有的alertmanager-main
kubectl delete secret alertmanager-main -n monitoring
重新创建
kubectl create secret generic alertmanager-main --from-file=alertmanager.yaml -n monitoring
然后就可以收到告警了
不过现在这个告警不好看,我们可以自定义模板。
创建一个template.tmp1的文件。如下
{{ define "wechat.default.message" }}
{{- if gt (len .Alerts.Firing) 0 -}}
{{- range $index, $alert := .Alerts -}}
{{- if eq $index 0 }}
==========异常告警==========
告警类型: {{ $alert.Labels.alertname }}
告警级别: {{ $alert.Labels.severity }}
告警详情: {{ $alert.Annotations.message }}{{ $alert.Annotations.description}};{{$alert.Annotations.summary}}
故障时间: {{ ($alert.StartsAt.Add 28800e9).Format "2006-01-02 15:04:05" }}
{{- if gt (len $alert.Labels.instance) 0 }}
实例信息: {{ $alert.Labels.instance }}
{{- end }}
{{- if gt (len $alert.Labels.namespace) 0 }}
命名空间: {{ $alert.Labels.namespace }}
{{- end }}
{{- if gt (len $alert.Labels.node) 0 }}
节点信息: {{ $alert.Labels.node }}
{{- end }}
{{- if gt (len $alert.Labels.pod) 0 }}
实例名称: {{ $alert.Labels.pod }}
{{- end }}
============END============
{{- end }}
{{- end }}
{{- end }}
{{- if gt (len .Alerts.Resolved) 0 -}}
{{- range $index, $alert := .Alerts -}}
{{- if eq $index 0 }}
==========异常恢复==========
告警类型: {{ $alert.Labels.alertname }}
告警级别: {{ $alert.Labels.severity }}
告警详情: {{ $alert.Annotations.message }}{{ $alert.Annotations.description}};{{$alert.Annotations.summary}}
故障时间: {{ ($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 }}
{{- if gt (len $alert.Labels.namespace) 0 }}
命名空间: {{ $alert.Labels.namespace }}
{{- end }}
{{- if gt (len $alert.Labels.node) 0 }}
节点信息: {{ $alert.Labels.node }}
{{- end }}
{{- if gt (len $alert.Labels.pod) 0 }}
实例名称: {{ $alert.Labels.pod }}
{{- end }}
============END============
{{- end }}
{{- end }}
{{- end }}
{{- end }}
然后将其和上面的altermanager.yaml放到同一个secret中。
先修改altermanager.yaml,如下:
global:
resolve_timeout: 5m
receivers:
- name: wechat
wechat_configs:
- agent_id: "100000x"
api_secret: Nm7PRrxxxxG8EpkyWuXDAWMLmFo
corp_id: wwf9d3833cd66f34d8
send_resolved: true
to_user: Joker
route:
group_by:
- job
group_interval: 5m
group_wait: 30s
receiver: wechat
repeat_interval: 12h
routes:
- match:
alertname: Watchdog
receiver: wechat
templates:
- /etc/alertmanager/config/template.tmp1
如下先删除,再创建
kubectl delete secret alertmanager-main -n monitoring
kubectl create secret generic alertmanager-main --from-file=alertmanager.yaml --from-file=template.tmp1 -n monitoring
然后报警出来就会好看些了。