码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 如何用 Prometheus Operator 监控 K8s 集群外服务?


    前言

    前面系列文章中:

    • Prometheus Operator 与 kube-prometheus 之一 - 简介 - 东风微鸣技术博客 (ewhisper.cn)
    • 监控 Kubernetes 集群证书过期时间的三种方案 - 东风微鸣技术博客 (ewhisper.cn)

    介绍了 Prometheus Operator 相比 原生 Prometheus 的一些优势, 其已经被各大厂商和流行开源云组件广泛采用. 推荐使用.

    但是实战中, 可能并不是所有组件都在 K8S 集群内, 如: LB、DB、全局DNS、云服务...

    如何用 Prometheus Operator 监控它们? 这里有以下几种方案(算不上方案, 小技巧而已)

    用 Prometheus Operator 监控 K8s 集群外服务方案

    如上文, 这里的 K8s 集群外服务, 指的是一些如 LB、DB、全局DNS、云服务... 的静态服务.

    针对此类服务, 有以下监控方案:

    1. 通过 Prometheus Operator CR - prometheus spec;
      1. 这种方案和 Prometheus 其他配置耦合性较高;
    2. 通过 external name Service + ServiceMonitor
      1. 这种方案有个前提, 即: 被监控的服务是域名;
    3. 通过 Service + Endpoint + ServiceMonitor
      1. 这种方案的适应性较强, 耦合性也较低. 推荐. 👍️
    4. 如果是 BlackboxProbe 类的监控, 即监控: Endpoint(HTTP/S、DNS、TCP、ICMP 和 grpc)的各种参数,包括 HTTP 响应时间、DNS 查询延迟、SSL 证书过期信息、TLS 版本等等。可以直接使用 Probe CR, 前文: 如何使用 Blackbox Exporter 监控 URL? - 东风微鸣技术博客 (ewhisper.cn) 已经提过了, 本次就不再赘述.

    方案一: prometheus spec

    简而言之, 就是直接在 prometheus spec 中加入类似这样的静态配置(static_configs):

    static_configs:
      - targets:
        - SERVICE-FQDN
    • 1
    • 2

    具体配置示例如下:

    apiVersion: monitoring.coreos.com/v1
    kind: Prometheus
    metadata:
      name: monitor-kube-prometheus-st-prometheus
    spec:
      additionalScrapeConfigs:
      - job_name: external
        metrics_path: /metrics
        static_configs:
          - targets:
            - :
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    方案二: external name Service + ServiceMonitor

    利用 Kubernetes 的 Externalname Serivce, 将服务映射到 DNS 名称, 而不是典型的选择算符,例如 my-service 或者 cassandra。

    配置 Externalname Service:

    apiVersion: v1
    kind: Service
    metadata:
      name: gpu-metrics-svc
      namespace: monitoring
      labels:
        k8s-app: gpu-metrics
    spec:
      type: ExternalName
      externalName: 
      clusterIP: ''
      ports:
        - name: metrics
          port: 9100
          protocol: TCP
          targetPort: 9100
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    配置指向该 Service 的 ServiceMonitor:

    apiVersion: monitoring.coreos.com/v1
    kind: ServiceMonitor
    metadata:
      name: gpu-metrics-sm
      labels:
        k8s-app: gpu-metrics
        prometheus: kube-prometheus
    spec:
      selector:
        matchLabels:
          k8s-app: gpu-metrics
        namespaceSelector:
          matchNames:
            - monitoring
      endpoints:
        - port: metrics
          interval: 10s
          honorLabels: true
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    方案三: Service + Endpoint + ServiceMonitor

    通过 Service + Endpoint 方式, 明确将外部服务映射为内部 Service.

    举例如下:

    kind: Service
    apiVersion: v1
    metadata:
      name: external-es-exporter
      labels:
        app: elasticsearch
      namespace: monitoring
    spec:
      type: ClusterIP
      ports:
        - name: metrics
          port: 9114
          protocol: TCP
          targetPort: 9114
    ---
    apiVersion: v1
    kind: Endpoints
    metadata:
      name: external-log-es-exporter
      labels:
        app: elasticsearch
      namespace: monitoring
    subsets:
      - addresses:
          - ip: 
          - ip: 
          - ip: 
        ports:
          - name: metrics
            port: 9114
            protocol: TCP
    • 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

    类似方案二, 再创建对应的 ServiceMonitor 即可:

    apiVersion: monitoring.coreos.com/v1
    kind: ServiceMonitor
    metadata:
      name: elasticsearch
    spec:
      selector:
        matchLabels:
          app: elasticsearch
        namespaceSelector:
          matchNames:
            - monitoring
        endpoints:
        - port: metrics
          path: /metrics
          interval: 30s       
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    这样虽然绕了一些, 但是可以保证, 修改组件 A 的监控的时候, 完全不会影响到组件 B 的配置; 另外, 也不会影响到 Prometheus 其他的监控.

    配置更精确; 粒度更细; 耦合度更低.

    🎉🎉🎉

    📚️ 参考文档

    • Scrape external service with FQDN · Issue #3204 · prometheus-operator/prometheus-operator (github.com)
    • kubernetes - How to monitor external service in prometheus-operator - Stack Overflow
    • Prometheus Operator — How to monitor an external service | by Ido Braunstain | DevOps College
    • Monitor external services with the prometheus operator | jpweber blog
    • prometheus operator scrape external target for HAProxy — xnum's blog

    本文由东风微鸣技术博客 EWhisper.cn 编写!

  • 相关阅读:
    numpy 笔记:重视 numpy 广播
    @ConditionalOnProperty注解作用
    UE4 / UE5 内存与性能优化
    Springboot 根据数据库表自动生成实体类和Mapper,只需三步
    opencv-python 4.9.2. 轮廓特征
    Nature发布:ChatGPT 帮助我进行学术写作的三种方式
    LSTM-Attention单维时间序列预测研究(Matlab代码实现)
    python多线程示例
    DigiCert代码签名证书都支持有哪些加密算法
    用代码构建UI界面
  • 原文地址:https://blog.csdn.net/east4ming/article/details/128110097
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号