Error: `selector` does not match template `labels` after adding selector

12/3/2019

I am trying to deploy this kubernetes deployment; however, when ever I do: kubectl apply -f es-deployment.yaml it throws the error: Error: `selector` does not match template `labels
I have already tried to add the selector, matchLabels under the specs section but it seems like that did not work. Below is my yaml file.

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert -f docker-compose.yaml --volumes hostPath
    kompose.version: 1.19.0 (f63a961c)
  creationTimestamp: null
  labels:
    io.kompose.service: elasticsearchconnector
  name: elasticsearchconnector
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        kompose.cmd: kompose convert -f docker-compose.yaml --volumes hostPath
        kompose.version: 1.19.0 (f63a961c)
      creationTimestamp: null
      labels:
        io.kompose.service: elasticsearchconnector
    spec:
      selector: 
        matchLabels:
          app: elasticsearchconnector
      containers:
      - env:
          - [env stuff]
        image: confluentinc/cp-kafka-connect:latest
        name: elasticsearchconnector
        ports:
        - containerPort: 28082
        resources: {}
        volumeMounts:
        - mountPath: /etc/kafka-connect
          name: elasticsearchconnector-hostpath0
        - mountPath: /etc/kafka-elasticsearch
          name: elasticsearchconnector-hostpath1
        - mountPath: /etc/kafka
          name: elasticsearchconnector-hostpath2
      restartPolicy: Always
      volumes:
      - hostPath:
          path: /home/centos/Sprint0Demo/DockerScriptsAWS/ServersCompose/home/centos/Sprint0Demo/Servers/elasticSearchConnector/etc/kafka-connect
        name: elasticsearchconnector-hostpath0
      - hostPath:
          path: /home/centos/Sprint0Demo/DockerScriptsAWS/ServersCompose/home/centos/Sprint0Demo/Servers/elasticSearchConnector/etc/kafka-elasticsearch
        name: elasticsearchconnector-hostpath1
      - hostPath:
          path: /home/centos/Sprint0Demo/DockerScriptsAWS/ServersCompose/home/centos/Sprint0Demo/Servers/elasticSearchConnector/etc/kafak
        name: elasticsearchconnector-hostpath2
status: {}
-- James Ukilin
deployment
kubernetes
selector
yaml

1 Answer

12/3/2019

Your labels and selectors are misplaced.

First, you need to specify which pods the deployment will control:

spec:
  replicas: 1
  selector: 
    matchLabels:
      app: elasticsearchconnector

Then you need to label the pod properly:

  template:
    metadata:
      annotations:
        kompose.cmd: kompose convert -f docker-compose.yaml --volumes hostPath
        kompose.version: 1.19.0 (f63a961c)
      creationTimestamp: null
      labels:
        io.kompose.service: elasticsearchconnector
        app: elasticsearchconnector
    spec:
      containers:
-- Burak Serdar
Source: StackOverflow