top of page
Writer's picturePlastron Technologies

Using Azure File Share as Persistent Volumes with Kubernetes




Kubernetes is like an ocean, the more you dig, the deeper it is. As a beginner it always bother me that if the pod life cycle comes to an end, then what will happen to its data. It was hard to digest that it will also go away. Just think of a scenario you are troubleshooting a data issue in your developer environment and due to some resource limitations your container crashes. Yes, that’s bad and you will loose all your data. That is when I explored about Persistent Volume and Persistent Volume claims.



What is Persistent Volume PV?

To put it in simple words, Persistent Volume (PV) is a storage element in a cluster, defined manually by an administrator or dynamically defined by a storage class. A PV has its own lifecycle, separate from the lifecycle of Kubernetes pods.



What is Persistent Volume Claim PVC?

Persistent Volume Claim (PVC) is a user’s storage request. An application running on a container can request a certain type of storage. For example, a container can specify the size of storage it needs or the way it needs to access the data (read only, write, read/write, with one-time or ongoing access).

We might have seen a lot of scenarios where we have a requirement to save data to a persistence volume or to read data from persistence storage. Here lets go through an option to make use of Azure File Share as Persistence Volume.We will go through the steps to create an Azure file Share as Persistence Volume, but before that lets have a look and pre requisites for this steps to work.


Prerequisites:

  • Azure account: This is where you will create the file share.

  • Kubernetes Cluster: For simplicity, I have created the cluster in the same azure subscription where we have Azure file share.

Before getting started i have bifurcated this tutorial into two parts:

  • In the first part we will create a file share dynamically using provisioners and use it as Persistence Volume.

  • In second part we will use the existing File Share created above as Persistence Volume.


 

Lets get our hands Dirty !!!

Lets first create the StorageClass(SC). Below is the Yaml configuration for the same. Save the configuration in the directory createfileshare.

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: sc-fileshare
provisioner: kubernetes.io/azure-file
mountOptions:
  - dir_mode=0777
  - file_mode=0777
  - uid=0
  - gid=0
  - mfsymlinks
  - cache=strict
parameters:
  skuName: Standard_LRS
  shareName: pod-fileshare

Here we have create a SC where we have defined provisioner as kubernetes.io/azure-file. In mount options we have defined the attributes of fileshare like access, modes, users, groups, etc.


Now lets login to our cluster. To login to the cluster you can get connection details from Azure Portal Cluster overview page itself by clicking on connect.

File share on Azure Portal

This will open the connection blade on right side as below:

Connection Credentials

Copy paste the commands on your prompt and you will be connected to your cluster.


Lets create SC now. Navigate into the directory from prompt and hit the following command:

$ kubectl apply -f azure-file-sc.yaml
$ kubectl get sc

OutPut:
PS C:\Projects\EXP\fileshare_Kubernetes\createfileshare> kubectl apply -f fileshare_sc.yaml
storageclass.storage.k8s.io/sc-fileshare created
PS C:\Projects\EXP\fileshare_Kubernetes\createfileshare> kubectl get sc 
NAME                    PROVISIONER                RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
...
sc-fileshare            kubernetes.io/azure-file   Delete          Immediate              false                  57s

Now lets create PVC. Refer the configuration below. Create a file with this configuration and save it in the same directory: fileshare_pvc.yaml.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-with-fileshare
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: sc-fileshare
  resources:
    requests:
      storage: 2Gi

In below configuration you can see we have created a pvc with name pvc_with_fileshare. Also note the storage class name is sc-fileshare which exactly matches the name in SC configuration.

$ kubectl apply -f fileshare_pvc.yaml
$ kubectl get pvc

OutPut:
PS C:\Projects\EXP\fileshare_Kubernetes\createfileshare> kubectl apply -f fileshare_pvc.yaml
persistentvolumeclaim/pvc-with-fileshare created
PS C:\Projects\EXP\fileshare_Kubernetes\createfileshare> kubectl get pvc
NAME                 STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE  
pvc-with-fileshare   Bound    pvc-90b939c0-c28a-4315-9002-89b197310541   2Gi        RWX            sc-fileshare   11s  

Now navigate to Storage account in default resource group created by kubernetes cluster(Starts with MC_). In my case its MC_mayur_rg_volume_fileshare_fileshare_test_eastus. Go to the storage account automatically created and click on file share as shown below.

File Share Dynamically Created

Here you will notice a file share is dynamically created.


Now to access this file share my cluster needs to have credentials to connect to it. You can find this credentials Access Keys on the left hand side. Kubernetes stores same set of access keys in secrets the moment it creates the file share. Hit the following command to get the access keys from secrets.

$ kubectl get secrets
$ kubectl get secret <secrete_name_from_above_command> -o yaml

Now lets deploy the pod which will make use of this file share using this secrets. For that create a file fileshare_pod_deployment.yaml and paste the following configuration

kind: Pod
apiVersion: v1
metadata:
  name: nginx
spec:
  containers:
  - name: filesharepod
    image: mcr.microsoft.com/oss/nginx/nginx:1.15.5-alpine
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 250m
        memory: 256Mi
    volumeMounts:
    - mountPath: "/mnt/azure"
      name: volume
  volumes:
    - name: volume
      persistentVolumeClaim:
        claimName: pvc-with-fileshare

Hit the following command:

$ kubectl apply -f fileshare_pod_deployment.yaml

Output:
PS C:\Projects\EXP\fileshare_Kubernetes\createfileshare> kubectl apply -f fileshare_pod_deployment.yaml
pod/nginx created
PS C:\Projects\EXP\fileshare_Kubernetes\createfileshare> kubectl get pods 
NAME    READY   STATUS              RESTARTS   AGE       
nginx   0/1     ContainerCreating   0          28s  

To confirm whether we are able to access the file share from within the pod lets shell into the pod with following command and create a file:

kubectl exec pod/nginx -it -- /bin/sh
> / # echo this is demo for file share as persistence volume. Thanks !!! > /mnt/azure/file.txt

After executing this command hop on to azure portal and you will notice the file with same content in file share.

Content Matched

Hence we are able to access file share as mount volume and are able to write data into it.

We can also check the PVC, PV and storage class in your AKS page in azure as shown below:



 


Using existing File Share as Persistence Volume

Now lets move to second part where we will be reading the existing file share as mount volume and read its data. As per current scenario we have a file share with a file already created in previous part. In this part we will try to create a Persistence Volume out of it. There are a few things which will change here. Unlike before we will not have secrets of this file share in our container. Hence we have to provide this secrets explicitly.

Lets create the kubernetes secrets:

kubectl create secret generic file-share-secret 
        --from-literal=azurestorageaccountname=f9daa8e9a361d48e89ab047
        --from-literal=azurestorageaccountkey=XXXXXXX

OutPut:
secret/file-share-secret created

Lets put our PV and PVC and pod configuration which will be using existing FileShare as below:

PVC : existingfileshare/existing_fileshare_pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-fileshare-existing
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: fs-storageclass
  resources:
    requests:
      storage: 2Gi

PV: existingfileshare/existing_fileshare_pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-fileshare
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  azureFile:
    secretName: file-share-secret
    shareName: existing-fileshare
    readOnly: false
  mountOptions:
  - dir_mode=0777
  - file_mode=0777
  - uid=1000
  - gid=1000
  - mfsymlinks

pod configuration:

apiVersion: v1
kind: Pod
metadata:
  name: existing-fs-pod
spec:
  containers:
  - image: mcr.microsoft.com/oss/nginx/nginx:1.15.5-alpine
    name: existing-fs-container
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 250m
        memory: 256Mi
    volumeMounts:
      - name: azure
        mountPath: /mnt/azure
  volumes:
  - name: azure
    persistentVolumeClaim:
      claimName: pvc-fileshare-existing

Hit following command to apply this configuration

PS C:\Projects\EXP\fileshare_Kubernetes\useFileShare> kubectl apply -f .

OutPut:
pod/existing-fs-pod created
persistentvolume/pv-fileshare created
persistentvolumeclaim/pvc-fileshare-existing created

Not lets verify if we are able to access the file.txt

PS C:\Projects\EXP\fileshare_Kubernetes\useFileShare> kubectl exec pod/existing-fs-pod -it -- /bin/sh
/ # cd mnt/azure
/mnt/azure # ls
file.txt
/mnt/azure # cat file.txt
this is demo for file share as persistence volume. Thanks !!!

This is all. We are able to access file.txt in existing file share.

0 comments

Recent Posts

See All

Comments


IMG_20210824_121542.jpg

Hi, I'm Mayur Gupta

I'm a Software developer by profession. love Devops and open source stuff. If you like my stories, consider making a donation.

  • Facebook
  • Twitter
  • LinkedIn
  • Instagram
PayPal ButtonPayPal Button

Creativity. Productivity. Vision.

Find latest tips tricks and stories around Kubernetes, containers, Programming and devops.

Subscribe

Thanks for submitting!

bottom of page