添加了 kubernetes ServiceMonitor 但未发现目标(0/0 向上)

标签 kubernetes prometheus

我正在尝试在 prometheus 上公开 kubernetes 应用程序的一些自定义指标。

我在 kubernetes 上成功部署了我的应用程序。还添加了 ServiceMonitor,但未发现任何目标 (0/0 up)。 该应用程序是一个带有相关 nginx-prometheus-exporter sidecar 的 nginx 服务器。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-example-v3
  labels:
    app: nginx-example-v3
spec:
  selector:
    matchLabels:
      app: nginx-example-v3
  template:
    metadata:
      labels:
        app: nginx-example-v3
    spec:
      containers:
      - name: nginx
        image: nginx
        resources:
          limits:
            memory: "128Mi"
            cpu: "100m"
        ports:
        - name: http
          containerPort: 8080
        volumeMounts:
        - name: "config"
          mountPath: "/etc/nginx/nginx.conf"
          subPath: "nginx.conf"
      - name: exporter
        image: nginx/nginx-prometheus-exporter:0.8.0
        ports:
        - containerPort: 9113
      volumes:
        - name: "config"
          configMap:
            name: "nginx-example-v2-config"
            
---
apiVersion: v1
kind: Service
metadata:
  labels:
    name: nginx-example-v3
  name: nginx-example-v3
spec:
  type: LoadBalancer
  selector:
    app: nginx-example-v3
  ports:
  - name: http
    port: 8080
    targetPort: 8080
  - name: http-exporter
    port: 9113
    targetPort: 9113

之后我可以看到/metrics API 中公开的 nginx 自定义指标: enter image description here

然后我应用监控服务:

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: nginx-example-v3
spec:
  endpoints:
  - bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
    interval: 15s
    port: web
  selector:
    matchLabels:
      app: nginx-example-v3

我可以在prometheus“服务发现”部分看到该服务已成功添加: enter image description here

但是没有发现代表普罗米修斯的目标(0/0 向上) enter image description here

我缺少什么??? 非常感谢任何帮助,因为我已经被困了这么多天了! 预先非常感谢您。 :-)

回复 @efotopoulou

最佳答案

我稍微更改了服务 list ,现在我可以获取指标了。 对新的讨论感到抱歉。我希望我的 list 可以帮助其他人配置他们的服务。 这是更新后的 list 。 :-)

apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx-example-v3
  name: nginx-example-v3
spec:
  type: LoadBalancer
  selector:
    app: nginx-example-v3
  ports:
  - name: http
    port: 8080
    targetPort: 8080
  - name: web
    port: 9113
    targetPort: 9113

关于添加了 kubernetes ServiceMonitor 但未发现目标(0/0 向上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64319806/

相关文章:

kubernetes - Prometheus 不同的结果,相同的 pod

kubernetes - 主机故障时 Pod 未移动

azure - 创建集群后将 RBAC 添加到 Azure Kubernetes (AKS)

networking - kubernetes pod内的代理不会拦截任何HTTP流量

prometheus - 计算 Prometheus 指标具有特定值的持续时间?

java - 当 Spring Boot 健康检查状态发生变化时收到通知

linux - 如何解决prometheus nofound token错误

go - Kubernetes `client-go`-如何在容器中获取容器状态

jenkins - 使用 GitHub/Jenkins/Kubernetes 实现 CI/CD 管道时的最佳实践

node.js - 普罗米修斯无法从本地主机访问指标