<p>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 <code>prometheus-monitoring</code>. 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.</p>
<p>A have no idea why my service is not connect to serviceMonitor</p>
<p><code>serviceMonitor/default/monitoring-webapp/0 (0 / 2 active targets)</code></p>
<p><code>app.py</code></p>
<pre><code>app = Flask(__name__)
metrics = PrometheusMetrics(app)

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

</code></pre>
<p><code>deployment.yaml</code></p>
<pre><code>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: &quot;64Mi&quot;
            cpu: &quot;250m&quot;
          limits:
            memory: &quot;128Mi&quot;
            cpu: &quot;500m&quot;
        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
</code></pre>

<p>Finally i figured it out. The issue was port name. Please find workable solution below</p>
<p><code>deployment.yaml</code></p>
<pre><code>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


</code></pre>
<p><code>service.yaml</code></p>
<pre><code>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

</code></pre>
<p>finally <code>monitor.yaml</code></p>
<pre><code>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
</code></pre>
