Kubernetes - Why does selector field fail to validate for Deployment?

5/20/2016

Using Kubernetes 1.2.4, why does my below Deployment definition (redis.yaml) cause the following error?

$ kubectl apply -f redis.yaml
error validating "redis.yaml": error validating data: found invalid field name for v1beta1.LabelSelector; if you choose to ignore these errors, turn validation off with --validate=false

redis.yaml:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: redis
spec:
  replicas: 3
  selector:
    name: redis
  template:
    metadata:
      labels:
        name: redis
    spec:
      containers:
      - name: redis
        image: kubernetes/redis:v1
        ports:
        - containerPort: 6379
        resources:
          limits:
            cpu: "0.1"
        volumeMounts:
        - mountPath: /redis-master-data
          name: data
      volumes:
        - name: data
          emptyDir: {}
-- aknuds1
kubernetes

3 Answers

5/20/2016

Selector directives in Deployments require you to use a sub-field of either matchLabels or matchExpressions, so in my case I need to make use of matchLabels:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: redis
spec:
  replicas: 3
  selector:
    matchLabels:
      name: redis
  template:
    metadata:
      labels:
        name: redis
    spec:
      containers:
      - name: redis
        image: kubernetes/redis:v1
        ports:
        - containerPort: 6379
        resources:
          limits:
            cpu: "0.1"
        volumeMounts:
        - mountPath: /redis-master-data
          name: data
      volumes:
        - name: data
          emptyDir: {}
-- aknuds1
Source: StackOverflow

5/20/2016

The selector field of a v1beta1.DeploymentSpec object is of type v1beta1.LabelSelector rather than just a plain map. So, you can either add the label under the matchLabels field of the selector:

redis-with-matchLabels.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: redis
spec:
  replicas: 3
  selector:
    matchLabels:
      name: redis
  template:
    metadata:
      labels:
        name: redis
    spec:
      containers:
      - name: redis
        image: kubernetes/redis:v1
        ports:
        - containerPort: 6379
        resources:
          limits:
            cpu: "0.1"
        volumeMounts:
        - mountPath: /redis-master-data
          name: data
      volumes:
        - name: data
          emptyDir: {}

Or leave the LabelSelector out of the DeploymentSpec, in which case it will match the labels from the PodSpec:

redis-podSpec-labels.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: redis
  template:
    metadata:
      labels:
        name: redis
    spec:
      containers:
      - name: redis
        image: kubernetes/redis:v1
        ports:
        - containerPort: 6379
        resources:
          limits:
            cpu: "0.1"
        volumeMounts:
        - mountPath: /redis-master-data
          name: data
      volumes:
        - name: data
          emptyDir: {}

See the Selector section of the Deployment docs.

-- CJ Cullen
Source: StackOverflow

3/26/2019

This can also happen if one accidentally changes the apiVersion to anything other than "v1beta1".

The schema is critical and will be checked. However, if the content is correct and the schema wrong, it might still work.

-- Joerg Krause
Source: StackOverflow