0

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
  • 521
  • 1
  • 6
  • 17

1 Answers1

2

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
  • 3,579
  • 21
  • 25
  • Thanks! I was unaware of that requirement, however even after adding the label field, the same error is outputted – appdap1 Sep 12 '17 at 16:02
  • Did you also change to the `.spec.selector.matchLabels` field? This is different from your selector field. (For me on k8s `1.7.3` the .yaml file in the answer works) – fishi0x01 Sep 12 '17 at 16:03
  • Thanks! It works now, I didn't initially add the matchLabels field. You were right about me wanting to use the selectors for the actual deployment/hub rather than the pods it creates. Is the matchLabel something we do for all Deployment objects instead of selectors or is it being used here only in this specific case? Also what would I do when I am trying to add selectors to each pod created by the deployment? Thanks for the help again! – appdap1 Sep 12 '17 at 16:14
  • A `.spec.selector` is optional. You do not necessarily need it for a Deployment. In fact, it can also be dangerous if by accident you use the same selector in different Deployments (stated in more detail [here](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#selector)). As of writing, a `.spec.selector` contains `matchExpressions` and/or `matchLabels`. This is documented [here](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/) in more detail. – fishi0x01 Sep 12 '17 at 16:25
  • Hi, i got same trouble,, use your `code` and the result is `Waiting, endpoint for service is not ready yet...` And got endless looping.. – Budi Mulyo Feb 11 '19 at 07:08
  • Which version you use? The answer was written for deployments `apiVersion: apps/v1beta1`. – fishi0x01 Feb 11 '19 at 09:29