Azure storage

12th Apr 2024

You will learn:

  • Create and use a static persistent volume in AKS
  • Create and use a dynamic persistent volume in AKS

Static storage allocation using Azure File

Created according to the tutorial on Azure Docs.

An Azure File persistent volume is a directory to which we attach using the SMB network file system. Using the same protocol from the Microsoft workshop, you can share folders over the network from your Windows computer or NAS device.

For convenience, we will use environment variables. We store in variables:

  • Resource group name (ResourceGroup)
  • Location of the data center (Location)
  • Name of the shared folder (ShareName)
  • Storage account login name (StorageAccount)

The export command creates a variable available in other commands. variable $RANDOM always returns a new random number.

# Change these four parameters as needed for your own environment
export STORAGE_ACCOUNT_NAME=mystorageaccount$RANDOM
export RESOURCE_GROUP=myAKSShare
export LOCATION=eastus
export SHARE_NAME=akshare

Create a resource group (ResourceGroup) and create in the resource group storage login account:

# Create a resource group
az group create --name $RESOURCE_GROUP --location $LOCATION
# Create a storage account
az storage account create -n $STORAGE_ACCOUNT_NAME -g $RESOURCE_GROUP -l $LOCATION --sku Standard_LRS

Create a persistent volume of type Azure File according to the login account. A persistent volume is created according to the specified connection string (ConnectionString). The login string contains the folder name, login name and password.

# Export the connection string as an environment variable, this is used when creating the Azure file share
export AZURE_STORAGE_CONNECTION_STRING=$(az storage account show-connection-string -n $STORAGE_ACCOUNT_NAME -g $RESOURCE_GROUP -o tsv)

# Create the file share
az storage share create -n $SHARE_NAME --connection-string $AZURE_STORAGE_CONNECTION_STRING

Put the connection details for the persistent volume in Kuberntes. You save the data in an object of type Secret:

# Get storage account key
STORAGE_KEY=$(az storage account keys list --resource-group $RESOURCE_GROUP --account-name $STORAGE_ACCOUNT_NAME --query "[0].value" -o tsv)
# Echo storage account name and key
echo Storage account name: $STORAGE_ACCOUNT_NAME
echo Storage account key: $STORAGE_KEY

# Create kubernetes secret
kubectl create secret generic azure-secret --from-literal=azurestorageaccountname=$STORAGE_ACCOUNT_NAME --from-literal=azurestorageaccountkey=$STORAGE_KEY
# Check the existence of the Secret type object
kubectl get secrets

Create a permanent volume. In the permanent bundle, you will use the credentials stored in Secret:

apiVersion: v1
kind: PersistentVolume
metadata:
   name: azurefile
spec:
   capacity:
     storage: 5Gi
   accessModes:
     - ReadWriteMany
   azureFile:
     # This is where you create a binding to the login credentials to the volume
     # login data are in the Secret type object
     secretName: azure-secret
     secretNamespace: default
     shareName: akshare
     readOnly: false
   mountOptions:
   - dir_mode=0755
   - file_mode=0755
   - uid=1000
   - gid=1000
   - mfsymlinks
   - nobr

Create a claim for permanent union.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
   name: azurefile
spec:
   accessModes:
     - ReadWriteMany
   storageClassName: ""
   resources:
     requests:
       storage: 5Gi

Now you can create an object that will use the persistent volume. For simplicity, create a `Pod' object directly. A Pod object wraps a container instance, but without additional properties such as automatic restart or scaling as with the Deployment type. But if you want, you can use it in the same way in a Deployment or StatefulSet object.

apiVersion: v1
kind: Sub
metadata:
   name: mypod
spec:
   containers:
   - image: mcr.microsoft.com/oss/nginx/nginx:1.15.5-alpine
     name: mypod
     resources:
       requests:
         cpu: 100m
         memory: 128 Mi
       limits:
         cpu: 250 m
         memory: 256 Mi
     volumeMounts:
       - name: azure
         mountPath: /mnt/azure
   volumes:
   - name: azure
     persistentVolumeClaim:
       claimName: azurefile

Dynamic allocation of Azure storage

https://docs.microsoft.com/en-us/azure/aks/azure-files-dynamic-pv

Create a Storage Class.

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: my-azurefile
provisioner: kubernetes.io/azure-file
mountOptions:
  - dir_mode=0777
  - file_mode=0777
  - uid=0
  - gid=0
  - mfsymlinks
  - cache=strict
  - actimeo=30
parameters:
  skuName: Standard_LRS

Create a claim for permanent union. With the claim, you request the dynamic allocation of a permanent volume from the selected class.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-azurefile
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: my-azurefile
  resources:
    requests:
      storage: 5Gi

You can use this permanent volume in your deployment (eg StatefulSet).

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

At the end

Don't forget to cancel all resources you have created.

To read

Previous Post

Azure storage