Kubernetes Deployment file error: Found invalid field selector for v1.PodSpec

9/12/2017

I'm getting an invalid field selector error when I try and create my deployment using a YAML file. The error is error validating data: found invalid field selector for v1.PodSpec and my file can be seen below.

apiVersion: apps/v1beta1
kind: Deployment
metadata:
 name: zalenium-deployment
spec:
 replicas: 1
 template:
  metadata:
   labels:
    app: zalenium
  spec:
   serviceAccountName: zalenium
   serviceAccount: zalenium
   selector:
    app: zalenium
    role: grid
   containers:
    - name: zalenium-pod
      image: dosel/zalenium
      ports:
      - containerPort: 4444
        protocol: TCP
      volumeMounts:
      - name: zalenium-shared
        mountPath: /tmp/mounted
      - name: zalenium-videos
        mountPath: /home/seluser/videos
      resources:
       requests:
        memory: "250m"
        cpu: "500m"
       limits:
        memory: "1Gi"
   volumes:
   - name: zalenium-shared
     persistentVolumeClaim:
      claimName: zalenium-shared-claim
   - name: zalenium-videos
     persistentVolumeClaim:
      claimName: zalenium-videos-claim

I have tried using online YAML File Validator and they don't seem to show anything wrong with the format. When I try and create the Deployment above with the validate=false flag, the deployment runs, but then the pods continuously crash and restart (crashLoopBackOff). What should I be looking into? I'm still getting familiar with k8s but from the error I would assume it had something to do with the container specs in my deployment. Any tips on approaching this? Thanks!

-- appdap1
docker
kubectl
kubernetes
selenium-grid
yaml

1 Answer

9/12/2017

As the error message states selector is an invalid field for v1.PodSpec - so this field is not valid at .spec.template.spec.selector. I think what you are looking for is a .spec.selector.

That being said, the doc states:

If specified, .spec.selector must match .spec.template.metadata.labels, or it will be rejected by the API.

So you must add role: grid also to your metadata labels (at .spec.template.metadata.labels). Your .yaml file would look sth like that then:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
 name: zalenium-deployment
spec:
 selector:
  matchLabels:
   app: zalenium
   role: grid
 replicas: 1
 template:
  metadata:
   labels:
    app: zalenium
    role: grid
  spec:
   serviceAccountName: zalenium
   serviceAccount: zalenium
   containers:
    - name: zalenium-pod
      image: dosel/zalenium
      ports:
      - containerPort: 4444
        protocol: TCP
      volumeMounts:
      - name: zalenium-shared
        mountPath: /tmp/mounted
      - name: zalenium-videos
        mountPath: /home/seluser/videos
      resources:
       requests:
        memory: "250m"
        cpu: "500m"
       limits:
        memory: "1Gi"
   volumes:
   - name: zalenium-shared
     persistentVolumeClaim:
      claimName: zalenium-shared-claim
   - name: zalenium-videos
     persistentVolumeClaim:
      claimName: zalenium-videos-claim
-- fishi0x01
Source: StackOverflow