开发者学堂课程【3天吃透 Prometheus:Prometheus 服务发现和重新打标】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/1244/detail/18446
Prometheus 服务发现和重新打标
五、基于 DNS 的服务发现
基于DNS的服务发现针对一组DNS域名进行定期查询,以发现待监控的目标。
①查询时使用的DNS服务器由/etc/ resolv.conf文件指定;
②该发现机制依赖于A、AAAA和SRV资源记录,且仅支持该类方法,尚不支持RFC6763中的高级DNS发现方式。
# A list of DNS domain names to be queried.(注意在此处要指定 SRV 资源记录的名称。
nanes :
[ - ]
# The type of DNS query to perform. One of SRV, A, or AAAA.
[ type: | default 'SRV' ]
# The port number used if the query type is not SRV.
[ port: ]
# The time after which the provided names are refreshed.
[ refresh interval: I default = 305 ]
③元数据标签:
_meta_ _dns_ name
_meta_ _dns_ _srv_ record_ _target
_meta_ _dns_ srv_ record_ port
六、基于 Kubernetes API 的服务发现机制
基于 Kubernetes API 的服务发现机制,支持将 API Server中Node、Service、 Endpoint、Pod 和 Ingress 等资源类型下相应的各资源对象视作 target, 并持续监视相关资源的变动;
①Node、Service、 Endpoint、 Pod 和 Ingress 资源分别由各自的发现机制进行定义;
②负责发现每种类型资源对象的组件,在 Prometheus 中称为- 一个“ role”
③同时支持在集群上基于 DaemonSet 控制器部署 node exporter 后发现各节点。
七、各个资源发现时要注意哪些问题
1)Node资源发现
Prometheus 的 node role 将 Kubernetes 集群中的每个节点视作一个 target, 这些节点都监听着 kubelet 使用的端口;
①node role 依次检索节点规范上的 NodeInternalIP、NodeInternalIP、 NodeExternalIP、NodeLegacyHostIP 和 NodeHostName 字段,并将发现的第一个地址作为目标地址(_address__) ;
②可用的meta标签有如下几个:
_meta_ _kubernetes_ node_ _name: The name of the node object.
__meta_ kubernetes_ node_ Jabel_ : Each label from the node object.
_meta_ _kubernetes_ node_ labelpresent_ .: true for each label from the node object.
_meta_ kubernetes_ node_ annotation_ < annotationname>: Each annotation from the node object.
_meta_ kubernetes_ node_ annotationpresent : true for each annotation from the node
object.
_meta_ kubernetes_ node_ _address_ < address_ .type>: The first address for each node address type, if it exists.
③节点上 instance 标签的值取自从 API Server 中发现的节点的名称。
2)Pod资源发现
Prometheus 的 pod role 负责发现Kubernetes集群上的每个 Pod 资源并暴露其容器为 target;
①把Pod_上声明的每个端口视作一个target;
②会为未指定端口的容器创建‘无端口 ”类型的 target,以便于用户通过relabel 机制手动添加端口;
③可用的部分metadata标签如下:
_meta_ _kubernetes_ namespace: The namespace of the pod object.
_meta_ _kubernetes_ _pod_ name: The name of the pod object.
__meta_ kubernetes_ pod_ jip: The pod IP of the pod object.
_meta_ kubernetes_ pod_ _label_ .: Each label from the pod object.
_meta_ kubernetes_ pod_ annotation_ .< annotationname>: Each annotation from the pod object.
_meta_ kubernetes_ pod_ ready: Set to true or false for the pod's ready state.
_meta_ kubernetes_ pod_ phase: Set to Pending, Running, Succeeded, Failed or Unknown in the lifecycle.
_meta_ kubernetes_ .pod_ node_ name: The name of the node the pod is scheduled onto.
_ meta_ kubernetes_ pod_ host jip: The current host IP of the pod object.
_ meta_ _kubernetes_ pod_ _uid: The UID of the pod object.
3)Endpoint 资源发现
Prometheus 的 endpoint role 从各 Endpoint 资源中发现目标;
①它把 endpoint 上的每个端口都视作一个单独的 target;
②若 endpoint 的后端工作负载是 Pod,则会把该 Pod 上其它未绑定到endpoint的端口同样视作一个单独的目标;
③可用的部分meta标签如下
_meta_ _kubernetes_ namespace: The namespace of the endpoints object.
_meta_ kubernetes_ endpoints_ name: The names of the endpoints object.
对于通过附加在Endpoint资源上的端口发现的各target,还以如下meta标签:
_meta_ kubernetes_ _endpoint _hostname: Hostname of the endpoint.
_meta_ kubernetes_ endpoint_ node_ name: Name of the node hosting the endpoint.
_meta_ .kubernetes_ endpoint_ ready: Set to true or false for the endpoint's ready state.
_meta_ kubernetes_ endpoint_ port _name: Name of the endpoint port.
_meta_ _kubernetes_ _endpoint port protocol: Protocol of the endpoint port.
v_meta_ .kubernetes_ endpoint _address_ target_ kind: Kind of the endpoint address target.
v_meta_ kubernetes_ _endpoint _address_ _target. name: Name of the endpoint address target.
若发现的 Endpoint 资源属于某个 Service,则相关 Service 的元标签也会添加在该 Endpoint 上;
若发现的 Endpoint 资源的后端端点是 Pod,则相关 Pod 的元标签也会添加在该 Endpoint 上。
4)Ingress资源发现
Prometheus 的 ingress role 负责从 API Server 中发现 Ingress 资源;
(1)它把 Ingress 资源上的每个path视作一个 target;
(2)特别适用于对 Ingress 进行黑盒监控的场景;
(3)相关的地址被设定为 Ingress 资源上相关 host 字段的值;
(4)可用的部署meta标签如下:
①_meta_ _kubernetes_ namespace: The namespace of the ingress object.
②_meta_ _kubernetes_ jingress_ name: The name of the ingress object.
③_meta_ _kubernetes_ ingress_ _label_ .: Each label from the ingress object.
④_meta_ kubernetes_ ingress_ annotation_ < annotationname>: Each annotation from the ingress object.
⑤_meta_ _kubernetes_ jingress_ scheme: Protocol scheme of ingress, https if TLS config is set. Defaults to http.
⑥_meta_ _kubernetes_ ingress_ path: Path from ingress spec. Defaults to /.
5)基于 Kubernetes 发现机制的部分配置参数
配置示例参考: https:/ / github.com/iKubernetes/ k8s prom/blob/ master/ prometheus/ prometheus –cfgyaml
八、重新打标
(1)对 target 重新打标是在数据抓取之前动态重写 target 标签的强大工具,在每个数据抓取配置中,可以定义多个 relabel 步骤, 它们将按照定义的顺序依次执行;
(2)对于发现的每个target, Prometheus默认会执行如下操作:
① job 的标签设定为其所属的 job_ _name 的值;
②_address__标签的值为该 target 的套接字地址“:
③ instance 标签的值为_ address__ 的值;
④_ scheme_ _标签的值为抓取该 target上指标时使用的协议(http或https) ;
0_ metrics_ _path_ 标签的值为抓取该 target上的指标时使用 URI 路径,默认为/metrics;
⑤_ param_ . 标签的值为传递的URL参数中第一一个 名称为的参数的值;
(3)重新标记期间,还可以使用该target上以“_meta_”开头的元标签;各服务发现机制为其target添加的元标签会有所不同;
(4)重新标记完成后,该target上以“_”开头的所有标签都会被移除;;若在relabel的过程中需要临时存储标签值,则要使用_ _tmp标签名称为前缀进行保存,以避免同 Prometheus 的内建标签冲突。
重新打标有这些配置,我们把它定义在 relabel_config 字段下,这样进行定义:
# The source labels select values from existing labels. Their content is concatenated
# using the configured separator and matched against the configured regular expression
# for the replace, keep, and drop actions。
[ source _labels: '[' [, ...] ']' ]
# Separator placed between concatenated source label values 。
[ separator: I default = ; ]
# Label to which the resulting value is written in a replace action.
# It is mandatory for replace actions. Regex capture groups are available.
[ target _label: ]
# Regular expression against which the extracted value is matched.
[ regex: | default■(.*) ]
# Modulus to take of the hash of the source label values .
[ modulus: ]
# Replacement value against which a regex replace is performed if the
# regular expression matches. Regex capture groups are available.
[ replacement: I default = $1 ]
# Action to perform based on regex matching.
[ action: | default = replace ]
字段用于定义重新标记的行为,其可用取值如下
(1)替换标签值
①replace: 首先将 source_ labels 中指定的各标签的值进行串连,而后将regex 字段中的正则表达式对源标签值进行匹配判定,若匹配,则将 target_label 字段中指定的标签的值替换为 replacement 字段中保存的值;
Replacement 可按需引用保存 regex 中的某个“分组模式”匹配到的值;默认保存整个 regex 匹配到的内容;进行值替换时,replacememnt 字段中指定标签的值也支持以分组格式进行引用;
②hashmod: 将 target_ label的值设置为一个 hash 值,该 hash 则由 modules字段指定的 hash 模对块对source_ Jabels 上各标签的串连值进行 hash 计算生成;
(2)删除指标:该处的每个指标名称对应一个 target
①keep: regex 不能匹配到 target 上的 source_ Jabels 上的各标签的串连值时,则删除该 target;
②drop: regex能 够匹配到 target 上的 source_ Jlabels 上的各标签的串连值时,则删除该target;
(3)创建或删除标签
①labelmap:将regex对 所有的标签名进行匹配判定,而后将匹配到的标签的值赋给 replacement 字段指定的标签名之上;通常用于取出匹配的标签名的一部分生成新标签;
②labeldrop:将 regex 对所有的标签名进行匹配判定,能够匹配到的标签将从该target的标签集中删除;
③labelkeep:将 regex 对所有的标签名进行匹配判定,不能匹配到的标签将从该target的标签集中删除;
注意:要确保在 labeldrop 或 labellkeep 操作后,余下的标签集依然能惟一标识该指标。
九、relabel 示例
1)Relabel示例之replace
下面示例,将三个源标签的值接顺序串联后,由指定的正则表达式进行模式匹配,而后由 replacement 引用模式匹配的结果,并加以改造后,将其赋值给 endpoint 标签。
生成的结果如下图:
2)Relabe 示例之 labelmap
下面的示例,将regex指定的模式对target.上的所有标签进行匹配判定,对于匹配到的标签名,它将以该标签名中匹配的部分为前缀,指定的“_name” 为后缀生成新的标签名,而新标签的值则与其原标签的值相同;
生成的结果如图所示