如何用 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、云服务... 的静态服务.
针对此类服务, 有以下监控方案:
- 通过 Prometheus Operator CR -
prometheusspec;- 这种方案和 Prometheus 其他配置耦合性较高;
- 通过 external name
Service+ServiceMonitor- 这种方案有个前提, 即: 被监控的服务是域名;
- 通过
Service+Endpoint+ServiceMonitor- 这种方案的适应性较强, 耦合性也较低. 推荐. ️
- 如果是 BlackboxProbe 类的监控, 即监控: Endpoint(HTTP/S、DNS、TCP、ICMP 和 grpc)的各种参数,包括 HTTP 响应时间、DNS 查询延迟、SSL 证书过期信息、TLS 版本等等。可以直接使用
ProbeCR, 前文: 如何使用 Blackbox Exporter 监控 URL? - 东风微鸣技术博客 (ewhisper.cn) 已经提过了, 本次就不再赘述.
方案一: prometheus spec
简而言之, 就是直接在 prometheus spec 中加入类似这样的静态配置(static_configs):
static_configs:
- targets:
- SERVICE-FQDN
具体配置示例如下:
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:
- <IP>:<PORT>
方案二: 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: <gpu-machine-ip>
clusterIP: ''
ports:
- name: metrics
port: 9100
protocol: TCP
targetPort: 9100
配置指向该 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
方案三: 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: <elasticsearch_ip_1>
- ip: <elasticsearch_ip_2>
- ip: <elasticsearch_ip_3>
ports:
- name: metrics
port: 9114
protocol: TCP
类似方案二, 再创建对应的 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
这样虽然绕了一些, 但是可以保证, 修改组件 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
本文由博客一文多发平台 OpenWrite 发布!
如何用 Prometheus Operator 监控 K8s 集群外服务?的更多相关文章
- 如何使用helm优雅安装prometheus-operator,并监控k8s集群微服务
前言:随着云原生概念盛行,对于容器.服务.节点以及集群的监控变得越来越重要.Prometheus 作为 Kubernetes 监控的事实标准,有着强大的功能和良好的生态.但是它不支持分布式,不支持数据 ...
- Kubernetes之利用prometheus监控K8S集群
prometheus它是一个主动拉取的数据库,在K8S中应该展示图形的grafana数据实例化要保存下来,使用分布式文件系统加动态PV,但是在本测试环境中使用本地磁盘,安装采集数据的agent使用Da ...
- 基于prometheus监控k8s集群
本文建立在你已经会安装prometheus服务的基础之上,如果你还不会安装,请参考:prometheus多维度监控容器 如果你还没有安装库k8s集群,情参考: 从零开始搭建基于calico的kuben ...
- Prometheus 监控K8S集群资源监控
Prometheus 监控K8S集群中Pod 目前cAdvisor集成到了kubelet组件内,可以在kubernetes集群中每个启动了kubelet的节点使用cAdvisor提供的metrics接 ...
- shell脚本监控k8s集群job状态,若出现error通过触发阿里云的进程监控报警
#!/bin/bash while [ 1 ] do job_error_no=`kubectl get pod -n weifeng |grep -i "job"|grep -c ...
- k8s暴露集群内和集群外服务的方法
集群内服务 一般 pod 都是根据 service 资源来进行集群内的暴露,因为 k8s 在 pod 启动前就已经给调度节点上的 pod 分配好 ip 地址了,因此我们并不能提前知道提供服务的 pod ...
- 如何用prometheus监控k8s集群中业务pod的metrics
一般,我们从网上看到的帖子和资料, 都是用prometheus监控k8s的各项资源, 如api server, namespace, pod, node等. 那如果是自己的业务pod上的自定义metr ...
- Prometheus神器之监控K8s集群
Prometheus 简介 Prometheus是SoundCloud开源的一款开源软件.它的实现参考了Google内部的监控实现,与源自Google的Kubernetes结合起来非常合适.另外相比i ...
- prometheus operator(Kubernetes 集群监控)
一.Prometheus Operator 介绍 Prometheus Operator 是 CoreOS 开发的基于 Prometheus 的 Kubernetes 监控方案,也可能是目前功能最全面 ...
- Rancher2.x 一键式部署 Prometheus + Grafana 监控 Kubernetes 集群
目录 1.Prometheus & Grafana 介绍 2.环境.软件准备 3.Rancher 2.x 应用商店 4.一键式部署 Prometheus 5.验证 Prometheus + G ...
随机推荐
- 【Azure Developer】Java代码访问Key Vault Secret时候的认证问题,使用 DefaultAzureCredentialBuilder 或者 ClientSecretCredentialBuilder
问题描述 使用Java SDK获取Key Vault Secret机密信息时,需要获取授权.通常是使用AAD的注册应用(Client ID, Tenant ID, Client Secret)来获取 ...
- Centos8 ssh配置三台虚拟机免密登录 root 及 非root 稍有差异;SSH的免密登录详细步骤
为了保证一台Linux主机的安全,所以我们每个主机登录的时候一般我们都设置账号密码登录.但是很多时候为了操作方便,我们都通过设置SSH免密码登录. 一.配置SSH 基本语法:假设要用用户名root登录 ...
- Codeforces(1500板刷)
目录 写在前面 1. A. Did We Get Everything Covered?(构造.思维) 题目链接 题意 题解 代码 总结 2 F. Greetings(离散化+树状数组) 题目链接 题 ...
- Spring Cloud 系列之Hystrix、Ribbon、Feign 源码剖析(一)引子
系列目录 Spring Cloud 系列之Hystrix.Ribbon.Feign 源码剖析(一)引子 Spring Cloud 系列之Hystrix.Ribbon.Feign 源码剖析(二)原理概括 ...
- html5与css3新特性
HTML5新特性 增加了一些新的标签.新的表单以及新的表单属性等 这些新特性都有兼容性问题,基本上IE9+以上版本浏览器才支持,如果不考虑兼容性问题,可以大量使用这些新特性 新增语义化标签 - < ...
- LosslessCut 视频 切割合并 - 软件推荐 - 非常好用
LosslessCut 视频切割合并 - 软件推荐 - 非常好用 下载地址 https://n802.com/f/14902046-490311155-557856 参考文章 http://www.r ...
- masscode.io snippets 和 vscode 联动 代码片段
https://masscode.io/ 软件作用 代码片段 vscode 可以联动使用 下载不行 慢的话, 下载 fastgithub,打开后再下载
- CentOS 7.9 环境下搭建k8s集群(一主两从)
目录 一.硬件准备(虚拟主机) 二.环境准备 1.所有机器关闭防火墙 2.所有机器关闭selinux 3.所有机器关闭swap 4.所有机器上添加主机名与ip的对应关系 5.在所有主机上将桥接的ipv ...
- decltype总结
decltype会识别const和引用 decltype一个表达式的时候会判断该表达式是左值还是右值,如果是左值就返回引用,否则就返回非引用. int &a = xx; const declt ...
- 聚焦“云XR如何赋能元宇宙”,3DCAT实时云渲染首届行业生态合作交流会成功举办
2021年12月17日下午,由深圳市瑞云科技有限公司主办,深圳市虚拟现实产业联合会协办的云XR如何赋能元宇宙--3DCAT实时云渲染首届行业生态合作交流会圆满落幕.此次活动围绕 "云XR如何 ...