Kubernetes Error Codes: Must Specify a Volume Type

Kubernetes Error Codes: Must Specify a Volume Type

Most Kubernetes workloads are defined as YAML manifests for describing the relationship between various Kubernetes variables. Though the Kubernetes community considers YAML as a human-readable format, it can be hard to understand at times.

A small change in a YAML manifest file can easily return an array of error codes that can become challenging to triage and debug. To understand the root cause of the Kubernetes misconfiguration, it is important to have a clear understanding of Kubernetes error codes, as they contain valuable information on how to resolve the issue.

In this article, you’ll learn more about the common culprits when you see the `must specify a volume type` error code. We’ll discuss the possible causes for the error message as well as some solutions to troubleshoot and resolve it.

What Does `must specify a volume type ` Mean?

Persistent Volumes (PV) provide permanent storage to the pod in a cluster, and their incorrect configuration is one common reason you might see this error code.

Specifically, incorrectly defining a PV without the correct storage class will return the following:


The Persistent Volume "Persistent_Volume_name" is invalid: spec: Required value: must specify a volume type

This error code highlights two key points:

  1. The volume you are trying to create for a pod has missing or invalid parameters.
  2. The name of the PV (that is incorrectly configured) has been specified on your pod.

To best illustrate this, we’ll go through an example below. To proceed, you’ll need a Kubernetes cluster with the kubectl CLI. You’ll also need a basic understanding of PVs and storage classes—let’s talk a little bit more about those first.

First, pods can’t directly access the `PersistentVolume`—instead, they have to request access via `PersistentVolumeClaims` (PVCs), which include details about the size and type of storage the application/pod needs.

In Kubernetes storage, `StorageClass` plays a vital role in the creation of new persistent volumes as per the workload requirements. For example, if you require fast Solid State Drives (SSDs) storage for one workload while other workloads can be satisfied with a hard disk drive, then that can be specified with the `StorageClass` manifest.

Kubernetes provides a long list of storage classes that can be used with your on-premise and cloud-based workloads. Here is the [full list](https://kubernetes.io/docs/concepts/storage/storage-classes/).

Now, to reproduce the error code, you’ll create a PV that will use the Google Cloud Engine (GCE) SSD as a storage class.

Here is a sample manifest for that storage class:


##storageclass.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: ssd
provisioner: kubernetes.io/gce-pd
parameters:
  type: pd-ssd
reclaimPolicy: Retain

Apply this manifest using the `kubectl apply -f storageclass.yaml` command. The response should be as follows:


storageclass.storage.k8s.io/ssd created

Next, let’s try to provision a PV using the GCE `ssd` storage class created above. Here is a sample manifest:


##pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: vol
spec:
  storageClassName: ssd
  capacity:
    storage: 5Gi
  persistentVolumeReclaimPolicy: Retain
  accessModes:
    - ReadWriteOnce

Apply this manifest using the `kubectl apply -f pv.yaml` command. You will receive the following error:


The PersistentVolume "vol" is invalid: spec: Required value: must specify a volume type

This is good news—you’ve replicated the error, so now let’s discuss how to resolve it.

Resolving the Error Code

The `must specify a volume type` error code can be solved in multiple ways by implementing the following workflows:

1. Only Creating the PVC

Kubernetes can make use of dynamic volume provisioning, where you don’t need to pre-provision a PV with specific storage. With dynamic volume provisioning, the defined storage class can provide access to storage based on the PVC request.

Here is a sample PVC manifest that can be used with the example above to resolve the `must specify a volume type` error code. By applying the below configuration, you are creating a `PersistentVolumeClaim` that provides a 5 GB disk to the pod. The claim searches for a GCE storage class named `ssd`  and uses it to acquire the requested space.


##pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: vol
  namespace: default
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: ssd

Apply the manifest using the `kubectl apply -f pvc.yaml` command. The response should be as follows:


persistentvolumeclaim/vol created

2. Using the StorageClass Provisioner

Most StorageClasses in Kubernetes have a provisioner that determines what volume plugin is used for provisioning the PVs.

In the example below, you’ll use Google’s `gce-pd` storage provisioner, which can automatically provision PVs:


##storageclass.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: gce-provision
provisioner: kubernetes.io/gce-pd
parameters:
  type: pd-ssd
reclaimPolicy: Retain

You don’t have to create the PV on your own: the provisioner will provide the PV with the requested storage size and kind, on your behalf, when you apply for a `PersistentVolumeClaim`.

Here is a sample PVC manifest that is connected with the storage class `gce-provision` defined above:


kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: gce-storage
  namespace: default
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
  storageClassName: gce-provision

The provisioner will essentially sort the dependencies required, which should resolve the error message.

You can read more about provisioners here.

3. Removing the `StorageClass`

Alternatively, if you don’t want to use any storage classes for dynamic provisioning, you can specify the storage as `manual` and provide two manifests: one for a `PersistentVolumeClaim` that can then find the matching `PersistentVolume` manifest to correct the `must specify a volume type` error code.

Here is the `pv.yaml` file for configuring volumes without storage classes:


##pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: vol
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 3Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/opt/volume/nginx"

And here is the `pvc.yaml` for the same purpose:


##pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: log-claim
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
  storageClassName: manual

Each cluster’s use cases are different, but if you don’t need to use any storage classes for dynamic provisioning, this is a good way to resolve the error.

Final Thoughts

In this article, you learned a few different solutions that can help overcome the `must specify a volume type` error. Attempting to manually provision a PersistentVolume when using dynamic volume provisioning for the pods is the primary reason behind the error code. However, there are many more instances in which the `must specify a volume type` error code can arise, so be sure to keep an eye out for identifying and managing those misconfigurations when they appear.

If you’d like to learn more, continue reading about `PersistentVolume`, `PersistentVolumeClaims` and more in the Kubernetes docs.

Learn from Nana, AWS Hero & CNCF Ambassador, how to enforce K8s best practices with Datree

Watch Now

🍿 Techworld with Nana: How to enforce Kubernetes best practices and prevent misconfigurations from reaching production. Watch now.

Headingajsdajk jkahskjafhkasj khfsakjhf

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

Reveal misconfigurations within minutes

3 Quick Steps to Get Started