Creating Persistent volume in Azure Kubernetes service (AKS) using Azure Files

Sonu Jose
DataSeries
Published in
5 min readSep 15, 2019

--

Azure Kubernetes service
Azure Kubernetes service

Kubernetes — a container orchestration platform, provides a way of provisioning a whole bunch of virtual machines and joining them together in to a single shared resource and using those resources to deploy a wide variety of containers — put together to make what we call a cloud native applications.

Why Persistent Storage needs to be provisioned in k8s?

Pods are the basic building block of Kubernetes, you can think it as an abstraction over containers. In Kubernetes the pods are ephemeral, disposable resources and should be primarily stateless.

The pods may get recreated and scheduled in any of the available nodes at any time by the Kubernetes scheduler based on the desired state particularly when they are part of a replicaset.

But not every workload is stateless, any meaningful application will eventually deal with data and data needs to be persisted. Pods often expect their storage to remain if a pod is rescheduled on a different host during a maintenance event, especially in StatefulSets.

Kubernetes Volume

In Kubernetes Volume is a separate object defined within the context of pod. It can be thought of as a directory which is accessible to the containers in a pod. It can be mounted into container at a particular path. We have different types of volumes in Kubernetes and the type defines how the volume is created and its content.

But Volumes like emptyDir, hostPath — that are defined and created as part of the pod life-cycle only exist until the pod is deleted. So there is this concept called as persistent volume, which can be mapped to cloud providers storage like azuredisk, NFS, ISCSI, Azure files etc..

Persistent Volumes and Claims in Kubernetes

A persistent volume (PV) is a storage resource created and managed by the Kubernetes API that can exist beyond the lifetime of an individual pod. A PersistentVolume can be statically created by a cluster administrator, or dynamically created by the Kubernetes API server.

PV is a resource in the cluster just like a node is a cluster resource having a life-cycle independent of any individual pod that uses the PV.

The Persistent volume requested by Kubernetes for its pods is known as Persistent Volume Claim (PVC). The user does not need to know the underlying provisioning. It is responsible for saying, the need for a Disk to the cloud provider. PVC acts as an abstraction between replica set and the cloud storage. The claims must be created in the same namespace where the pod is created.

Storage options in Azure Kubernetes Service (AKS)

In AKS traditional volumes to store and retrieve data are created as Kubernetes resources backed by Azure Storage. You can manually create these data volumes to be assigned to pods directly, or have Kubernetes automatically create them. These data volumes can use Azure Disks or Azure Files.

aks storage options

In this blog we will be discussing about how to mount Azure Files as Kubernetes volume. You can either mount volume directly to pod or can mount using Persistent volume and Persistent volume claim

Create an Azure File share in Storage Account

First, you’ll want to create a storage account in Azure, and then a File Share folder in that storage account. Use the scripts to create storage account and File share.

Using Terraform for creating Azure Storage Account and a file Share

terraform init
terraform plan
terraform apply

Now in your Azure portal, we can see the File share files-k8s been created.

Creating Storage Account Secret

Kubernetes needs credentials to access the file share we have created. These credentials are stored in a Kubernetes secret, which is referenced when you create a Kubernetes pod.

Obtain an Microsoft Azure storage account and extract the storage account name (which you provided) and one of the storage account keys. You can use kubectl directly to create the secret:

Mount volume directly in Pod

In the deployment, you need to provide the following information:

  • secretName: the name of the secret that contains both Azure storage account name and key.
  • shareName: The share name to be used.
  • readOnly: Whether the filesystem is used as readOnly.
  • secretNamespace: (optional) The namespace in which the secret was created; default is used if not set
kubectl apply -f fileshare-dep.yml

Perfect! Now we have 2 pods running in the namespace filesharetest and both will be mounted to the fileshare files-k8s .

kubectl describe pod YOUR_POD_NAME --namespace filesharetest

We can see the Fileshare is mounted to the location /usr/share/nginx/html in the K8s pod. Now you can add a file inside the Azure File share files-k8s and verify exec to one of our pods to see the mount in action.

kubectl exec -it YOUR_POD_NAME -n filesharetest /bin/sh

If you do the same kubectl exec steps and evaluate that directory on a different pod, you’ll see the same file! That’s all there is to it, pretty simple!

Method — 2: Mount volume via PV and PVC

The same mechanism can also be used to mount the Azure File Storage using a Persistent Volume and a Persistent Volume Claim:

Correspondingly, you then mount the volume inside pods using the normal persistentVolumeClaim reference.

PersistentVolume and PersistentVolumeClaims in Kubernetes, along with the support of multiple cloud vendors storage solutions, can prove useful in many cases. Hopefully this helps you get up and running!

References:

Thanks,

Sonu Jose

Follow us on Twitter 🐦 and Facebook 👥 and join our Facebook Group 💬.

To join our community Slack 🗣️ and read our weekly Faun topics 🗞️, click here⬇

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

--

--

Sonu Jose
DataSeries

Software Engineer at VMware. Loves building tools and applications for Platform Engineers. K8s Enthusiast | Golang Developer | Writer in Faun and Dataseries.