使用 prometheus pod 监控 golang webapp pod

时间:2021-01-30 15:31:27

标签: kubernetes prometheus

我有一个在 kubernetes 集群中运行的 golang webapp pod,我尝试部署一个 prometheus pod 来监控 golang webapp pod。

我在 service.yaml 文件中指定了 prometheus.io/port: to 2112,这是 golang webapp 正在侦听的端口,但是当我转到 Prometheus 仪表板时,它说 2112 端点已关闭.

我正在关注 this guide,尝试了此线程的解决方案 thread,但仍然得到结果说 2112 端点已关闭。

下面是我的 service.yaml 和 deployment.yaml

apiVersion: v1
kind: Service
metadata:
  name: prometheus-service
  namespace: monitoring
  annotations:
      prometheus.io/scrape: 'true'
      prometheus.io/path: '/metrics'
      prometheus.io/port:   '2112'
spec:
  selector: 
    app: prometheus-server
  type: NodePort  
  ports:
    - port: 8080
      targetPort: 9090 
      nodePort: 30000
---
apiVersion: v1
kind: Service
metadata: 
  namespace: monitoring
  name: goapp
spec:
  type: NodePort
  selector:
    app: golang
  ports:
    - name: main
      protocol: TCP
      port: 80
      targetPort: 2112
      nodePort: 30001






apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus-deployment
  namespace: monitoring
  labels:
    app: prometheus-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: prometheus-server
  template:
    metadata:
      labels:
        app: prometheus-server
    spec:
      containers:
        - name: prometheus
          image: prom/prometheus
          args:
            - "--config.file=/etc/prometheus/prometheus.yml"
            - "--storage.tsdb.path=/prometheus/"
          ports:
            - containerPort: 9090
          volumeMounts:
            - name: prometheus-config-volume
              mountPath: /etc/prometheus/
            - name: prometheus-storage-volume
              mountPath: /prometheus/
      volumes:
        - name: prometheus-config-volume
          configMap:
            defaultMode: 420
            name: prometheus-server-conf
  
        - name: prometheus-storage-volume
          emptyDir: {}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: monitoring
  name: golang
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: golang
    spec:
      containers:
        - name: gogo
          image: effy77/gogo2
          ports: 
            - containerPort: 2112
  selector:
    matchLabels:
      app: golang

我会尝试将 prometheus.io/port: 2112 添加到 prometheus 部署部分,因为我怀疑这可能是原因。

1 个答案:

答案 0 :(得分:2)

我对在哪里放置注释感到困惑,从这个 thread 中得到了我的澄清,我需要将它放在需要被 prothemeus 抓取的服务的元数据下。所以就我而言,它需要在 goapp 的元数据中。

apiVersion: v1
kind: Service
metadata: 
  namespace: monitoring
  name: goapp
  annotations:
      prometheus.io/scrape: 'true'
      prometheus.io/path: '/metrics'
      prometheus.io/port:   '2112'