一、自动发现类型

在上一篇文中留了一个坑:

监控某个statefulset服务的时候,我在service文件中定义了个EP,然后把pod的ip写死在配置文件中,这样,当pod重启后,IP地址变化,就监控不到数据了,这肯定是不合理的。

如果在我们的 Kubernetes 集群中有了很多的 Service/Pod,那么我们都需要一个一个的去建立一个对应的 ServiceMonitor 对象来进行监控吗?这样岂不是也很麻烦么?

为解决上面的问题,Prometheus Operator 为我们提供了一个额外的抓取配置的来解决这个问题,我们可以通过额外的配置来获取k8s的资源监控(pod、service、node等)。

promethues支持多种文件发现。

其中通过kubernetes_sd_configs,可以达到我们想要的目的,监控其各种资源。kubernetes SD 配置允许从kubernetes REST API接受搜集指标,且总是和集群保持同步状态,以下任何一种role类型都能够配置来发现我们想要的对象,来自官网翻译的。

1、Node

Node role发现每个集群中的目标是通过默认的kubelet的HTTP端口。目标地址默认是kubernetes如下地址中node的第一个地址(NodeInternalIPNodeExternalIP,NodeLegacyHostIP, and NodeHostName.)

可用的meta标签有:

__meta_kubernetes_node_name: The name of the node object.
__meta_kubernetes_node_label_<labelname>: Each label from the node object.
__meta_kubernetes_node_labelpresent_<labelname>: true for each label from the node object.
__meta_kubernetes_node_annotation_<annotationname>: Each annotation from the node object.
__meta_kubernetes_node_annotationpresent_<annotationname>: 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.

此外,node的实例标签将会被设置成从API server传递过来的node的name。

2、Service

service角色会为每个服务发现一个服务端口。对于黑盒监控的服务,这个比较有用。address将会被设置成service的kubernetes DNS名称以及各自的服务端口。

Available meta labels:

__meta_kubernetes_namespace: The namespace of the service object.
__meta_kubernetes_service_annotation_<annotationname>: Each annotation from the service object.
__meta_kubernetes_service_annotationpresent_<annotationname>: "true" for each annotation of the service object.
__meta_kubernetes_service_cluster_ip: The cluster IP address of the service. (Does not apply to services of type ExternalName)
__meta_kubernetes_service_external_name: The DNS name of the service. (Applies to services of type ExternalName)
__meta_kubernetes_service_label_<labelname>: Each label from the service object.
__meta_kubernetes_service_labelpresent_<labelname>: true for each label of the service object.
__meta_kubernetes_service_name: The name of the service object.
__meta_kubernetes_service_port_name: Name of the service port for the target.
__meta_kubernetes_service_port_protocol: Protocol of the service port for the target.

  

3、Pod

Pod role会发现所有pods以及暴露的容器作为target。每个容器声明一个端口,一个单独的target就会生成。如果一个容器没有指定端口,通过relabel手动指定一个端口,一个port-free target容器将会生成。

Available meta labels:

__meta_kubernetes_namespace: The namespace of the pod object.
__meta_kubernetes_pod_name: The name of the pod object.
__meta_kubernetes_pod_ip: The pod IP of the pod object.
__meta_kubernetes_pod_label_<labelname>: Each label from the pod object.
__meta_kubernetes_pod_labelpresent_<labelname>: truefor each label from the pod object.
__meta_kubernetes_pod_annotation_<annotationname>: Each annotation from the pod object.
__meta_kubernetes_pod_annotationpresent_<annotationname>: true for each annotation from the pod object.
__meta_kubernetes_pod_container_init: true if the container is an InitContainer
__meta_kubernetes_pod_container_name: Name of the container the target address points to.
__meta_kubernetes_pod_container_port_name: Name of the container port.
__meta_kubernetes_pod_container_port_number: Number of the container port.
__meta_kubernetes_pod_container_port_protocol: Protocol of the container port.
__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_ip: The current host IP of the pod object.
__meta_kubernetes_pod_uid: The UID of the pod object.
__meta_kubernetes_pod_controller_kind: Object kind of the pod controller.
__meta_kubernetes_pod_controller_name: Name of the pod controller.

  

4、endpoints

endpoints role从每个服务监听的endpoints发现。每个endpoint都会发现一个port。如果endpoint是一个pod,所有包含的容器不被绑定到一个endpoint port,也会被targets被发现。

Available meta labels:

__meta_kubernetes_namespace: The namespace of the endpoints object.
__meta_kubernetes_endpoints_name: The names of the endpoints object.
For all targets discovered directly from the endpoints list (those not additionally inferred from underlying pods), the following labels are attached:
__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.
__meta_kubernetes_endpoint_address_target_kind: Kind of the endpoint address target.
__meta_kubernetes_endpoint_address_target_name: Name of the endpoint address target.
If the endpoints belong to a service, all labels of the role: service discovery are attached.
For all targets backed by a pod, all labels of the role: pod discovery are attached.

  

5、ingress

ingress role将会发现每个ingress。ingress在黑盒监控上比较有用。address将会被设置成ingress指定的配置。

Available meta labels:

__meta_kubernetes_namespace: The namespace of the ingress object.
__meta_kubernetes_ingress_name: The name of the ingress object.
__meta_kubernetes_ingress_label_<labelname>: Each label from the ingress object.
__meta_kubernetes_ingress_labelpresent_<labelname>: true for each label from the ingress object.
__meta_kubernetes_ingress_annotation_<annotationname>: Each annotation from the ingress object.
__meta_kubernetes_ingress_annotationpresent_<annotationname>: true for each annotation from the ingress object.
__meta_kubernetes_ingress_scheme: Protocol scheme of ingress, https if TLS config is set. Defaults to http.
__meta_kubernetes_ingress_path: Path from ingress spec. Defaults to /.

  

二、自动发现Pod配置

比如业务上有一个微服务,类型为statefulset,启动后是2个pod的副本集,pod暴露的数据接口为http://pod_ip:7000/metrics。由于pod每次重启后,ip都会变化,所以只能通过自动发现的方式获取数据。

apiVersion: apps/v1kind: StatefulSet
metadata:labels:run: jx3recipename: jx3recipeannotations:prometheus.io/scrape: "true"
spec:selector:matchLabels:app: jx3recipeserviceName: jx3recipe-servicereplicas: 2template:metadata:labels:app: jx3recipeappCluster: jx3recipe-clusterspec:terminationGracePeriodSeconds: 20containers:- image: hub.kce.ooo.com/jx3pvp/jx3recipe:qa-latestimagePullPolicy: AlwayssecurityContext:runAsUser: 1000name: jx3recipelifecycle:preStop:exec:command: ["kill","-s","SIGINT","1"]volumeMounts:- name: config-volumemountPath: /data/conf.ymlsubPath: conf.ymlresources:requests:cpu: "100m"memory: "500Mi"env:- name: JX3PVP_ENVvalue: "qa"- name: JX3PVP_RUN_MODEvalue: "k8s"- name: JX3PVP_SERVICE_IDvalueFrom:fieldRef:fieldPath: metadata.name- name: JX3PVP_LOCAL_IPvalueFrom:fieldRef:fieldPath: status.podIP- name: JX3PVP_CONSUL_IPvalue: $(CONSUL_AGENT_SERVICE_HOST)ports:- name: bizcontainerPort: 8000protocol: "TCP"- name: admincontainerPort: 7000protocol: "TCP"volumes:- name: config-volumeconfigMap:name: app-configure-file-jx3recipeitems:- key: jx3recipe.ymlpath: conf.yml

  

1、创建发现规则

设定发现pod规则:文件名为promethues-additional.yaml

  • pod名称的label为jx3recipe
  • pod的label_appCluster匹配为 jx3recipe-cluster
  • pod的address为http://.*:7000/metrics格式
- job_name: 'kubernetes-service-pod'kubernetes_sd_configs:- role: podrelabel_configs:- source_labels: [__meta_kubernetes_pod_container_name]action: replacetarget_label: jx3recipe- action: labelmapregex: __meta_kubernetes_pod_label_(.+)- source_labels:  ["__meta_kubernetes_pod_label_appCluster"]regex: "jx3recipe-cluster"action: keep- source_labels: [__address__]action: keepregex: '(.*):7000'

  

2、创建对应的Secret对象

kubectl create secret generic additional-configs --from-file=prometheus-additional.yaml -n monitoring

 

创建完成后,会将上面配置信息进行 base64 编码后作为 prometheus-additional.yaml 这个 key 对应的值存在:

apiVersion: v1
data:prometheus-additional.yaml: LSBqb2JfbmFtZTogJ2t1YmVybmV0ZXMtc2VydmljZS1wb2QnCiAga3ViZXJuZXRlc19zZF9jb25maWdzOgogIC0gcm9sZTogcG9kCiAgcmVsYWJlbF9jb25maWdzOgogIC0gc291cmNlX2xhYmVsczogW19fbWV0YV9rdWJlcm5ldGVzX3BvZF9jb250YWluZXJfbmFtZV0KICAgIGFjdGlvbjogcmVwbGFjZQogICAgdGFyZ2V0X2xhYmVsOiBqeDNyZWNpcGUKICAtIGFjdGlvbjogbGFiZWxtYXAKICAgIHJlZ2V4OiBfX21ldGFfa3ViZXJuZXRlc19wb2RfbGFiZWxfKC4rKQogIC0gc291cmNlX2xhYmVsczogIFsiX19tZXRhX2t1YmVybmV0ZXNfcG9kX2xhYmVsX2FwcENsdXN0ZXIiXQogICAgcmVnZXg6ICJqeDNyZWNpcGUtY2x1c3RlciIKICAgIGFjdGlvbjoga2VlcAogIC0gc291cmNlX2xhYmVsczogW19fYWRkcmVzc19fXQogICAgYWN0aW9uOiBrZWVwCiAgICByZWdleDogJyguKik6NzAwMCcK
kind: Secret
metadata:creationTimestamp: "2019-09-10T09:32:22Z"name: additional-configsnamespace: monitoringresourceVersion: "1004681"selfLink: /api/v1/namespaces/monitoring/secrets/additional-configsuid: e455d657-d3ad-11e9-95b4-fa163e3c10ff
type: Opaque

  然后我们只需要在声明 prometheus 的资源对象文件中添加上这个额外的配置:(prometheus-prometheus.yaml)

3、promethues添加资源对象

修改prometheus-prometheus.yaml文件

apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:labels:prometheus: k8sname: k8snamespace: monitoring
spec:alerting:alertmanagers:- name: alertmanager-mainnamespace: monitoringport: webbaseImage: quay.io/prometheus/prometheusnodeSelector:beta.kubernetes.io/os: linuxreplicas: 2secrets:- etcd-certsresources:requests:memory: 400MiruleSelector:matchLabels:prometheus: k8srole: alert-rulessecurityContext:fsGroup: 2000runAsNonRoot: truerunAsUser: 1000additionalScrapeConfigs:name: additional-configskey: prometheus-additional.yamlserviceAccountName: prometheus-k8sserviceMonitorNamespaceSelector: {}serviceMonitorSelector: {}version: v2.5.0

  增加了下面这一段:

additionalScrapeConfigs:name: additional-configskey: prometheus-additional.yaml

  

4、应用配置

kubectl apply -f prometheus-prometheus.yaml

  过一段时间,刷新promethues上的config,将会看到下面红色框框的配置。

5、添加权限

 在 Prometheus Dashboard 的配置页面下面我们可以看到已经有了对应的的配置信息了,但是我们切换到 targets 页面下面却并没有发现对应的监控任务,查看 Prometheus 的 Pod 日志:

可以看到有很多错误日志出现,都是xxx is forbidden,这说明是 RBAC 权限的问题,通过 prometheus 资源对象的配置可以知道 Prometheus 绑定了一个名为 prometheus-k8s 的 ServiceAccount 对象,而这个对象绑定的是一个名为 prometheus-k8s 的 ClusterRole:(prometheus-clusterRole.yaml)

修改为:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:name: prometheus-k8s
rules:
- apiGroups:- ""resources:- nodes- services- endpoints- pods- nodes/proxyverbs:- get- list- watch
- apiGroups:- ""resources:- configmaps- nodes/metricsverbs:- get
- nonResourceURLs:- /metricsverbs:- get

  更新上面的 ClusterRole 这个资源对象,然后重建下 Prometheus 的所有 Pod,正常就可以看到 targets 页面下面有 kubernetes-service-pod这个监控任务了:

至此,一个自动发现pod的配置就完成了,其他资源(service、endpoint、ingress、node同样也可以通过自动发现的方式实现。)

转载于:https://www.cnblogs.com/skyflask/p/11498834.html

Prometheus监控神技--自动发现配置相关推荐

  1. Linux Zabbix——zabbix可视化、监控模板配置、自定义监控参数、自动发现监控下设备、数据库监控、企业proxy分布式监控搭建配置...

    Zabbix可视化.监控模板配置.自定义监控参数.自动发现监控下设备.数据库监控.proxy分布式监控搭建配置- 文章篇幅较长,可以选择目录查看感兴趣的模块. 1.Zabbix可视化 1. 简介 企业 ...

  2. zabbix 监控使用宏自动发现网卡并进行监控

    一.自动发现网卡并进行监控 首先新建一个模板 1.探索规则名称为Network interface discovery 键值为net.if.discovery 2.filter处要把宏给添加上{#IF ...

  3. SpringCloud 入门教程(四): 分布式环境下自动发现配置服务

    前一章, 我们的Hello world应用服务,通过配置服务器Config Server获取到了我们配置的hello信息"hello world". 但自己的配置文件中必须配置co ...

  4. zabbix监控项之自动发现规则,通过shell脚本输出json格式数据

    例:假如要监控所有正在运行的服务进程端口,因为每台服务器的进程都不同,是一个会变化的监控项,所以应用模板对每台服务器监控时需要用到自动发现添加监控项,以netstat命令查看到的所有进程举例. 一.首 ...

  5. heartbeat如何监控程序_heartbeat+drbd监控脚本(自动发现问题,并邮件通知)

    生产环境有一些服务器是heartbeat+drbd做的高可用(参考:https://blog.51cto.com/net881004/2117869),但是有时候由于各种原因会产生问题(比如脑裂),而 ...

  6. CC00339.CloudKubernetes——|KuberNetes运维.V56|——|Prometheus.v01|自动发现.v01|创建secret|

    一.自动发现配置:创建secret ### --- 配置Prometheus-additional.yaml配置文件 ~~~ 创建prometheus-additional配置文件[root@k8s- ...

  7. Zabbix监控系统系列之十五:自动发现Oracle表空间并监控

    ======= 系列目录 ======= Zabbix监控系统系列之一 : Server部署 Zabbix监控系统系列之二 : 初始化配置 Zabbix监控系统系列之三 :版本升级 Zabbix监控系 ...

  8. zabbix自动发现与监控内存和CPU使用率最高的进程

    监控需求 某项目的应用服务器CPU和内存使用率的监控,通过zabbix系统监控记录应用服务器上进程的CPU和内存的使用情况,并以图表的形式实时展现,以便于我们分析服务器的性能瓶颈. 监控方式 利用za ...

  9. zabbix 监控项自动发现过滤_Zabbix5.2由浅入深之官方自动发现规则初探(网络篇)

    今天的主题是官方模板的自动发现规则分析,在监控工作中常常会遇到一些可变化的OID值,也就是父OID+索引,而索引本身是变化的,如果监控设备数量固定,一个个写问题不大,但在规模增加到一定程度手动添加已然 ...

最新文章

  1. 大雁塔为什么七层_“大唐不夜城”广场旁边的千年古佛塔为何被叫“大雁塔”?...
  2. 缓存击穿问题解决思路
  3. 零基础自学python的建议-python回顾——对零基础学者的建议(一)
  4. 如何停止java线程
  5. 在行进中开火 Fire and Motion By Joel Spolsky
  6. matlab画圆的命令_matlab 如何画圆
  7. 十大最热门人工智能技术
  8. RuntimeError: no valid convolution algorithms available in CuDNN
  9. 自制力宛若受到控制的火焰
  10. .vimrc快捷键设置
  11. 文华财经期货指标公式,期货交易用什么指标信号简单,准确率高,
  12. tcc-transaction深入理解
  13. 2017 CCCC预赛总结
  14. android支付宝转跳转,APP跳转支付宝指定界面
  15. 转载tangl_99的Servlet 实现文件上传下载--自己的一点心得
  16. 还在为取快递而烦恼吗?智宇物联助力自动快递柜
  17. mongodb 3.4.10 更换集群IP
  18. 【Unity Shader编程】之十五 屏幕高斯模糊(Gaussian Blur)后期特效的实现
  19. 【音视频数据数据处理 12】【H.264篇】解析H.264原始码流中的I帧 / P帧 / B帧数据(暂未解决,本文先放着,来日更新)
  20. 存储之Block-MultiQueue机制详解(二)

热门文章

  1. sa结构组网方式_5G独立组网SA模式下的驻网流程浅析
  2. 安装pyinstaller报错----Fatal error in launcher:
  3. 反射-获取信息详细(转)
  4. 腾讯云轻量服务器在线安装MySQL
  5. isInterrupted() interrupt() interrupted() 方法的简单解释
  6. android 4.4.4版本模拟器,新一代神器!安卓模拟器4.4.2内核之上的穹顶之战
  7. ORB-SLAM2原理分析
  8. Matlab求解微分方程数值解
  9. notion函数_【notion】关于进度管理-时间函数的一点小心得
  10. IP-guard屏幕监控,违规操作全记录