Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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: {}
like image 685
aknuds1 Avatar asked May 20 '16 16:05

aknuds1


2 Answers

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: {}
like image 185
aknuds1 Avatar answered Sep 29 '22 20:09

aknuds1


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.

like image 43
CJ Cullen Avatar answered Sep 29 '22 18:09

CJ Cullen