Expanding a pod volume
To expand a volume:
- Enable volume expansion.
- Create a PersistentVolumeClaim object.
- Create a pod with a dynamically provisioned volume.
- Delete the pod with the volume.
- Request volume expansion.
- Create a pod with a volume.
Before you start, install kubectl
Enable volume expansion
To enable the volume expansion feature, make sure the storage class (StorageClass
) description contains the allowVolumeExpansion: true
parameter. In Managed Service for Kubernetes storage, this feature is enabled by default:
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: yc-network-hdd
provisioner: disk-csi-driver.mks.ycloud.io
volumeBindingMode: WaitForFirstConsumer
parameters:
type: network-hdd
csi.storage.k8s.io/fstype: ext4
allowVolumeExpansion: true
reclaimPolicy: Delete
Create a PersistentVolumeClaim object
-
Save the following PersistentVolumeClaim creation specification to a YAML file named
pvc-expansion.yaml
.To learn more about the
PersistentVolumeClaim
creation specification, see the Kubernetes documentation .apiVersion: v1 kind: PersistentVolumeClaim metadata: name: pvc-expansion spec: accessModes: - ReadWriteOnce storageClassName: yc-network-hdd resources: requests: storage: 1Gi
-
Create the
PersistentVolumeClaim
object:kubectl create -f pvc-expansion.yaml
Result:
persistentvolumeclaim/pvc-expansion created
Create a pod with a dynamically provisioned volume
-
Save the following pod creation specification to a YAML file named
pod.yaml
.To learn more about the pod creation specification, see the Kubernetes documentation
.apiVersion: v1 kind: Pod metadata: name: pod spec: containers: - name: app image: ubuntu 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: pvc-expansion
-
Create a pod:
kubectl create -f pod.yaml
Result:
pod/pod created
Delete the pod with the volume
To request volume expansion, you need to delete the pod.
-
Delete the pod:
kubectl delete pod pod
Result:
pod "pod" deleted
Request volume expansion
Edit the spec.resources.requests.storage
field of the PersistentVolumeClaim
object.
-
Open the YAML file named
pvc-expansion.yaml
:kubectl edit pvc pvc-expansion
In the text editor, change the disk size value and save it:
# Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: v1 kind: PersistentVolumeClaim metadata: ... spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi # Change to 2Gi. ... status: accessModes: - ReadWriteOnce capacity: storage: 1Gi phase: Bound
-
Wait for the volume to expand. Check the change results:
kubectl get pvc pvc-expansion -o yaml
The
spec.resources.requests.storage
field shows the requested volume size:apiVersion: v1 kind: PersistentVolumeClaim metadata: ... spec: accessModes: - ReadWriteOnce resources: requests: storage: 2Gi ... status: accessModes: - ReadWriteOnce capacity: storage: 1Gi ...
Create a pod with a volume
-
To resize the volume, create a pod:
kubectl create -f pod.yaml
Result:
pod/pod created
-
Check the change results:
kubectl get pvc pvc-expansion -o yaml
The volume size increased. The
status.capacity.storage
field now shows the expanded size:apiVersion: v1 kind: PersistentVolumeClaim metadata: ... spec: accessModes: - ReadWriteOnce resources: requests: storage: 2Gi ... status: accessModes: - ReadWriteOnce capacity: storage: 2Gi ...