K8s deployment error: `selector` does not match template `labels`

7/10/2020

I'm trying to migrate from docker-compose to kubernetes. I had some issues with volume, so what I did is:

kompose convert --volumes hostPath

Then I had another issue

no matches for kind "Deployment" in version "extensions/v1beta1"

So I've changed ApiVersion from extensions/v1beta1 to app/v1 and added "selector". Now I can't manage with that issue:

Error from server (Invalid): error when creating "database-deployment.yaml": Deployment.apps "database" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"io.kompose.service":"database"}: `selector` does not match template `labels`
Error from server (Invalid): error when creating "phpmyadmin-deployment.yaml": Deployment.apps "phpmyadmin" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"io.kompose.service":"phpmyadmin"}: `selector` does not match template `labels`
Error from server (Invalid): error when creating "webserver-deployment.yaml": Deployment.apps "webserver" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"io.kompose.service":"webserver"}: `selector` does not match template `labels`

database-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert --volumes hostPath
    kompose.version: 1.19.0 (f63a961c)
  creationTimestamp: null
  labels:
    io.kompose.service: database
    app: database
  name: database
spec:
  selector:
    matchLabels:
      app: database
  template:
    metadata:
      labels:
        io.kompose.service: database
  replicas: 1
  strategy: {}
  template:
    metadata:
      annotations:
        kompose.cmd: kompose convert --volumes hostPath
        kompose.version: 1.19.0 (f63a961c)
      creationTimestamp: null
      labels:
        io.kompose.service: database
    spec:
      containers:
      - env:
        - name: MYSQL_DATABASE
          value: Bazadanerro
        - name: MYSQL_PASSWORD
          value: P@$w0rd
        - name: MYSQL_ROOT_PASSWORD
          value: P@$w0rd
        - name: MYSQL_USER
          value: dockerro
        image: mariadb
        name: mysql
        resources: {}
      restartPolicy: Always
status: {}

phpmyadmin-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert --volumes hostPath
    kompose.version: 1.19.0 (f63a961c)
  creationTimestamp: null
  labels:
    io.kompose.service: phpmyadmin
    app: phpmyadmin
  name: phpmyadmin
spec:
  selector:
    matchLabels:
      app: phpmyadmin
  template:
    metadata:
      labels:
        io.kompose.service: database
  replicas: 1
  strategy: {}
  template:
    metadata:
      annotations:
        kompose.cmd: kompose convert --volumes hostPath
        kompose.version: 1.19.0 (f63a961c)
      creationTimestamp: null
      labels:
        io.kompose.service: phpmyadmin
    spec:
      containers:
      - env:
        - name: MYSQL_PASSWORD
          value: P@$w0rd
        - name: MYSQL_ROOT_PASSWORD
          value: P@$w0rd
        - name: MYSQL_USER
          value: dockerro
        - name: PMA_HOST
          value: database
        - name: PMA_PORT
          value: "3306"
        image: phpmyadmin/phpmyadmin
        name: phpmyadmins
        ports:
        - containerPort: 80
        resources: {}
      restartPolicy: Always
status: {}

And webserver-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert --volumes hostPath
    kompose.version: 1.19.0 (f63a961c)
  creationTimestamp: null
  labels:
    io.kompose.service: webserver
    app: webserverro
  name: webserver
spec:
  selector:
    matchLabels: 
      app: webserverro
  template:
    metadata:
      labels:
        io.kompose.service: webserver
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        kompose.cmd: kompose convert --volumes hostPath
        kompose.version: 1.19.0 (f63a961c)
      creationTimestamp: null
      labels:
        io.kompose.service: webserver
    spec:
      containers:
      - image: webserver
        name: webserverro
        ports:
        - containerPort: 80
        resources: {}
        volumeMounts:
        - mountPath: /var/www/html
          name: webserver-hostpath0
      restartPolicy: Always
      volumes:
      - hostPath:
          path: /root/webserverro/root/webserverro
        name: webserver-hostpath0
status: {}

What am I doing wrong?

-- Incybro Shostak
kubernetes
kubernetes-deployment

2 Answers

7/10/2020

The error is self-explanatory: "selector" does not match template "labels".

Edit your YAML files and set the same key-value pairs in both selector.matchLabels and metadata.labels.

spec:
  selector:
    matchLabels: # <---- This
      app: database
  template:
    metadata:
      labels: # <---- This
        io.kompose.service: database 
  • Why selector field is important?

The selector field defines how the Deployment finds which Pods to manage. In this case, you simply 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.

Update:

One possible sample can be:

spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: database
  template:
    metadata:
      labels:
         app.kubernetes.io/name: database

Update2:

no matches for kind "Deployment" in version "extensions/v1beta1"

The apiVersion for Deployment object is now apps/v1.

apiVersion: apps/v1 # <-- update here.
kind: Deployment
... ... ...
-- Kamol Hasan
Source: StackOverflow

7/10/2020

In all of these files you have two copies of the pod spec template:. These don't get merged; the second one just replaces the first one.

<!-- language: lang-yaml -->
spec:
  selector: { ... }
  template:                      # This will get ignored
    metadata:
      labels:
        io.kompose.service: webserver
        app: webserverro
  template:                      # and completely replaced with this
    metadata:
      annotations:
        kompose.cmd: kompose convert --volumes hostPath
        kompose.version: 1.19.0 (f63a961c)
      labels:                    # without the app: label
        io.kompose.service: webserver
    spec: { ... }

Remove the first template: block and move the full set of labels into the one template: block that remains.

-- David Maze
Source: StackOverflow