提问者:小点点

将烧瓶应用程序连接到库伯内特斯集群中的普罗米修斯


我是新来的Prometheus,我在库伯内特斯集群中有一个简单的烧瓶应用程序,我在集群中也有Prometheus监控Grafana服务,在命名空间调用prometheus监控中。但问题是,当我通过. yaml文件创建ServiceMonitor以连接我的应用程序以监控Prometheus时,我看到没有添加目标,但在配置中我看到添加了作业。但Prometheus中的状态-服务发现被删除。

A不知道为什么我的服务没有连接到serviceMonitor

serviceMonitor/default/监控-webapp/0(0/2个活动目标)

app.py

app = Flask(__name__)
metrics = PrometheusMetrics(app)

@app.route('/api')
def index():
    return 'ok'

部署。yam l

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp-deployment
  labels:
    app: webapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: webapp
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: dmitriy83/flask_one:latest
        imagePullPolicy: Always
        resources:
          requests:
            memory: "64Mi"
            cpu: "250m"
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 5000
        env:
          - name: flask_url
            value: http://flasktwo-service:5003
      imagePullSecrets:
      - name: dockersecret
---
apiVersion: v1
kind: Service
metadata:
  name: webapp-service
spec:
  selector:
    app: webapp
  ports:
    - name: service
      protocol: TCP
      port: 5000
      targetPort: 5000

---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: monitoring-webapp
  labels:
      release: prometheus-monitoring
      app: webapp
spec:
  endpoints:
  - path: /metrics
    port: service
    targetPort: 5000
  namespaceSelector:
    matchNames:
    - default
  selector:
    matchLabels:
      app: webapp

共1个答案

匿名用户

最后我想通了。问题是端口名。请在下面找到可行的解决方案

部署。yam l

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp
  labels:
    component: backend
    instance: app
    name: containers-my-app
  namespace: default
spec:
  selector:
    matchLabels:
      component: backend
      instance: app
      name: containers-my-app
  template:
    metadata:
      labels:
        component: backend
        instance: app
        name: containers-my-app
    spec:
      containers:
      - name: app
        image: dmitriy83/flask_one:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 5000
          name: webapp
      imagePullSecrets:
        - name: myregistrykey


服务。yam l

apiVersion: v1
kind: Service
metadata:
  name: webapp
  labels:
    component: backend
    instance: app
    name: containers-my-app
  namespace: default
spec:
  type: ClusterIP
  ports:
  - name: http
    port: 5000
    protocol: TCP
    targetPort: webapp # one of the major thing w/o it you could not have active targets in Prometheus
  selector:
    component: backend
    instance: app
    name: containers-my-app

最后监视器. yaml

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: webapp-super
  labels:
    component: backend
    instance: app
    name: containers-my-app
    release: kube-prometheus-stack # You need to verify what is your realease name pf prometheus
  namespace: prometheus-monitoring # choose in what name space your prometheus is 
spec:
  namespaceSelector:
    matchNames:
    - default
  selector:
    matchLabels:
      component: backend
      instance: app
      name: containers-my-app
  endpoints:
  - port: http # http - is a port name which was put in service.yaml