将以上内容保存为example-rule.yaml文件,并且通过kubectl命令创建相应的资源:
prometheusrule "prometheus-example-rules" created
告警规则创建成功后,通过在Prometheus中使用ruleSelector通过选择需要关联的PrometheusRule即可:
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
name: inst
namespace: monitoring
spec:
serviceAccountName: prometheus
serviceMonitorSelector:
matchLabels:
team: frontend
ruleSelector:
matchLabels:
role: alert-rules
prometheus: example
resources:
requests:
memory: 400Mi
Prometheus重新加载配置后,从UI中我们可以查看到通过PrometheusRule自动创建的告警规则配置:
如果查看Alerts页面,我们会看到告警已经处于触发状态。
使用Operator管理Alertmanager实例
为了通过Prometheus Operator管理Alertmanager实例,用户可以通过自定义资源Alertmanager进行定义,如下所示,通过replicas可以控制Alertmanager的实例数:
apiVersion: monitoring.coreos.com/v1
kind: Alertmanager
name: inst
spec:
replicas: 3
当replicas大于1时,Prometheus Operator会自动通过集群的方式创建Alertmanager。将以上内容保存为文件alertmanager-inst.yaml,并通过以下命令创建:
查看Pod的情况如下所示,我们会发现Alertmanager的Pod实例一直处于ContainerCreating的状态中:
$ kubectl -n monitoring get pods
NAME READY STATUS RESTARTS AGE
alertmanager-inst-0 0/2 ContainerCreating 0 32s
通过kubectl describe命令查看该Alertmanager的Pod实例状态,可以看到类似于以下内容的告警信息:
MountVolume.SetUp failed for volume "config-volume" : secrets "alertmanager-inst" not found
这是由于Prometheus Operator通过Statefulset的方式创建的Alertmanager实例,在默认情况下,会通过alertmanager-{ALERTMANAGER_NAME}
的命名规则去查找Secret配置并以文件挂载的方式,将Secret的内容作为配置文件挂载到Alertmanager实例当中。因此,这里还需要为Alertmanager创建相应的配置内容,如下所示,是Alertmanager的配置文件:
global:
resolve_timeout: 5m
route:
group_by: ['job']
group_wait: 30s
group_interval: 5m
repeat_interval: 12h
receiver: 'webhook'
receivers:
- name: 'webhook'
webhook_configs:
- url: 'http://alertmanagerwh:30500/'
在Secret创建成功后,查看当前Alertmanager Pod实例状态。如下所示:
$ kubectl -n monitoring get pods
NAME READY STATUS RESTARTS AGE
alertmanager-inst-0 2/2 Running 0 5m
alertmanager-inst-2 2/2 Running 0 37s
使用port-forward将Alertmanager映射到本地:
$ kubectl -n monitoring port-forward statefulsets/alertmanager-inst 9093:9093
访问http://localhost:9093/#/status,并查看当前集群状态:
接下来,我们只需要修改我们的Prometheus资源定义,通过alerting指定使用的Alertmanager资源即可:
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
name: inst
namespace: monitoring
spec:
serviceAccountName: prometheus
serviceMonitorSelector:
matchLabels:
team: frontend
ruleSelector:
matchLabels:
role: alert-rules
prometheus: example
alerting:
alertmanagers:
- name: alertmanager-example
namespace: monitoring
port: web
resources:
通过服务发现规则将Prometheus与Alertmanager进行了自动关联。