K8s部署错误:“选择器”与模板“标签”不匹配

时间:2020-07-10 10:12:15

标签: kubernetes kubernetes-deployment

我正在尝试从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`

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

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

我在做什么错了?

2 个答案:

答案 0 :(得分:2)

错误是不言自明的:"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字段很重要?

selector字段定义部署如何查找要管理的Pod。在这种情况下,您只需选择在Pod模板(应用程序:nginx)中定义的标签。但是,只要Pod模板本身满足该规则,就可以使用更复杂的选择规则。

更新

一个可能的示例可以是:

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

Update2:

没有匹配版本“ extensions / v1beta1”中的“部署”种类

apiVersion对象的Deployment现在为 apps / v1

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

答案 1 :(得分:1)

在所有这些文件中,您都有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:块中。