欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-29 22:33:51
...

报错信息

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

原因

原因:这个问题的本质原因是,两个相同的Deployment(一个已部署,一个要部署),但它们选择器不同。

场景复现

例:
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

部署完成之后,查看一下结果

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

接下来,我们修改deployment的选择器,主要内容如下

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

完成文件如下:

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

部署的时候,会出现如下报错:

$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

可以看已经部署的deployment的selector:

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

可以看到deployment名字为my-app的已经有了选择器,内容为app=my-app。而此时,报错的原因是因为新部署的deployment名字也为my-app,但是选择器的内容是app=my-app-change。

解决方式:

1: 可以将原先的deployment删除后再部署
2: 修改deployment的名字,不要重名

相关标签: 云原生