1. Disk Attachment for Stateful Sets in Azure Kubernetes


    When working with stateful applications on Kubernetes, especially on a managed service like Azure Kubernetes Service (AKS), it's common to have a need for persistent storage. This is where concepts like Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) come into play. These Kubernetes resources are used to provision storage for stateful sets and ensure that data persists across pod restarts and rescheduling.

    In Azure, disks can be attached as volumes to the pods running in AKS. This typically involves defining an Azure Disk as a PersistentVolume (PV), and then claiming storage from it with a PersistentVolumeClaim (PVC) within your Kubernetes cluster.

    Below, I will show you how to use Pulumi to provision an Azure Managed Disk and then how to configure it for use with Kubernetes as a PV and a PVC, which will be consumed by a stateful set.

    First, we will declare an Azure Managed Disk using the Disk resource from Pulumi's Azure Native provider. Then, we'll create the corresponding Kubernetes Persistent Volume (PV) and Persistent Volume Claim (PVC) that will make use of the Azure Disk.

    Here's a full Pulumi program that accomplishes this:

    import pulumi import pulumi_kubernetes as k8s from pulumi_azure_native import compute from pulumi_azure_native import resources # Configure the Azure Native Provider to use the desired subscription, # resource group, and location for your resources. subscription_id = 'your-subscription-id' resource_group_name = 'your-resource-group' location = 'your-location' # Create a new resource group if it doesn't exist resource_group = resources.ResourceGroup('my-resource-group', resource_group_name=resource_group_name, location=location) # Create an Azure Managed Disk managed_disk = compute.Disk("my-managed-disk", resource_group_name=resource_group.name, location=resource_group.location, creation_data=compute.CreationDataArgs( create_option=compute.DiskCreateOption.Empty ), disk_size_gb=10) # Create a Persistent Volume using the Azure Managed Disk managed_disk_pv = k8s.core.v1.PersistentVolume("my-managed-disk-pv", metadata=k8s.meta.v1.ObjectMetaArgs( name="my-managed-disk-pv" ), spec=k8s.core.v1.PersistentVolumeSpecArgs( capacity={"storage": "10Gi"}, access_modes=["ReadWriteOnce"], azure_disk=k8s.core.v1.AzureDiskVolumeSourceArgs( kind="Managed", disk_name=managed_disk.name, disk_uri=managed_disk.id, caching_mode="None" ), persistent_volume_reclaim_policy="Retain" )) # Create a Persistent Volume Claim to attach the Persistent Volume to pods managed_disk_pvc = k8s.core.v1.PersistentVolumeClaim("my-managed-disk-pvc", metadata=k8s.meta.v1.ObjectMetaArgs( name="my-managed-disk-pvc" ), spec=k8s.core.v1.PersistentVolumeClaimSpecArgs( access_modes=["ReadWriteOnce"], storage_class_name="", # Use Azure default storage class resources=k8s.core.v1.ResourceRequirementsArgs( requests={"storage": "10Gi"} ), volume_name=managed_disk_pv.metadata.name )) # Export the Azure Disk URI to access it later pulumi.export('disk_uri', managed_disk.id)

    In the code above:

    • We declare a resource group my-resource-group to organize the resources we provision.
    • The compute.Disk resource, named my-managed-disk, represents an Azure Managed Disk with a specified size (10 GB) in the given resource group and location.
    • The k8s.core.v1.PersistentVolume resource is defined with the azure_disk argument, which is configured with the necessary details from the managed_disk.
    • The k8s.core.v1.PersistentVolumeClaim resource, named my-managed-disk-pvc, is used to claim the storage represented by the persistent volume in our Kubernetes cluster. This PVC can then be used by workloads within the cluster by referencing it in the pod's volume configuration.
    • Finally, we export the disk URI to be used or referenced elsewhere.

    With the above Pulumi program, you can provision the underlying Azure infrastructure and Kubernetes resources necessary for stateful applications that require persistent storage. These resources can then be referenced in your stateful set manifest to ensure that each pod gets its own persisted disk space.

    Remember that you need to replace placeholder values like 'your-subscription-id', 'your-resource-group', and 'your-location' with your actual Azure subscription ID, desired resource group name, and location, respectively. Also, ensure that you have the Pulumi CLI set up with the appropriate credentials to communicate with your Azure account and Kubernetes cluster.