Error on StatefulSet creation for mongodb on Kubernetes

8/16/2021

While creating stateful set for mongodb on kubernetes, I am getting below error.

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

statefulset.yaml

---
  apiVersion: "apps/v1"
  kind: "StatefulSet"
  metadata:
     name: "mongo-development"
     namespace: "development"
  spec:
    selector:
      matchLabels:
        app: "mongo-development"
    serviceName: "mongo-development"
    replicas: 1
    template:
      metadata:
        labels:
           app: "mongo-development"
      spec:
        containers:
          -
            name: "mongo-development"
            image: "mongo"
            imagePullPolicy: "Always"
            env:
             -
                name: "MONGO_INITDB_ROOT_USERNAME"
                value: "xxxx"

             -
                name: "MONGO_INITDB_ROOT_PASSWORD"
                value: "xxxx"

            ports:
              -
                containerPort: 27017
                name: "mongodb"

            volumeMounts: 
              - 
                name: "mongodb-persistent-storage"
                mountPath: "/var/lib/mongodb"
      volumes: 
          - 
            name: "mongodb-persistent-storage"
            persistentVolumeClaim: 
              claimName: "mongodb-pvc-development"

pvc.yaml

---
  apiVersion: "v1"
  kind: "PersistentVolumeClaim"
  metadata:
    name: "mongodb-pvc-development"
    namespace: "development"
    labels:
      app: "mongo-development"
  spec:
    accessModes:
      - ReadWriteOnce
    resources:
      requests:
        storage: 5Gi
    storageClassName: gp2

service.yaml

---
  apiVersion: "v1"
  kind: "Service"
  metadata:
    name: "mongo-development"
    namespace: "development"
    labels:
      app: "mongo-development"
  spec:
    ports:
      - 
        name: "mongodb"
        port: 27017
        targetPort: 27017
    clusterIP: "None"
    selector:
      app: "mongo-development"

Can someone please help what I am doing wrong here.

-- SVD
kubernetes
kubernetes-statefulset
mongodb

2 Answers

8/16/2021

You probably applied the statefulset.yaml, changed something like a label afterwards and tried to reapply the statefulset.yaml. As the error says you can only change certain fields after creating a statefulset.

Just delete the statefulset and create it again:

kubectl delete -f statefulset.yaml
kubectl apply -f statefulset.yaml
-- Chris
Source: StackOverflow

8/16/2021

It's clear restriction from Kubernetes side for stateful sets.

You delete the stateful set and apply the config again to resolve the issue.

You can also read more at : https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/#updating-statefulsets

As mentioned in error : in stateful you can change only replicas, template, and updateStrategy. Other fileds in spec won't be possible to update.

-- Harsh Manvar
Source: StackOverflow