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

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

我正在尝试从 docker-compose 迁移到 kubernetes。我在音量方面遇到了一些问题,所以我所做的是:

kompose convert --volumes hostPath

然后我遇到了另一个问题

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

所以我将 ApiVersion 从 extensions/v1beta1 更改为 app/v1 并添加了“选择器”。现在我无法解决这个问题:


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`

数据库-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: {}

和网络服务器-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: {}

我做错了什么?

错误不言自明:"selector" does not match template "labels"

编辑您的 YAML 文件并在 selector.matchLabelsmetadata.labels 中设置相同的键值对。

spec:
  selector:
    matchLabels: # <---- This
      app: database
  template:
    metadata:
      labels: # <---- This
        io.kompose.service: database 
  • 为什么 selector 字段很重要?

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.

更新:

一个可能的样本可以是:

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

更新2:

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

Deployment 对象的 apiVersion 现在是 apps/v1.

apiVersion: apps/v1 # <-- update here.
kind: Deployment
... ... ...

在所有这些文件中,您都有 pod 规范的两个副本 template:。这些不会合并;第二个只是取代了第一个。

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: { ... }

删除第一个 template: 块并将整套标签移动到剩下的一个 template: 块中。