<p>I ran into the above stated error and the most popular answer for this error is adding 'selector:' to the yaml file. I get this error even after adding it. Can you please help me rectify this issue?</p>
<p>deployment.yml</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: sampleapp
  labels:
    app: sampleapp
spec:
  replicas: 4
  selector:
    matchLabels:
        app: sampleapp
  template:
    metadata:
      annotations:
        prometheus.io/scrape: &quot;true&quot;
      labels:
        app: sampleapp
    spec:
      containers:
      - name: sampleapp
        #replace &lt;foobar&gt; with your container registry. Example: contosodemo.azurecr.io
        image: containerregistrycanary.azurecr.io/azure-pipelines-canary-k8s
        imagePullPolicy: Always
        ports:
        - containerPort: 8000
        - containerPort: 8080
</code></pre>
<p>fortio.yml</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: fortio
spec:
  replicas: 1
  selector:
  template:
    metadata:
      labels:
        app: fortio
    spec:
      containers:
      - name: fortio
        image: fortio/fortio:latest_release
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
          name: http-fortio
        - containerPort: 8079
          name: grpc-ping
</code></pre>
<p>servicemonitor.yml</p>
<pre><code>apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: sampleapp
  labels:
    release: sampleapp
spec:
  selector:
    matchLabels:
      app: sampleapp
  endpoints:
  - port: metrics
</code></pre>

vishal_P
<p>You need to add selection rules to your <code>selector</code> in the <em>fortio.yml</em>, e.q.</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: fortio
spec:
  replicas: 1
  selector:
    matchLabels:
       app: fortio
  template:
    metadata:
      labels:
        app: fortio
    spec:
      containers:
      - name: fortio
        image: fortio/fortio:latest_release
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
          name: http-fortio
        - containerPort: 8079
          name: grpc-ping

</code></pre>
<blockquote>
<p>The .spec.selector field defines how the Deployment finds which Pods
to manage. In this case, you select a label that is defined in the Pod
template (app: nginx). However, more sophisticated selection rules are
possible, as long as the Pod template itself satisfies the rule.</p>
</blockquote>
<p><a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#creating-a-deployment" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#creating-a-deployment</a></p>
