Volume snapshots let you create a copy of your volume at a specific point in
time. Use this copy to bring a volume back to a prior state or to
provision a new volume. In Kubernetes, snapshots are provided through the
VolumeSnapshot
object.
Before you begin
To run commands against a Kubernetes cluster, ensure you have the following resources:
Locate the Kubernetes cluster name, or ask your Platform Administrator what the cluster name is.
Sign in and generate the kubeconfig file for the Kubernetes cluster if you don't have one.
Use the kubeconfig path of the Kubernetes cluster to replace
KUBERNETES_CLUSTER_KUBECONFIG
in these instructions.
To get the required permissions to manage volume snapshots, ask your
Organization IAM Admin to grant you the Namespace Admin role (namespace-admin
)
in your project namespace.
Take a volume snapshot
To take a snapshot of a PersistentVolumeClaim
object, create a
VolumeSnapshot
object. The system does not guarantee data consistency. Pause
the application and flush data before taking a snapshot.
Create a
VolumeSnapshot
custom resource:kubectl --kubeconfig KUBERNETES_CLUSTER_KUBECONFIG \ --namespace NAMESPACE apply -f - <<EOF apiVersion: snapshot.storage.k8s.io/v1 kind: VolumeSnapshot metadata: name: VOLUME_SNAPSHOT_NAME spec: source: persistentVolumeClaimName: PVC_NAME EOF
Replace the following:
KUBERNETES_CLUSTER_KUBECONFIG
: the kubeconfig file for the cluster.NAMESPACE
: the project namespace in which to create the volume snapshot.VOLUME_SNAPSHOT_NAME
: theVolumeSnapshot
object name.PVC_NAME
: the name of the PVC for which you are creating a snapshot.
The snapshot operation is complete when the
.status.readyToUse
field becomestrue
. You can use the following command to check the status:kubectl --kubeconfig KUBERNETES_CLUSTER_KUBECONFIG get volumesnapshot \ -o custom-columns='NAME:.metadata.name,READY:.status.readyToUse'
Update the PVC manifest with the volume snapshot specified as a data source:
kubectl --kubeconfig KUBERNETES_CLUSTER_KUBECONFIG \ --namespace NAMESPACE apply -f - <<EOF apiVersion: v1 kind: PersistentVolumeClaim metadata: name: PVC_NAME spec: dataSource: name: VOLUME_SNAPSHOT_NAME kind: VolumeSnapshot apiGroup: snapshot.storage.k8s.io storageClassName: standard-rwo accessModes: - ReadWriteOnce resources: requests: storage: 10Gi EOF
Replace the following:
KUBERNETES_CLUSTER_KUBECONFIG
: the kubeconfig file for the cluster.NAMESPACE
: the namespace in which the PVC resource exists.PVC_NAME
: the name of the PVC for which you are creating a snapshot.VOLUME_SNAPSHOT_NAME
: theVolumeSnapshot
object name you're configuring a volume snapshot.