Prometheus+Grafana+Alertmanager搭建全方位的监控告警系统-超详细文档(下)

本文涉及的产品
可观测可视化 Grafana 版,10个用户账号 1个月
可观测监控 Prometheus 版,每月50GB免费额度
全局流量管理 GTM,标准版 1个月
简介: Prometheus+Grafana+Alertmanager搭建全方位的监控告警系统-超详细文档

3)创建service
在8s的master1节点生成一个kube-state-metrics-svc.yaml文件

cat >kube-state-metrics-svc.yaml <<EOF
apiVersion: v1
kind: Service
metadata:
  annotations:
    prometheus.io/scrape: 'true'
  name: kube-state-metrics
  namespace: kube-system
  labels:
    app: kube-state-metrics
spec:
  ports:
  - name: kube-state-metrics
    port: 8080
    protocol: TCP
  selector:
    app: kube-state-metrics
EOF

通过kubectl apply更新yaml

kubectl apply -f kube-state-metrics-svc.yaml

查看service是否创建成功

kubectl get svc -n kube-system | grep kube-state-metrics

显示如下,说明创建成功

    kube-state-metrics   ClusterIP   10.105.53.102    <none>        8080/TCP                 2m38s

    在grafana web界面导入Kubernetes Cluster (Prometheus)-1577674936972.json,出现如下页面

    在grafana web界面导入Kubernetes cluster monitoring (via Prometheus) (k8s 1.16)-1577691996738.json,出现如下页面

    Kubernetes Cluster (Prometheus)-1577674936972.json和Kubernetes cluster monitoring (via Prometheus) (k8s 1.16)-1577691996738.json文件在百度网盘,地址如下:

      链接:https://pan.baidu.com/s/1QAMqT8scsXx-lzEPI6MPgA 提取码:i4yd

      安装和配置Alertmanager-发送报警到qq邮箱

      在k8s的master1节点创建alertmanager-cm.yaml文件

      cat >alertmanager-cm.yaml <<EOF
      kind: ConfigMap
      apiVersion: v1
      metadata:
        name: alertmanager
        namespace: monitor-sa
      data:
        alertmanager.yml: |-
          global:
            resolve_timeout: 1m
            smtp_smarthost: 'smtp.163.com:25'
            smtp_from: '15011572657@163.com'
            smtp_auth_username: '15011572657'
            smtp_auth_password: 'BDBPRMLNZGKWRFJP'
            smtp_require_tls: false
          route:
            group_by: [alertname]
            group_wait: 10s
            group_interval: 10s
            repeat_interval: 10m
            receiver: default-receiver
          receivers:
          - name: 'default-receiver'
            email_configs:
            - to: '1980570647@qq.com'
              send_resolved: true
      EOF

      通过kubectl apply 更新文件

      kubectl apply -f alertmanager-cm.yaml

      alertmanager配置文件解释说明:

      smtp_smarthost: 'smtp.163.com:25'
      #用于发送邮件的邮箱的SMTP服务器地址+端口
      smtp_from: '15011572657@163.com'
      #这是指定从哪个邮箱发送报警
      smtp_auth_username: '15011572657'
      #这是发送邮箱的认证用户,不是邮箱名
      smtp_auth_password: 'BDBPRMLNZGKWRFJP'
      #这是发送邮箱的授权码而不是登录密码
      email_configs:
         - to: '1980570647@qq.com'
      #to后面指定发送到哪个邮箱,我发送到我的qq邮箱,大家需要写自己的邮箱地址,不应该跟smtp_from的邮箱名字重复

      在k8s的master1节点重新生成一个prometheus-cfg.yaml文件

      cat prometheus-cfg.yaml

      kind: ConfigMap
      apiVersion: v1
      metadata:
        labels:
          app: prometheus
        name: prometheus-config
        namespace: monitor-sa
      data:
        prometheus.yml: |
          rule_files:
          - /etc/prometheus/rules.yml
          alerting:
            alertmanagers:
            - static_configs:
              - targets: ["localhost:9093"]
          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
            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-schedule'
            scrape_interval: 5s
            static_configs:
            - targets: ['192.168.0.6:10251']
          - job_name: 'kubernetes-controller-manager'
            scrape_interval: 5s
            static_configs:
            - targets: ['192.168.0.6:10252']
          - job_name: 'kubernetes-kube-proxy'
            scrape_interval: 5s
            static_configs:
            - targets: ['192.168.0.6:10249','192.168.0.56:10249']
          - job_name: 'kubernetes-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: ['192.168.0.6:2379']
        rules.yml: |
          groups:
          - name: example
            rules:
            - alert: kube-proxy的cpu使用率大于80%
              expr: rate(process_cpu_seconds_total{job=~"kubernetes-kube-proxy"}[1m]) * 100 > 80
              for: 2s
              labels:
                severity: warnning
              annotations:
                description: "{{$labels.instance}}的{{$labels.job}}组件的cpu使用率超过80%"
            - alert:  kube-proxy的cpu使用率大于90%
              expr: rate(process_cpu_seconds_total{job=~"kubernetes-kube-proxy"}[1m]) * 100 > 90
              for: 2s
              labels:
                severity: critical
              annotations:
                description: "{{$labels.instance}}的{{$labels.job}}组件的cpu使用率超过90%"
            - alert: scheduler的cpu使用率大于80%
              expr: rate(process_cpu_seconds_total{job=~"kubernetes-schedule"}[1m]) * 100 > 80
              for: 2s
              labels:
                severity: warnning
              annotations:
                description: "{{$labels.instance}}的{{$labels.job}}组件的cpu使用率超过80%"
            - alert:  scheduler的cpu使用率大于90%
              expr: rate(process_cpu_seconds_total{job=~"kubernetes-schedule"}[1m]) * 100 > 90
              for: 2s
              labels:
                severity: critical
              annotations:
                description: "{{$labels.instance}}的{{$labels.job}}组件的cpu使用率超过90%"
            - alert: controller-manager的cpu使用率大于80%
              expr: rate(process_cpu_seconds_total{job=~"kubernetes-controller-manager"}[1m]) * 100 > 80
              for: 2s
              labels:
                severity: warnning
              annotations:
                description: "{{$labels.instance}}的{{$labels.job}}组件的cpu使用率超过80%"
            - alert:  controller-manager的cpu使用率大于90%
              expr: rate(process_cpu_seconds_total{job=~"kubernetes-controller-manager"}[1m]) * 100 > 0
              for: 2s
              labels:
                severity: critical
              annotations:
                description: "{{$labels.instance}}的{{$labels.job}}组件的cpu使用率超过90%"
            - 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}

      注意:通过上面命令生成的promtheus-cfg.yaml文件会有一些问题,1和1和2这种变量在文件里没有,需要在k8s的master1节点打开promtheus-cfg.yaml文件,手动把1和1和2这种变量写进文件里,promtheus-cfg.yaml文件需要手动修改部分如下:

      22行的replacement: ':9100'变成replacement: '${1}:9100'
      42行的replacement: /api/v1/nodes//proxy/metrics/cadvisor变成
                    replacement: /api/v1/nodes/${1}/proxy/metrics/cadvisor
      73行的replacement:  变成replacement: $1:$2
      103行的replacement:  变成replacement: $1:$2

      通过kubectl apply 更新文件

      kubectl apply -f prometheus-cfg.yaml

      在k8s的master1节点重新生成一个prometheus-deploy.yaml文件

      cat >prometheus-deploy.yaml <<EOF
      ---
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: prometheus-server
        namespace: monitor-sa
        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: node1
            serviceAccountName: monitor
            containers:
            - name: prometheus
              image: prom/prometheus:v2.2.1
              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
              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
      EOF

      生成一个etcd-certs,这个在部署prometheus需要

      kubectl -n monitor-sa 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
      通过kubectl apply更新yaml文件

      kubectl apply -f prometheus-deploy.yaml

      #查看prometheus是否部署成功

      kubectl get pods -n monitor-sa | grep prometheus

      显示如下,可看到pod状态是running,说明prometheus部署成功

      NAME                                 READY   STATUS    RESTARTS   AGE
      prometheus-server-85dbc6c7f7-nsg94   1/1     Running   0          6m7

      在k8s的master1节点重新生成一个alertmanager-svc.yaml文件

      cat >alertmanager-svc.yaml <<EOF
      ---
      apiVersion: v1
      kind: Service
      metadata:
        labels:
          name: prometheus
          kubernetes.io/cluster-service: 'true'
        name: alertmanager
        namespace: monitor-sa
      spec:
        ports:
        - name: alertmanager
          nodePort: 30066
          port: 9093
          protocol: TCP
          targetPort: 9093
        selector:
          app: prometheus
        sessionAffinity: None
        type: NodePort
      EOF

      通过kubectl apply更新yaml文件

      kubectl apply -f prometheus-svc.yaml

      #查看service在物理机映射的端口

      kubectl get svc -n monitor-sa

      显示如下:

      NAME           TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
      alertmanager   NodePort   10.111.49.65   <none>        9093:31043/TCP   25s
      prometheus     NodePort   10.96.45.93    <none>        9090:30090/TCP   34h

      注意:上面可以看到prometheus的service暴漏的端口是31043,alertmanager的service暴露的端口是30066

      访问prometheus的web界面

      点击status->targets,可看到如下

      点击Alerts,可看到如下

      把controller-manager的cpu使用率大于90%展开,可看到如下

      FIRING表示prometheus已经将告警发给alertmanager,在Alertmanager 中可以看到有一个 alert。

      登录到alertmanager web界面

      浏览器输入192.168.0.6:30066,显示如下

      这样我在我的qq邮箱,1980570647@qq.com就可以收到报警了,如下

      配置Alertmanager报警-发送报警到钉钉

      打开电脑版钉钉创建机器人

      1.创建钉钉机器人

      打开电脑版钉钉,创建一个群,创建自定义机器人,按如下步骤创建
      https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
      我创建的机器人如下:
      群设置-->智能群助手-->添加机器人-->自定义-->添加
      机器人名称:kube-event
      接收群组:钉钉报警测试
      安全设置:
      自定义关键词:cluster1
      上面配置好之后点击完成即可,这样就会创建一个kube-event的报警机器人,创建机器人成功之后怎么查看webhook,按如下:
      点击智能群助手,可以看到刚才创建的kube-event这个机器人,点击kube-event,就会进入到kube-event机器人的设置界面
      出现如下内容:
      机器人名称:kube-event
      接受群组:钉钉报警测试
      消息推送:开启
      webhook:https://oapi.dingtalk.com/robot/send?access_token=9c03ff1f47b1d15a10d852398cafb84f8e81ceeb1ba557eddd8a79e5a5e5548e
      安全设置:
      自定义关键词:cluster1

      2.安装钉钉的webhook插件,在k8s的master1节点操作

      tar zxvf prometheus-webhook-dingtalk-0.3.0.linux-amd64.tar.gz

      prometheus-webhook-dingtalk-0.3.0.linux-amd64.tar.gz压缩包所在的百度网盘地址如下:

      链接:https://pan.baidu.com/s/1_HtVZsItq2KsYvOlkIP9DQ 
      提取码:d59o

      cd prometheus-webhook-dingtalk-0.3.0.linux-amd64

      启动钉钉报警插件

      nohup ./prometheus-webhook-dingtalk --web.listen-address="0.0.0.0:8060" --ding.profile="cluster1=https://oapi.dingtalk.com/robot/send?access_token=9c03ff1f47b1d15a10d852398cafb84f8e81ceeb1ba557eddd8a79e5a5e5548e" &

      对原来的文件做备份

      cp alertmanager-cm.yaml alertmanager-cm.yaml.bak

      重新生成一个新的alertmanager-cm.yaml文件

      cat >alertmanager-cm.yaml <<EOF
      kind: ConfigMap
      apiVersion: v1
      metadata:
        name: alertmanager
        namespace: monitor-sa
      data:
        alertmanager.yml: |-
          global:
            resolve_timeout: 1m
            smtp_smarthost: 'smtp.163.com:25'
            smtp_from: '15011572657@163.com'
            smtp_auth_username: '15011572657'
            smtp_auth_password: 'BDBPRMLNZGKWRFJP'
            smtp_require_tls: false
          route:
            group_by: [alertname]
            group_wait: 10s
            group_interval: 10s
            repeat_interval: 10m
            receiver: cluster1
          receivers:
          - name: cluster1
            webhook_configs:
            - url: 'http://192.168.124.16:8060/dingtalk/cluster1/send'
              send_resolved: true
      EOF

      通过kubectl apply使配置生效

      kubectl delete -f alertmanager-cm.yaml

      kubectl  apply  -f alertmanager-cm.yaml

      kubectl delete -f prometheus-cfg.yaml

      kubectl apply  -f prometheus-cfg.yaml

      kubectl delete  -f prometheus-deploy.yaml

      kubectl apply  -f  prometheus-deploy.yaml

      通过上面步骤,就可以实现钉钉报警了


      接下来会给大家写通过alertmanager发送微信报警,根据报警级别实现钉钉+微信+邮箱同时报警,同时还会扩展prometheus监控,如监控tomcat、nginx、mysql、redis、mongodb等组件,也会介绍使用prometheus的pushgateway进行自定义数据的监控等,请关注我来持续学习。

      相关实践学习
      通过Ingress进行灰度发布
      本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
      容器应用与集群管理
      欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
      相关文章
      |
      1月前
      |
      Prometheus 运维 监控
      智能运维实战:Prometheus与Grafana的监控与告警体系
      【10月更文挑战第26天】Prometheus与Grafana是智能运维中的强大组合,前者是开源的系统监控和警报工具,后者是数据可视化平台。Prometheus具备时间序列数据库、多维数据模型、PromQL查询语言等特性,而Grafana支持多数据源、丰富的可视化选项和告警功能。两者结合可实现实时监控、灵活告警和高度定制化的仪表板,广泛应用于服务器、应用和数据库的监控。
      210 3
      |
      1月前
      |
      Prometheus 监控 Cloud Native
      基于Docker安装Grafana和Prometheus
      Grafana 是一款用 Go 语言开发的开源数据可视化工具,支持数据监控和统计,并具备告警功能。通过 Docker 部署 Grafana 和 Prometheus,可实现系统数据的采集、展示和告警。默认登录用户名和密码均为 admin。配置 Prometheus 数据源后,可导入主机监控模板(ID 8919)进行数据展示。
      92 2
      |
      1月前
      |
      Prometheus 运维 监控
      智能运维实战:Prometheus与Grafana的监控与告警体系
      【10月更文挑战第27天】在智能运维中,Prometheus和Grafana的组合已成为监控和告警体系的事实标准。Prometheus负责数据收集和存储,支持灵活的查询语言PromQL;Grafana提供数据的可视化展示和告警功能。本文介绍如何配置Prometheus监控目标、Grafana数据源及告警规则,帮助运维团队实时监控系统状态,确保稳定性和可靠性。
      173 0
      |
      3月前
      |
      存储 Prometheus 监控
      在Ubuntu系统上安装与配置Prometheus的步骤
      通过以上步骤,您应该已经成功在Ubuntu系统上安装并配置了Prometheus。您现在可以开始使用Prometheus收集和分析您的系统和应用程序的指标数据了。
      216 1
      |
      4月前
      |
      Prometheus 监控 Cloud Native
      自定义grafana_table(数据源Prometheus)
      综上所述,自定义 Grafana 表格并将 Prometheus 作为数据源的关键是理解 PromQL 的查询机制、熟悉 Grafana 面板的配置选项,并利用 Grafana 强大的转换和自定义功能使数据展示更为直观和有洞见性。随着对这些工具更深入的了解,您将可以创建出更高级的监控仪表盘,以支持复杂的业务监控需求。
      344 1
      |
      4月前
      |
      Prometheus 监控 Cloud Native
      【监控】prometheus传统环境监控告警常用配置
      【监控】prometheus传统环境监控告警常用配置
      【监控】prometheus传统环境监控告警常用配置
      |
      19天前
      |
      存储 Prometheus 监控
      监控堆外第三方监控工具Prometheus
      监控堆外第三方监控工具Prometheus
      37 3
      |
      22天前
      |
      存储 Prometheus 运维
      在云原生环境中,阿里云ARMS与Prometheus的集成提供了强大的应用实时监控解决方案
      在云原生环境中,阿里云ARMS与Prometheus的集成提供了强大的应用实时监控解决方案。该集成结合了ARMS的基础设施监控能力和Prometheus的灵活配置及社区支持,实现了全面、精准的系统状态、性能和错误监控,提升了应用的稳定性和管理效率。通过统一的数据视图和高级查询功能,帮助企业有效应对云原生挑战,促进业务的持续发展。
      29 3
      |
      1月前
      |
      Prometheus 监控 Cloud Native
      在 HBase 集群中,Prometheus 通常监控哪些类型的性能指标?
      在 HBase 集群中,Prometheus 监控关注的核心指标包括 Master 和 RegionServer 的进程存在性、RPC 请求数、JVM 内存使用率、磁盘和网络错误、延迟和吞吐量、资源利用率及 JVM 使用信息。通过 Grafana 可视化和告警规则,帮助管理员实时监控集群性能和健康状况。
      |
      3月前
      |
      Prometheus 监控 Cloud Native
      介绍如何使用Prometheus进行监控
      介绍如何使用Prometheus进行监控
      271 3

      推荐镜像

      更多