Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This article demonstrates how to set up L7 policies with Advanced Container Networking Services in AKS clusters. Continue only after you have reviewed the limitations and considerations listed on the Layer 7 Policy Overview page.
Prerequisites
- An Azure account with an active subscription. If you don't have one, create a free account before you begin.
Use the Bash environment in Azure Cloud Shell. For more information, see Get started with Azure Cloud Shell.
If you prefer to run CLI reference commands locally, install the Azure CLI. If you're running on Windows or macOS, consider running Azure CLI in a Docker container. For more information, see How to run the Azure CLI in a Docker container.
If you're using a local installation, sign in to the Azure CLI by using the az login command. To finish the authentication process, follow the steps displayed in your terminal. For other sign-in options, see Authenticate to Azure using Azure CLI.
When you're prompted, install the Azure CLI extension on first use. For more information about extensions, see Use and manage extensions with the Azure CLI.
Run az version to find the version and dependent libraries that are installed. To upgrade to the latest version, run az upgrade.
The minimum version of Azure CLI required for the steps in this article is 2.71.0. Run az --version
to find the version. If you need to install or upgrade, see Install Azure CLI.
Install the aks-preview
Azure CLI extension
Install or update the Azure CLI preview extension using the az extension add
or az extension update
command.
The minimum version of the aks-preview Azure CLI extension is 14.0.0b6
# Install the aks-preview extension
az extension add --name aks-preview
# Update the extension to make sure you have the latest version installed
az extension update --name aks-preview
Register the AdvancedNetworkingL7PolicyPreview
feature flag
Register the AdvancedNetworkingL7PolicyPreview
feature flag using the az feature register
command.
az feature register --namespace "Microsoft.ContainerService" --name "AdvancedNetworkingL7PolicyPreview"
Verify successful registration using the az feature show
command. It takes a few minutes for the registration to complete.
az feature show --namespace "Microsoft.ContainerService" --name "AdvancedNetworkingL7PolicyPreview"
Once the feature shows Registered
, refresh the registration of the Microsoft.ContainerService
resource provider using the az provider register
command.
Enable Advanced Container Networking Services
To proceed, you must have an AKS cluster with Advanced Container Networking Services enabled.
The az aks create
command with the Advanced Container Networking Services flag, --enable-acns
, creates a new AKS cluster with all Advanced Container Networking Services features. These features encompass:
Container Network Observability: Provides insights into your network traffic. To learn more visit Container Network Observability.
Container Network Security: Offers security features like Fully Qualified Domain Name (FQDN) filtering. To learn more visit Container Network Security.
Note
Clusters with the Cilium data plane support Container Network Observability and Container Network security starting with Kubernetes version 1.29.
For this demo, the --acns-advanced-networkpolicies
parameter must be set to "L7" to enable L7 policies. Setting this parameter to "L7" also enables FQDN filtering. If you only want to enable FQDN filtering, set the parameter to "FQDN". To disable both features, you can follow the instructions provided in Disable Container Network Security.
export CLUSTER_NAME="<aks-cluster-name>"
# Create an AKS cluster
az aks create \
--name $CLUSTER_NAME \
--resource-group $RESOURCE_GROUP \
--generate-ssh-keys \
--network-plugin azure \
--network-dataplane cilium \
--enable-acns \
--acns-advanced-networkpolicies L7
Enable Advanced Container Networking Services on an existing cluster
The az aks update
command with the Advanced Container Networking Services flag, --enable-acns
, updates an existing AKS cluster with all Advanced Container Networking Services features which includes Container Network Observability and the Container Network Security feature.
Note
Only clusters with the Cilium data plane support Container Network Security features of Advanced Container Networking Services.
For this demo, the --acns-advanced-networkpolicies
parameter must be set to "L7" to enable L7 policies. Setting this parameter to "L7" also enables FQDN filtering. If you only want to enable FQDN filtering, set the parameter to "FQDN". To disable both features, you can follow the instructions provided in Disable Container Network Security.
az aks update \
--resource-group $RESOURCE_GROUP \
--name $CLUSTER_NAME \
--enable-acns \
--acns-advanced-networkpolicies L7
Get cluster credentials
Get your cluster credentials using the az aks get-credentials
command.
az aks get-credentials --name $CLUSTER_NAME --resource-group $RESOURCE_GROUP
Set up http-server application on your AKS cluster
Apply the below YAML to your AKS cluster to set up the http-server
application.
apiVersion: apps/v1
kind: Deployment
metadata:
name: http-server
labels:
app: http-server
spec:
replicas: 1
selector:
matchLabels:
app: http-server
template:
metadata:
labels:
app: http-server
spec:
containers:
- name: http-server
image: nginx:latest
ports:
- containerPort: 8080
volumeMounts:
- name: config-volume
mountPath: /etc/nginx/conf.d
volumes:
- name: config-volume
configMap:
name: nginx-config
---
apiVersion: v1
kind: Service
metadata:
name: http-server
spec:
selector:
app: http-server
ports:
- protocol: TCP
port: 80
targetPort: 8080
---
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
data:
default.conf: |
server {
listen 8080;
___location / {
return 200 "Hello from the server root!\n";
}
___location /products {
return 200 "Listing products...\n";
}
}
Set up http-client application on your AKS Cluster
Apply the below YAML to your AKS cluster to set up the http-client
application.
apiVersion: apps/v1
kind: Deployment
metadata:
name: http-client
labels:
app: http-client
spec:
replicas: 1
selector:
matchLabels:
app: http-client
template:
metadata:
labels:
app: http-client
spec:
containers:
- name: http-client
image: curlimages/curl:latest
command: ["sleep", "infinity"]
Test connectivity with a policy
Next, apply the following Layer 7 policy to allow only GET
requests from the http-client
application to the /products
endpoint on the http-server
:
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: allow-get-products
spec:
description: "Allow only GET requests to /products from http-client to http-server"
endpointSelector:
matchLabels:
app: http-server
ingress:
- fromEndpoints:
- matchLabels:
app: http-client
toPorts:
- ports:
- port: "8080"
protocol: TCP
rules:
http:
- method: "GET"
path: "/products"
Verify policy
To verify the policy's enforcement, execute these commands from the http-client
pod:
kubectl exec -it <your-http-client-pod-name> -n default -- curl -v http://http-server:80/products
You should expect an output like Listing products...
when you run the above command
kubectl exec -it <your-http-client-pod-name> -n default -- curl -v -XPOST http://http-server:80/products -d "test=data"
You should expect an output like Access Denied
when you run the above command
Observing L7 metrics
If you have Advanced Container Network Service's container network observability enabled, you can visualize the traffic on Grafana.
To simplify the analysis of these L7 metrics, we provide preconfigured Azure Managed Grafana dashboards. You can find them under the Dashboards > Azure Managed Prometheus folder, with filenames like "Kubernetes/Networking/L7 (Namespace)" and "Kubernetes/Networking/L7 (Workload)".
You should see metrics similar to the following:
Clean up resources
If you don't plan on using this application, delete the other resources you created in this article using the az group delete
command.
az group delete --name $RESOURCE_GROUP
Next steps
In this how-to article, you learned how to enable and apply L7 Policies with Advanced Container Networking Services for your AKS cluster.
- For more information about Advanced Container Networking Services for Azure Kubernetes Service (AKS), see What is Advanced Container Networking Services for Azure Kubernetes Service (AKS)?.
Azure Kubernetes Service