kubernetes K8-无法在K8升级之前升级statefulset API

xzabzqsa  于 2023-10-17  发布在  Kubernetes
关注(0)|答案(1)|浏览(102)

我正在将K8s从1.15升级到1.16。在此之前,我必须将statefulset yaml API迁移到apps/v1版本。但是K8S不允许我这么做。
yamls的前一个版本在这里(变量存储在另一个文件中):

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: {{ .NAME_KAFKA }}
  namespace: {{ .NS }}
spec:
  serviceName: {{ .NAME_KAFKA  }}-service
  replicas: {{ .CLUSTER_SIZE_KAFKA }}
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: {{ .NAME_KAFKA }}
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "9009"
    spec:
      priorityClassName: {{ .PRIORITY_HIGHEST }}
      nodeSelector:
        lifecycle: OnDemand
      terminationGracePeriodSeconds: 301
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: "app"
                    operator: In
                    values:
                    - {{ .NAME_KAFKA }}
              topologyKey: "kubernetes.io/hostname"
      containers:
      - name: local-{{ .NAME_KAFKA }}
        imagePullPolicy: Always
        image: {{ .REPO }}/{{ .IMAGE_KAFKA }}:{{ .VERSION_KAFKA }}
        resources:
          requests:
            memory: 768Mi
            cpu: 500m
          limits:
            memory: 768Mi
            cpu: 500m
        ports:
        - containerPort: 9092
          name: server
        - containerPort: 9009
          name: prometheus
        volumeMounts:
        - name: datadir
          mountPath: /var/lib/kafka
        env:
        - name: KAFKA_HEAP_OPTS
          value : "-Xmx512M -Xms512M"
        - name: KAFKA_OPTS
          value: "-Dlogging.level=INFO"
        readinessProbe:
          tcpSocket:
            port: 9092
          initialDelaySeconds: 60
          timeoutSeconds: 5
          periodSeconds: 5
          successThreshold: 1
          failureThreshold: 5
      securityContext:
        runAsUser: 1000
        fsGroup: 1000
  volumeClaimTemplates:
  - metadata:
      name: datadir
    spec:
      accessModes:
        - ReadWriteOnce
      storageClassName: gp2
      resources:
        requests:
          storage: 20Gi

我在yaml文件中将api-version从apps/v1 beta1更改为apps/v1并尝试应用。可以预见的是,我收到了这个错误。

error: error validating "STDIN": error validating data: ValidationError(StatefulSet.spec): missing required field "selector" in io.k8s.api.apps.v1.StatefulSetSpec; if you choose to ignore these errors, turn validation off with --validate=false

所以我添加字段StatefulSet.spec.selector

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ .NAME_KAFKA }}
  namespace: {{ .NS }}
spec:
  selector:
    matchExpressions:
      - key: "app"
        operator: In
        values:
          - {{ .NAME_KAFKA }}
  serviceName: {{ .NAME_KAFKA  }}-service
  replicas: {{ .CLUSTER_SIZE_KAFKA }}
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: {{ .NAME_KAFKA }}
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "9009"
    spec:
      priorityClassName: {{ .PRIORITY_HIGHEST }}
      nodeSelector:
        lifecycle: OnDemand
      terminationGracePeriodSeconds: 301
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: "app"
                    operator: In
                    values:
                    - {{ .NAME_KAFKA }}
              topologyKey: "kubernetes.io/hostname"
      containers:
      - name: local-{{ .NAME_KAFKA }}
        imagePullPolicy: Always
        image: {{ .REPO }}/{{ .IMAGE_KAFKA }}:{{ .VERSION_KAFKA }}
        resources:
          requests:
            memory: 768Mi
            cpu: 500m
          limits:
            memory: 768Mi
            cpu: 500m
        ports:
        - containerPort: 9092
          name: server
        - containerPort: 9009
          name: prometheus
        volumeMounts:
        - name: datadir
          mountPath: /var/lib/kafka
        env:
        - name: KAFKA_HEAP_OPTS
          value : "-Xmx512M -Xms512M"
        - name: KAFKA_OPTS
          value: "-Dlogging.level=INFO"
        readinessProbe:
          tcpSocket:
            port: 9092
          initialDelaySeconds: 60
          timeoutSeconds: 5
          periodSeconds: 5
          successThreshold: 1
          failureThreshold: 5
      securityContext:
        runAsUser: 1000
        fsGroup: 1000
  volumeClaimTemplates:
  - metadata:
      name: datadir
    spec:
      accessModes:
        - ReadWriteOnce
      storageClassName: gp2
      resources:
        requests:
          storage: 20Gi

但是当我尝试应用它时,我收到这个错误:

The StatefulSet "kafka-name" is invalid: spec: Forbidden: updates to statefulset spec for fields other than 'replicas', 'template', and 'updateStrategy' are forbidden

根据K8的文档,应该有一种方法来更新这个yaml中的API,而不需要在K8中重建所有有状态集。但我怎么能做到呢?

58wvjzkj

58wvjzkj1#

升级到1.16后,您可以转换资源。
正如在博客中所写的,你可以使用kubectl转换。迁移建议转换。参见kubernetes.io/blog/2019/07/18/api-deprecations-in-1-16

kubectl convert -f ./my-statefulset.yaml --output-version apps/v1

另一个选项是删除带有cascade=false的statefulset。
它会删除statefulset并保持pod正常运行。现在您应该能够再次应用新的statefulset文件。

kubectl delete statefulset/<your-statefulset> --cascade=false

相关问题