connect flask app to Prometheus in Kubernetes cluster

I'm new with Prometheus and I have simple flask app in Kubernetes cluster also I have Prometheus-Monitoring-Grafana services in cluster too in namespace calles prometheus-monitoring. But the problem is when I create ServiceMonitor via .yaml file to connect my app to monitor with Prometheus I see that targets is not added but in config i see that job was added. But status in Prometheus - Service Discovery is Dropped.

A have no idea why my service is not connect to serviceMonitor

serviceMonitor/default/monitoring-webapp/0 (0 / 2 active targets)

app.py

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

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

deployment.yaml

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

Solution 1:

Finally i figured it out. The issue was port name. Please find workable solution below

deployment.yaml

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


service.yaml

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

finally monitor.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