Kubernetes reported an error: matchexpressions: [] v1. Labelselector requirement (NIL)}: field is immutable

error message

MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: field is immutable

Reason

reason: the essential reason for this problem is that two identical Deployment (one deployed and one to deploy) have different selectors.

scene duplicate

case:
app.yaml

apiVersion: apps/v1                                                                  kind: Deployment
metadata:
  name: my-app
  labels:
    app: my-app
spec:
  replicas: 10
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
        version: v1.0.0
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "9101"
    spec:
      containers:
      - name: my-app
        image: containersol/k8s-deployment-strategies
        ports:
        - name: http
          containerPort: 8080
        - name: probe
          containerPort: 8086
        env:
        - name: VERSION
          value: v1.0.0
        livenessProbe:
          httpGet:
            path: /live
            port: probe
          initialDelaySeconds: 5
          periodSeconds: 5
        readinessProbe:
          httpGet:
            path: /ready
            port: probe
          periodSeconds: 5

after deployment, take a look at the results

$kubectl get deployment
NAME     READY   UP-TO-DATE   AVAILABLE   AGE
my-app   10/10   10           10          84s

Next, we modify the selector of deployment, which mainly reads </p b>

spec:
  replicas: 10
  selector:
    matchLabels:
      app: my-app-change
  template:
    metadata:
      labels:
        app: my-app-change
        version: v1.0.0

the completion file is as follows :

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  labels:
    app: my-app
spec:
  replicas: 10
  selector:
    matchLabels:
      app: my-app-change
  template:
    metadata:
      labels:
        app: my-app-change
        version: v1.0.0
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "9101"
    spec:
      containers:
      - name: my-app
        image: containersol/k8s-deployment-strategies
        ports:
        - name: http
          containerPort: 8080
        - name: probe
          containerPort: 8086
        env:
        - name: VERSION
          value: v1.0.0
        livenessProbe:
          httpGet:
            path: /live
            port: probe
          initialDelaySeconds: 5
          periodSeconds: 5
        readinessProbe:
          httpGet:
            path: /ready
            port: probe
          periodSeconds: 5

When kubectl is deployed, the following error occurs:

$kubectl apply -f app.yaml
The Deployment "my-app" is invalid: spec.selector: Invalid value: v1.LabelSelector{MatchLabels:map[string]string{"app":"my-app-change"}, MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: field is immutable

and you can see the selector for deployed:

$kubectl describe deployment my-app
Name:                   my-app
...
Selector:               app=my-app
...

you can see that the deployment name my-app already has a selector and the content is app=my-app. At this time, the reason for the error is that the name of the newly deployed deployment is also MY-app, but the content of the selector is APP = My-app-change.

solution:

1: you can delete the original deployment and then deploy
2: modify the name of deployment instead of repeating

Read More: