• Prometheus接入AlterManager配置邮件告警(基于K8S环境部署)


    注意:请基于 Prometheus+Grafana监控K8S集群(基于K8S环境部署)文章之上做本次实验。

    一、配置AlterManager告警发送至邮箱

    1、创建AlterManager ConfigMap资源清单

    vim alertmanager-cm.yaml
    ---
    kind: ConfigMap
    apiVersion: v1
    metadata:
      name: alertmanager
      namespace: prometheus
    data:
      alertmanager.yml: |-
        global:  
          resolve_timeout: 1m
          smtp_smarthost: 'smtp.qq.com:25'
          smtp_from: '1507341994@qq.com'  # 从这个邮箱发送告警
          smtp_auth_username: '1507341994@qq.com'  # 发送告警邮箱账号
          smtp_auth_password: 'eptesvmdjfpcbaab'   # 邮箱验证码,用自己的别用我的!!
          smtp_require_tls: false
        route:   # 路由配置(将邮箱发送那个路由)
          group_by: [alertname]
          group_wait: 10s
          group_interval: 10s
          repeat_interval: 10m
          receiver: default-receiver   # 告警发送到default-receiver接受者
        receivers:
        - name: 'default-receiver'     # 定义default-receiver接受者
          email_configs:
          - to: '1507341994@qq.com'   # 告警发送邮箱地址
            send_resolved: true
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    执行YAML资源清单:

    kubectl apply -f alertmanager-cm.yaml
    
    • 1

    2、配置文件核心配置说明

    • group_by: [alertname]:采用哪个标签来作为分组依据。
    • group_wait:10s:组告警等待时间。就是告警产生后等待10s,如果有同组告警一起发出。
    • group_interval: 10s :上下两组发送告警的间隔时间。
    • repeat_interval: 10m:重复发送告警的时间,减少相同邮件的发送频率,默认是1h。
    • receiver: default-receiver:定义谁来收告警。
    • smtp_smarthost: SMTP服务器地址+端口。
    • smtp_from:指定从哪个邮箱发送报警。
    • smtp_auth_username:邮箱账号。
    • smtp_auth_password: 邮箱密码(授权码)。

    二、Prometheus接入AlterManager配置

    1、创建新的Prometheus ConfigMap资源清单,添加监控K8S集群告警规则

    vim prometheus-alertmanager-cfg.yaml
    ---
    kind: ConfigMap
    apiVersion: v1
    metadata:
      labels:
        app: prometheus
      name: prometheus-config
      namespace: prometheus
    data:
      prometheus.yml: |
        rule_files: 
        - /etc/prometheus/rules.yml   # 告警规则位置
        alerting:
          alertmanagers:
          - static_configs:
            - targets: ["localhost:9093"] # 接入AlterManager
        global:
          scrape_interval: 15s
          scrape_timeout: 10s
          evaluation_interval: 1m
        scrape_configs:
        - job_name: 'kubernetes-node'
          kubernetes_sd_configs:
          - role: node
          relabel_configs:
          - source_labels: [__address__]
            regex: '(.*):10250'
            replacement: '${1}:9100'
            target_label: __address__
            action: replace
          - action: labelmap
            regex: __meta_kubernetes_node_label_(.+)
        - job_name: 'kubernetes-node-cadvisor'
          kubernetes_sd_configs:
          - role:  node
          scheme: https
          tls_config:
            ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
          bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
          relabel_configs:
          - action: labelmap
            regex: __meta_kubernetes_node_label_(.+)
          - target_label: __address__
            replacement: kubernetes.default.svc:443
          - source_labels: [__meta_kubernetes_node_name]
            regex: (.+)
            target_label: __metrics_path__
            replacement: /api/v1/nodes/${1}/proxy/metrics/cadvisor
        - job_name: 'kubernetes-apiserver'
          kubernetes_sd_configs:
          - role: endpoints
          scheme: https
          tls_config:
            ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
          bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
          relabel_configs:
          - source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
            action: keep
            regex: default;kubernetes;https
        - job_name: 'kubernetes-service-endpoints'
          kubernetes_sd_configs:
          - role: endpoints
          relabel_configs:
          - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scrape]
            action: keep
            regex: true
          - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_scheme]
            action: replace
            target_label: __scheme__
            regex: (https?)
          - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_path]
            action: replace
            target_label: __metrics_path__
            regex: (.+)
          - source_labels: [__address__, __meta_kubernetes_service_annotation_prometheus_io_port]
            action: replace
            target_label: __address__
            regex: ([^:]+)(?::\d+)?;(\d+)
            replacement: $1:$2
          - action: labelmap
            regex: __meta_kubernetes_service_label_(.+)
          - source_labels: [__meta_kubernetes_namespace]
            action: replace
            target_label: kubernetes_namespace
          - source_labels: [__meta_kubernetes_service_name]
            action: replace
            target_label: kubernetes_name 
        - job_name: 'kubernetes-pods'    # 监控Pod配置,添加注解后才可以被发现
          kubernetes_sd_configs:
          - role: pod
          relabel_configs:
          - action: keep
            regex: true
            source_labels:
            - __meta_kubernetes_pod_annotation_prometheus_io_scrape
          - action: replace
            regex: (.+)
            source_labels:
            - __meta_kubernetes_pod_annotation_prometheus_io_path
            target_label: __metrics_path__
          - action: replace
            regex: ([^:]+)(?::\d+)?;(\d+)
            replacement: $1:$2
            source_labels:
            - __address__
            - __meta_kubernetes_pod_annotation_prometheus_io_port
            target_label: __address__
          - action: labelmap
            regex: __meta_kubernetes_pod_label_(.+)
          - action: replace
            source_labels:
            - __meta_kubernetes_namespace
            target_label: kubernetes_namespace
          - action: replace
            source_labels:
            - __meta_kubernetes_pod_name
            target_label: kubernetes_pod_name
        - job_name: 'kubernetes-etcd'   # 监控etcd配置
          scheme: https
          tls_config:
            ca_file: /var/run/secrets/kubernetes.io/k8s-certs/etcd/ca.crt
            cert_file: /var/run/secrets/kubernetes.io/k8s-certs/etcd/server.crt
            key_file: /var/run/secrets/kubernetes.io/k8s-certs/etcd/server.key
          scrape_interval: 5s
          static_configs:
          - targets: ['16.32.15.200:2379']
      rules.yml: |  # K8S集群告警规则配置文件
        groups:
        - name: example
          rules:
          - alert: apiserver的cpu使用率大于80%
            expr: rate(process_cpu_seconds_total{job=~"kubernetes-apiserver"}[1m]) * 100 > 80
            for: 2s
            labels:
              severity: warnning
            annotations:
              description: "{{$labels.instance}}的{{$labels.job}}组件的cpu使用率超过80%"
          - alert:  apiserver的cpu使用率大于90%
            expr: rate(process_cpu_seconds_total{job=~"kubernetes-apiserver"}[1m]) * 100 > 90
            for: 2s
            labels:
              severity: critical
            annotations:
              description: "{{$labels.instance}}的{{$labels.job}}组件的cpu使用率超过90%"
          - alert: etcd的cpu使用率大于80%
            expr: rate(process_cpu_seconds_total{job=~"kubernetes-etcd"}[1m]) * 100 > 80
            for: 2s
            labels:
              severity: warnning
            annotations:
              description: "{{$labels.instance}}的{{$labels.job}}组件的cpu使用率超过80%"
          - alert:  etcd的cpu使用率大于90%
            expr: rate(process_cpu_seconds_total{job=~"kubernetes-etcd"}[1m]) * 100 > 90
            for: 2s
            labels:
              severity: critical
            annotations:
              description: "{{$labels.instance}}的{{$labels.job}}组件的cpu使用率超过90%"
          - alert: kube-state-metrics的cpu使用率大于80%
            expr: rate(process_cpu_seconds_total{k8s_app=~"kube-state-metrics"}[1m]) * 100 > 80
            for: 2s
            labels:
              severity: warnning
            annotations:
              description: "{{$labels.instance}}的{{$labels.k8s_app}}组件的cpu使用率超过80%"
              value: "{{ $value }}%"
              threshold: "80%"      
          - alert: kube-state-metrics的cpu使用率大于90%
            expr: rate(process_cpu_seconds_total{k8s_app=~"kube-state-metrics"}[1m]) * 100 > 0
            for: 2s
            labels:
              severity: critical
            annotations:
              description: "{{$labels.instance}}的{{$labels.k8s_app}}组件的cpu使用率超过90%"
              value: "{{ $value }}%"
              threshold: "90%"      
          - alert: coredns的cpu使用率大于80%
            expr: rate(process_cpu_seconds_total{k8s_app=~"kube-dns"}[1m]) * 100 > 80
            for: 2s
            labels:
              severity: warnning
            annotations:
              description: "{{$labels.instance}}的{{$labels.k8s_app}}组件的cpu使用率超过80%"
              value: "{{ $value }}%"
              threshold: "80%"      
          - alert: coredns的cpu使用率大于90%
            expr: rate(process_cpu_seconds_total{k8s_app=~"kube-dns"}[1m]) * 100 > 90
            for: 2s
            labels:
              severity: critical
            annotations:
              description: "{{$labels.instance}}的{{$labels.k8s_app}}组件的cpu使用率超过90%"
              value: "{{ $value }}%"
              threshold: "90%"      
          - alert: kube-proxy打开句柄数>600
            expr: process_open_fds{job=~"kubernetes-kube-proxy"}  > 600
            for: 2s
            labels:
              severity: warnning
            annotations:
              description: "{{$labels.instance}}的{{$labels.job}}打开句柄数>600"
              value: "{{ $value }}"
          - alert: kube-proxy打开句柄数>1000
            expr: process_open_fds{job=~"kubernetes-kube-proxy"}  > 1000
            for: 2s
            labels:
              severity: critical
            annotations:
              description: "{{$labels.instance}}的{{$labels.job}}打开句柄数>1000"
              value: "{{ $value }}"
          - alert: kubernetes-schedule打开句柄数>600
            expr: process_open_fds{job=~"kubernetes-schedule"}  > 600
            for: 2s
            labels:
              severity: warnning
            annotations:
              description: "{{$labels.instance}}的{{$labels.job}}打开句柄数>600"
              value: "{{ $value }}"
          - alert: kubernetes-schedule打开句柄数>1000
            expr: process_open_fds{job=~"kubernetes-schedule"}  > 1000
            for: 2s
            labels:
              severity: critical
            annotations:
              description: "{{$labels.instance}}的{{$labels.job}}打开句柄数>1000"
              value: "{{ $value }}"
          - alert: kubernetes-controller-manager打开句柄数>600
            expr: process_open_fds{job=~"kubernetes-controller-manager"}  > 600
            for: 2s
            labels:
              severity: warnning
            annotations:
              description: "{{$labels.instance}}的{{$labels.job}}打开句柄数>600"
              value: "{{ $value }}"
          - alert: kubernetes-controller-manager打开句柄数>1000
            expr: process_open_fds{job=~"kubernetes-controller-manager"}  > 1000
            for: 2s
            labels:
              severity: critical
            annotations:
              description: "{{$labels.instance}}的{{$labels.job}}打开句柄数>1000"
              value: "{{ $value }}"
          - alert: kubernetes-apiserver打开句柄数>600
            expr: process_open_fds{job=~"kubernetes-apiserver"}  > 600
            for: 2s
            labels:
              severity: warnning
            annotations:
              description: "{{$labels.instance}}的{{$labels.job}}打开句柄数>600"
              value: "{{ $value }}"
          - alert: kubernetes-apiserver打开句柄数>1000
            expr: process_open_fds{job=~"kubernetes-apiserver"}  > 1000
            for: 2s
            labels:
              severity: critical
            annotations:
              description: "{{$labels.instance}}的{{$labels.job}}打开句柄数>1000"
              value: "{{ $value }}"
          - alert: kubernetes-etcd打开句柄数>600
            expr: process_open_fds{job=~"kubernetes-etcd"}  > 600
            for: 2s
            labels:
              severity: warnning
            annotations:
              description: "{{$labels.instance}}的{{$labels.job}}打开句柄数>600"
              value: "{{ $value }}"
          - alert: kubernetes-etcd打开句柄数>1000
            expr: process_open_fds{job=~"kubernetes-etcd"}  > 1000
            for: 2s
            labels:
              severity: critical
            annotations:
              description: "{{$labels.instance}}的{{$labels.job}}打开句柄数>1000"
              value: "{{ $value }}"
          - alert: coredns
            expr: process_open_fds{k8s_app=~"kube-dns"}  > 600
            for: 2s
            labels:
              severity: warnning 
            annotations:
              description: "插件{{$labels.k8s_app}}({{$labels.instance}}): 打开句柄数超过600"
              value: "{{ $value }}"
          - alert: coredns
            expr: process_open_fds{k8s_app=~"kube-dns"}  > 1000
            for: 2s
            labels:
              severity: critical
            annotations:
              description: "插件{{$labels.k8s_app}}({{$labels.instance}}): 打开句柄数超过1000"
              value: "{{ $value }}"
          - alert: kube-proxy
            expr: process_virtual_memory_bytes{job=~"kubernetes-kube-proxy"}  > 2000000000
            for: 2s
            labels:
              severity: warnning
            annotations:
              description: "组件{{$labels.job}}({{$labels.instance}}): 使用虚拟内存超过2G"
              value: "{{ $value }}"
          - alert: scheduler
            expr: process_virtual_memory_bytes{job=~"kubernetes-schedule"}  > 2000000000
            for: 2s
            labels:
              severity: warnning
            annotations:
              description: "组件{{$labels.job}}({{$labels.instance}}): 使用虚拟内存超过2G"
              value: "{{ $value }}"
          - alert: kubernetes-controller-manager
            expr: process_virtual_memory_bytes{job=~"kubernetes-controller-manager"}  > 2000000000
            for: 2s
            labels:
              severity: warnning
            annotations:
              description: "组件{{$labels.job}}({{$labels.instance}}): 使用虚拟内存超过2G"
              value: "{{ $value }}"
          - alert: kubernetes-apiserver
            expr: process_virtual_memory_bytes{job=~"kubernetes-apiserver"}  > 2000000000
            for: 2s
            labels:
              severity: warnning
            annotations:
              description: "组件{{$labels.job}}({{$labels.instance}}): 使用虚拟内存超过2G"
              value: "{{ $value }}"
          - alert: kubernetes-etcd
            expr: process_virtual_memory_bytes{job=~"kubernetes-etcd"}  > 2000000000
            for: 2s
            labels:
              severity: warnning
            annotations:
              description: "组件{{$labels.job}}({{$labels.instance}}): 使用虚拟内存超过2G"
              value: "{{ $value }}"
          - alert: kube-dns
            expr: process_virtual_memory_bytes{k8s_app=~"kube-dns"}  > 2000000000
            for: 2s
            labels:
              severity: warnning
            annotations:
              description: "插件{{$labels.k8s_app}}({{$labels.instance}}): 使用虚拟内存超过2G"
              value: "{{ $value }}"
          - alert: HttpRequestsAvg
            expr: sum(rate(rest_client_requests_total{job=~"kubernetes-kube-proxy|kubernetes-kubelet|kubernetes-schedule|kubernetes-control-manager|kubernetes-apiservers"}[1m]))  > 1000
            for: 2s
            labels:
              team: admin
            annotations:
              description: "组件{{$labels.job}}({{$labels.instance}}): TPS超过1000"
              value: "{{ $value }}"
              threshold: "1000"   
          - alert: Pod_restarts
            expr: kube_pod_container_status_restarts_total{namespace=~"kube-system|default|monitor-sa"} > 0
            for: 2s
            labels:
              severity: warnning
            annotations:
              description: "在{{$labels.namespace}}名称空间下发现{{$labels.pod}}这个pod下的容器{{$labels.container}}被重启,这个监控指标是由{{$labels.instance}}采集的"
              value: "{{ $value }}"
              threshold: "0"
          - alert: Pod_waiting
            expr: kube_pod_container_status_waiting_reason{namespace=~"kube-system|default"} == 1
            for: 2s
            labels:
              team: admin
            annotations:
              description: "空间{{$labels.namespace}}({{$labels.instance}}): 发现{{$labels.pod}}下的{{$labels.container}}启动异常等待中"
              value: "{{ $value }}"
              threshold: "1"   
          - alert: Pod_terminated
            expr: kube_pod_container_status_terminated_reason{namespace=~"kube-system|default|monitor-sa"} == 1
            for: 2s
            labels:
              team: admin
            annotations:
              description: "空间{{$labels.namespace}}({{$labels.instance}}): 发现{{$labels.pod}}下的{{$labels.container}}被删除"
              value: "{{ $value }}"
              threshold: "1"
          - alert: Etcd_leader
            expr: etcd_server_has_leader{job="kubernetes-etcd"} == 0
            for: 2s
            labels:
              team: admin
            annotations:
              description: "组件{{$labels.job}}({{$labels.instance}}): 当前没有leader"
              value: "{{ $value }}"
              threshold: "0"
          - alert: Etcd_leader_changes
            expr: rate(etcd_server_leader_changes_seen_total{job="kubernetes-etcd"}[1m]) > 0
            for: 2s
            labels:
              team: admin
            annotations:
              description: "组件{{$labels.job}}({{$labels.instance}}): 当前leader已发生改变"
              value: "{{ $value }}"
              threshold: "0"
          - alert: Etcd_failed
            expr: rate(etcd_server_proposals_failed_total{job="kubernetes-etcd"}[1m]) > 0
            for: 2s
            labels:
              team: admin
            annotations:
              description: "组件{{$labels.job}}({{$labels.instance}}): 服务失败"
              value: "{{ $value }}"
              threshold: "0"
          - alert: Etcd_db_total_size
            expr: etcd_debugging_mvcc_db_total_size_in_bytes{job="kubernetes-etcd"} > 10000000000
            for: 2s
            labels:
              team: admin
            annotations:
              description: "组件{{$labels.job}}({{$labels.instance}}):db空间超过10G"
              value: "{{ $value }}"
              threshold: "10G"
          - alert: Endpoint_ready
            expr: kube_endpoint_address_not_ready{namespace=~"kube-system|default"} == 1
            for: 2s
            labels:
              team: admin
            annotations:
              description: "空间{{$labels.namespace}}({{$labels.instance}}): 发现{{$labels.endpoint}}不可用"
              value: "{{ $value }}"
              threshold: "1"
        - name: 物理节点状态-监控告警
          rules:
          - alert: 物理节点cpu使用率
            expr: 100-avg(irate(node_cpu_seconds_total{mode="idle"}[5m])) by(instance)*100 > 90
            for: 2s
            labels:
              severity: ccritical
            annotations:
              summary: "{{ $labels.instance }}cpu使用率过高"
              description: "{{ $labels.instance }}的cpu使用率超过90%,当前使用率[{{ $value }}],需要排查处理" 
          - alert: 物理节点内存使用率
            expr: (node_memory_MemTotal_bytes - (node_memory_MemFree_bytes + node_memory_Buffers_bytes + node_memory_Cached_bytes)) / node_memory_MemTotal_bytes * 100 > 90
            for: 2s
            labels:
              severity: critical
            annotations:
              summary: "{{ $labels.instance }}内存使用率过高"
              description: "{{ $labels.instance }}的内存使用率超过90%,当前使用率[{{ $value }}],需要排查处理"
          - alert: InstanceDown
            expr: up == 0
            for: 2s
            labels:
              severity: critical
            annotations:   
              summary: "{{ $labels.instance }}: 服务器宕机"
              description: "{{ $labels.instance }}: 服务器延时超过2分钟"
          - alert: 物理节点磁盘的IO性能
            expr: 100-(avg(irate(node_disk_io_time_seconds_total[1m])) by(instance)* 100) < 60
            for: 2s
            labels:
              severity: critical
            annotations:
              summary: "{{$labels.mountpoint}} 流入磁盘IO使用率过高!"
              description: "{{$labels.mountpoint }} 流入磁盘IO大于60%(目前使用:{{$value}})"
          - alert: 入网流量带宽
            expr: ((sum(rate (node_network_receive_bytes_total{device!~'tap.*|veth.*|br.*|docker.*|virbr*|lo*'}[5m])) by (instance)) / 100) > 102400
            for: 2s
            labels:
              severity: critical
            annotations:
              summary: "{{$labels.mountpoint}} 流入网络带宽过高!"
              description: "{{$labels.mountpoint }}流入网络带宽持续5分钟高于100M. RX带宽使用率{{$value}}"
          - alert: 出网流量带宽
            expr: ((sum(rate (node_network_transmit_bytes_total{device!~'tap.*|veth.*|br.*|docker.*|virbr*|lo*'}[5m])) by (instance)) / 100) > 102400
            for: 2s
            labels:
              severity: critical
            annotations:
              summary: "{{$labels.mountpoint}} 流出网络带宽过高!"
              description: "{{$labels.mountpoint }}流出网络带宽持续5分钟高于100M. RX带宽使用率{{$value}}"
          - alert: TCP会话
            expr: node_netstat_Tcp_CurrEstab > 1000
            for: 2s
            labels:
              severity: critical
            annotations:
              summary: "{{$labels.mountpoint}} TCP_ESTABLISHED过高!"
              description: "{{$labels.mountpoint }} TCP_ESTABLISHED大于1000%(目前使用:{{$value}}%)"
          - alert: 磁盘容量
            expr: 100-(node_filesystem_free_bytes{fstype=~"ext4|xfs"}/node_filesystem_size_bytes {fstype=~"ext4|xfs"}*100) > 80
            for: 2s
            labels:
              severity: critical
            annotations:
              summary: "{{$labels.mountpoint}} 磁盘分区使用率过高!"
              description: "{{$labels.mountpoint }} 磁盘分区使用大于80%(目前使用:{{$value}}%)"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470
    • 471
    • 472
    • 473
    • 474
    • 475
    • 476
    • 477
    • 478
    • 479
    • 480
    • 481
    • 482
    • 483
    • 484
    • 485
    • 486

    执行资源清单:

    kubectl apply -f prometheus-alertmanager-cfg.yaml
    
    • 1

    2、由于在prometheus中新增了etcd,所以生成一个etcd-certs,这个在部署prometheus需要

    kubectl -n prometheus create secret generic etcd-certs --from-file=/etc/kubernetes/pki/etcd/server.key  --from-file=/etc/kubernetes/pki/etcd/server.crt --from-file=/etc/kubernetes/pki/etcd/ca.crt
    
    • 1

    三、部署Prometheus+AlterManager(放到一个Pod中)

    1、在node-1节点创建/data/alertmanager目录,存放alertmanager数据

    mkdir /data/alertmanager -p
    chmod -R 777 /data/alertmanager
    
    • 1
    • 2

    2、删除旧的prometheus deployment资源

    kubectl delete deploy prometheus-server -n prometheus
    
    • 1

    3、创建deployment资源

    vim prometheus-alertmanager-deploy.yaml
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: prometheus-server
      namespace: prometheus
      labels:
        app: prometheus
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: prometheus
          component: server
        #matchExpressions:
        #- {key: app, operator: In, values: [prometheus]}
        #- {key: component, operator: In, values: [server]}
      template:
        metadata:
          labels:
            app: prometheus
            component: server
          annotations:
            prometheus.io/scrape: 'false'
        spec:
          nodeName: node-1 # 调度到node-1节点
          serviceAccountName: prometheus # 指定sa服务账号
          containers:
          - name: prometheus
            image: prom/prometheus:v2.33.5
            imagePullPolicy: IfNotPresent
            command:
            - "/bin/prometheus"
            args:
            - "--config.file=/etc/prometheus/prometheus.yml"
            - "--storage.tsdb.path=/prometheus"
            - "--storage.tsdb.retention=24h"
            - "--web.enable-lifecycle"
            ports:
            - containerPort: 9090
              protocol: TCP
            volumeMounts:
            - mountPath: /etc/prometheus
              name: prometheus-config
            - mountPath: /prometheus/
              name: prometheus-storage-volume
            - name: k8s-certs
              mountPath: /var/run/secrets/kubernetes.io/k8s-certs/etcd/
          - name: alertmanager
            #image: prom/alertmanager:v0.14.0
            image: prom/alertmanager:v0.23.0
            imagePullPolicy: IfNotPresent
            args:
            - "--config.file=/etc/alertmanager/alertmanager.yml"
            - "--log.level=debug"
            ports:
            - containerPort: 9093
              protocol: TCP
              name: alertmanager
            volumeMounts:
            - name: alertmanager-config
              mountPath: /etc/alertmanager
            - name: alertmanager-storage
              mountPath: /alertmanager
            - name: localtime
              mountPath: /etc/localtime
          volumes:
            - name: prometheus-config
              configMap:
                name: prometheus-config
            - name: prometheus-storage-volume
              hostPath:
               path: /data
               type: Directory
            - name: k8s-certs
              secret:
               secretName: etcd-certs
            - name: alertmanager-config
              configMap:
                name: alertmanager
            - name: alertmanager-storage
              hostPath:
               path: /data/alertmanager
               type: DirectoryOrCreate
            - name: localtime
              hostPath:
               path: /usr/share/zoneinfo/Asia/Shanghai
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88

    执行YAML资源清单:

    kubectl apply -f prometheus-alertmanager-deploy.yaml
    
    • 1

    查看状态:

    kubectl get pods -n prometheus
    
    • 1

    在这里插入图片描述

    4、创建AlterManager SVC资源

    vim alertmanager-svc.yaml 
    ---
    apiVersion: v1
    kind: Service
    metadata:
      labels:
        name: prometheus
        kubernetes.io/cluster-service: 'true'
      name: alertmanager
      namespace: prometheus
    spec:
      ports:
      - name: alertmanager
        nodePort: 30066
        port: 9093
        protocol: TCP
        targetPort: 9093
      selector:
        app: prometheus
      sessionAffinity: None
      type: NodePort
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    执行YAML资源清单:

    kubectl apply -f alertmanager-svc.yaml 
    
    • 1

    查看状态:

    kubectl get svc -n prometheus
    
    • 1

    在这里插入图片描述

    四、测试告警

    浏览器访问:http://IP:30066
    在这里插入图片描述
    如上图可以看到,Prometheus的告警信息已经发到AlterManager了,AlertManager收到报警数据后,会将警报信息进行分组,然后根据AlertManager配置的 group_wait 时间先进行等待。等wait时间过后再发送报警信息至邮件!

    如上图,告警信息已经成功发往邮件了!!!

  • 相关阅读:
    Http Content-type 对照表
    文件系统实现
    商务呈现之危机公关处理
    VSCode 远程反复输入密码不能链接问题解决
    Selenium自动化测试框架工作原理你明白了吗?
    jar依赖批量上传Nexus服务器(二)
    Geogebra 教程之 04 Geogebra 小程序
    算法总结-最短距离和问题
    NET MVC中如何使用Element-Plus
    【WiFI问题自助】解决WiFi能连上但是没有网的问题
  • 原文地址:https://blog.csdn.net/weixin_45310323/article/details/133965945