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:
This error code highlights two key points:
- The volume you are trying to create for a pod has missing or invalid parameters.
- 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:
Apply this manifest using the `kubectl apply -f storageclass.yaml` command. The response should be as follows:
Next, let’s try to provision a PV using the GCE `ssd` storage class created above. Here is a sample manifest:
Apply this manifest using the `kubectl apply -f pv.yaml` command. You will receive the following error:
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.
Apply the manifest using the `kubectl apply -f pvc.yaml` command. The response should be as follows:
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:
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:
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:
And here is the `pvc.yaml` for the same purpose:
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.
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.