How to backup and restore a Persistent Volumes in AKS

Sam A
2 min readDec 9, 2022

Create a snapshot

az snapshot create -g $resourceGroupName -n $snapshotName --source $diskName
#Get the snapshot Id
snapshotId=$(az snapshot show --name $snapshotName --resource-group $resourceGroupName --query [id] -o tsv)

create a disk out of the snapshot

#Create a new Managed Disks using the snapshot Id
#Note that managed disk will be created in the same location as the snapshot
az disk create --resource-group $resourceGroupName --name $diskName --sku $storageType --size-gb $diskSize --source $snapshotId

create a new PV from the disk

apiVersion: v1
kind: PersistentVolume
metadata:
annotations:
pv.kubernetes.io/migrated-to: disk.csi.azure.com
pv.kubernetes.io/provisioned-by: kubernetes.io/azure-disk
finalizers:
- kubernetes.io/pv-protection
- external-attacher/disk-csi-azure-com
name: pvc-test-disk
spec:
accessModes:
- ReadWriteOnce
azureDisk:
cachingMode: ReadWrite
diskName: test-snapshot
diskURI: /subscriptions/<sub-id>/resourceGroups/<rg-name>/providers/Microsoft.Compute/disks/test-disk
fsType: ""
kind: Managed
readOnly: false
capacity:
storage: 500Gi
persistentVolumeReclaimPolicy: Delete
storageClassName: default
volumeMode: Filesystem

create a new PVC from disk

piVersion: v1
kind: PersistentVolumeClaim
metadata:
labels:
app: test-strg
release: test-str
role: test-strg
name: test-vol
namespace: test
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Gi
volumeName: pvc-test-disk
volumeMode: Filesystem

Now in you application mount the new volume instead of the old one

apiVersion: apps/v1
kind: Deployment
metadata:
name: test-strg
labels:
app: test-strg
namespace: test
spec:
replicas: 1
selector:
matchLabels:
app: test-strg
template:
metadata:
labels:
app: test-strg
spec:
containers:
- name: test-strg
image: docker.io/debian:stable
command: [ "bash" ]
args: [ "-c", "sleep infinity" ]
volumeMounts:
- mountPath: /test
name: test-strg
volumes:
- name: test-strg
persistentVolumeClaim:
claimName: test-vol

--

--

Sam A

Senior DevOps Consultant, a tech enthusiast and cloud automation expert that helps companies improve efficiency by incorporating automation