Static Volume in Azure Kubernetes Service

Static Volume in Azure Kubernetes Service

Create a static volume with Azure disks in Azure Kubernetes Service (AKS)

This article shows you how to manually create an Azure disk and attach it to a pod in Kubernetes

Note: An Azure disk can only be mounted to a single pod at a time. If you need to share a persistent volume across multiple pods, use Azure Files.

Create an Azure disk

When you create an Azure disk for AKS, create the disk resource in the node resource group. This will allows the AKS cluster to access and manage the disk resource

In this article creating the disk group in the same resource group

First, Identify the resource group name using the az aks show command

 az aks show --resource-group <ResourceGroup Name> --name <AKSCluster Name> --query nodeResourceGroup -o tsv

Example

shyju [ ~ ]$ az aks show --resource-group MYAKS --name MYCLUSTER --query nodeResourceGroup -o tsv
MC_MYAKS_MYCLUSTER_eastus #Resource group name

Create a disk using the az disk create command. Specify the node resource group name

az disk create \
  --resource-group <resource group name>\
  --name myAKSDisk \
  --size-gb 10 \
  --query id --output tsv

Example

shyju [ ~ ]$ az disk create  --resource-group MC_MYAKS_MYCLUSTER_eastus --name myAKSDisk  --size-gb 10 --query id --output tsv
/subscriptions/19a0ff5b-030e-48d7-84f0-fdbb3098b3f4/resourceGroups/MC_MYAKS_MYCLUSTER_eastus/providers/Microsoft.Compute/disks/myAKSDisk
shyju [ ~ ]$

The disk resource ID is displayed once the command has successfully completed, as shown in the following example output. This disk ID is used to mount the disk in the next section.

/subscriptions/<subscriptionID>/resourceGroups/MC_myAKSCluster_myAKSCluster_eastus/providers/Microsoft.Compute/disks/myAKSDisk

Mount disk as a volume

Create a PersistentVolume

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-azuredisk
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: managed-csi
  csi:
    driver: disk.csi.azure.com
    readOnly: false
    volumeHandle: /subscriptions/<subscriptionID>/resourceGroups/MC_myAKSCluster_myAKSCluster_eastus/providers/Microsoft.Compute/disks/myAKSDisk
    volumeAttributes:
      fsType: ext4

Output

shyju [ ~ ]$ kubectl create -f pv.yaml 
persistentvolume/pv-azuredisk created

PV Status

Kubectl get pv

Output

shyju [ ~ ]$ kubectl get pv
NAME           CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv-azuredisk   10Gi       RWO            Retain           Available           managed-csi             5s
shyju [ ~ ]$

Create a PersistentVolumeClaim that uses the PersistentVolume.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-azuredisk
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  volumeName: pv-azuredisk
  storageClassName: managed-csi
shyju [ ~ ]$ kubectl create -f pvc.yaml 
persistentvolumeclaim/pvc-azuredisk created

Output

shyju [ ~ ]$ kubectl get pvc
NAME            STATUS   VOLUME         CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc-azuredisk   Bound    pv-azuredisk   10Gi       RWO            managed-csi    34s
shyju [ ~ ]$

Check the CSI drivers

shyju [ ~ ]$ kubectl get sc 
NAME                    PROVISIONER          RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
azurefile               file.csi.azure.com   Delete          Immediate              true                   18m
azurefile-csi           file.csi.azure.com   Delete          Immediate              true                   18m
azurefile-csi-premium   file.csi.azure.com   Delete          Immediate              true                   18m
azurefile-premium       file.csi.azure.com   Delete          Immediate              true                   18m
default (default)       disk.csi.azure.com   Delete          WaitForFirstConsumer   true                   18m
managed                 disk.csi.azure.com   Delete          WaitForFirstConsumer   true                   18m
managed-csi             disk.csi.azure.com   Delete          WaitForFirstConsumer   true                   18m
managed-csi-premium     disk.csi.azure.com   Delete          WaitForFirstConsumer   true                   18m
managed-premium         disk.csi.azure.com   Delete          WaitForFirstConsumer   true                   18m
shyju [ ~ ]$

Create a POD reference for your PersistentVolumeClaim

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  nodeSelector:
    kubernetes.io/os: linux
  containers:
  - image: httpd
    name: mypod
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 250m
        memory: 256Mi
    volumeMounts:
      - name: azure
        mountPath: /mnt/azure
  volumes:
    - name: azure
      persistentVolumeClaim:
        claimName: pvc-azuredisk

Creating the pod

shyju [ ~ ]$ kubectl create -f pod.yaml 
pod/mypod created

Output

shyju [ ~ ]$ kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
mypod   1/1     Running   0          20s

Verify the disk status in the pod

shyju [ ~ ]$ kubectl exec mypod -- df -h
Filesystem                Size      Used Available Use% Mounted on
overlay                 123.9G     23.2G    100.7G  19% /
tmpfs                    64.0M         0     64.0M   0% /dev
tmpfs                     3.4G         0      3.4G   0% /sys/fs/cgroup
/dev/sdc                  9.9G     24.0K      9.8G   0% /mnt/azure
/dev/sda1               123.9G     23.2G    100.7G  19% /etc/hosts
/dev/sda1               123.9G     23.2G    100.7G  19% /dev/termination-log
/dev/sda1               123.9G     23.2G    100.7G  19% /etc/hostname
/dev/sda1               123.9G     23.2G    100.7G  19% /etc/resolv.conf
shm                      64.0M         0     64.0M   0% /dev/shm
tmpfs                   256.0M     12.0K    256.0M   0% /run/secrets/kubernetes.io/serviceaccount
tmpfs                     3.4G         0      3.4G   0% /proc/acpi
tmpfs                    64.0M         0     64.0M   0% /proc/kcore
tmpfs                    64.0M         0     64.0M   0% /proc/keys
tmpfs                    64.0M         0     64.0M   0% /proc/timer_list
tmpfs                    64.0M         0     64.0M   0% /proc/sched_debug
tmpfs                     3.4G         0      3.4G   0% /proc/scsi
tmpfs                     3.4G         0      3.4G   0% /sys/firmware

Hope you got an idea of how to map the disk in POD inside the AKS Cluster Thank you!

Related/References

docs.microsoft.com/en-us/azure/aks/azure-di..

Community and Social Footprints :

Did you find this article valuable?

Support Cloudnloud Tech Community by becoming a sponsor. Any amount is appreciated!