<p>While creating stateful set for mongodb on kubernetes, I am getting below error.</p>
<p>&quot;is invalid: spec: Forbidden: updates to statefulset spec for fields other than 'replicas', 'template', and 'updateStrategy' are forbidden&quot;</p>
<p>statefulset.yaml</p>
<pre><code>---
  apiVersion: &quot;apps/v1&quot;
  kind: &quot;StatefulSet&quot;
  metadata:
     name: &quot;mongo-development&quot;
     namespace: &quot;development&quot;
  spec:
    selector:
      matchLabels:
        app: &quot;mongo-development&quot;
    serviceName: &quot;mongo-development&quot;
    replicas: 1
    template:
      metadata:
        labels:
           app: &quot;mongo-development&quot;
      spec:
        containers:
          -
            name: &quot;mongo-development&quot;
            image: &quot;mongo&quot;
            imagePullPolicy: &quot;Always&quot;
            env:
             -
                name: &quot;MONGO_INITDB_ROOT_USERNAME&quot;
                value: &quot;xxxx&quot;

             -
                name: &quot;MONGO_INITDB_ROOT_PASSWORD&quot;
                value: &quot;xxxx&quot;

            ports:
              -
                containerPort: 27017
                name: &quot;mongodb&quot;

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

</code></pre>
<p>pvc.yaml</p>
<pre><code>---
  apiVersion: &quot;v1&quot;
  kind: &quot;PersistentVolumeClaim&quot;
  metadata:
    name: &quot;mongodb-pvc-development&quot;
    namespace: &quot;development&quot;
    labels:
      app: &quot;mongo-development&quot;
  spec:
    accessModes:
      - ReadWriteOnce
    resources:
      requests:
        storage: 5Gi
    storageClassName: gp2
</code></pre>
<p>service.yaml</p>
<pre><code>---
  apiVersion: &quot;v1&quot;
  kind: &quot;Service&quot;
  metadata:
    name: &quot;mongo-development&quot;
    namespace: &quot;development&quot;
    labels:
      app: &quot;mongo-development&quot;
  spec:
    ports:
      - 
        name: &quot;mongodb&quot;
        port: 27017
        targetPort: 27017
    clusterIP: &quot;None&quot;
    selector:
      app: &quot;mongo-development&quot;
</code></pre>
<p>Can someone please help what I am doing wrong here.</p>

<p>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.</p>
<p>Just delete the statefulset and create it again:</p>
<pre><code>kubectl delete -f statefulset.yaml
kubectl apply -f statefulset.yaml
</code></pre>
