Share via


Azure Disk tips for Kubernetes

When I need a Persistent Volume Claim, I always use AzureDisk. However, it was complicated.
Before creating a persistent volume claim, I needed to create Storage Account and upload VHD on it.

azuredisk

But there is a good way to create a persistent volume using AzureDisk without creating Storage Account in Advance. Until Now, there is not mentioned on the official website.  The document is for v1. I share it on this blog post.

1. Create a Storage Class

Let's create a storage class. Please use storage.k8s.io/v1beta1. You can see the spec in here
https://github.com/kubernetes/kubernetes/blob/master/docs/api-reference/storage.k8s.io/v1beta1/definitions.html

Also, you need to use the same location as your k8s cluster.

 kind: StorageClass
apiVersion: storage.k8s.io/v1beta1
metadata:
 name: slow
provisioner: kubernetes.io/azure-disk
parameters:
 skuName: Standard_LRS
 location: japaneast

2. Persistent volume claim

You need to add the annotation to the Persistent Volume Claim definition. You don't need to specify storageClassName on spec. I guess this notation might be changed after releasing k8s 1.6 on Azure.

 kind: PersistentVolumeClaim

apiVersion: v1
metadata:
  name: myclaim
  annotations: 
    volume.beta.kubernetes.io/storage-class: slow
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 8Gi

3. Create a pod

Now we have no secret for creating a pod using the Azure disk. Enjoy.

 kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: mysql
  labels:
    app: wordpress
spec:
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.6
        ports:
          - containerPort: 3306
            protocol: TCP
            name: mysql
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: mysqlvolume
        env:
        - name: "MYSQL_ROOT_PASSWORD"
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        - name: "MYSQL_DATABASE"
          value: "sample"
        - name: "MYSQL_USER"
          value: "mysql"
        - name: "MYSQL_PASSWORD"
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
      volumes:
      - name: mysqlvolume
        persistentVolumeClaim:
          claimName: mysql-pv