<p>I have the following file using which I'm setting up Prometheus on my Kubernetes cluster:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus-deployment
  namespace: plant-simulator-monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      name: prometheus-server
  template:
    metadata:
      labels:
        app: prometheus-server
    spec:
      containers:
        - name: prometheus
          image: prom/prometheus:latest
          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/
          resources:
            requests:
              memory: "512Mi"
              cpu: "500m"
            limits:
              memory: "1Gi"
              cpu: "1000m"
      volumes:
        - name: prometheus-config-volume
          configMap:
            defaultMode: 420
            name: prometheus-server-conf

        - name: prometheus-storage-volume
          emptyDir: {}
</code></pre>

<p>When I apply this to my Kubernetes cluster, I see the following error:</p>

<pre><code>ts=2020-03-16T21:40:33.123641578Z caller=sync.go:165 component=daemon err="plant-simulator-monitoring:deployment/prometheus-deployment: running kubectl: The Deployment \"prometheus-deployment\" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{\"app\":\"prometheus-server\"}: `selector` does not match template `labels`"
</code></pre>

<p>I could not see anything wrong with my yaml file. Is there something that I'm missing?</p>

joesan
<p>As I mentioned in comments, You have issue with matching <code>labels</code>. </p>

<p>In <code>spec.selector.matchLabels</code> you have <code>name: prometheus-server</code> and in <code>spec.template.medatada.labels</code> you have <code>app: prometheus-server</code>. Values there need to be the same. Below what I get when used your yaml:</p>

<pre><code>$ kubectl apply -f deploymentoriginal.yaml
The Deployment "prometheus-deployment" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"app":"prometheus-server"}: `selector` does not match template `labels`
</code></pre>

<p>And output when I used below yaml with the same labels:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus-deployment
  namespace: plant-simulator-monitoring
spec:
  replicas: 1
  selector:
    matchLabels:
      name: prometheus-server
  template:
    metadata:
      labels:
        name: prometheus-server
    spec:
      containers:
        - name: prometheus
          image: prom/prometheus:latest
          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/
          resources:
            requests:
              memory: "512Mi"
              cpu: "500m"
            limits:
              memory: "1Gi"
              cpu: "1000m"
      volumes:
        - name: prometheus-config-volume
          configMap:
            defaultMode: 420
            name: prometheus-server-conf
        - name: prometheus-storage-volume
          emptyDir: {}

$ kubectl apply -f deploymentselectors.yaml
deployment.apps/prometheus-deployment created
</code></pre>

<p>More detailed info about selectors/labels can be found in <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/" rel="nofollow noreferrer">Official Kubernetes docs</a>.</p>
