1

n.Prometheus监控之所遇问题解决总结

WeiyiGeek 2021-07-12
6808
  • 0x01 Prometheus 安装异常整理

    • 问题1.访问node_exporter程序的9100的/metrics提示context deadline exceeded错误

    • 问题2.采用Docker进行prometheus server端安装报 msg="Failed to create directory for logging active queries"
      错误;

    • 问题3.安装运行prometheus_server端时显示 This filesystem is not supported and may lead to data corruption and data loss
      警告。

    • 问题4.使用Prometheus监控外部k8s集群时提示 x509: certificate signed by unknown authority
      错误

    • 问题5.使用Prometheus监控外部k8s集群时提示 cannot list resource \"services\" in API group
      错误

  • 0x02 Prometheus 使用异常整理

    • 问题1.Warning: Error fetching server time: Detected 82.30200004577637 seconds time difference between your browser and the server.

    • 问题2.在启动postgres_exporter监测失败

  • 0x03 AlertManager 使用异常整理

    • 问题1.配置使用企业邮箱进行报警时显示 email.loginAuth failed: 530 Must issue a STARTTLS command first
      错误

    • 问题2.配置使用企业邮箱进行报警时显示 starttls failed: x509: certificate signed by unknown authority
      错误

    • 问题3.邮件预警配置时遇到的错误记录

    • 问题4.邮件预警配置使用时报 notify retry canceled after 2 attempts: *email.loginAuth auth: 535 Error:
      错误信息。

0x01 Prometheus 安装异常整理

问题1.访问node_exporter程序的9100的/metrics提示context deadline exceeded错误

异常信息: Get http://192.168.90.177:9100/metrics: context deadline exceeded

问题原因: 有可能是系统端口未开放。
解决办法:指定其他端口或者更改防火墙访问策略。

# 解决方式1
nohup ./node_exporter --web.listen-address=":9100" &

# 解决方式2
# CentOS
firewall-cmd --zone=public --add-port=9100/tcp --permanent #永久生效没有permanent参数重启后失效
firewall-cmd --reload #重新载入配置生效
# Ubuntu
sudo ufw allow 9100


问题2.采用Docker进行prometheus server端安装报 msg="Failed to create directory for logging active queries"
错误;

  • 异常信息: 在持久化映射目录下没有 prometheus.yml
    文件因此会被临时创建一个目录文件导致出错或者因为持久化的数据目录权限问题。

$ docker logs prometheus_server
# prometheus_server | level=error ts=2021-04-30T07:50:11.241Z caller=query_logger.go:109 component=activeQueryTracker msg="Failed to create directory for logging active queries"
# prometheus_server | level=error ts=2021-04-30T07:50:11.241Z caller=query_logger.go:87 component=activeQueryTracker msg="Error opening query log file" file=data/queries.active err="open data/queries.active: no such file or directory"
# prometheus_server | panic: Unable to create mmap-ed active query log
# component=activeQueryTracker msg="Error opening query log file" file=/prometheus/queries.active err="open prometheus/queries.active: permission denied"
# panic: Unable to create mmap-ed active query log
  • 解决办法: 在映射的持久化目录下创建 prometheus.yml
    文件并进行相应权限配置。

chmod +777 nfsdisk-31/monitor/prometheus
docker run -p 9090:9090 -v tmp/prometheus.yml:/etc/prometheus/prometheus.yml \
-v nfsdisk-31/monitor/prometheus:/prometheus \
prom/prometheus

Tips : 注意 Prometheus 2.x 版本容器的数据目录是 /prometheus而非/prometheus-data
目录;


问题3.安装运行prometheus_server端时显示 This filesystem is not supported and may lead to data corruption and data loss
警告。

异常信息: 由于prometheus包括本地磁盘时间序列数据库,但也可以选择与远程存储系统集成,此文件系统不受支持可能导致数据损坏和数据丢失。

prometheus_server | level=warn ts=2021-05-10T05:46:49.843Z caller=main.go:813 fs_type=NFS_SUPER_MAGIC msg="This filesystem is not supported and may lead to data corruption and data loss. Please carefully read https://prometheus.io/docs/prometheus/latest/storage/ to learn more about supported filesystems."

异常原因: Prometheus的本地存储不支持不兼容POSIX的文件系统,因为可能会发生不可恢复的损坏。不支持NFS文件系统(包括AWS的EFS)。NFS可能符合POSIX,但大多数实现均不符合。强烈建议使用本地文件系统以提高可靠性,所以此种共享存储文件的方式不推荐。
解决办法: 如果您的本地存储由于某种原因而损坏,解决该问题的最佳策略是关闭Prometheus,然后删除整个存储目录,您也可以尝试删除单个块目录或WAL目录以解决问题。如果数据没问题只是报警你使用了NFS共享存储文件的格式建议采用 FC SAN 存储直连
或者加大磁盘存储空间。


问题4.使用Prometheus监控外部k8s集群时提示 x509: certificate signed by unknown authority
错误

异常信息:

prometheus_server | level=error ts=2021-05-10T05:43:12.126Z caller=klog.go:96 component=k8s_client_runtime func=ErrorDepth msg="pkg/mod/k8s.io/client-go@v0.20.5/tools/cache/reflector.go:167: Failed to watch *v1.Pod: failed to list *v1.Pod: Get \"https://k8s-dev.weiyigeek:6443/api/v1/pods?limit=500&resourceVersion=0\": x509: certificate signed by unknown authority"

解决办法: 在prometheus.yaml主配置中在kubernetes_sd_file对象内的使用 insecure_skip_verify: true
来跳过 tls 验证。

tls_config:
insecure_skip_verify: true


问题5.使用Prometheus监控外部k8s集群时提示 cannot list resource \"services\" in API group
错误

异常信息:

prometheus_server | level=error ts=2021-05-10T09:09:30.960Z caller=klog.go:96 component=k8s_client_runtime func=ErrorDepth msg="pkg/mod/k8s.io/client-go@v0.20.5/tools/cache/reflector.go:167: Failed to watch *v1.Service: failed to list *v1.Service: services is forbidden: User \"system:serviceaccount:default:prometheus\" cannot list resource \"services\" in API group \"\" at the cluster scope"

问题原因: 当前Token所属prometheus用户无该services查看浏览权限需要在 ClsuterRole
进行添加相对应的权限。

kubectl get clusterrole prometheus -o yaml # 采用yaml格式查看其权限集群角色权限
# NAME CREATED AT
# prometheus 2021-05-14T08:29:08Z


0x02 Prometheus 使用异常整理

问题1.Warning: Error fetching server time: Detected 82.30200004577637 seconds time difference between your browser and the server.

  • 异常信息: 服务器时间:检测到您的浏览器和服务器之间有82.30200004577637秒的时间差。Prometheus依赖于精确的时间,而时间漂移可能会导致意外的查询结果。

  • 问题原因: 服务器的时间与本地时间不一致从而导致, PS 在 Prometheus web 中偏差大于 5 min 时,无法查询到任何数据,当时间偏差小于 5min 时,可以查到数据,并且正常在 Grafana 中显示。

  • 解决办法: 服务器时间与本机时间设置一致即可;


问题2.在启动postgres_exporter监测失败

解决办法: 在启动postgres_exporter的Linux用户下加入 DATA_SOURCE_NAME
环境变量

# 环境变量添加
tee -a ~/.bash_profile <<'EOF'
export DATA_SOURCE_NAME="postgresql://postgres:postgres@127.0.0.1:5432/postgres?sslmode=disable"
EOF


# 环境变量生效
source .bash_profile && echo $DATA_SOURCE_NAME

# 启动 postgres 导出器
./postgres_exporter

0x03 AlertManager 使用异常整理

问题1.配置使用企业邮箱进行报警时显示 email.loginAuth failed: 530 Must issue a STARTTLS command first
错误

问题原因: 接入的邮件服务器必须使用tls并且进行有效身份校验。
解决办法:

smtp_require_tls: true




    


问题2.配置使用企业邮箱进行报警时显示 starttls failed: x509: certificate signed by unknown authority
错误

问题原因: 未知机构签署的证书即客户端访问服务端时证书不受信赖
解决办法: 需要在 email_configs
下配置 insecure_skip_verify: true 来跳过 tls 验证

receivers:
- name: 'default-email'
email_configs:
- to: 'master@weiyigeek.top'
insecure_skip_verify: true
send_resolved: true


问题3.邮件预警配置时遇到的错误记录

# - 当在global配置 smtp.weiyigeek.top:25
报错信息:
level=error ts=2020-04-08T06:02:44.036Z caller=notify.go:372 component=dispatcher msg=“Error on notify” err=“send STARTTLS command: x509: certificate is valid for *.mxhichina.com, mxhichina.com, not smtp.weiyigeek.top” context_err=“context deadline exceeded”
level=error ts=2020-04-08T06:02:44.036Z caller=dispatch.go:301 component=dispatcher msg=“Notify for alerts failed” num_alerts=1 err=“send STARTTLS command: x509: certificate is valid for *.mxhichina.com, mxhichina.com, not smtp.weiyigeek.top”

# - 当在global配置
# smtp.weiyigeek.top
# smtp_require_tls:false
报错信息:
level=warn ts=2020-10-12T10:34:11.780Z caller=notify.go:674 component=dispatcher receiver=mail-receiver integration=email[0] msg=“Notify attempt failed, will retry later” attempts=1 err="*smtp.plainAuth auth: unencrypted connection"
level=error ts=2020-10-12T10:34:21.581Z caller=dispatch.go:309 component=dispatcher msg=“Notify for alerts failed” num_alerts=1 err=“mail-receiver/email[0]: notify retry canceled after 7 attempts: *smtp.plainAuth auth: unencrypted connection”

# - 配置smtp.qiye.aliyun.com:465
报错信息:
level=warn ts=2020-10-12T11:36:41.779Z caller=notify.go:674 component=dispatcher receiver=mail-receiver integration=email[0] msg=“Notify attempt failed, will retry later” attempts=1 err="‘require_tls’ is true (default) but “smtp.qiye.aliyun.com:465” does not advertise the STARTTLS extension"
level=error ts=2020-10-12T11:36:51.578Z caller=dispatch.go:309 component=dispatcher msg=“Notify for alerts failed” num_alerts=1 err=“mail-receiver/email[0]: notify retry canceled after 8 attempts: ‘require_tls’ is true (default) but “smtp.qiye.aliyun.com:465” does not advertise the STARTTLS extension”tail: opt/logs/alertmanager-9093.log: file truncated

解决办法: 配置以下两行发送邮件正常

smtp.qiye.aliyun.com:465
smtp_require_tls: false


问题4.邮件预警配置使用时报 notify retry canceled after 2 attempts: *email.loginAuth auth: 535 Error:
错误信息。

错误信息:

level




    
=error ts=2021-05-20T14:34:43.637Z caller=dispatch.go:309 component=dispatcher msg="Notify for alerts failed" num_alerts=2 err="default-email/email[0]: notify retry canceled after 2 attempts: *email.loginAuth auth: 535 Error: \ufffd\ufffdʹ\ufffd\ufffd\ufffd\ufffdȨ\ufffd\ufffd\ufffd\ufffd¼\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd뿴: http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256"

错误原因: 邮箱认证密码未采用用于登录第三方客户端的专用密码或者设置的tmpl模板路径不正确,此时只需按照上述的地址进行申请发信邮箱的专用密码。(实际测试发现QQ邮箱必须要使用第三方专用密码,而企业邮箱可以直接用登陆密码进行认证 不推荐


  • 0x00 前言简述

  • 0x01 AlertManager之邮件告警

    • 1.配置并使用腾讯企业邮箱进行邮件发送警告

    • 2.配置并使用自定义邮箱报警样式模板

    • 3.配置并使用企业微信发送报警通知

  • 0x02 实现Prometheus安全认证配置


0x00 前言简述

描述: Prometheus 告警分为两个部分进行实现监控规则匹配以及告警信息的通知。


  1. 首先需要在 Prometheus
    中添加告警规则,定义告警产生的逻辑。


    1. 其次 Alertmanager
      系统将触发的警报转化为通知,例如邮件、呼叫和聊天消息。

    告警状态
    Prometheus Alert 告警状态有三种状态: Inactive、Pending、Firing

    • Inactive:非活动状态,表示正在监控,但是还未有任何警报触发。

    • Pending:表示这个警报必须被触发。由于警报可以被分组、压抑/抑制或静默/静音,所以等待验证,一旦所有的验证都通过,则将转到 Firing 状态。

    • Firing:将警报发送到 AlertManager,它将按照配置将警报的发送给所有接收者。一旦警报解除则将状态转到 Inactive如此循环。


    参考来源

    • https://prometheus.io/docs/alerting/configuration/


    0x01 AlertManager之邮件告警

    描述: 在前面几章的学习中都没有讲解AlertManager报警管理系统的使用,博主专门把他放在本章进行讲解,因为其实现报警通知的方式有多种多样。

    Email_config 配置参考: https://prometheus.io/docs/alerting/latest/configuration/#email_config

    # - 注意此处非全局所以没有smtp前缀
    
    
    
    
        
    
    # Whether or not to notify about resolved alerts.
    [ send_resolved: <boolean> | default = false ]

    # The email address to send notifications to.
    to: <tmpl_string>

    # The sender address.
    [ from: <tmpl_string> | default = global.smtp_from ]

    # The SMTP host through which emails are sent.
    [ smarthost: <string> | default = global.smtp_smarthost ]

    # The hostname to identify to the SMTP server.
    [ hello: <string> | default = global.smtp_hello ]

    # SMTP authentication information.
    [ auth_username: <string> | default = global.smtp_auth_username ]
    [ auth_password: <secret> | default = global.smtp_auth_password ]
    [ auth_secret: <secret> | default = global.smtp_auth_secret ]
    [ auth_identity: <string> | default = global.smtp_auth_identity ]

    # The SMTP TLS requirement.
    # Note that Go does not support unencrypted connections to remote SMTP endpoints.
    [ require_tls: <bool> | default = global.smtp_require_tls ]

    # TLS configuration.
    tls_config:
    [ <tls_config> ]

    # The HTML body of the email notification.
    [ html: <tmpl_string> | default = '{{ template "email.default.html" . }}' ]
    # The text body of the email notification.
    [ text: <tmpl_string> ]

    # Further headers email header key/value pairs. Overrides any headers
    # previously set by the notification implementation.
    [ headers: { <string>: <tmpl_string>, ... } ]

    实践目标:

    • 1.配置并使用腾讯企业邮箱进行邮件发送警告

    • 2.配置并使用自定义邮箱报警样式模板

    • 3.配置并使用企业微信发送报警通知


    1.配置并使用腾讯企业邮箱进行邮件发送警告

    • Step 1.首先我们需要配置 Prometheus.yml 抓取目标、加载规则文件,并与Alertmanager通信设置。

    global:
    scrape_interval: 2m
    scrape_timeout: 10s
    # - 每分钟进行报警规则的评估 (建议根据实际情况进行修改)
    evaluation_interval: 1m
    external_labels:
    monitor: 'prom-demo'

    alerting:
    # - 设置报警管理系统的目标地址 (可以配置多个AlertManager)
    alertmanagers:
    - scheme: http
    static_configs:
    - targets:
    - '192.168.12.107:30093'

    rule_files:
    # - 指标监控告警规则 (可以根据不同的场景进行报警规则)
    - etc/prometheus/conf.d/rules/*.rules

    # - 采集监控的静态目标和自动化发现目标
    scrape_configs:
    - job_name: 'prom-Server'
    static_configs:
    - targets: ['localhost:9090']
    - job_name: 'cAdvisor'
    static_configs:
    - targets: ['192.168.12.111:9100']
    - job_name: 'linux_exporter'
    scrape_interval: 30s
    file_sd_configs:
    - files:
    - etc/prometheus/conf.d/discovery/k8s_nodes.yaml
    # - 自动刷新自动发现配置文件
    refresh_interval: 1m


    • Step 2.配置 Prometheus 报警规则此次判断采集节点的状态和节点主机负载情况

    tee alert.rules <<'EOF'
    groups:
    - name: node-normal
    rules:
    - alert: service_down
    expr: up {job="linux_exporter"} == 0
    # - 对于持续时间的设置在实际环境中至少建议5min,以减少噪声从而减轻固有监控各种情况,此处设置为30s便于报警情况的观察。
    for: 30s
    labels:
    severity: 'critical'
    team: node
    annotations:
    summary: "主机 {{ $labels.instance }} 监控服务已停止运行超过 15s!"
    description: "请系统管理员尽快进行人工干预处理!"
    - alert: high_load
    expr: node_load1 > 0.7
    for: 5m
    labels:
    severity: 'warning'
    team: node
    annotations:
    summary: "主机 {{ $labels.instance }} 高负载大于0.7以上运行超过 5m!"
    EOF

    Tips : 说明一下该 rules 目的是监测 node 是否存活 expr 为 PromQL 表达式验证特定节点 job="linux_exporter" 是否活着,for 表示报警状态为 Pending 后等待 30s 变成 Firing 状态,一旦变成 Firing 状态则将报警发送到 AlertManager 并且 labels ( 标签
    )和 annotations( 注释
    ) 对该 alert 添加更多的标识和注解信息。

    • Step 3.配置 Alertmanager 报警通知,此时配置腾讯企业邮箱的方式进行邮件发送报警。

    tee alertmanager.yaml <<'EOF'
    # - 全局配置
    global:
    resolve_timeout: 5m
    smtp_from: 'monitor@weiyigeek.top'
    # - 企业邮箱 SMTP 服务地址
    smtp_smarthost: 'smtp.exmail.qq.com:465'
    smtp_auth_username: 'monitor@weiyigeek.top'
    # - 第三方登录企业邮箱的授权码
    smtp_auth_password: xxxxxxxxxxx'
    smtp_require_tls: false
    # smtp_hello: 'qq.com'
    # - 设置报警的分发策略
    route:
    group_by: ['alertname']
    group_wait: 30s
    group_interval: 1m
    # - 如果在10分钟内还未解除报警则进行发送告警
    repeat_interval: 10m
    receiver: 'default-email'
    # - 配置告警消息接受者信息
    receivers:
    - name: 'default-email'
    email_configs:
    - to: 'master@weiyigeek.top'
    send_resolved: true
    # - 抑制规则配置
    inhibit_rules:
    - source_match:
    severity: 'critical'
    target_match:
    severity: 'warning'
    equal: ['alertname', 'instance']
    EOF


    • Step 4.重启Prometheus和Alertmanager服务,同时关闭一台node进行触发报警发送 Email,上边我们定义的 rule 规则为监测 job="linux_exporter" Node 是否活着,那么就可以停掉 node-exporter 服务来间接起到 Node Down 的作用,从而达到报警条件,触发报警规则。

    # - 重启容器
    docker restart prometheus_alertmanager prometheus_server

    # - 关闭192.168.12.109机器的node_exporter服务
    ssh -p 20211 weiyigeek@weiyigeek-109 "sudo -S systemctl stop node_exporter.service"
    [sudo] password for weiyigeek:


    • Step 5.效果查看及报警流程说明,关闭后等待prometheus定时拉取各job的任务执行,其次我们可以在 Targets
      刷新查看 linux_exporter
      任务中状态为 unhealthy 192.168.12.109
      的节点状态是否变成了DOWN,等待 30s 后,alert 页面由绿色 Service_down (0 active) Inactive 状态变成了黄色 Service_down (1 active) Pending 状态,继续等待 30s 后状态变成红色 Firing 状态,向 AlertManager 发送报警信息,此时 AlertManager 则按照配置规则向接受者发送邮件告警。

    linux_exporter (5/6 up)
    Endpoint State Error
    http://192.168.12.109:9100/metrics DOWN Get "http://192.168.12.109:9100/metrics": dial tcp 192.168.12.109:9100: connect: connection refused


    • Step 6.我们在 AlertManager 查看发送报警信息以及发送的报警邮箱,可以看到接收到的报警包含该节点的标签以及我们添加的 Annotations
      注释信息。

    • Step 7.然后我们将 192.168.12.109
      机器的linux_exporter服务进行开启查看其恢复以及警报解除情况。在等待 30s 之后,Prometheus Alerts 页面变成绿色 Service_down (0 active) Inactive
      状态,同时也收到了报警解除邮件提醒。

    ssh -p 20211 weiyigeek@weiyigeek-109 "sudo -S systemctl start node_exporter.service"

    Tips : 每次停止/恢复服务后 30s 之后才会发现 Alert 状态变化,是因为 prometheus.yml中 global ->