一、自定义监控
1.新增PrometheusRule资源,定义自己的监控项目
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
labels: #安装kube-prometheus对应的标签
app.kubernetes.io/component: prometheus
app.kubernetes.io/instance: k8s
app.kubernetes.io/name: prometheus
app.kubernetes.io/part-of: kube-prometheus
app.kubernetes.io/version: 2.34.0
prometheus: k8s
role: alert-rules
name: prometheus-k8s-test-rules
namespace: monitoring
spec:
groups: #普罗米修斯规则文件的内容
- name: test-load #组的名称,必须唯一
rules: #报警规则
- alert: test-load-1 #报警名称
expr: node_load1 > 1 #报警规则
for: 2m #时间
labels: #添加的标签
team: node
annotations: #报警输出内容
summary: "{{$labels.instance}}: load 1 >1"
description: "{{$labels.instance}}: job {{$labels.job}} 测试测试 负载大于1"
2.导入查看规则
#导入
[root@master ~]# kubectl apply -f test-prometheus-rule.yaml
prometheusrule.monitoring.coreos.com/prometheus-k8s-test-rules created
#查看
[root@master manifests]# kubectl get PrometheusRule -n monitoring |grep test
prometheus-k8s-test-rules 2m36s
3.浏览器查看prometheus上新增规则是否存在
二、alertmanger报警配置
1、创建一个alertmanger的配置文件并配置上报警配置(这里以邮箱测试)
global:
resolve_timeout: 5m #处理超时时间,默认为5min
smtp_smarthost: 'smtp.163.com:465' # 邮箱smtp服务器代理
smtp_from: 'xxxx@163.com' # 发送邮箱名称
smtp_auth_username: 'xxxx@163.com' # 邮箱名称
smtp_auth_password: 'xxxxxxxxx' #邮箱密码
smtp_require_tls: false
route:
group_by: ['alertname'] # 报警分组名称
group_wait: 10s # 最初即第一次等待多久时间发送一组警报的通知
group_interval: 10s # 在发送新警报前的等待时间
repeat_interval: 1m # 发送重复警报的周期
receiver: 'email' # 发送警报的接收者的名称,以下receivers name的名称
receivers:
- name: 'email' # 警报
email_configs: # 邮箱配置
- to: 'xxxxxx@163.com' # 接收警报的email配置
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
3.查看默认的alertmanager的secret资源(后面要将alertmanager-main资源替换掉,或者手动改secret)
[root@master ~]# kubectl get secret -n monitoring |grep alertmanager-main
alertmanager-main Opaque 1 5h25m
4.创建alertmanger-email对应的secret资源文件
kubectl create secret generic -n monitoring alertmanager-main --from-file=./alertmanager.yaml --dry-run -o yaml >alertmanager-main-secret.yaml #文件名称必须是alertmanager.yaml,因为是将系统默认的配置覆盖掉
5.导入secret资源(替换默认的alertmanager-main)
[root@master ~]# kubectl apply -f alertmanager-main-secret.yaml
secret/alertmanager-main configured
6.删除旧的alertmanager的pod
kubectl delete pod -n monitoring alertmanager-main-0 alertmanager-main-1 alertmanager-main-2
7.查看重启的alertmanager的pod
[root@master ~]# kubectl get pod -n monitoring
NAME READY STATUS RESTARTS AGE
alertmanager-main-0 2/2 Running 0 5s
alertmanager-main-1 2/2 Running 0 5s
alertmanager-main-2 2/2 Running 0 6s
8.先删除自定义测试的规则,过一会后重新导入规则(方便触发规则测试)
[root@master ~]# kubectl delete -f test-prometheus-rule.yaml
prometheusrule.monitoring.coreos.com "prometheus-k8s-test-rules" deleted
[root@master ~]# kubectl apply -f test-prometheus-rule.yaml
prometheusrule.monitoring.coreos.com/prometheus-k8s-test-rules created
9.查看邮箱,发现规则触发并发送了邮件