About Me

My photo
I am MCSE in Data Management and Analytics with specialization in MS SQL Server and MCP in Azure. I have over 13+ years of experience in IT industry with expertise in data management, Azure Cloud, Data-Canter Migration, Infrastructure Architecture planning and Virtualization and automation. Contact me if you are looking for any sort of guidance in getting your Infrastructure provisioning automated through Terraform. I sometime write for a place to store my own experiences for future search and read by own blog but can hopefully help others along the way. Thanks.

Difference between Immediate binding mode and External binding mode in AKS

 In Azure Kubernetes Service (AKS), a Persistent Volume (PV) represents a physical storage resource in the cluster, while a Persistent Volume Claim (PVC) represents a request for storage by a pod. When a pod requests storage through a PVC, it is dynamically bound to an available PV that matches the storage requirements specified in the PVC.

There are three volume binding modes that can be used in AKS:

Immediate binding mode: This is the default binding mode, where the PVC is immediately bound to an available PV that matches its storage requirements. If no matching PV is available, the PVC will remain in the pending state until a suitable PV becomes available.

WaitForFirstConsumer binding mode: In this mode, the PV is not bound to the PVC until a pod using the PVC is created. This ensures that the PV is not wasted if the pod never gets created.

External binding mode: This mode allows you to pre-provision a PV that is not managed by the AKS cluster, and then bind it to a PVC in the cluster. This is useful if you have an existing storage resource that you want to use with your AKS cluster.

In summary, the main difference between the volume binding modes in PV and PVC of AKS is how the PV and PVC are bound to each other. Immediate binding mode binds the PVC to an available PV right away, WaitForFirstConsumer binding mode waits until a pod using the PVC is created, and External binding mode allows you to pre-provision a PV outside of the cluster and bind it to a PVC.


1.Example of  WaitForFirstConsumer

in WaitForFirstConsumer binding mode, a PV is only bound to a PVC when a pod that uses the PVC is scheduled to a node. This means that the PV is not provisioned until it is actually needed by a pod. This mode is useful when you want to conserve resources and only provision storage when it is actually required by a pod.


Here is an example of using WaitForFirstConsumer binding mode in AKS:


Create a PV with the following YAML definition:


apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  storageClassName: slow
  azureDisk:
    kind: Managed
    diskName: my-disk
    diskURI: /subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.Compute/disks/my-disk
  volumeMode: Filesystem
  persistentVolumeReclaimPolicy: Delete
  mountOptions:
  - debug

This creates a PV with 10GB storage capacity, with the storage being provided by an Azure Managed Disk named "my-disk". Note that we are using the storageClassName field to specify a storage class of "slow", and the persistentVolumeReclaimPolicy field to specify that the PV should be deleted when the PVC is deleted.


Create a PVC with the following YAML definition:


apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: slow
  volumeMode: Filesystem
  volumeBindingMode: WaitForFirstConsumer

This creates a PVC with 5GB storage capacity, requesting storage from a PV with storage class "slow" and the same volumeMode as the PV, which is Filesystem. Note that we are using the volumeBindingMode field to specify WaitForFirstConsumer binding mode.


Deploy a stateful application that uses the PVC for storage:


apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: my-app
spec:
  serviceName: my-app
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: my-image:latest
        volumeMounts:
        - name: my-volume
          mountPath: /data
      volumes:
      - name: my-volume
        persistentVolumeClaim:
          claimName: my-pvc
          volumeMode: Filesystem

This deploys a stateful application with one replica and a container named "my-container". The container uses a volume mount to mount the PVC at the path /data. Note that we are using the same PVC that we created earlier, and we are specifying the volumeMode field in the `persistent


2.Example of ImmediateBindings


here's an example of using Immediate binding mode in AKS:

Create a PV with the following YAML definition:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  storageClassName: fast
  azureDisk:
    kind: Managed
    diskName: my-disk
    diskURI: /subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.Compute/disks/my-disk
  volumeMode: Filesystem
  persistentVolumeReclaimPolicy: Delete
  mountOptions:
  - debug

This creates a PV with 10GB storage capacity, with the storage being provided by an Azure Managed Disk named "my-disk". Note that we are using the storageClassName field to specify a storage class of "fast", and the persistentVolumeReclaimPolicy field to specify that the PV should be deleted when the PVC is deleted.


Create a PVC with the following YAML definition:


apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: fast
  volumeMode: Filesystem
  volumeBindingMode: Immediate

This creates a PVC with 5GB storage capacity, requesting storage from a PV with storage class "fast" and the same volumeMode as the PV, which is Filesystem. Note that we are using the volumeBindingMode field to specify Immediate binding mode.


Deploy a stateful application that uses the PVC for storage:


apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: my-app
spec:
  serviceName: my-app
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: my-image:latest
        volumeMounts:
        - name: my-volume
          mountPath: /data
      volumes:
      - name: my-volume
        persistentVolumeClaim:
          claimName: my-pvc
          volumeMode: Filesystem

This deploys a stateful application with one replica and a container named "my-container". The container uses a volume mount to mount the PVC at the path /data. Note that we are using the same PVC that we created earlier, and we are specifying the volumeMode field in the persistentVolumeClaim object to be Filesystem, which matches the volumeMode of the PV.


Since we are using Immediate binding mode, the PV is immediately bound to the PVC as soon as the PVC is created. This means that the PV is provisioned and reserved for the PVC even if the PVC is not yet used by any pod.

No comments: