简体   繁体   English

更新 k8s Prometheus 操作员的配置以添加抓取目标

[英]Updating a k8s Prometheus operator's configs to add a scrape target

I have an existing deployment of prometheus operator on my k8s cluster.我在我的 k8s 集群上部署了 prometheus operator。 Now, I want to add a scrape target for a custom exporter I created.现在,我想为我创建的自定义导出器添加一个抓取目标。 I created my prometheus.yaml file, but can't find how to apply this to the existing prometheus operator.我创建了我的 prometheus.yaml 文件,但找不到如何将其应用于现有的 prometheus 运算符。

I have no idea how and where you create/modify your prometheus.yaml file, but will show you the convenient way of managing it.我不知道您如何以及在何处创建/修改您的prometheus.yaml文件,但会向您展示管理它的便捷方法。

First of all you I would recommend you store prometheus configuration file prometheus.yaml as a configmap.首先,我建议您将 prometheus 配置文件prometheus.yaml存储为 configmap。 This is super useful plus changes you will do in CM will be automatically without your involving spreading/propagating to the pod that work with/consume this CM.这是非常有用的,而且您将在 CM 中进行的更改将自动进行,而无需涉及传播/传播到使用/使用此 CM 的 pod。

So after you make changes with new scrapes in CM - it will take some time to propagate changes.因此,在您使用 CM 中的新内容进行更改后 - 传播更改需要一些时间 Total delay from the moment when the ConfigMap is updated to the moment when new keys are projected to the pod can be as long as kubelet sync period (1 minute by default) + ttl of ConfigMaps cache (1 minute by default) in kubelet.从更新 ConfigMap 到将新密钥投射到 pod 的总延迟可以与 kubelet 同步周期(默认 1 分钟)+ kubelet 中 ConfigMaps 缓存的 ttl(默认 1 分钟)一样长。

Now time to make changes live.. Prometheus has an option to reload its configuration on flight.现在是时候进行更改了。Prometheus 可以选择在运行时重新加载其配置。 You have 2 options how to do that.你有 2 个选择如何做到这一点。 More info you can find opening Reloading Prometheus' Configuration url.更多信息你可以找到打开重新加载普罗米修斯的配置url。

I will stop here only on one solution: You can send a HTTP POST to the Prometheus web server:我将仅在一种解决方案上停在这里:您可以将 HTTP POST 发送到 Prometheus web 服务器:

curl -X POST http://localhost:9090/-/reload

Note that as of Prometheus 2.0, the --web.enable-lifecycle command line flag must be passed for HTTP reloading to work.请注意,从 Prometheus 2.0 开始,必须传递 --web.enable-lifecycle 命令行标志才能使 HTTP 重新加载工作。 If the reload is successful Prometheus will log that it has updated its targets:如果重新加载成功,Prometheus 将记录它已更新其目标:

INFO[0248] Loading configuration file prometheus.yml source=main.go:196
INFO[0248] Stopping target manager... source=targetmanager.go:203
INFO[0248] Target manager stopped. source=targetmanager.go:216
INFO[0248] Starting target manager... source=targetmanager.go:114

And cherry on cake for you:)和樱桃蛋糕给你:)

There is a great Prometheus, ConfigMaps and Continuous Deployment that explain how to monitor Prometheus with prometheus(Maybe it will be applicable somehow in your another question).有一个很棒的Prometheus、ConfigMaps 和 Continuous Deployment解释了如何使用 prometheus 监控 Prometheus(也许它会以某种方式适用于您的另一个问题)。 The main stuff I want to show you is that you can automate POST request.我想向您展示的主要内容是您可以自动执行 POST 请求。

So basically you need a tiny sidecar container that will check CM changes and send POST in case of new changes.所以基本上你需要一个小的 sidecar 容器来检查 CM 变化并在有新变化时发送 POST。 This sidecar should be in the same pod with prometheus这个sidecar应该和prometheus在同一个pod中

Copy pasting example from article here for future reference从此处复制粘贴示例以供将来参考

 spec:
  containers:
  - name: prometheus
    ...
    volumeMounts:
    - name: config-volume
      mountPath: /etc/prometheus
  - name: watch
    image: weaveworks/watch:master-5b2a6e5
    imagePullPolicy: IfNotPresent
    args: ["-v", "-t", "-p=/etc/prometheus", "curl", "-X", "POST", "--fail", "-o", "-", "-sS", "http://localhost:80/-/reload"]
    volumeMounts:
    - name: config-volume
      mountPath: /etc/prometheus
  volumes:
    - name: config-volume
      configMap:
        name: prometheus-config

Documentation for the operator: https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/additional-scrape-config.md运营商文档: https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/additional-scrape-config.md

But you have to use additionalScrapeConfigs Secret :但是你必须使用 additionalScrapeConfigs Secret

    additionalScrapeConfigsSecret:
      enabled: true
      name: additional-scrape-configs
      key: prometheus-additional.yaml

Else you get the Error cannot unmarshal.!map into []yaml.MapSlice否则你会得到错误cannot unmarshal.!map into []yaml.MapSlice

Here is a better documentation: https://github.com/prometheus-community/helm-charts/blob/8b45bdbdabd9b54766c4beb3c562b766b268a034/charts/kube-prometheus-stack/values.yaml#L2691这是一个更好的文档: https://github.com/prometheus-community/helm-charts/blob/8b45bdbdabd9b54766c4beb3c562b766b268a034/charts/kube-prometheus-stack/values.yaml#L2691

According to this, you could add scrape configs without packaging into a secret like this:根据这个,你可以添加抓取配置而不用像这样打包成一个秘密:

additionalScrapeConfigs: |
  - job_name: "prometheus"
  static_configs:
  - targets: ["localhost:9090"]

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

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