Understanding the New AKS Deployment Safeguards (2024)

Introduction

Last week at Build, we've introduced a new feature in Public Preview for Azure Kubernetes Service (AKS) called Deployment Safeguards.

Deployment Safeguards, as a part of Azure Policy for AKS, provides a configurable and fast way to make sure your Kubernetes deployment follow through best practices and limits that are set beforehand. In this article, we will explore how it works in real time and how can we use it to tailor AKS to your needs.

Playground Setup

For the sake of this article, I'll create a new cluster from scratch.

There are a few things we need to set up first.

For my test environment, I'm running these commands on WSL/Ubuntu with local Azure CLI.

If you're not logged in, executeaz loginand then choose the right subscription,

If you're using Azure CLI with Login Experience v2, you can just choose the subscription from the drop-down, and disregard the second command:

az loginaz account set -s "your-subscription-id"

AKS Deployment Safeguard is currently a preview feature, so we'll need to make sure our AKS extension is up-to-date:

az extension add --name aks-previewaz extension update --name aks-preview

Next, register the feature flag of Deployment Safeguards -

az feature register --namespace Microsoft.ContainerService --name SafeguardsPreview

This will take a couple of minutes; the end result should show asRegistered:

Understanding the New AKS Deployment Safeguards (1)

Next, refresh theMicrosoft.ContainerServiceresource provider so changes will be applied:

az provider register --namespace Microsoft.ContainerService

Create a new test resource group and AKS cluster -

az group create --name safeguard-test --location eastusaz aks create --name safeaks --resource-group safeguard-test --node-count 2 --location eastus --enable-addons azure-policy --safeguards-level Warning --safeguards-version v2.0.0 

This will create a new Azure Kubernetes Service (AKS) cluster, with 2 nodes.

This cluster will have theAzure Policy for AKS add-on enabled with Safeguard level set toWarningand version set to 2.0.0.

Node count is set to 2 to allow for faster creation and a bit of redundancy.

I have created 2 clusters, one with Safeguards level set toWarningand one withEnforcement.

I have set the safeguard level to Warning for the first cluster as we wish to experiment with it, Warning will notify us that a resource/yaml is out of policy but won't block it.

Setting safeguard level toEnforcementwill automatically block resource files that do not adhere to the safeguards that were set, and will change the ones it can change to adhere to those policies, instead of blocking them.

You can enable Deployment Safeguards on an existing cluster usingaz aks update:

az aks update --name clustername --resource-group resourcegroup --safeguards-level Warning --safeguards-version v2.0.0

You can change a cluster's safeguards level fromWarningto Enforcementand vice-versa also usingaz aks update:

az aks update --name safeaks --resource-group safeguard-test --safeguards-level Enforcement

If you wish to turn off Deployment Safeguards completely:

az aks update --name safeaks --resource-group safeguard-test --safeguards-level Off

That should wrap it up for the prerequisites.

Deployment Safeguards in Action

After the cluster is created, please allow at least 30 minutes for Deployment Safeguards and Azure Policy for AKS to successfully sync.

If you've followed with the new cluster creation, set kubectl to the new cluster context by using:

az aks get-credentials --name safeaks --resource-group safeguard-test

Let's runkubectl get nodes -o widejust to verify connectvitiy -

kubectl get nodes -o wide

Output should look like this:

Understanding the New AKS Deployment Safeguards (2)

Testing Deployment Safeguards

While the entirety of available safeguard policies is listed here,

We will focus onResource Limits Enforcement, together with a few others which I'll explain below.

Testing Deployment Safeguards

Let's create a normal pod that runs an Nginx image, without any special configuration, and save it as no-limits.yaml:

apiVersion: v1kind: Podmetadata: name: no-limits-herespec: containers: - name: nginx image: nginx

Let's apply it to ourWarninglevel cluster and see what happens, using kubectl apply:

kubectl apply -f no-limits.yaml

We're immediately presented with the following output:

Understanding the New AKS Deployment Safeguards (3)

Let's break it down:

Deployment Safeguards expects a liveness and a readiness probe,resource limits, and an image pull secret.

But, since it's set onWarning, it allows the manifest to go through.

In a cluster where safeguards level is set toEnforcement,the pod is blocked from being scheduled:

Understanding the New AKS Deployment Safeguards (4)

Let's "fix" our pod to adhere to some of the policies, but let's keep it withoutresource limits:

apiVersion: v1kind: Secretmetadata: name: registrykey namespace: defaultdata: .dockerconfigjson: >- eyJhdXRocyI6eyJodHRwczovL215LXNlY3VyZS1yZWdpc3RyeS5jb20iOnsidXNlcm5hbWUiOiJkb2NrZXIt dXNlciIsInBhc3N3b3JrIjoic2VjdXJlLXBhc3N3b3JkIiwiZW1haWwiOiJ1c2VyQGV4YW1wbGUuY29tIn19fQ==type: kubernetes.io/dockerconfigjson---apiVersion: v1kind: Podmetadata: name: no-limits-herespec: containers: - name: nginx image: my-awesome-registry.com/nginx:latest readinessProbe: httpGet: path: / port: 80 initialDelaySeconds: 5 periodSeconds: 5 livenessProbe: httpGet: path: / port: 80 initialDelaySeconds: 10 periodSeconds: 10 imagePullSecrets: - name: registrykey

This "fixed" pod now adheres to the readiness and liveness probe safeguards, adds a pseudo pullsecret, but does not adhere to the resource limits safeguard.

Important Note - This is a pseudo dockerconfigjson, key and of course, container registry. The container will not run. It's on purpose.

Let's save this in a new file called no-limits-updated.yaml,and apply it to the Enforcementcluster:

kubectl apply -f no-limits-updated.yaml

We're presented with the following output:

Understanding the New AKS Deployment Safeguards (5)

Kubernetes is not happy with our dummy secret. That's fine. Let's explore and see what happened to our pod.

Our pod did not run [as implied above] but Deployment Safeguards has made changes to it, specifically on the Limits and Requests part.

Let's query it and see what happened:

kubectl get pod no-limits-here -o=jsonpath='{.spec.containers[*].resources}'

You should see the following:

{"limits":{"cpu":"500m","memory":"500Mi"},"requests":{"cpu":"500m","memory":"500Mi"}}

Deployment Safeguards has made our pod adhere to the Limits and Requests section, even without us specifying it.

This is done on the Enforcement level to make sure your workload is aligned with the limits and requests safeguard.

The change happened because Limits and Requests areeligible for mutation.

Other policies that are currently available with mutations are:

  • Reserved System Pool Taints
  • Pod Disruption Budget
  • ReadOnlyRootFileSystem
  • RootFilesystemInitContainers

Deployment Safeguards will edit and change your workload to align with these safeguards.

On all other safeguards that are not eligible for mutation, the workload will be rejected on anEnforcement cluster.

You can also exclude a certain namespace from being enforced by Deployment Safeguards using:

az aks update --name safeaks --resource-group safeguard-test --safeguards-level Warning --safeguards-version v2.0.0 --safeguards-excluded-ns myawesomenamespace

Clean up the resources:

az aks delete --name safeaks --resource-group safeguard-test --yesaz group delete --name safeguard-test

Conclusion

Azure Kubernetes Service's Deployment Safeguards feature is a robust tool that ensures Kubernetes deployments adhere to best practices and predefined limits. With options for both Warning and Enforcement levels, users can either be alerted of non-compliance or have their deployments automatically adjusted to meet the required standards. This feature enhances security and operational efficiency, making AKS an even more reliable and user-friendly platform for managing containerized applications.

Understanding the New AKS Deployment Safeguards (2024)

FAQs

Understanding the New AKS Deployment Safeguards? ›

Deployment Safeguards, as a part of Azure Policy for AKS, provides a configurable and fast way to make sure your Kubernetes deployment follow through best practices and limits that are set beforehand.

How do you protect AKS? ›

The essential 8 AKS security best practices
  1. Integrate with Microsoft Entra ID.
  2. Configure cluster security.
  3. Implement pod security and credential protection.
  4. Implement namespace isolation.
  5. Deploy secure container images.
  6. Enhance network security through CNI and network policies.
  7. Integrate Azure Key Vault for secrets management.
Jan 26, 2024

What is AKS gatekeeper? ›

An Azure Policy add-on for AKS lets you install a managed instance of Gatekeeper, an open-source Kubernetes admission controller. Gatekeeper checks each request that involves creating or updating a resource. Initiatives—an initiative is a set of policies that support an organization's compliance goals.

What does Aks do? ›

AKS offers the quickest way to start developing and deploying cloud-native apps in Azure, datacenters, or at the edge, with built-in code-to-cloud pipelines and guardrails. As a hosted Kubernetes service, Azure handles critical tasks, like health monitoring and maintenance.

What are the advantages of Azure Kubernetes service? ›

AKS provides features such as automatic scaling, self-healing, and rolling updates, which help ensure that applications are always available and up-to-date. Another advantage of AKS is its high availability. AKS uses multiple nodes in different availability zones, ensuring the Kubernetes cluster is always available.

What are limitations of AKS? ›

Heavy AKS activity can cause default API rate limits to trigger, causing throttling and eventually failures for AKS clusters.

How do I know if my AKS is RBAC enabled? ›

The best way to check for AKS is to check the cluster's resource details, e.g. at resources.azure.com. If "enableRBAC": true, your cluster has RBAC enabled. Existing non-RBAC enabled AKS clusters cannot currently be updated for RBAC use.

What is the alternative to gatekeeper in Kubernetes? ›

Alternatives. Whilst OPA Gatekeeper's Rego allows for very flexible validation policies, its complexity can be unintuitive compared to its main competitor Kyverno which was specifically built for Kubernetes.

What are the two methods used by AKS to access the key vault? ›

There are two methods for authentication; select the one which applies to you:
  • 4A - Managed Identity (recommended) OR.
  • 4B - Service Principal. Use this method only if Managed Identities are not available or not desired in your cluster.

What is opa gatekeeper in Kubernetes? ›

Gatekeeper is a Kubernetes-native admission controller that extends the capabilities of OPA to Kubernetes clusters. By combining OPA's policy engine with Kubernetes' admission control mechanism, Gatekeeper enforces policies on Kubernetes resources during creation and update operations.

What is the difference between AKS and Kubernetes? ›

Kubernetes cluster—a cluster running your workloads, deployed on AKS. With AKS you only manage agent nodes; AKS assumes responsibility for the Kubernetes control plane. Virtual network—AKS creates a virtual network in which agent nodes can be deployed.

What is the difference between AKS service and deployment? ›

Deployments ensure the desired state of your application, handling the lifecycle and scaling, while Services act as the gateway for accessing your application, enabling seamless communication and load balancing. Kubernetes Deployments play a crucial role in managing and scaling containerized applications.

Are AKS secrets encrypted? ›

In the Kubernetes API server, secrets are stored in etcd, which is a highly available key values store used as the Kubernetes backing store for all cluster data. AKS Arc comes with encryption of etcd secrets and automates the management and rotation of encryption keys.

What is the difference between deployment and service Kubernetes? ›

Kubernetes Service vs Deployment

What's the difference between a Service and a Deployment in Kubernetes? A deployment is responsible for keeping a set of pods running. A service is responsible for enabling network access to a set of pods.

What is a container in Aks? ›

Azure Kubernetes Service (AKS) is a managed Kubernetes service that you can use to deploy and manage containerized applications. You need minimal container orchestration expertise to use AKS. AKS reduces the complexity and operational overhead of managing Kubernetes by offloading much of that responsibility to Azure.

What is the main advantage of using deployments in Kubernetes? ›

Kubernetes helps DevOps teams work more efficiently, too. With Kubernetes, DevOps engineers can build, test, and deploy microservices apps in the same platform. This benefit eliminates the headache and risk that occur when apps developed in one platform have to move to a different one for production.

How do I protect my Azure network? ›

Use Azure Private Link to access Azure PaaS Services (for example, Azure Storage and SQL Database) over a private endpoint in your virtual network. Private Endpoints allow you to secure your critical Azure service resources to only your virtual networks.

How do I make my AKS private? ›

There are several options for establishing network connectivity to the private cluster.
  1. Create a virtual machine in the same Azure Virtual Network (VNet) as the AKS cluster.
  2. Use a virtual machine in a separate network and set up Virtual network peering. ...
  3. Use an Express Route or VPN connection.
Jan 2, 2024

What is needed to secure the cluster created on Azure? ›

Use Microsoft Entra ID and Kubernetes role-based access control (Kubernetes RBAC) to secure API server access. Secure container access to node resources. Upgrade an AKS cluster to the latest Kubernetes version. Keep nodes up to date and automatically apply security patches.

How do I protect my Azure account? ›

  1. Protect your Azure Active Directory account with MFA. ...
  2. Enable conditional access or make sure you enable the security defaults on your free Azure AD tenant. ...
  3. Use just-in-time access for tasks that require higher privileges. ...
  4. Use managed identities where possible. ...
  5. Store secrets, keys and certificates in Azure Key Vault.
Jun 22, 2023

Top Articles
Latest Posts
Article information

Author: Msgr. Refugio Daniel

Last Updated:

Views: 6018

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Msgr. Refugio Daniel

Birthday: 1999-09-15

Address: 8416 Beatty Center, Derekfort, VA 72092-0500

Phone: +6838967160603

Job: Mining Executive

Hobby: Woodworking, Knitting, Fishing, Coffee roasting, Kayaking, Horseback riding, Kite flying

Introduction: My name is Msgr. Refugio Daniel, I am a fine, precious, encouraging, calm, glamorous, vivacious, friendly person who loves writing and wants to share my knowledge and understanding with you.