Getting ValidationError(Deployment.spec): missing required field "selector" in io.k8s.api.apps.v1.DeploymentSpec

4/25/2021

I intially referred to similar question here: https://stackoverflow.com/q/59480373

I changed the apiVersion i.e. apiVersion: extensions/v1beta1 to apiVersion: apps/v1, I am using kubeadm and 3 nodes running workers.

However in my yaml I do have matching label selecters

    labels:
      app: rabbitmq
      role: master
      tier: queue

and if I try to run the below yaml deployment : I get

The Deployment "rabbitmq" is invalid:

  • spec.selector: Required value
  • spec.template.metadata.labels: Invalid value: mapstringstring{"app":"rabbitmq", "role":"master", "tier":"queue"}: selector does not match template labels

rabbitmq.yaml

---
# EXPORT SERVICE INTERFACE
kind: Service
apiVersion: v1
metadata:
    name: message-queue
    labels:
      app: rabbitmq
      role: master
      tier: queue
spec:
  ports:
  - port: 5672
    targetPort: 5672
  selector:
      app: rabbitmq
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: rabbitmq-pv-claim
  labels:
    app: rabbitmq
    role: master
    tier: queue
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: rabbitmq
spec:
  replicas: 1
  template:
    metadata:
      labels:
         app: rabbitmq
         role: master
         tier: queue
    spec:
      containers:
      - name: rabbitmq
        image: bitnami/rabbitmq:3.7
        envFrom:
        - configMapRef:
            name: bitnami-rabbitmq-config
        ports:
        - name: queue
          containerPort: 5672
        - name: queue-mgmt
          containerPort: 15672
        livenessProbe:
          exec:
            command:
            - rabbitmqctl
            - status
          initialDelaySeconds: 120
          timeoutSeconds: 5
          failureThreshold: 6
        readinessProbe:
          exec:
            command:
            - rabbitmqctl
            - status
          initialDelaySeconds: 10
          timeoutSeconds: 3
          periodSeconds: 5
        volumeMounts:
        - name: rabbitmq-storage
          mountPath: /bitnami
      volumes:
      - name: rabbitmq-storage
        persistentVolumeClaim:
          claimName: rabbitmq-pv-claim
-- Ciasto piekarz
kubernetes

1 Answer

4/25/2021

You are missing the selector: field under Deployment.spec. Currently you only have replicas: and template: there.

Example from Deployment docs:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    ...

Also see the format with:

kubectl explain Deployment.spec
-- Jonas
Source: StackOverflow