`selector` does not match template `labels` after adding selector - Azure DevOPs

7/31/2020

I am trying to deploy this Kubernetes YAML through Azure DevOps; however, whenever I try to deploy i.e. (kubectl apply -f vote.yaml) it throws the error:

2020-07-31T01:20:54.1660561Z ##[section]Starting: Deploy to Kubernetes cluster 2020-07-31T01:20:54.1670716Z
============================================================================== 2020-07-31T01:20:54.1671079Z Task         : Deploy to Kubernetes 2020-07-31T01:20:54.1671502Z Description  : Use Kubernetes manifest files to deploy to clusters or even bake the manifest files to be used for deployments using Helm charts 2020-07-31T01:20:54.1671888Z Version : 0.169.4 2020-07-31T01:20:54.1672199Z Author       : Microsoft Corporation 2020-07-31T01:20:54.1672502Z Help         : https://aka.ms/azpipes-k8s-manifest-tsg 2020-07-31T01:20:54.1672846Z
============================================================================== 2020-07-31T01:20:54.8558266Z
============================================================================== 2020-07-31T01:20:54.8569661Z 			Kubectl Client Version: v1.18.6 2020-07-31T01:20:54.8569971Z 			Kubectl Server Version: v1.16.10 2020-07-31T01:20:54.8570282Z
============================================================================== 2020-07-31T01:20:54.8752992Z [command]/usr/bin/kubectl apply -f /home/vsts/work/_temp/Deployment_azure-vote-back_1596158454873,/home/vsts/work/_temp/Service_azure-vote-back_1596158454873,/home/vsts/work/_temp/Deployment_azure-vote-front_1596158454873,/home/vsts/work/_temp/Service_azure-vote-front_1596158454874
--namespace default 2020-07-31T01:20:56.7312155Z service/azure-vote-back unchanged 2020-07-31T01:20:56.7333257Z service/azure-vote-front unchanged 2020-07-31T01:20:56.7368496Z
##[error]Error from server (Invalid): error when creating "/home/vsts/work/_temp/Deployment_azure-vote-back_1596158454873":
**Deployment.apps "azure-vote-back" is invalid:**
**spec.template.metadata.labels: Invalid value:**
**map[string]string{"app":"azure-vote-back"}: `selector` does not match**
**template `labels` Error from server (Invalid): error when creating** "/home/vsts/work/_temp/Deployment_azure-vote-front_1596158454873": Deployment.apps "azure-vote-front" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"app":"azure-vote-front"}: `selector` does not match template `labels` 2020-07-31T01:20:56.7381535Z Error from server (Invalid): error when creating "/home/vsts/work/_temp/Deployment_azure-vote-back_1596158454873": Deployment.apps "azure-vote-back" is invalid:
**spec.template.metadata.labels: Invalid value: map[string]string{"app":"azure-vote-back"}: `selector` does not match template `labels**` 2020-07-31T01:20:56.7383295Z Error from server (Invalid): error when creating "/home/vsts/work/_temp/Deployment_azure-vote-front_1596158454873":
**Deployment.apps "azure-vote-front" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"app":"azure-vote-front"}: `selector` does not match template `labels`** 2020-07-31T01:20:56.7396706Z ##[section]Finishing: Deploy to Kubernetes cluster

I have already tried to add the selector, matchLabels under the specs section but it seems like that did not work. Below is my YAML file.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: azure-vote-back
spec:
  replicas: 1
  selector:
    matchLabels:
      name: azure-vote-back
  template:
    metadata:
      labels:
        app: azure-vote-back
    spec:
      nodeSelector:
        "beta.kubernetes.io/os": linux
      containers:
      - name: azure-vote-back
        image: redis
        ports:
        - containerPort: 6379
          name: redis
---
apiVersion: v1
kind: Service
metadata:
  name: azure-vote-back
spec:
  ports:
  - port: 6379
  selector:
    app: azure-vote-back
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: azure-vote-front
spec:
  replicas: 1
  selector:
    matchLabels:
      name: azure-vote-front
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  minReadySeconds: 5 
  template:
    metadata:
      labels:
        app: azure-vote-front
    spec:
      nodeSelector:
        "beta.kubernetes.io/os": linux
      containers:
      - name: azure-vote-front
        image: dzwebappdocker001acr.azurecr.io/azure-vote-front
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: 250m
          limits:
            cpu: 500m
        env:
        - name: REDIS
          value: "azure-vote-back"
---
apiVersion: v1
kind: Service
metadata:
  name: azure-vote-front
spec:
  type: LoadBalancer
  ports:
  - port: 80
  selector:
    app: azure-vote-front
-- Okunsanmi Adedolapo
azure-devops
kubernetes
yaml

1 Answer

7/31/2020

Resolved: I was using "name" instead of "app" in the selector

-- Okunsanmi Adedolapo
Source: StackOverflow