Deployment invalid: spec.template.metadata.labels: Invalid value

apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend-security-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: backend-security
  template:
    metadata:
      labels: # labels to select/identify the deployment
        app: backend-security
    spec:     # pod spec                  
      containers: 
      - name: backend-security
        image: yurifull/backend-security:v1.0.0 # image we pushed
        ports:
        - containerPort: 3001

this works for me...


This might happen during one additional case wherein you have made changes to deployment labels and you're deploying through argocd. In this case Argocd sync would fail

The solution is to delete the deployment and recreate it. Since labels are immutable in Kubernetes

you can refer this: https://github.com/kubernetes/kubernetes/issues/50808


You need to add selector in spec of Deployment.

And also, these selector should match with labels in PodTemplate.

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: moverick-mule-pre
spec:
  replicas: 2
  selector:
    matchLabels:
      app: moverick-mule-pre
      commit: $CI_COMMIT_SHA
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      labels:
        app: moverick-mule-pre
        commit: $CI_COMMIT_SHA

Otherwise, you will get error like below

The Deployment "moverick-mule-pre" is invalid:

  • spec.selector: Required value
  • spec.template.metadata.labels: Invalid value: map[string]string{...} selector does not match template labels