The Deployment "nodejs-deployment" is invalid spec.template.metadata.labels: Invalid value

3/31/2021

I'm new in Kubernetes and I was tring to deploy a nodejs service to kubernetes. For that I created a docker image and upload it to dockerhub and finally I created a deployment file that contains all required configurations in order to accomplish the deployment. The deployment file is shown above. I then executed the command 'kubectl apply -f deployment_local.yaml' and I came across with this error: "*spec.template.metadata.labels:Invalid value mapstringstring{"app":"nodejs\u00a0\u00a0"}:selector does not match template labels"

I'm tring to fix this bug but I could not fix it. Pls help understand this error because I'm strugglying for a lot of time.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nodejs-deployment
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nodejs
  template:
    metadata:
      labels:
        app: nodejs  
    spec:
      containers:
      - name: nodeapp
        image: lucasseabra/nodejs-starter
---
apiVersion: v1
kind: Service
metadata:
  name: nodejs-entrypoint
  namespace: default
spec:
  type: NodePort
  selector:
    app: nodejs
  ports:
  - port: 3000
    targetPort: 3000
    nodePort: 30001
-- Lucas Seabra
deployment
kubernetes
node.js

1 Answer

3/31/2021

As the error message was trying to tell you, there are two "non-breaking space" characters after nodejs: map[string]string{"app":"nodejs\u00a0\u00a0"}

I would guess it was a side-effect of copy-pasting from a webpage

If you even do a "select all" on your posted question here, you'll see that SO has converted the two characters into normal spaces, but they do show up in the selection extension past the "nodejs" text

If your editor is not able to show you the characters, then either manually retype the labels, or try copying this (which is just yours but with trailing spaces removed)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nodejs-deployment
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nodejs
  template:
    metadata:
      labels:
        app: nodejs
    spec:
      containers:
      - name: nodeapp
        image: lucasseabra/nodejs-starter
---
apiVersion: v1
kind: Service
metadata:
  name: nodejs-entrypoint
  namespace: default
spec:
  type: NodePort
  selector:
    app: nodejs
  ports:
  - port: 3000
    targetPort: 3000
    nodePort: 30001
-- mdaniel
Source: StackOverflow