你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。

快速入门:使用 Azure 资源管理器 部署 Azure 防火墙 for Containers

在本快速入门中,您将使用 Azure 资源管理器 (ARM) 模板创建新的 Azure Kubernetes Service (AKS) 集群以及 Azure 防火墙 Premium for Azure 防火墙 for Containers。 该模板还配置托管身份、防火墙策略以及网络路由。

默认情况下,模板仅部署 Azure 资源。 然后,您按照该模板的 README 安装 Azure 防火墙 Controller,并附加示例安全策略。 可选参数还用于安装控制器、示例策略和测试模块。

Azure 资源管理器模板是一个 JavaScript 对象表示法(JSON)文件,用于定义项目的基础结构和配置。 模板使用声明性语法。 你可以在不编写用于创建部署的编程命令序列的情况下,描述预期部署。

Important

Azure 防火墙 for Containers 目前处于公开预览阶段。 有关适用于测试版、预览版或尚未正式发布的 Azure 功能的法律条款,请参阅 Microsoft Azure 预览版的补充使用条款。

如果您的环境满足先决条件,选择“部署到 Azure”。 模板会在 Azure 门户的自定义部署页面上打开。

按钮用于将 ARM 模板部署到 Azure。

Prerequisites

  • 拥有有效订阅的 Azure 帐户。 免费创建帐户。
  • 在受支持的预览区域访问 Azure 防火墙 for Containers。
  • Microsoft.ContainerService订阅中注册的资源Microsoft.Network提供者。
  • 在资源组中创建 AKS、网络资源、托管身份和角色分配的权限。 创建角色分配需要在分配范围内指定一个合适的角色,例如基于角色的 访问控制 管理员。
  • 所选 AKS 节点虚拟机大小的容量和配额。 默认部署使用两个节点,大小为 Standard_DS3_v2;该大小必须在您所选区域中可用。
  • 用于部署后步骤的 Azure CLI、kubectl 和 Helm。

使用独立的评估环境。 模板会创建一个新集群,并将 AKS 子网的出站流量通过防火墙路由。

要将模板的 Location 参数保持默认值,请在以下预览区域之一选择或创建资源组:

Region Azure 区域值
美国中部 EUAP centraluseuap
美国东部 2 EUAP eastus2euap
美国中西部 westcentralus
东亚 eastasia
英国南部 uksouth
美国中部 centralus

查看模板

模板来自 Azure Quickstart Templates。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.48.1.52935",
      "templateHash": "16236338146850503249"
    }
  },
  "parameters": {
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources. Must be an Azure Firewall for Containers preview region (centraluseuap, eastus2euap, westcentralus, eastasia, uksouth, centralus)."
      }
    },
    "namePrefix": {
      "type": "string",
      "defaultValue": "afc",
      "maxLength": 12,
      "metadata": {
        "description": "Prefix used to name the resources."
      }
    },
    "nodeVmSize": {
      "type": "string",
      "defaultValue": "Standard_DS3_v2",
      "metadata": {
        "description": "VM size for the AKS nodes. Must be available in the selected region."
      }
    },
    "nodeCount": {
      "type": "int",
      "defaultValue": 2,
      "minValue": 1,
      "maxValue": 10,
      "metadata": {
        "description": "Number of AKS nodes."
      }
    },
    "controllerVersion": {
      "type": "string",
      "defaultValue": "20261103.2",
      "metadata": {
        "description": "Azure Firewall for Containers controller Helm chart version."
      }
    },
    "controllerNamespace": {
      "type": "string",
      "defaultValue": "firewall-namespace",
      "metadata": {
        "description": "Kubernetes namespace for the controller. Used in the workload identity federated credential subject."
      }
    },
    "deployControllerAndSample": {
      "type": "bool",
      "defaultValue": false,
      "metadata": {
        "description": "false (default): deploy Azure resources only; the outputs contain the remaining commands. true: also run a deployment script that installs the controller and applies a sample security policy, attachment, and test pods. The script needs a storage account with shared key access, so it fails where Azure Policy denies shared key access."
      }
    },
    "azCliVersion": {
      "type": "string",
      "defaultValue": "2.61.0",
      "metadata": {
        "description": "Azure CLI version used by the deployment script."
      }
    },
    "utcValue": {
      "type": "string",
      "defaultValue": "[utcNow()]",
      "metadata": {
        "description": "Unique suffix that forces the deployment script to rerun on each deployment. Leave the default."
      }
    }
  },
  "variables": {
    "$fxv#0": "#!/bin/bash\n# Runs inside Microsoft.Resources/deploymentScripts (Azure CLI container, signed in with the script identity).\n# Installs the Azure Firewall for Containers controller and applies a sample policy, attachment, and test pods.\nset -euo pipefail\n\necho \"Getting cluster credentials for ${CLUSTER_NAME}\"\naz aks get-credentials --resource-group \"$RESOURCE_GROUP\" --name \"$CLUSTER_NAME\" --admin --file ./kubeconfig --overwrite-existing --only-show-errors\nchmod 600 ./kubeconfig\nexport KUBECONFIG=\"$PWD/kubeconfig\"\n\ndownload() { # url dest; the Azure CLI container image may not include curl\n  if command -v curl >/dev/null 2>&1; then curl -fsSL \"$1\" -o \"$2\"\n  elif command -v wget >/dev/null 2>&1; then wget -qO \"$2\" \"$1\"\n  else python3 -c \"import sys, urllib.request; urllib.request.urlretrieve(sys.argv[1], sys.argv[2])\" \"$1\" \"$2\"\n  fi\n}\n\necho \"Installing kubectl and helm\"\naz aks install-cli --install-location /usr/local/bin/kubectl --kubelogin-install-location /usr/local/bin/kubelogin --only-show-errors >/dev/null\ndownload https://get.helm.sh/helm-v3.16.2-linux-amd64.tar.gz /tmp/helm.tar.gz\ntar -xzf /tmp/helm.tar.gz -C /tmp\ninstall /tmp/linux-amd64/helm /usr/local/bin/helm\n\necho \"Installing controller ${CONTROLLER_VERSION}\"\nhelm upgrade --install azfw-controller \\\n  oci://mcr.microsoft.com/firewall/charts/azurefirewall-controller \\\n  --namespace \"$CONTROLLER_NAMESPACE\" \\\n  --create-namespace \\\n  --version \"$CONTROLLER_VERSION\" \\\n  --set \"azfwController.podIdentity.clientID=${CONTROLLER_CLIENT_ID}\" \\\n  --wait --timeout 10m\nkubectl wait --for=condition=Available deploy --all --namespace \"$CONTROLLER_NAMESPACE\" --timeout=10m\nkubectl wait --for=condition=Established crd/securitypolicyattachments.firewall.networking.azure.io --timeout=5m\n\necho \"Applying sample security policy and attachment\"\nkubectl apply -f - <<EOF\napiVersion: firewall.networking.azure.io/v1alpha1\nkind: SecurityPolicy\nmetadata:\n  name: example-securitypolicy-rules\n  namespace: default\nspec:\n  applicationRuleCollections:\n    - name: test-application-rules\n      refs:\n        - podSelector:\n            matchLabels:\n              role: developer\n      action: Allow\n      egress:\n        - name: allow-test-destinations\n          toPortProtocol:\n            - protocolType: HTTPS\n              port: 443\n          to:\n            - fqdn:\n                - \"*.microsoft.com\"\n                - \"*.google.com\"\n---\napiVersion: firewall.networking.azure.io/v1alpha1\nkind: SecurityPolicyAttachment\nmetadata:\n  name: example-attachment\n  namespace: default\nspec:\n  securityPolicyRefs:\n    - name: example-securitypolicy-rules\n      namespace: default\n  firewallRef:\n    id: ${FIREWALL_ID}\n  aksWorkloadSubnets:\n    - ${AKS_SUBNET_ID}\nEOF\n\necho \"Waiting for the attachment to deploy\"\nkubectl wait --for=jsonpath='{.status.conditions[?(@.type==\"Deployment\")].reason}'=Ready \\\n  securitypolicyattachment/example-attachment --namespace default --timeout=40m\nkubectl get securitypolicyattachment example-attachment --namespace default \\\n  -o jsonpath='{range .status.conditions[*]}{.type}={.status} {.reason}{\"\\n\"}{end}'\n\necho \"Creating test pods\"\nkubectl apply -f - <<'EOF'\napiVersion: v1\nkind: Pod\nmetadata:\n  name: developer-pod\n  namespace: default\n  labels:\n    role: developer\nspec:\n  containers:\n    - name: developer\n      image: mcr.microsoft.com/azure-cli:latest\n      command: [\"sleep\", \"infinity\"]\n---\napiVersion: v1\nkind: Pod\nmetadata:\n  name: other-pod\n  namespace: default\n  labels:\n    role: other\nspec:\n  containers:\n    - name: other\n      image: mcr.microsoft.com/azure-cli:latest\n      command: [\"sleep\", \"infinity\"]\nEOF\nkubectl wait --for=condition=Ready pod/developer-pod pod/other-pod --namespace default --timeout=10m\n\nATTACH_STATUS=$(kubectl get securitypolicyattachment example-attachment --namespace default \\\n  -o jsonpath='{.status.conditions[?(@.type==\"Deployment\")].reason}')\ncat > \"$AZ_SCRIPTS_OUTPUT_PATH\" <<EOF\n{\"controllerVersion\": \"${CONTROLLER_VERSION}\", \"attachmentDeployment\": \"${ATTACH_STATUS}\"}\nEOF\necho \"Done\"\n",
    "vnetName": "[format('{0}-vnet', parameters('namePrefix'))]",
    "aksSubnetName": "aks-subnet",
    "vnetAddressPrefix": "10.224.0.0/12",
    "aksSubnetPrefix": "10.224.0.0/16",
    "firewallSubnetPrefix": "10.225.0.0/26",
    "firewallMgmtSubnetPrefix": "10.225.1.0/26",
    "clusterName": "[format('{0}-aks', parameters('namePrefix'))]",
    "firewallName": "[format('{0}-firewall', parameters('namePrefix'))]",
    "firewallPolicyName": "[format('{0}-firewall-policy', parameters('namePrefix'))]",
    "routeTableName": "[format('{0}-aks-rt', parameters('namePrefix'))]",
    "controllerServiceAccount": "azurefirewall-controller-sa",
    "networkContributorRoleId": "[subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '4d97b98b-1d4f-4787-a291-c67834d212e7')]",
    "aksClusterAdminRoleId": "[subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '0ab0b1a8-8aac-4efd-b8c2-3ee1fb270be8')]",
    "aksSubnetId": "[format('{0}/subnets/{1}', resourceId('Microsoft.Network/virtualNetworks', variables('vnetName')), variables('aksSubnetName'))]"
  },
  "resources": [
    {
      "type": "Microsoft.ManagedIdentity/userAssignedIdentities",
      "apiVersion": "2024-11-30",
      "name": "[format('{0}-aks-identity', parameters('namePrefix'))]",
      "location": "[parameters('location')]"
    },
    {
      "type": "Microsoft.ManagedIdentity/userAssignedIdentities",
      "apiVersion": "2024-11-30",
      "name": "[format('{0}-controller-identity', parameters('namePrefix'))]",
      "location": "[parameters('location')]"
    },
    {
      "type": "Microsoft.Network/virtualNetworks",
      "apiVersion": "2025-05-01",
      "name": "[variables('vnetName')]",
      "location": "[parameters('location')]",
      "properties": {
        "addressSpace": {
          "addressPrefixes": [
            "[variables('vnetAddressPrefix')]"
          ]
        },
        "subnets": [
          {
            "name": "[variables('aksSubnetName')]",
            "properties": {
              "addressPrefix": "[variables('aksSubnetPrefix')]",
              "defaultOutboundAccess": false
            }
          },
          {
            "name": "AzureFirewallSubnet",
            "properties": {
              "addressPrefix": "[variables('firewallSubnetPrefix')]",
              "defaultOutboundAccess": false
            }
          },
          {
            "name": "AzureFirewallManagementSubnet",
            "properties": {
              "addressPrefix": "[variables('firewallMgmtSubnetPrefix')]",
              "defaultOutboundAccess": false
            }
          }
        ]
      }
    },
    {
      "type": "Microsoft.Authorization/roleAssignments",
      "apiVersion": "2022-04-01",
      "scope": "[resourceId('Microsoft.Network/virtualNetworks', variables('vnetName'))]",
      "name": "[guid(resourceId('Microsoft.Network/virtualNetworks', variables('vnetName')), resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-aks-identity', parameters('namePrefix'))), variables('networkContributorRoleId'))]",
      "properties": {
        "roleDefinitionId": "[variables('networkContributorRoleId')]",
        "principalId": "[reference(resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-aks-identity', parameters('namePrefix'))), '2024-11-30').principalId]",
        "principalType": "ServicePrincipal"
      },
      "dependsOn": [
        "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-aks-identity', parameters('namePrefix')))]",
        "[resourceId('Microsoft.Network/virtualNetworks', variables('vnetName'))]"
      ]
    },
    {
      "type": "Microsoft.Network/publicIPAddresses",
      "apiVersion": "2025-05-01",
      "name": "[format('{0}-fw-ip', parameters('namePrefix'))]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard"
      },
      "properties": {
        "publicIPAllocationMethod": "Static",
        "publicIPAddressVersion": "IPv4"
      }
    },
    {
      "type": "Microsoft.Network/publicIPAddresses",
      "apiVersion": "2025-05-01",
      "name": "[format('{0}-fw-mgmt-ip', parameters('namePrefix'))]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard"
      },
      "properties": {
        "publicIPAllocationMethod": "Static",
        "publicIPAddressVersion": "IPv4"
      }
    },
    {
      "type": "Microsoft.Network/firewallPolicies",
      "apiVersion": "2025-05-01",
      "name": "[variables('firewallPolicyName')]",
      "location": "[parameters('location')]",
      "properties": {
        "sku": {
          "tier": "Premium"
        },
        "threatIntelMode": "Alert"
      }
    },
    {
      "type": "Microsoft.Network/firewallPolicies/ruleCollectionGroups",
      "apiVersion": "2025-05-01",
      "name": "[format('{0}/{1}', variables('firewallPolicyName'), 'default-allow-aks-controlplane')]",
      "properties": {
        "priority": 100,
        "ruleCollections": [
          {
            "ruleCollectionType": "FirewallPolicyFilterRuleCollection",
            "name": "AllowAksControlPlaneNetwork",
            "priority": 100,
            "action": {
              "type": "Allow"
            },
            "rules": [
              {
                "ruleType": "NetworkRule",
                "name": "Allow_AKS_ControlPlane_Network",
                "ipProtocols": [
                  "TCP"
                ],
                "sourceAddresses": [
                  "*"
                ],
                "destinationAddresses": [
                  "AzureKubernetesService"
                ],
                "destinationPorts": [
                  "443"
                ]
              }
            ]
          },
          {
            "ruleCollectionType": "FirewallPolicyFilterRuleCollection",
            "name": "AllowAksControlPlaneApp",
            "priority": 101,
            "action": {
              "type": "Allow"
            },
            "rules": [
              {
                "ruleType": "ApplicationRule",
                "name": "Allow_AKS_ControlPlane_App",
                "sourceAddresses": [
                  "*"
                ],
                "fqdnTags": [
                  "AzureKubernetesService"
                ],
                "protocols": [
                  {
                    "protocolType": "Https",
                    "port": 443
                  }
                ]
              },
              {
                "ruleType": "ApplicationRule",
                "name": "Allow_AFC_Service",
                "sourceAddresses": [
                  "*"
                ],
                "targetFqdns": [
                  "*.afc.azure.com"
                ],
                "protocols": [
                  {
                    "protocolType": "Https",
                    "port": 443
                  }
                ]
              }
            ]
          }
        ]
      },
      "dependsOn": [
        "[resourceId('Microsoft.Network/firewallPolicies', variables('firewallPolicyName'))]"
      ]
    },
    {
      "type": "Microsoft.Network/azureFirewalls",
      "apiVersion": "2025-05-01",
      "name": "[variables('firewallName')]",
      "location": "[parameters('location')]",
      "properties": {
        "sku": {
          "name": "AZFW_VNet",
          "tier": "Premium"
        },
        "firewallPolicy": {
          "id": "[resourceId('Microsoft.Network/firewallPolicies', variables('firewallPolicyName'))]"
        },
        "ipConfigurations": [
          {
            "name": "data-ipconfig",
            "properties": {
              "subnet": {
                "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vnetName'), 'AzureFirewallSubnet')]"
              },
              "publicIPAddress": {
                "id": "[resourceId('Microsoft.Network/publicIPAddresses', format('{0}-fw-ip', parameters('namePrefix')))]"
              }
            }
          }
        ],
        "managementIpConfiguration": {
          "name": "mgmt-ipconfig",
          "properties": {
            "subnet": {
              "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vnetName'), 'AzureFirewallManagementSubnet')]"
            },
            "publicIPAddress": {
              "id": "[resourceId('Microsoft.Network/publicIPAddresses', format('{0}-fw-mgmt-ip', parameters('namePrefix')))]"
            }
          }
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.Network/firewallPolicies/ruleCollectionGroups', variables('firewallPolicyName'), 'default-allow-aks-controlplane')]",
        "[resourceId('Microsoft.Network/publicIPAddresses', format('{0}-fw-mgmt-ip', parameters('namePrefix')))]",
        "[resourceId('Microsoft.Network/firewallPolicies', variables('firewallPolicyName'))]",
        "[resourceId('Microsoft.Network/publicIPAddresses', format('{0}-fw-ip', parameters('namePrefix')))]",
        "[resourceId('Microsoft.Network/virtualNetworks', variables('vnetName'))]"
      ]
    },
    {
      "type": "Microsoft.ContainerService/managedClusters",
      "apiVersion": "2025-07-01",
      "name": "[variables('clusterName')]",
      "location": "[parameters('location')]",
      "identity": {
        "type": "UserAssigned",
        "userAssignedIdentities": {
          "[format('{0}', resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-aks-identity', parameters('namePrefix'))))]": {}
        }
      },
      "properties": {
        "dnsPrefix": "[variables('clusterName')]",
        "agentPoolProfiles": [
          {
            "name": "nodepool1",
            "mode": "System",
            "count": "[parameters('nodeCount')]",
            "vmSize": "[parameters('nodeVmSize')]",
            "osType": "Linux",
            "type": "VirtualMachineScaleSets",
            "vnetSubnetID": "[variables('aksSubnetId')]"
          }
        ],
        "networkProfile": {
          "networkPlugin": "azure",
          "outboundType": "loadBalancer",
          "loadBalancerSku": "standard",
          "serviceCidr": "10.240.0.0/16",
          "dnsServiceIP": "10.240.0.10"
        },
        "oidcIssuerProfile": {
          "enabled": true
        },
        "securityProfile": {
          "workloadIdentity": {
            "enabled": true
          }
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-aks-identity', parameters('namePrefix')))]",
        "[extensionResourceId(resourceId('Microsoft.Network/virtualNetworks', variables('vnetName')), 'Microsoft.Authorization/roleAssignments', guid(resourceId('Microsoft.Network/virtualNetworks', variables('vnetName')), resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-aks-identity', parameters('namePrefix'))), variables('networkContributorRoleId')))]",
        "[resourceId('Microsoft.Network/virtualNetworks', variables('vnetName'))]"
      ]
    },
    {
      "type": "Microsoft.Authorization/roleAssignments",
      "apiVersion": "2022-04-01",
      "name": "[guid(resourceGroup().id, resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-controller-identity', parameters('namePrefix'))), variables('networkContributorRoleId'))]",
      "properties": {
        "roleDefinitionId": "[variables('networkContributorRoleId')]",
        "principalId": "[reference(resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-controller-identity', parameters('namePrefix'))), '2024-11-30').principalId]",
        "principalType": "ServicePrincipal"
      },
      "dependsOn": [
        "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-controller-identity', parameters('namePrefix')))]"
      ]
    },
    {
      "type": "Microsoft.ManagedIdentity/userAssignedIdentities/federatedIdentityCredentials",
      "apiVersion": "2024-11-30",
      "name": "[format('{0}/{1}', format('{0}-controller-identity', parameters('namePrefix')), 'azurefirewall-controller')]",
      "properties": {
        "issuer": "[reference(resourceId('Microsoft.ContainerService/managedClusters', variables('clusterName')), '2025-07-01').oidcIssuerProfile.issuerURL]",
        "subject": "[format('system:serviceaccount:{0}:{1}', parameters('controllerNamespace'), variables('controllerServiceAccount'))]",
        "audiences": [
          "api://AzureADTokenExchange"
        ]
      },
      "dependsOn": [
        "[resourceId('Microsoft.ContainerService/managedClusters', variables('clusterName'))]",
        "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-controller-identity', parameters('namePrefix')))]"
      ]
    },
    {
      "type": "Microsoft.Network/routeTables",
      "apiVersion": "2025-05-01",
      "name": "[variables('routeTableName')]",
      "location": "[parameters('location')]",
      "properties": {
        "routes": [
          {
            "name": "to-firewall",
            "properties": {
              "addressPrefix": "0.0.0.0/0",
              "nextHopType": "VirtualAppliance",
              "nextHopIpAddress": "[reference(resourceId('Microsoft.Network/azureFirewalls', variables('firewallName')), '2025-05-01').ipConfigurations[0].properties.privateIPAddress]"
            }
          }
        ]
      },
      "dependsOn": [
        "[resourceId('Microsoft.Network/azureFirewalls', variables('firewallName'))]"
      ]
    },
    {
      "type": "Microsoft.Network/virtualNetworks/subnets",
      "apiVersion": "2025-05-01",
      "name": "[format('{0}/{1}', variables('vnetName'), variables('aksSubnetName'))]",
      "properties": {
        "addressPrefix": "[variables('aksSubnetPrefix')]",
        "defaultOutboundAccess": false,
        "routeTable": {
          "id": "[resourceId('Microsoft.Network/routeTables', variables('routeTableName'))]"
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.ContainerService/managedClusters', variables('clusterName'))]",
        "[resourceId('Microsoft.Network/routeTables', variables('routeTableName'))]",
        "[resourceId('Microsoft.Network/virtualNetworks', variables('vnetName'))]"
      ]
    },
    {
      "condition": "[parameters('deployControllerAndSample')]",
      "type": "Microsoft.ManagedIdentity/userAssignedIdentities",
      "apiVersion": "2024-11-30",
      "name": "[format('{0}-script-identity', parameters('namePrefix'))]",
      "location": "[parameters('location')]"
    },
    {
      "condition": "[parameters('deployControllerAndSample')]",
      "type": "Microsoft.Authorization/roleAssignments",
      "apiVersion": "2022-04-01",
      "scope": "[resourceId('Microsoft.ContainerService/managedClusters', variables('clusterName'))]",
      "name": "[guid(resourceId('Microsoft.ContainerService/managedClusters', variables('clusterName')), format('{0}-script-identity', parameters('namePrefix')), variables('aksClusterAdminRoleId'))]",
      "properties": {
        "roleDefinitionId": "[variables('aksClusterAdminRoleId')]",
        "principalId": "[reference(resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-script-identity', parameters('namePrefix'))), '2024-11-30').principalId]",
        "principalType": "ServicePrincipal"
      },
      "dependsOn": [
        "[resourceId('Microsoft.ContainerService/managedClusters', variables('clusterName'))]",
        "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-script-identity', parameters('namePrefix')))]"
      ]
    },
    {
      "condition": "[parameters('deployControllerAndSample')]",
      "type": "Microsoft.Resources/deploymentScripts",
      "apiVersion": "2023-08-01",
      "name": "[format('{0}-install-controller', parameters('namePrefix'))]",
      "location": "[parameters('location')]",
      "kind": "AzureCLI",
      "identity": {
        "type": "UserAssigned",
        "userAssignedIdentities": {
          "[format('{0}', resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-script-identity', parameters('namePrefix'))))]": {}
        }
      },
      "properties": {
        "azCliVersion": "[parameters('azCliVersion')]",
        "timeout": "PT1H",
        "retentionInterval": "P1D",
        "cleanupPreference": "OnSuccess",
        "forceUpdateTag": "[parameters('utcValue')]",
        "environmentVariables": [
          {
            "name": "RESOURCE_GROUP",
            "value": "[resourceGroup().name]"
          },
          {
            "name": "CLUSTER_NAME",
            "value": "[variables('clusterName')]"
          },
          {
            "name": "CONTROLLER_VERSION",
            "value": "[parameters('controllerVersion')]"
          },
          {
            "name": "CONTROLLER_NAMESPACE",
            "value": "[parameters('controllerNamespace')]"
          },
          {
            "name": "CONTROLLER_CLIENT_ID",
            "value": "[reference(resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-controller-identity', parameters('namePrefix'))), '2024-11-30').clientId]"
          },
          {
            "name": "FIREWALL_ID",
            "value": "[resourceId('Microsoft.Network/azureFirewalls', variables('firewallName'))]"
          },
          {
            "name": "AKS_SUBNET_ID",
            "value": "[variables('aksSubnetId')]"
          }
        ],
        "scriptContent": "[variables('$fxv#0')]"
      },
      "dependsOn": [
        "[resourceId('Microsoft.ContainerService/managedClusters', variables('clusterName'))]",
        "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vnetName'), variables('aksSubnetName'))]",
        "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities/federatedIdentityCredentials', format('{0}-controller-identity', parameters('namePrefix')), 'azurefirewall-controller')]",
        "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-controller-identity', parameters('namePrefix')))]",
        "[resourceId('Microsoft.Authorization/roleAssignments', guid(resourceGroup().id, resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-controller-identity', parameters('namePrefix'))), variables('networkContributorRoleId')))]",
        "[resourceId('Microsoft.Network/azureFirewalls', variables('firewallName'))]",
        "[extensionResourceId(resourceId('Microsoft.ContainerService/managedClusters', variables('clusterName')), 'Microsoft.Authorization/roleAssignments', guid(resourceId('Microsoft.ContainerService/managedClusters', variables('clusterName')), format('{0}-script-identity', parameters('namePrefix')), variables('aksClusterAdminRoleId')))]",
        "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-script-identity', parameters('namePrefix')))]",
        "[resourceId('Microsoft.Network/virtualNetworks', variables('vnetName'))]"
      ]
    }
  ],
  "outputs": {
    "clusterName": {
      "type": "string",
      "value": "[variables('clusterName')]"
    },
    "firewallId": {
      "type": "string",
      "value": "[resourceId('Microsoft.Network/azureFirewalls', variables('firewallName'))]"
    },
    "firewallPrivateIp": {
      "type": "string",
      "value": "[reference(resourceId('Microsoft.Network/azureFirewalls', variables('firewallName')), '2025-05-01').ipConfigurations[0].properties.privateIPAddress]"
    },
    "firewallPolicyId": {
      "type": "string",
      "value": "[resourceId('Microsoft.Network/firewallPolicies', variables('firewallPolicyName'))]"
    },
    "aksSubnetId": {
      "type": "string",
      "value": "[variables('aksSubnetId')]"
    },
    "controllerIdentityClientId": {
      "type": "string",
      "value": "[reference(resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-controller-identity', parameters('namePrefix'))), '2024-11-30').clientId]"
    },
    "getCredentialsCommand": {
      "type": "string",
      "value": "[format('az aks get-credentials --resource-group {0} --name {1}', resourceGroup().name, variables('clusterName'))]"
    },
    "helmInstallCommand": {
      "type": "string",
      "value": "[format('helm install azfw-controller oci://mcr.microsoft.com/firewall/charts/azurefirewall-controller --namespace {0} --create-namespace --version {1} --set azfwController.podIdentity.clientID={2}', parameters('controllerNamespace'), parameters('controllerVersion'), reference(resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', format('{0}-controller-identity', parameters('namePrefix'))), '2024-11-30').clientId)]"
    },
    "attachmentFirewallRef": {
      "type": "string",
      "value": "[format('firewallRef: {{ id: {0} }}', resourceId('Microsoft.Network/azureFirewalls', variables('firewallName')))]"
    },
    "controllerAndSampleInstalled": {
      "type": "bool",
      "value": "[parameters('deployControllerAndSample')]"
    }
  }
}

模板创建以下资源:

Resource Purpose
虚拟网络和子网 提供 AKS 子网、AzureFirewallSubnet和AzureFirewallManagementSubnet
AKS 群集 使用 Azure CNI、OIDC 颁发程序和工作负载标识运行工作负载
Azure 防火墙 Premium 和公用 IP 地址 检查集群的出口流量
防火墙策略和规则收集组 允许必要的 AKS 出站流量以及到 *.afc.azure.com 的 HTTPS
路由表与子网的关联 将 AKS 子网的出站流量路由至防火墙
托管身份、角色分配和联合身份凭据 为 AKS 网络和 Azure 防火墙 控制器授权
可选部署脚本及其身份 当 Deploy Controller And Sample 为 true 时,安装控制器、示例策略、附加资源和测试 Pod

部署模板

除非您的环境允许可选部署脚本,否则请将“ 部署控制器和样本 ”设置为 false 。

  1. 在介绍末尾选择“部署到 Azure”并登录 Azure。 门户会打开 自定义部署 页面。

  2. 配置以下设置:

    设置 Value
    订阅 选择用于评估的订阅
    资源组 选择或创建资源组;新的专用资源组让清理更安全
    地区 使用先决条件中列出的预览区域
    名称前缀 使用默认值afc,或为资源名称输入前缀
    节点 VM 大小 仅在所选区域内可用时使用 Standard_DS3_v2;否则,输入可用大小
    部署控制器和示例 保持 false 仅部署 Azure 资源,然后按照 README 的安装步骤操作。 仅当部署脚本允许共享密钥访问时,才设置为 true 。

    Note

    将 Deploy Controller And Sample 设为 true 时,模板会运行一个部署脚本,该脚本需要一个启用了共享密钥访问的存储帐户。 如果内置的 Azure Policy Storage accounts should prevent shared key access 被分配了 拒绝 效应,则部署将失败。 保持默认值(false),并按照其中的安装步骤操作,或者参阅 template README,检查相关策略并申请豁免。

  3. 保持其他参数的默认值。 选择 查看 + 创建并查看验证结果。

  4. 选择“创建”。

Azure 资源部署大约需要 10-15 分钟;若将 部署控制器和示例 设置为 true,则需要 30-45 分钟。 部署时间可能有所不同。

查看已部署的资源

  1. 在 Azure 门户中,打开资源组并选择“部署”。 打开模板部署并确认其已成功部署。

  2. 查看已部署的 AKS 集群、Azure 防火墙、防火墙策略、虚拟网络、路由和身份。 选择 “输出” 以查找集群名称、防火墙资源 ID 以及后续步骤的命令。

  3. 完成部署选项的控制器和策略设置:

    部署选项 后续操作
    部署控制器和样本 为 false 按照 “安装控制器并附加策略”操作。 使用 getCredentialsCommand 和 helmInstallCommand 部署输出代替 README 示例命令。 在创建示例防火墙参考和策略附件时,请使用输出中的资源 ID
    部署控制器和示例 为 true 部署脚本会安装控制器、示例策略、附件和测试 Pod。 运行 getCredentialsCommand 以从您自己的环境中连接。 跳过 README 的控制器安装和 Pod 创建命令,这样你就不会重新创建现有资源

在运行 Azure CLI 命令之前,请登录并选择本次部署所使用的订阅。 如果您更改了 Name Prefix,请使用部署输出,如 clusterName、firewallId、aksSubnetId 和 controllerIdentityClientId,而不是 README 的默认资源名称。

使用 README 的 流量测试 来验证示例策略的允许和拒绝行为。 如果部署脚本已经创建了测试 Pod,请使用这些现有 Pod 的测试命令。 仅仅成功部署 Azure 并不能验证流量过滤。

清理资源

如果您专门为此快速启动创建了资源组,请在完成部署评估后将其删除:

  1. 在 Azure 门户中,打开资源组并查看其资源。 确认其中没有你需要保留的资源。
  2. 选择“ 删除资源组”。
  3. 输入资源组名称并确认删除。

删除资源组会移除其中的所有资源。 如果您重复使用了资源组,请不要删除该组。 仅删除您为此评估创建的资源。 如果需要删除单个策略或附件,请遵循 控制器清理指南 。

后续步骤