简体   繁体   English

从Kubernetes中的Docker收集指标

[英]Collect metrics from dockers in Kubernetes

I work with Docker and Kubernetes. 我使用Docker和Kubernetes。 I would like to collect application specific metrics from each Docker. 我想从每个Docker收集特定于应用程序的指标。 There are various applications, each running in one or more Dockers. 有各种应用程序,每个应用程序都在一个或多个Docker中运行。 I would like to collect the metrics in JSON format in order to perform further processing on each type of metrics. 我想以JSON格式收集指标,以便对每种类型的指标进行进一步处理。

I am trying to understand what is the best practice, if any and what tools can I use to achieve my goal. 我试图了解什么是最佳实践(如果有的话)以及可以使用哪些工具实现我的目标。

Currently I am looking into several options, none looks too good: 目前,我正在研究几种选择,但看起来都不太好:

  1. Connecting to kubectl, getting a list of pods, performing a command (exec) at each pod to cause the application to print/send JSON with metrics. 连接到kubectl,获取pod列表,在每个pod上执行命令(exec),以使应用程序使用指标打印/发送JSON。 I don't like this option as it means that I need to be aware to which pods exist and access each, while the whole point of having Kubernetes is to avoid dealing with this issue. 我不喜欢这个选项,因为它意味着我需要知道存在哪些Pod并访问每个Pod,而拥有Kubernetes的全部目的是避免处理此问题。

  2. I am looking for Kubernetes API HTTP GET request that will allow me to pull a specific file. 我正在寻找Kubernetes API HTTP GET请求,该请求将允许我提取特定文件。 The closest I found is GET /api/v1/namespaces/{namespace}/pods/{name}/log and it seems it is not quite what I need. 我找到的最接近的是GET /api/v1/namespaces/{namespace}/pods/{name}/log ,看来这不是我所需要的。 And again, it forces me to mention each pop by name. 再一次,这迫使我按名称提及每个流行音乐。

  3. I consider to use ExecAction in Probe to send JSON with metrics periodically. 我考虑在Probe中使用ExecAction定期发送带有指标的JSON。 It is a hack (as this is not the purpose of Probe), but it removes the need to handle each specific pod 这是一个hack(因为这不是Probe的目的),但是它消除了处理每个特定吊舱的需要

  4. I can't use Prometheus for reasons that are out of my control but I wonder how Prometheus collects metric. 由于无法控制的原因,我无法使用Prometheus,但我想知道Prometheus如何收集指标。 Maybe I can use similar approach? 也许我可以使用类似的方法?

Any possible solution will be appreciated. 任何可能的解决方案将不胜感激。

From an architectural point of view you have 2 options here: 从架构的角度来看,这里有2个选项:

1) pull model : your application exposes metrics data through a mechanisms (for instance using the HTTP protocol on a different port) and an external tool scrapes your pods at a timed interval (getting pod addresses from the k8s API); 1) pull model :您的应用程序通过一种机制(例如,在不同端口上使用HTTP协议)公开指标数据,而外部工具则以一定的时间间隔抓取您的Pod(从k8s API获取Pod地址); this is the model used by prometheus for instance. 例如,这就是普罗米修斯使用的模型。

2) push model : your application actively pushes metrics to an external server, tipically a time series database such as influxdb, when it is most relevant to it. 2) push model :您的应用程序会主动将指标推送到外部服务器,最重要的是将时间序列数据库(例如influxdb)与它最相关。

In my opinion, option 2 is the easiest to implement, because: 我认为,方案2是最容易实现的,因为:

  • you don't need to deal with k8s APIs in order to discover pods addresses; 您无需处理k8s API即可发现Pod地址;
  • you don't need to create a local storage layer to store your metrics (because you push them one by one as they occour); 您无需创建本地存储层来存储指标(因为它们在占用时被一个接一个地推送);

But there is a drawback: you need to be careful how you implement this, it could cause your API to become slower and you might need to deal with asynchronous calls to your metrics server. 但是有一个缺点:您需要小心实现,这可能会导致API变慢,并且可能需要处理对指标服务器的异步调用。

This is obviously a very generic answer, but I hope it could point you in the right direction. 这显然是一个非常通用的答案,但我希望它可以为您指明正确的方向。

Pity you can not use Prometheus, but it's a good lead for what can be done in this scope. 可惜你不能使用普罗米修斯,但这是在这个范围内可以做的一个很好的线索。 What Prom does is as follows : 舞会的工作如下:

1: it assumes that metrics you want to scrape (collect) are exposed with some http endpoint that Prometheus can access. 1:假设您要抓取(收集)的指标与Prometheus可以访问的一些http端点公开。

2: it connects to kubernetes api to perform a discovery of endpoints to scrape metrics from (there is a config for it, but generaly it means it has to be able to connect to the API and list services/deployments/pods and analyze their annotations (as they have info about metrics endpoints) to compose a list of places to scrape data from 2:它连接到kubernetes api来执行发现端点以从中刮取指标(有一个配置,但是通常这意味着它必须能够连接到API并列出服务/部署/吊舱并分析其注释(因为他们具有有关指标终结点的信息)来组成从中抓取数据的场所列表

3: periodicaly (15s, 60s etc.) it connects to the endpoints and collects the exposed metrics. 3:周期性地(15s,60s等)连接到端点并收集公开的指标。

That's it. 而已。 Rest is storage/postprocessing. 剩下的就是存储/后处理。 The kube related part might be a significant amount of work to do though, so it would be way better to go with something that already exists. 与kube相关的部分可能要做很多工作,因此最好使用已经存在的东西。

Sidenote: while this is generaly a pull based model, there are cases where pull is not possible (vide short lived scripts like php), that is where Prometheus pushgateway comes into play to allow pushing metrics to a place where Prometheus will pull from. 旁注:虽然这通常是基于拉的模型,但在某些情况下无法进行拉(例如php等短期脚本),这就是Prometheus pushgateway发挥作用的地方,它允许将度量推到Prometheus将要从中拉出的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM