Remediate your Azure Kubernetes Service clusters at scale using Azure Policy’s mutation support!

  • Thread starter Thread starter NehaKulkarni
  • Start date Start date
N

NehaKulkarni

We are thrilled to announce Azure Policy for Kubernetes Public Preview support for Mutation, available for Azure Kubernetes Service clusters! This release is brought to you in collaboration with Azure Kubernetes Service and Azure Container Compute Upstream.



Gatekeeper's mutation capability enables users to change Kubernetes resources at create/update time based on different criteria defined in mutation templates. By embedding mutation templates in your Azure Policy custom definitions, Azure Policy extends Gatekeeper’s mutation feature to programmatically modify components within your Kubernetes clusters at-scale across your subscriptions and management groups, with the help of various Azure Policy features, like selectors and overrides. For an overview of Azure Policy capabilities for Kubernetes, go to Azure Policy for Kubernetes.



When using mutation, you can do things like:

  • Ensure all pods in a given scope have imagePullPolicy set
  • Setting security context of containers in a selected scope to be non-privileged
  • Adding a sidecar to a Pod

Get started

Here’s an example of a simple Gatekeeper mutation template, pulled from Gatekeeper documentation. It is annotated to highlight a few things:

large?v=v2&px=999.jpg

Note: Azure Policy for Kubernetes does not currently support Gatekeeper’s ‘AssignImage’ mutator



You can choose to author your own mutation templates to embed in Azure Policy definitions, or you can use a sample from an existing repository, such as the Gatekeeper library.



Note: you will need to register the mutation feature on subscription(s) that you’d like to assign mutation policies within. The mutation feature’s resource ID is: Microsoft.Features/providers/Microsoft.ContainerService/features/AKS-AzurePolicyMutation

For more information, go to: Set up preview features in Azure subscription - Azure Resource Manager | Microsoft Learn.



When crafting a mutation policy definition, use the ‘mutationInfo’ property to embed your mutation template in your JSON definition. To learn more, go to: Understand how effects work - Azure Policy | Microsoft Learn



You can apply mutation policies similarly to how you apply other Azure Policies today (such as via API, Portal and PowerShell). This example will walk you through applying a mutation definition through the Azure Portal.



Azure Portal

  1. Go to Azure Policy’s Portal view by searching ‘Policy’. Next, click the ‘Definitions’ tab to create a new definition.

large?v=v2&px=999.jpg

  1. Create your Azure custom policy with a mutation template embedded. Ensure that you are using mode = “Microsoft.Kubernetes.Data”. Note that the “mutate” effect only works with mutation templates, not constraint templates.
  2. Assign your policy definition to the scope of your choice (such as your management group, subscription or resource group)

large?v=v2&px=999.jpg



This definition will then apply to all clusters in that scope! You can choose to leverage exclusions or Azure Policy’s exemptions if certain clusters should not be mutated by this policy assignment.



Your work is done! You can navigate to the ‘Compliance’ view to see all the clusters in scope for this mutation policy assignment.



If you run into issues, feel free to navigate to our troubleshooting tools in ‘Related Resources’. Try it out, and we would love to hear what you think in the comments below!



For related resources, go to:


Continue reading...
 
Back
Top