Azure Container Service : Using the Azure File Storage as a persistent (kubernetes) volume

Introduction

Today’s post is a brief one… Though it packs some punch! In the past I talked about storage patterns for docker/containers. Today we’ll touch how you can leverage the Azure File Storage as a shared & persistent storage for your container deployments. Kubernetes has been gaining a lot of traction, and that one has support for the Azure File Storage as a persistent volume too.

 

Demo Files

Want to run this yourself? Check out the following GitHub repository!

 

Brief Demo

So I’ve deployed a kubernetes cluster (via Azure Container Service) and setup my kubectl. Now let’s start deploying our service… In the yaml description, you can see the part describing our volumes ;

apiVersion: v1
kind: Service
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  ports:
    - port: 80
  selector:
    app: wordpress
    tier: frontend
  type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv002
spec:
  capacity:
    storage: 5Gi
  accessModes:
  - ReadWriteOnce
  azureFile:
    secretName: azure-secret
    shareName: wordpress
    readOnly: false
  claimRef:
    namespace: default
    name: az-files-02
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: az-files-02
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: frontend
    spec:
      containers:
      - image: wordpress:4.7.3-apache
        name: wordpress
        env:
        - name: WORDPRESS_DB_USER
          value: my-user-name
        - name: WORDPRESS_DB_HOST
          value: my-host-name
        - name: WORDPRESS_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: password
        ports:
        - containerPort: 80
          name: wordpress
        volumeMounts:
        - name: wordpress-persistent-storage
          mountPath: /var/www/html
      volumes:
      - name: wordpress-persistent-storage
        persistentVolumeClaim:
          claimName: az-files-02

 

So that is the magic that we’ll use to create a shared volume… Now let’s deploy this shall we?

What happened here? Our service was deployed. Everything went fine. And you’ll also notice that ACS (Azure Container Service) went back to Azure and created the loadbalancer & external ip!

How cool is that?!? Now let’s check the storage account… and we’ll see that wordpress has started adding files.

Checking our Kubernetes dashboard, the deployment looks good too…

And finally, the WordPress installation is showing on our exposed IP!

 

Closing Thoughts

I’ve often said that Azure Storage is often given the credit it deserves. It’s a service that truly provides you with a lot of flexibility in creating solutions.

Advertisements

3 thoughts on “Azure Container Service : Using the Azure File Storage as a persistent (kubernetes) volume

  1. What happens when you stop you deployment and delete the PVC? Will the persistent storage/volume remain?

    If I then wanted to restart the deployment, how can I ensure it reuses the same persistent storage?

    I’m interested in the maintaining my data between Kubernetes cluster restarts.

      1. I think Stateful sets is what I really want. Their use of PVs match how I want to use them. Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s