OpenEBS Snapshots using Kubectl

Snapshot-provisioner will be used to restore a snapshot as a new persistent volume via dynamic provisioning.OpenEBS operator will deploy each of Snapshot-controller and snapshot-provisioner container inside the single pod called snapshot-controller.apiVersion: v1kind: ServiceAccountmetadata: name: snapshot-controller-runner namespace: default—apiVersion: ClusterRolemetadata: name: snapshot-controller-role namespace: defaultrules: – apiGroups: [""] resources: ["pods"] verbs: ["get", "list", "delete"] – apiGroups: [""] resources: ["persistentvolumes"] verbs: ["get", "list", "watch", "create", "delete"] – apiGroups: [""] resources: ["persistentvolumeclaims"] verbs: ["get", "list", "watch", "update"] – apiGroups: [""] resources: ["storageclasses"] verbs: ["get", "list", "watch"] – apiGroups: [""] resources: ["events"] verbs: ["list", "watch", "create", "update", "patch"] – apiGroups: [""] resources: ["customresourcedefinitions"] verbs: ["create", "list", "watch", "delete"] – apiGroups: [""] resources: ["volumesnapshots"] verbs: ["get", "list", "watch", "create", "update", "patch", "delete"] – apiGroups: [""] resources: ["volumesnapshotdatas"] verbs: ["get", "list", "watch", "create", "update", "patch", "delete"] – apiGroups: [""] resources: ["services"] verbs: ["get"]—kind: ClusterRoleBindingapiVersion: name: snapshot-controller namespace: defaultroleRef: apiGroup: kind: ClusterRole name: snapshot-controller-rolesubjects:- kind: ServiceAccount name: snapshot-controller-runner namespace: default—kind: DeploymentapiVersion: extensions/v1beta1metadata: name: snapshot-controller namespace: defaultspec: replicas: 1 strategy: type: Recreate template: metadata: labels: app: snapshot-controller spec: serviceAccountName: snapshot-controller-runner containers: – name: snapshot-controller image: openebs/snapshot-controller:ci imagePullPolicy: Always – name: snapshot-provisioner image: openebs/snapshot-provisioner:ci imagePullPolicy: AlwaysOnce Snapshot-controller is running, you will be able to see the created CustomResourceDefinitions(CRD).$ kubectl get crdNAME 1mCreate Snapshot:To create snapshot let’s now create a PersistentVolumeClaim which is going to be snapshotted.kind: PersistentVolumeClaimapiVersion: v1metadata: name: demo-vol1-claim namespace: defaultspec: storageClassName: openebs-standard accessModes: – ReadWriteOnce resources: requests: storage: 5GNote that this is using the openebs-standardStorageClasswhich will dynamically provision a OpenEBSPersistentVolume..Let’s now create a Pod that will create some data in the volume..We will take a snapshot of the data and restore it later.For example busy-box application Pod createdate.txt and hostname.txt files in a mounted openEBS volume.apiVersion: v1kind: Podmetadata: name: busybox namespace: defaultspec: containers: — command: — sh — -c — ‘date > /mnt/store1/date.txt; hostname >> /mnt/store1/hostname.txt; tail -f /dev/null;’ image: busybox imagePullPolicy: Always name: busybox volumeMounts: — mountPath: /mnt/store1 name: demo-vol1 volumes: — name: demo-vol1 persistentVolumeClaim: claimName: demo-vol1-claimOnce the busyboxpod is in running state we are now ready to take a snapshot..Once we create the VolumeSnapshot resource below , snapshot-controller will attempt to create the actual snapshot by interacting with the OpenEBS snapshot API’s..If successful, the VolumeSnapshot resource is bound to a corresponding VolumeSnapshotData resource..To create snapshot we need to reference the PersistentVolumeClaim name in snapshot spec that references the data we want to snapshot.$ cat snapshot.yamlapiVersion: VolumeSnapshotmetadata: name: snapshot-demo namespace: defaultspec: persistentVolumeClaimName: demo-vol1-claim$ kubectl create -f snapshot.yamlvolumesnapshot "snapshot-demo" created$ kubectl get volumesnapshot NAME AGE snapshot-demo 18sThe conditions towards the bottom of the detailed output below show that our snapshot was created successfully..We can also check snapshot-controller’s logs to verify this.$ kubectl get volumesnapshot -o yaml apiVersion: v1 items: – apiVersion: kind: VolumeSnapshot metadata: clusterName: "" creationTimestamp: 2018-01-24T06:58:38Z generation: 0 labels: SnapshotMetadata-PVName: pvc-f1c1fdf2-00d2-11e8-acdc-54e1ad0c1ccc SnapshotMetadata-Timestamp: "1516777187974315350" name: snapshot-demo namespace: default resourceVersion: "1345" selfLink: /apis/ uid: 014ec851-00d4-11e8-acdc-54e1ad0c1ccc spec: persistentVolumeClaimName: demo-vol1-claim snapshotDataName: k8s-volume-snapshot-2a788036-00d4-11e8-9aa2-54e1ad0c1ccc status: conditions: – lastTransitionTime: 2018-01-24T06:59:48Z message: Snapshot created successfully reason: "" status: "True" type: Ready creationTimestamp: nullWe can now look at the corresponding VolumeSnapshotData resource that was created..Notice the reference to the openebsVolumesnapshot under spec which also references the VolumeSnapshot resource we created above and the PersistentVolume that the snapshot has been taken from..This PersistentVolume was dynamically provisioned by openebs-provisioner when we created our demo-vol1-claim PersistentVolumeClaim earlier.kubectl get volumesnapshotdata -o yamlapiVersion: kind: VolumeSnapshotData metadata: clusterName: "" creationTimestamp: 2018-01-24T06:59:48Z name: k8s-volume-snapshot-2a788036-00d4-11e8-9aa2-54e1ad0c1ccc namespace: "" resourceVersion: "1344" selfLink: /apis/ uid: 2a789f5a-00d4-11e8-acdc-54e1ad0c1ccc spec: openebsVolume: snapshotId: pvc-f1c1fdf2-00d2-11e8-acdc 54e1ad0c1ccc_1516777187978793304 persistentVolumeRef: kind: PersistentVolume name: pvc-f1c1fdf2-00d2-11e8-acdc-54e1ad0c1ccc volumeSnapshotRef: kind: VolumeSnapshot name: default/snapshot-demo status: conditions: – lastTransitionTime: null message: Snapshot created successfully reason: "" status: "True" type: Ready creationTimestamp: nullkind: Listmetadata: resourceVersion: "" selfLink: ""Restore Snapshot:Now we have created a snapshot, we can restore it to new PVC..To do this we need to create a special StorageClassimplemented by snapshot-provisioner..We will then create a PersistentVolumeClaim referencing this StorageClass for dynamically provision new PersistentVolume..An annotation on the PersistentVolumeClaim will inform snapshot-provisioner on where to find the information it needs to deal with the OpenEBS Apiserver to restore the snapshot.. More details

Leave a Reply