Azure Capacity Reservations with Automatic Consumption
Solving the ask – Automatic Capacity Reservations
Historically, the setting to use a Capacity Reservations Groups must be defined while the virtual machine is being deployed or changed while the machine is deallocated. While this is still the requirement, the drafted Azure Policy (at the bottom of this document) automates the additional setting to be specified even if it wasn’t specified on the VM Creation process.
For example, some solutions, such as Citrix DaaS hosted on Azure, use automation such as recreation of VMs to handle update management or scalability demands. The update process doesn’t have the ability to add this setting without code changes to their process which requires a significant investment of time per cloud provider to update. If this capacity isn’t available for any reason, the scale out or recreation could fail causing the environment to become unhealthy.
What are Azure Capacity Reservations?
Azure Capacity Reservations are a new feature that allows you to reserve compute resources for your virtual machines (VMs) in a specific region and/or availability zone. By using Azure Capacity Reservations, you can ensure that your VMs have guaranteed access to the resources they need, even during periods of high demand or unexpected events.
Reserved Virtual Machine Instance (RI) to on-demand Capacity Reservation work together. You can apply existing or future RIs to on-demand capacity reservations and receive RI discounts. Available RIs are applied automatically to Capacity Reservation the same way they’re applied to VMs.
Azure Capacity Reservation Group (CRG) is a grouping of Capacity Reservations. VM creation targets the CRG and not the specific Capacity Reservation.
How to create Azure Capacity Reservations?
To get started with Azure Capacity Reservations, you need to follow these steps:
Create a capacity reservation group: A reservation group is a logical container that holds one or more reservations. You can assign VMs to a reservation group to use the reserved resources.
Create a capacity reservation: A reservation is a specific amount of compute resources that you reserve for a certain VM size, region, and availability zone. You can create multiple reservations within a reservation group.
How do I use the Capacity Reservations?
To deploy your virtual machines to the capacity that is created, you need to follow either of these options:
Assign VMs to a reservation group: You can assign existing deallocated VMs or new VMs to a reservation group, either individually or as part of a scale set. The VMs will use the reserved resources from the reservation group, as long as they match the reservation criteria.
Leverage Azure Policy (New as of July 2024):
Using Azure policy, you can deploy a policy per subscription that specifying:
VM SKU
Region
Target Capacity Reservation
Is this a zonal deployment
When VMs are created, this policy will automatically add the virtual machine to the Azure Capacity Reservation Group.
How do I use Azure Policy to set the Capacity Reservation?
With the below parameters set via policy per subscription, VMs of the desired SKU and availability will automatically be added based on:
Region
SKU
Multiple SKUs can be targeted with the policy
Zonal – yes or no
Capacity Reservation ID
The Capacity reservation targeted with the policy will need to match the zonal decision or VM Creations will fail.
Using this functionality, it is now possible to specify a minimum quantity of VM Compute that is guaranteed to be available.
What does this look like in production?
Create Capacity Reservation Group with correct quantities
Create the policy definition
Assign the policy to have VMs automatically added
Create Virtual Machines
Create Capacity Reservations with correct quantities
Create the Capacity Reservation Group to hold the specific Capacity Reservations based on regional or zonal alignment. For each SKU, create a capacity reservation with the minimum amount of compute you wish to be reserved per region or zone. Capacity Reservations can be overallocated. If you reserve 3 nodes of E2s_v4 in zone 1, but attempt to deploy 10 VMs, you are guaranteed to have the 3 available for your consumption and the remaining 7 are subject to normal availability within the zone.
In this example, I have created a Capacity Reservation Groups for regional and another for zonal alignment:
Regional: CRGRG1-WUS3-r
Zonal: CRGRG1-WUS3-z
Create the policy definition
To simplify the experience, I have created a single Azure Policy that is able to be referenced for both scenarios.
An example copy of the policy can be found at the bottom of this article.
Assign the policy to have VMs automatically added
Based on the assignment parameters below:
This example for regional:
allowedLocations
westus3
CapacityReservationID
/subscriptions/########-####-####-####-############/resourceGroups/CRGRG/providers/Microsoft.Compute/capacityReservationGroups/CRGRG1-WUS3-r
listOfCRGSKUs
Standard_E2s_v4
Zonal
False
Based on the assignment parameters below (this example for zonal):
allowedLocations
westus3
CapacityReservationID
/subscriptions/########-####-####-####-############/resourceGroups/CRGRG/providers/Microsoft.Compute/capacityReservationGroups/CRGRG1-WUS3-z
listOfCRGSKUs
Standard_E2s_v4
Zonal
True
Create Virtual Machines within the specified context
Based on the above, if a VM is created with the SKU E2s_v4 in availability zone 1, it will automatically be added to CRGRG1-WUS3-z.
Based on the above, if I create a VM with the SKU E2s_v4 without specifying an availability zone, it will automatically be added to CRGRG1-WUS3-r.
The above Azure Policy and assignments added this line of code in the VM Creation template.
“type”: “Microsoft.Compute/virtualMachines”,
“location”: “westus3”,
“properties”: {
“capacityReservation”: {
“capacityReservationGroup”: {
“id”: “/subscriptions/########-####-####-####-############/resourceGroups/CRGRG/providers/Microsoft.Compute/capacityReservationGroups/CRGRG1-WUS3-z”
}
}
An example copy of the policy can be found below:
The below policy is a draft and is based on new functionality. It should not be treated as production ready unless deemed so by the appropriate resources at your organization.
{
“mode”: “All”,
“policyRule”: {
“if”: {
“allOf”: [
{
“field”: “type”,
“equals”: “Microsoft.Compute/virtualMachines”
},
{
“field”: “location”,
“in”: “[parameters(‘allowedLocations’)]”
},
{
“field”: “Microsoft.Compute/virtualMachines/sku.name”,
“in”: “[parameters(‘listOfCRGSKUs’)]”
},
{
“field”: “Microsoft.Compute/virtualMachines/zones”,
“exists”: “[parameters(‘Zonal’)]”
},
{
“field”: “Microsoft.Compute/virtualMachines/capacityReservation.capacityReservationGroup.id”,
“notequals”: “[parameters(‘CapacityReservationID’)]”
}
]
},
“then”: {
“effect”: “modify”,
“details”: {
“roleDefinitionIds”: [
“/providers/Microsoft.Authorization/roleDefinitions/9980e02c-c2be-4d73-94e8-173b1dc7cf3c”
],
“operations”: [
{
“operation”: “addOrReplace”,
“field”: “Microsoft.Compute/virtualMachines/capacityReservation.capacityReservationGroup.id”,
“value”: “[parameters(‘CapacityReservationID’)]”
}
]
}
}
},
“parameters”: {
“allowedLocations”: {
“type”: “Array”,
“metadata”: {
“displayName”: “Specific Region that is covered under this CRG”,
“description”: “The list of locations that resource groups can be created in.”,
“strongType”: “location”
}
},
“listOfCRGSKUs”: {
“type”: “Array”,
“metadata”: {
“displayName”: “Allowed Size SKUs”,
“description”: “The list of size SKUs that can be specified for virtual machines.”,
“strongType”: “VMSKUs”
}
},
“CapacityReservationID”: {
“type”: “String”,
“metadata”: {
“displayName”: “Capacity Reservation ID”,
“description”: “The capacity reservation targets this policy to target. Resource ID should be formatted as: ‘/subscriptions/{SubID}/resourceGroups/{RGName}/providers/Microsoft.Compute/capacityReservationGroups/{CRG Name}.'”
}
},
“Zonal”: {
“type”: “String”,
“metadata”: {
“displayName”: “Is this Zonal?”,
“description”: “If this is Zonal, it will attempt to add to the Zonal Capacity Reservation Groups. If not, it will add to the reginoal Capacity Reservation Groups.”
},
“allowedValues”: [
“true”,
“false”
]
}
}
}
Microsoft Tech Community – Latest Blogs –Read More