Static provisioning is the traditional method of provisioning volumes. It requires someone (typically an administrator) to manually provision a disk and create a PersistentVolume resource using the disk information. The developer can then use the PersistentVolume resource within their StatefulSet resource, as in the following diagram:
Figure 6.9 – Static provisioning
Let’s now look at a static provisioning example.
To access the resources for this section, cd into the following:
$ cd ~/modern-devops/ch6/statefulsets/
So, we first need to create a disk within the cloud platform. Since we’re using Google Cloud, let’s proceed and use the gcloud commands to do so.
Use the following command to create a persistent zonal disk. Ensure that you use the same zone as your Kubernetes cluster. As we are using the us-central1-a zone for the Kubernetes cluster, we will use the same in the following command:
$ gcloud compute disks create nginx-manual \
–size 50GB –type pd-ssd –zone us-central1-a
Created [https://www.googleapis.com/compute/v1/projects/<project_id>/zones/us-central1-a/ disks/nginx-manual].
NAME ZONE SIZE_GB TYPE STATUS
nginx-manual us-central1-a 50 pd-ssd READY
As the disk is now ready, we can then create a PersistentVolume resource from it.
The manifest file, nginx-manual-pv.yaml, looks like this:
apiVersion: v1
kind: PersistentVolume
metadata:
name: nginx-manual-pv
labels:
usage: nginx-manual-disk
spec:
capacity:
storage: 50G
accessModes:
- ReadWriteOnce gcePersistentDisk:
pdName: nginx-manual
fsType: ext4
The spec section contains capacity, accessModes, and the kind of disk it needs to provision.
You can specify one or more access modes to a PersistentVolumes:
- ReadWriteOnce: Only one pod can read and write to the disk at a time; therefore, you cannot mount such a volume to multiple pods
- ReadOnlyMany: Multiple pods can read from the same volume simultaneously, but no pod can write to the disk
- ReadWriteMany: Multiple pods can read and write to the same volume at once
Tip
Not all kinds of storage support all access modes. You need to decide the volume type during the initial requirement analysis and architectural assessment phase.
OK—let’s now go and apply the manifest to provision the PersistentVolume resource using the following command:
$ kubectl apply -f nginx-manual-pv.yaml
Let’s now check whether the Persistent Volume is available by using the following command:
$ kubectl get pv
NAME
CAPACITY
ACCESS MODES
RECLAIM POLICY
STATUS
nginx-manual-pv
50G
RWO
Retain
Available
As the Persistent Volume is now available, we must create a headless Service resource to help maintain network identity in the StatefulSet resource. The following nginx-manual-service. yaml manifest describes it:
apiVersion: v1
kind: Service
metadata:
name: nginx-manual
labels:
app: nginx-manual
spec:
ports:
– port: 80
name: web
clusterIP: None
selector:
app: nginx-manual
It is very similar to the regular Service resource, except that we have specified clusterIP as None.
Now, let’s go and apply the manifest using the following command:
$ kubectl apply -f nginx-manual-service.yaml