Kubernetes not loading mongo deployment. "Invalid value ... field is immutable"

6/19/2021

I'm getting the following error when running skaffold dev on my microservices project. I literally taken the code straight out of a tutorial on microservices, but am still getting the error:

The Deployment "orders-mongo-depl" is invalid: spec.selector: Invalid value: v1.LabelSelector{MatchLabels:map[string]string{"app":"orders-mongo"}, MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: field is immutable
kubectl apply: exit status 1

Here is my "orders-mongo-depl.yaml" file

apiVersion: apps/v1
kind: Deployment
metadata:
  name: orders-mongo-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: orders-mongo
  template:
    metadata:
      labels:
        app: orders-mongo
    spec:
      containers:
        - name: orders-mongo
          image: mongo
---
apiVersion: v1
kind: Service
metadata:
  name: orders-mongo-srv
spec:
  selector:
    app: orders-mongo
  ports:
    - name: db
      protocol: TCP
      port: 27017
      targetPort: 27017

Here is my skaffold.yaml file

apiVersion: skaffold/v2alpha3
kind: Config
deploy:
  kubectl:
    manifests:
      - ./infra/k8s/*
build:
  local:
    push: false
  artifacts:
    - image: stephengrider/auth
      context: auth
      docker:
        dockerfile: Dockerfile
      sync:
        manual:
          - src: 'src/**/*.ts'
            dest: .
    - image: stephengrider/client
      context: client
      docker:
        dockerfile: Dockerfile
      sync:
        manual:
          - src: '**/*.js'
            dest: .
    - image: stephengrider/tickets
      context: tickets
      docker:
        dockerfile: Dockerfile
      sync:
        manual:
          - src: 'src/**/*.ts'
            dest: .
    - image: stephengrider/orders
      context: orders
      docker:
        dockerfile: Dockerfile
      sync:
        manual:
          - src: 'src/**/*.ts'
            dest: .

I have tried restarting skaffold, deleting and restarting Minikube, changing the minikube driver between virtualbox and docker, and restarting my computer. I am on the latest version of ubuntu and have up to date minikube, kubernetes and docker.

-- Patrick Gorman
docker
kubernetes
minikube
mongodb
skaffold

1 Answer

6/24/2021

Posting this as an answer out of comments since it resolved the issue.

Short answer

To cleanup all deployments and objects, following command should be issued:

skaffold delete

A bit more details

During development and testing objects are created. When any changes are done within the config or objects itself, error is fired e.g.

The Deployment "orders-mongo-depl" is invalid: spec.selector: Invalid value: v1.LabelSelector{MatchLabels:map[string]string{"app":"orders-mongo"}, MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: field is immutable

Short test in kubernetes showed that changing selector in service or development producing exactly the same error which leads to necessity to correct manifest/objects or reset deployments in skaffold if not clear where discrepancy comes from.

Skaffold cleanup reference

-- moonkotte
Source: StackOverflow