agalisteo
agalisteo

Reputation: 583

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

Deploying my service to production:

envsubst < ./kubernetes/pre-production/aks.yaml | kubectl apply -f -

I'm getting the following error:

The Deployment "moverick-mule-pre" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"commit":"750a26deebc3582bec4bfbb2426b3f22ee042eaa", "app":"moverick-mule-pre"}: selector does not match template labels

My yaml file is:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: moverick-mule-pre
spec:
  replicas: 2
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      labels:
        app: moverick-mule-pre
        commit: $CI_COMMIT_SHA
    spec:
      containers:
      - name: moverick-mule-pre
        image: $REGISTRY_SERVER_PRE/$CI_PROJECT_NAME:$CI_COMMIT_REF_NAME
        imagePullPolicy: Always
        ports:
        - containerPort: 80
        envFrom:
          - secretRef:
              name: moverick-pre
        livenessProbe:
          httpGet:
            path: /console
            port: 80
          initialDelaySeconds: 5
          periodSeconds: 5
        volumeMounts:
        - name: logs
          mountPath: /opt/mule/logs/
        - name: asc
          mountPath: /opt/mule/asc/
      imagePullSecrets:
      - name: registry-pre
      volumes:
      - name: logs
        azureFile:
          secretName: azure-files-pre
          shareName: logs-pre
          readOnly: false
      - name: asc
        azureFile:
          secretName: azure-asc-pre
          shareName: asc-pre
          readOnly: false
---
apiVersion: v1
kind: Service
metadata:
  name: moverick-mule-pre
spec:
  ports:
  - port: 80
  selector:
    app: moverick-mule-pre

Upvotes: 40

Views: 84050

Answers (5)

Kokorin Pavel
Kokorin Pavel

Reputation: 1

In some cases, app-revision is mismatched between metadata.labels.app-revision and spec.template.metadata.labels.app-revision this also could lead to this kind of error.

Upvotes: 0

goulashsoup
goulashsoup

Reputation: 3076

Just for someone coming across this, in my case trying to deploy (kubectl apply -f) resulted in the described error but there were some pods with status ImagePullBackOff, after removing these kubectl apply worked.

Upvotes: 0

RITESH SANJAY MAHAJAN
RITESH SANJAY MAHAJAN

Reputation: 191

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

Upvotes: 6

Yurifull
Yurifull

Reputation: 391

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...

Upvotes: 3

Shahriar
Shahriar

Reputation: 13806

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

Upvotes: 85

Related Questions