# Use an Azure File share

GKE on Azure supports mounting
[Azure Files](https://azure.microsoft.com/en-us/services/storage/files/#documentation)
shares. If you already have an Azure File share to use with
GKE on Azure, you can create a PersistentVolume (PV) object and
reserve it for a specific PersistentVolumeClaim (PVC).

This page explains how to create a PV by using an existing share
populated with data, and how to use the PV in a Pod.

## Before you begin

- [Connect to your cluster](https://docs.cloud.google.com/kubernetes-engine/multi-cloud/docs/azure/how-to/connect-and-authenticate-to-your-cluster.md.txt)
- Have access to or create an Azure File share. For more information, see [Create an Azure file share](https://docs.microsoft.com/en-us/azure/storage/files/storage-how-to-create-file-share?tabs=azure-portal).

## Store your Azure storage account information

GKE on Azure stores information to access your Azure storage account
in a Secret. If you haven't created a Secret in your cluster, you must add one.
If you have this Secret in you cluster, skip to
[Create a PersistentVolume for a pre-existing share](https://docs.cloud.google.com/kubernetes-engine/multi-cloud/docs/azure/how-to/use-azure-file.md.txt#create).

1. To create the Secret, copy the following manifest into a file named
   'azure-service-account-key.yaml'.

   ```yaml
   ---
   apiVersion: v1
   kind: Secret
   metadata:
     name: azure-secret-account-key
   type: Opaque
   stringData:
     accountname: STORAGE_ACCOUNT_NAME
     accountkey: STORAGE_ACCOUNT_KEY
   ```

   Replace the following:
   - <var translate="no">STORAGE_ACCOUNT_NAME</var>: your Azure storage account name
   - <var translate="no">STORAGE_ACCOUNT_KEY</var>: your Azure storage account key
2. Apply the file to your cluster with the `kubectl` tool:

   ```bash
   kubectl apply -f azure-service-account-key.yaml
   ```

## Create a PersistentVolume for a pre-existing share

You import an existing Azure File share by specifying a new PV in your cluster.
To create the PV, do the following:

1. Copy the following YAML into a file named `existing-volume.yaml`:

   ```yaml
   apiVersion: v1
   kind: PersistentVolume
   metadata:
     name: VOLUME_NAME
   spec:
     capacity:
       storage: VOLUME_CAPACITY
     storageClassName: standard-rwx
     accessModes:
       - ReadWriteMany
     persistentVolumeReclaimPolicy: Retain
     csi:
       driver: file.csi.azure.com
       readOnly: false
       volumeHandle: VOLUME_ID
   ```

   Replace the following:
   - <var translate="no">VOLUME_NAME</var>: a name for the volume
   - <var translate="no">VOLUME_CAPACITY</var>: size of the volume. For example, `30Gi`. For more information on specifying volume capacity in Kubernetes, see the [Meaning of memory](https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/#meaning-of-memory).
   - <var translate="no">VOLUME_ID</var>: a unique ID for the volume, formatted as a string of `RESOURCE_GROUP_NAME#STORAGE_ACCOUNT_NAME#FILESHARE_NAME#` where
   - <var translate="no">FILE_SHARE_NAME</var>: the Azure File share name

   If your storage account is in a different resource group than your
   cluster, you need to add a reference to a Secret that contains your
   storage account key. To add the reference, insert the following in the
   `spec.csi` section:

   ```yaml
   # Optional. Only required if your storageAccount is in a different resource group than the cluster.
       nodeStageSecretRef:
         name: NODE_STAGE_SECRET_NAME
         namespace: NODE_STAGE_SECRET_NAMESPACE
   ```

   Replace the following:
   - <var translate="no">NODE_STAGE_SECRET_NAME</var>: the name of the Secret
   - <var translate="no">NODE_STAGE_SECRET_NAMESPACE</var> the Namespace that contains the Secret
2. Apply the YAML to your cluster.

   ```bash
   kubectl apply -f existing-volume.yaml
   ```
3. Confirm the creation of your PV with `kubectl describe pv`.

   ```bash
   kubectl describe pv VOLUME_NAME
   ```

   The output of this command contains the status of the PV.

## Use the volume with a PersistentVolumeClaim and Pod

After you have imported your volume, you can create a PVC and a Pod that
mounts the PVC.

1. The following YAML creates a PVC and attaches it to a Pod running the Nginx web
   server. Copy it into a file named `nginx.yaml`:

   ```yaml
   apiVersion: v1
   kind: PersistentVolumeClaim
   metadata:
     name: my-pvc
   spec:
     storageClassName: STORAGE_CLASS_NAME
     volumeName: VOLUME_NAME
     accessModes:
       - ACCESS_MODE
     resources:
       requests:
         storage: VOLUME_CAPACITY
   ---

   apiVersion: v1
   kind: Pod
   metadata:
     name: web-server
   spec:
     containers:
      - name: web-server
        image: nginx
        volumeMounts:
          - mountPath: /var/lib/www/html
            name: data
     volumes:
      - name: data
        persistentVolumeClaim:
          claimName: my-pvc
   ```

   Replace the following:
   - <var translate="no">STORAGE_CLASS</var>: the name of the StorageClass from the PersistentVolume you created previously. For example, `standard-rwo`.
   - <var translate="no">ACCESS_MODE</var>: the access mode of the volume. For Azure Disk, use `ReadWriteOnce`. For Azure File, use `ReadWriteMany`.
   - <var translate="no">VOLUME_CAPACITY</var>: size of the volume. For example, `30Gi`.
2. Apply the YAML to your cluster.

   ```bash
   kubectl apply -f nginx.yaml
   ```
3. Check the status of your Nginx instance with `kubectl describe`. The output
   should have a `STATUS` of `Running`.

   ```bash
   kubectl describe pod web-server
   ```
4. To delete the Pod, use the `kubectl delete` command.

   ```bash
   kubectl delete -f nginx.yaml
   ```

## What's next

- Use additional [storage drivers](https://docs.cloud.google.com/kubernetes-engine/multi-cloud/docs/azure/how-to/storage-drivers.md.txt) with GKE on Azure.
- Read the documentation for the [Azure File CSI driver](https://github.com/kubernetes-sigs/azurefile-csi-driver/tree/v1.7.0/docs).