在 K8s 的 Prometheus 服务中添加 additionalScrapeConfigs

add additionalScrapeConfigs into Prometheus service in K8s

Kubectl 版本:

Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.2", GitCommit:"092fbfbf53427de67cac1e9fa54aaa09a28371d7", GitTreeState:"clean", BuildDate:"2021-06-16T12:59:11Z", GoVersion:"go1.16.5", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.11", GitCommit:"c6a2f08fc4378c5381dd948d9ad9d1080e3e6b33", GitTreeState:"clean", BuildDate:"2021-05-13T17:14:18Z", GoVersion:"go1.15.12", Compiler:"gc", Platform:"linux/amd64"}

我创建了一个包含以下主要内容的 prometheus-values.yaml 文件:

defaultRules:
  create: true
  rules:
    alertmanager: false
    etcd: true
    general: true
    k8s: true
    kubeApiserver: true
    kubePrometheusNodeAlerting: true
    kubePrometheusNodeRecording: true
    kubernetesAbsent: true
    kubernetesApps: true
    kubernetesResources: true
    kubernetesStorage: true
    kubernetesSystem: true
    kubeScheduler: true
    network: true
    node: true
    prometheus: true
    prometheusOperator: true
    time: true

  ## Labels for default rules
  labels: {}
  ## Annotations for default rules
  annotations:
    priority: 'high'


kubeControllerManager:
  enabled: true
  serviceMonitor:
    https: false
    insecureSkipVerify: true

kubelet:
  enabled: true
  serviceMonitor:
    https: false

prometheus:
  prometheusSpec:
    externalUrl: 'https://myapp-demo.eastus.cloudapp.azure.com/prometheus/'
    routePrefix: '/prometheus'
  podDisruptionBudget:
    enabled: true
    minAvailable: 1
  ingress:
    enabled: true
    annotations:
      # nginx.ingress.kubernetes.io/auth-type: basic
      # nginx.ingress.kubernetes.io/auth-secret: admin
      # nginx.ingress.kubernetes.io/auth-realm: "Authentication Required - ok"

    tls: 
      # - secretName: cluster1
      - hosts: 
          - myapp-demo.eastus.cloudapp.azure.com
    hosts:
      - myapp-demo.eastus.cloudapp.azure.com
    # path: "/prometheus/"
    paths: 
      - "/prometheus/"
    pathType: ImplementationSpecific
  
  prometheusSpec:
    additionalScrapeConfigs:
      - job_name: myapp_cluster
        metrics_path: /MyApp/Metrics
        basic_auth:
          username: prometheus
          password: MyAppPass
        # static_configs:
        #   - targets: ["10.244.0.5:8080"]
        scrape_interval: 15s
        kubernetes_sd_configs:
          - role: endpoints
            namespaces:
              names:
              - myapp-ha
        relabel_configs:
          - action: keep
            source_labels:
            - __meta_kubernetes_service_name
            regex: myapp-svc
          - action: keep
            source_labels:
            - __meta_kubernetes_endpoint_port_name
            regex: HTTP

为简单起见删除了 Grafana 设置,然后我使用以下命令部署 prometheus:

helm install prometheus prometheus-community/kube-prometheus-stack --values prometheus_values.yaml

如果我删除部分 prometheusSpec,我可以通过以下方式访问普罗米修斯:'https://myapp-demo.eastus.cloudapp.azure.com/prometheus/'。

但是,如果我如上所示包含 prometheusSpec,我将从相同的 link 访问中获得 404 page not found

同时,我使用命令:

kubectl exec -it prometheus-prometheus-kube-prometheus-prometheus-0 -- cat /etc/prometheus/config_out/prometheus.env.yaml

检查 prometheus 配置,看起来是正确的。

当我使用命令 kubectl get endpoints 时,我可以看到端点 myapp-svc 具有值:10.244.0.5:8080,10.244.1.8:8080,10.244.2.7:8080

感谢有人可以与我分享 'additionalScrapeConfigseither viaendpointsorpodofkubernetes_sd_configs`

的可行配置

这是为了提高知名度而发布的社区 Wiki 答案。随意扩展它。

正如评论中已经确认的那样,解决方案是分叉 helm 图表并直接修改 values.yaml