Mount Azure File Storage in Kubernetes Pod using Symlink 💡
Tags: azure, kubernetes, symlink, devops
Published: 2021-03-19 17:46:49

## What is Symlink?
In laymen's terms, A symlink or a Symbolic Link is a shortcut to another file or directory. There are two types of links that exist in UNIX based systems
**Soft links** —It is like a shortcut to a file or directory. Soft links can point to a file or a directory on a different filesystem or partition as well.
**Hard links** — Hard links are associating two or more file names with the same inode.


## What is File storage?
In File Storage(file-based storage), data is stored as a single piece of information inside a folder. Files can be stored in folders, which can then be placed inside other folders in a hierarchical structure. NAS is typical example of File Storage. Azure File Share is example of file storage in Cloud.

## What is SMB, CIFS?
IBM developed SMB protocol, to allow systems to read and write files to a remote host over a LAN. The directories on the remote hosts made available via SMB are called smb shares.
CIFS is an implementation of the SMB protocol but created by Microsoft.

`Azure file shares can be mounted in Linux distributions using the SMB 3.0`

## Can we use Linux symlink to mount using CIFS??
mount.cifs mounts a Linux CIFS filesystem. The mount.cifs utility `attaches` the UNC name (exported network resource) specified as service (using //server/share syntax, where "server" is the server name or IP address and "share" is the name of the share) to the `local directory mount-point`.


## Let's mount Azure Files
**What do we need**: We need to have an Azure storage account with an Azure file share to be created beforehand for this.
**Roadblock**: A fact to note that Azure Files does not support Hard Link or Soft Link on Linux using CIFS directly.
**Solution**: Currently, the Linux client supports another style of symbolic links called Minshall+French symlinks. You can use mfsymlinks mount option.


```sh
$ mount -t cifs //<storage-account-name>.file.core.windows.net/<share-name> <mount-point> -o vers=<smb-version>,username=<storage-account-name>,password=<storage-account-key>,dir_mode=0777,file_mode=0777,serverino,mfsymlinks
# This format for storing symlinks on SMB shares 

```
**Container Specifics**: We are going to use CIFS to mount the file system so our container should have CIFS libraries as part of its build.

Example Dockerfile for container
```
FROM ubuntu:20.10
RUN apt-get update -y && apt-get install -y cifs-utils
```

**Kubernetes** — I am going to run a container in which I am going to mount Azure File share to a container location created at runtime. The noticeable point is that you need to run the container in privilege mode for this.

```
apiVersion: v1
kind: Pod
metadata:
  labels:
    os: ubuntu
  name: symlink-example
spec:
   containers:
  - image: singharunk/dev:latest  #container image with cifs package
    name: symlink
    env:
    - name: DEMO_ACC
      value: "xxxxxx"
    - name: DEMO_KEY
      value: "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    - name: DEMO_SHARE
      value: "share"
    securityContext:
      privileged: true
      allowPrivilegeEscalation: true
    command: ["/bin/sh"]
    args: ["-c", "while true; do echo hello; sleep 10;done"]
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "mkdir /opt/share && mount --verbose -t cifs //$DEMO_ACC.file.core.windows.net/$DEMO_SHARE /opt/share -o vers=3.0,username=$DEMO_ACC,password=$DEMO_KEY,dir_mode=0777,file_mode=0777,serverino"]
  restartPolicy: Always
```

The above YAML file will be able to mount your Azure File Share in your Ubuntu container. The above technique I mentioned is not a standard way to mount storage on the Kubernetes cluster. But there can be use cases where you may have to use this strategy to get storage mounted on Containers as a workaround. 

That’s all for this post. Keep learning and Stay healthy.

> [SMB AND CIFS] https://docs.microsoft.com/en-us/windows/win32/fileio/microsoft-smb-protocol-and-cifs-protocol-overview
> [MORE ON CIFS] https://manpages.debian.org/stretch/cifs-utils/mount.cifs.8.en.html