How to resolve 'error validating data: ValidationError(Deployment.spec): unknown field volumeClaimTemplates in io.k8s.api.apps.v1.DeploymentSpec' error?
Problem
When installing an application in Kubernetes (k8s), you might encounter the following error:
root@launch-advisor:~# kubectl apply -f deployment-redis.yaml
error: error validating "deployment-redis.yaml": error validating data: ValidationError(Deployment.spec): unknown field "volumeClaimTemplates" in io.k8s.api.apps.v1.DeploymentSpec; if you choose to ignore these errors, turn validation off with --validate=false
The content of deployment-redis.yaml
is:
apiVersion: apps/v1kind: Deploymentmetadata: name: redis namespace: ns-bswen labels: app: redisspec: selector: matchLabels: app: redis replicas: 1 template: metadata: labels: app: redis spec: containers: - name: redis image: redis:6.0.9 ports: - containerPort: 6379 command: ["redis-server", "/etc/redis/redis.conf"] volumeMounts: - name: "redis-conf" mountPath: /etc/redis/redis.conf subPath: redis_conf - name: "redis-data" mountPath: "/var/lib/redis" volumes: - name: "redis-conf" configMap: name: "redis" - name: "redis-data" persistentVolumeClaim: claimName: redis-pvc volumeClaimTemplates: - metadata: name: redis-pvc annotations: volume.beta.kubernetes.io/storage-class: "my-nfs-storage" spec: accessModes: ["ReadWriteOnce"] resources: requests: storage: 100Mi
Environment
- Docker: Server Version: 19.03.13
- Kubectl version:
Client Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.3", GitCommit:"06ad960bfd03b39c8310aaf92d1e7c12ce618213", GitTreeState:"clean", BuildDate:"2020-02-13T18:06:54Z", GoVersion:"go1.13.8", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.6", GitCommit:"dff82dc0de47299ab66c83c626e08b245ab19037", GitTreeState:"clean", BuildDate:"2020-07-15T16:51:04Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
Reason
The error occurs because volumeClaimTemplates can ONLY be used in StatefulSets, not Deployments.
Solution
To resolve this issue, switch from Deployment to StatefulSet as follows:
apiVersion: apps/v1kind: StatefulSetmetadata: name: redis namespace: ns-bswen labels: app: redisspec: serviceName: redis selector: matchLabels: app: redis replicas: 1 template: metadata: labels: app: redis spec: containers: - name: redis image: redis:6.0.9 ports: - containerPort: 6379 command: ["redis-server", "/etc/redis/redis.conf"] volumeMounts: - name: "redis-conf" mountPath: /etc/redis/redis.conf subPath: redis_conf - name: "redis-data" mountPath: "/var/lib/redis" volumes: - name: "redis-conf" configMap: name: "redis" - name: "redis-data" persistentVolumeClaim: claimName: redis-pvc volumeClaimTemplates: - metadata: name: redis-pvc annotations: volume.beta.kubernetes.io/storage-class: "my-nfs-storage" spec: accessModes: ["ReadWriteOnce"] resources: requests: storage: 100Mi
Summary
This post explained how to resolve the “unknown field volumeClaimTemplates” error in Kubernetes. The key takeaway is that volumeClaimTemplates is only supported in StatefulSets, not Deployments. By switching to a StatefulSet, you can ensure each pod replica gets its own unique storage, which is essential for applications like databases or distributed systems. Always choose the appropriate Kubernetes resource based on your application’s storage requirements.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!