Installing the GCP Compute PD CSI driver to manage PD volumes

The GCP Compute PD CSI driver provides a Kubernetes CSI interface that allows GKE clusters to simply manage the life cycle of GKE volumes for persistent volumes. In this recipe, we will learn the steps required to install the GCP Compute PD CSI driver by observing the following steps:

  1. Clone the GCP CSI driver project:
$ git clone https://github.com/kubernetes-sigs/gcp-compute-persistent-disk-csi-driver.git
$ cd gcp-compute-persistent-disk-csi-driver/
  1. Replace the PROJECT name with your GCP project name, GCE_PD_SA_DIRin the location where your service account private key file is stored and set the GCP service account variables:
$ EXPORT PROJECT="DevOpsCookBook"
$ GCE_PD_SA_NAME=my-gce-pd-csi-sa
$ GCE_PD_SA_DIR=/my/safe/credentials/directory
$ ./deploy/setup-project.sh
  1. Deploy the GCP Compute PD CSI driver:
$ GCE_PD_SA_DIR=/my/safe/credentials/directory
$ GCE_PD_DRIVER_VERSION=stable
$ ./deploy/kubernetes/deploy-driver.sh
  1. Verify that the driver is running:
$ kubectl get pods -n kube-system | grep ebs-csi
csi-gce-pd-controller 4/4 Running 0 31s
csi-gce-pd-node-f8w8w 3/3 Running 0 31s
csi-gce-pd-node-g8qn5 3/3 Running 0 31s
csi-gce-pd-node-n2fhp 3/3 Running 0 31s
  1. Now, create a new regional storage class using the pd.csi.storage.gke.io provisioner:
$ cat <<EOF | kubectl apply -f -
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: gcp-csi-pd
provisioner: pd.csi.storage.gke.io
parameters:
type: pd-standard
replication-type: regional-pd
volumeBindingMode: WaitForFirstConsumer
EOF
  1. Create a PVC:
$ cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: csi-gcp-pd-pvc
spec:
accessModes:
- ReadWriteOnce
storageClassName: gcp-csi-pd
resources:
requests:
storage: 4Gi
EOF
  1. Create a pod that will use the PVC and that writes to the /data/out.txt file:
$ cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: mytestapp
spec:
containers:
- name: app
image: centos
command: ["/bin/sh"]
args: ["-c", "while true; do echo $(date -u) >> /data/out.txt; sleep 5; done"]
volumeMounts:
- name: persistent-storage
mountPath: /data
volumes:
- name: persistent-storage
persistentVolumeClaim:
claimName: csi-gcp-pd-pvc
EOF
  1. Verify that our mytestapp pod writes data to the volume:
$ kubectl exec -it mytestapp cat /data/out.txt
Mon Sep 9 18:20:38 UTC 2019
  1. Remove the resources:
$ kubectl delete pod mytestapp && kubectl delete pvc csi-gcp-pd-pvc 

Now you know how to utilize a CSI driver to deploy GCE PD volumes on GKE clusters.