How to update a storageClassName in a statefulset?

6/14/2019

I am new to k8's and trying to update storageClassName in a StatefulSet.(from default to default-t1 only change in the yaml)

I tried running kubectl apply -f test.yaml

The only difference between 1st and 2nd Yaml(one using to apply update) is storageClassName: default-t1 instead of default

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  podManagementPolicy: "Parallel"
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: k8s.gcr.io/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: default
      resources:
        requests:
          storage: 1Gi

Every-time I try to update it I get The StatefulSet "web" is invalid: spec: Forbidden: updates to statefulset spec for fields other than 'replicas', 'template', and 'updateStrategy' are forbidden

What am I missing or what steps should I take to do this?

-- e.f.a.
kubernetes
python

2 Answers

6/17/2019
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  podManagementPolicy: "Parallel"
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: k8s.gcr.io/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: <storage class name>
      resources:
        requests:
          storage: 1Gi

in the above file add storage class and you can apply it like.

kubectl apply -f .yaml

-- Harsh Manvar
Source: StackOverflow

6/17/2019

First line of StatefulSet shoud define an "apiVersion", you can see example here statefulset. Please add it in first line:

apiVersion: apps/v1

Could you show me output of your 'www' PVC file?

kubectl get pvc www -o yaml

In PVC you have field "storageClassName", which should be set on your StorageClass you want to use, so in your case it would be:

storageClassName: default-t1
-- muscat
Source: StackOverflow