gcePersistentDisk

gcePersistentDisk volume mounts a Google Compute Engine (GCE) Persistent Disk (PD) into a Pod. If you provision it statically, you'll have to create it first with the gcloud command or in the GCE console. The following is an example:

# cat 2-6-8_gce/static_mount.yaml
apiVersion: v1
kind: Pod
metadata:
name: gce-pd-pod
spec:
containers:
- image: nginx
name: gce-pd-example
volumeMounts:
- mountPath: /mount-path
name: gce-pd
ports:
- containerPort: 80
volumes:
- name: gce-pd
gcePersistentDisk:
pdName: example
fsType: ext4

Alternatively, and more cost-effectively, we could use dynamic provisioning. Then we don't need to provision PD beforehand. For enabling dynamic provisioning, the DefaultStorageClass admission controller has to be enabled on the API server. In some Kubernetes environments, it has been enabled by default, such as in GCE. We could explicitly disable it by setting the storageClassName: "" in Pod/Deployment/ReplicaSet configuration file.

Next, we'll introduce how to create a non-default StorageClass:

// list storageclasses (sc)
# kubectl get sc
NAME PROVISIONER
standard (default) kubernetes.io/gce-pd

We can see we have a default storage class named standard. If that's the desired provider, then you don't need to create your own storage classes. In the following example, we'll create a new storage class named example:

// gce storage class
# cat 2-6-8_gce/storageclass.yaml
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: example
provisioner: kubernetes.io/gce-pd
parameters:
type: pd-standard
zones: us-central1-a

// create storage class
# kubectl create -f storageclass.yaml
storageclass "example" created

// check current storage classes
# kubectl get sc
NAME PROVISIONER
example kubernetes.io/gce-pd
standard (default) kubernetes.io/gce-pd

For the type, you can specify any storage type that GCE supports, such as pd-ssd. You can specify zones by changing zone parameters, too. Next, we'll add a PersistentVolumeClaim for using this storage class:

# 2-6-8_gce/pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: gce-example
spec:
accessModes:
- ReadWriteOnce
storageClassName: example
resources:
requests:
storage: 5Gi

// create pvc
# kubectl create -f pvc.yaml
persistentvolumeclaim "gce-example" created

// check pvc status
# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
gce-example Bound pvc-d04218e3-ede5-11e7-aef7-42010a8001f4 5Gi RWO example 1h

This configuration file will create a PVC by specifying the storage class named example. A PV will be created by the claim. When a PVC is in Bound status, Kubernetes will always bind that PV to the matching PVC. Then, let's have a Pod using this PVC:

# cat 2-6-8_gce/pod.yaml
kind: Pod
apiVersion: v1
metadata:
name: gce-pd-pod
spec:
volumes:
- name: gce-pd
persistentVolumeClaim:
claimName: gce-example
containers:
- name: gce-pd-example
image: nginx
ports:
- containerPort: 80
volumeMounts:
- mountPath: /mount-path
name: gce-pd

// create a pod
# kubectl create -f pod.yaml
pod "gce-pd-pod" created

// check the volume setting in pod
# kubectl describe pod gce-pd-pod
...
Containers:
gce-pd-example:
Container ID:
Mounts:
/mount-path from gce-pd (rw)
...
Volumes:
gce-pd:
Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
ClaimName: gce-example
ReadOnly: false

We can see that gce-pd is mounted under /mount-path. Let's see if the volume has been provisioned dynamically.

Alternatively, you could use gcloud compute disks list. gcloud in a command-line tool in GCE.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.50