首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在Kubernetes上为Prometheus写入配置文件

无法在Kubernetes上为Prometheus写入配置文件
EN

Stack Overflow用户
提问于 2017-07-07 16:19:19
回答 2查看 2.5K关注 0票数 0

我在kubernetes集群上运行prometheus,试图抓取pod、节点和服务。当我通过发送POST请求重新加载配置时,我得到了以下错误-

代码语言:javascript
复制
failed to reload config: couldn't load configuration (-config.file=/etc/prometheus/conf/prometheus.yml): unknown fields in kubernetes_sd_config: api_server

在尝试按照官方文档编写配置文件时,我无法理解relabel_configs、source_labels、target_labels、action、keep、regex等部分。有没有人能解释一下这些部分,还有普罗米修斯中标签的用法。提前谢谢。

以下是prometheus.yml文件-

代码语言:javascript
复制
scrape_configs:


- job_name: 'kubernetes-nodes'

  # Default to scraping over https. If required, just disable this or change to
  # `http`.
  scheme: https

  # This TLS & bearer token file config is used to connect to the actual scrape
  # endpoints for cluster components. This is separate to discovery auth
  # configuration because discovery & scraping are two separate concerns in
  # Prometheus. The discovery auth config is automatic if Prometheus runs inside
  # the cluster. Otherwise, more config options have to be provided within the
  # <kubernetes_sd_config>.
  tls_config:
    ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
    # If your node certificates are self-signed or use a different CA to the
    # master CA, then disable certificate verification below. Note that
    # certificate verification is an integral part of a secure infrastructure
    # so this should only be disabled in a controlled environment. You can
    # disable certificate verification by uncommenting the line below.
    #
    # insecure_skip_verify: true
  bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token

  kubernetes_sd_configs:
  - api_server: "https://kubernetes.default.svc"
  - role: node

  relabel_configs:
  - action: labelmap
    regex: __meta_kubernetes_node_label_(.+)
  - target_label: __address__
    replacement: kubernetes.default.svc:443
  - source_labels: [__meta_kubernetes_node_name]
    regex: (.+)
    target_label: __metrics_path__
    replacement: /api/v1/nodes/${1}/proxy/metrics

# Scrape config for service endpoints.
#
# The relabeling allows the actual service scrape endpoint to be configured
# via the following annotations:
#
# * `prometheus.io/scrape`: Only scrape services that have a value of `true`
# * `prometheus.io/scheme`: If the metrics endpoint is secured then you will need
# to set this to `https` & most likely set the `tls_config` of the scrape config.
# * `prometheus.io/path`: If the metrics path is not `/metrics` override this.
# * `prometheus.io/port`: If the metrics are exposed on a different port to the
# service then set this appropriately.


# Example scrape config for probing services via the Blackbox Exporter.
#
# The relabeling allows the actual service scrape endpoint to be configured
# via the following annotations:
#
# * `prometheus.io/probe`: Only probe services that have a value of `true`
- job_name: 'kubernetes-services'

  metrics_path: /probe
  params:
    module: [http_2xx]

  kubernetes_sd_configs:
  - api_server: "https://kubernetes.default.svc"
  - role: service

  relabel_configs:
  - source_labels: [__meta_kubernetes_service_annotation_prometheus_io_probe]
    action: keep
    regex: true
  - source_labels: [__address__]
    target_label: __param_target
  - target_label: __address__
    replacement: blackbox
  - source_labels: [__param_target]
    target_label: instance
  - action: labelmap
    regex: __meta_kubernetes_service_label_(.+)
  - source_labels: [__meta_kubernetes_namespace]
    target_label: kubernetes_namespace
  - source_labels: [__meta_kubernetes_service_name]
    target_label: kubernetes_name

# Example scrape config for pods
#
# The relabeling allows the actual pod scrape endpoint to be configured via the
# following annotations:
#
# * `prometheus.io/scrape`: Only scrape pods that have a value of `true`
# * `prometheus.io/path`: If the metrics path is not `/metrics` override this.
# * `prometheus.io/port`: Scrape the pod on the indicated port instead of the
# pod's declared ports (default is a port-free target if none are declared).
- job_name: 'kubernetes-pods'

  kubernetes_sd_configs:
  - api_server: "https://kubernetes.default.svc"
  - role: pod

  relabel_configs:
  - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
    action: keep
    regex: true
  - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
    action: replace
    target_label: __metrics_path__
    regex: (.+)
  - source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
    action: replace
    regex: ([^:]+)(?::\d+)?;(\d+)
    replacement: $1:$2
    target_label: __address__
  - action: labelmap
    regex: __meta_kubernetes_pod_label_(.+)
  - source_labels: [__meta_kubernetes_namespace]
    action: replace
    target_label: kubernetes_namespace
  - source_labels: [__meta_kubernetes_pod_name]
    action: replace
    target_label: kubernetes_pod_name
EN

回答 2

Stack Overflow用户

发布于 2017-07-08 05:53:35

您的yaml文件已关闭,请尝试以下操作:

代码语言:javascript
复制
  - job_name: 'kubernetes-services'

...

    kubernetes_sd_configs:
    - api_server: "https://kubernetes.default.svc"
      role: service

...
票数 1
EN

Stack Overflow用户

发布于 2017-07-11 01:39:07

这是正在运行的Prometheus Configmap示例文件fwiw。

https://github.com/kayrus/prometheus-kubernetes/blob/master/prometheus-configmap.yaml#L214-L241

我发现为了减少kubectl认为它正在做的事情的噪音,可以使用yamllint。如果您使用选项yaml获得配置映射;当在kubectl命令中读回该文件时,将用于存储数据的所有部分放入data:部分,并且它应该知道忽略其他3个部分(apiVersion、kind和)

因此,当/如果将其作为新的配置映射加载时,请确保只有data:节。

apiVersion: v1 data: kind: ConfigMap metadata:

命令来获取配置映射

kubectl get configmap prometheus-config --namespace prometheus -o yaml > prometheus.yml

删除两个文件(您的文件和示例文件)中所有多余的注释和多余的空行,将其另存为prometheus#.yml,然后获取yamllint并在文件上运行它

yamllint -d relaxed prometheus[#].yml

大多数时候,yamllint会抱怨行的长度超过80个字符。如果这是一个JSON语法问题,那么它很快就会出现。

HTH

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44965892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档