快速入門:建立由 ALB 控制器管理之適用於容器的應用程式閘道

本指南會假設您有遵循由 ALB 控制器管理部署策略,其中所有適用於容器的應用程式閘道資源都是由 ALB 控制器進行管理。 生命週期取決於 Kubernetes 中定義的資源。 在叢集上定義 ApplicationLoadBalancer 自訂資源時,ALB 控制器會建立適用於容器的應用程式閘道資源。 適用於容器的應用程式閘道生命週期是以自訂資源的生命週期為基礎。

必要條件

請確定已先將 ALB 控制器部署於 Kubernetes 叢集內。 如果尚未部署 ALB 控制器,請參閱快速入門:部署適用於容器的應用程式閘道 ALB 控制器

為適用於容器的應用程式閘道準備虛擬網路/子網路

如果沒有至少擁有 250 個可用 IP 位址的可用子網路,並委派給適用於容器的應用程式閘道資源,請使用下列步驟來建立新的子網路並啟用子網路委派。 新的子網路位址空間不能與 VNet 中的任何現有子網路重疊。

如果您想要將適用於容器的應用程式閘道部署到包含 AKS 叢集的虛擬網路,請執行下列命令來尋找並指派叢集的虛擬網路。 下一個步驟會使用這項資訊。

AKS_NAME='<your cluster name>'
RESOURCE_GROUP='<your resource group name>'

MC_RESOURCE_GROUP=$(az aks show --name $AKS_NAME --resource-group $RESOURCE_GROUP --query "nodeResourceGroup" -o tsv)
CLUSTER_SUBNET_ID=$(az vmss list --resource-group $MC_RESOURCE_GROUP --query '[0].virtualMachineProfile.networkProfile.networkInterfaceConfigurations[0].ipConfigurations[0].subnet.id' -o tsv)
read -d '' VNET_NAME VNET_RESOURCE_GROUP VNET_ID <<< $(az network vnet show --ids $CLUSTER_SUBNET_ID --query '[name, resourceGroup, id]' -o tsv)

執行下列命令來建立包含至少 250 個可用 IP 位址的新子網路,並為適用於容器的應用程式閘道關聯資源啟用子網路委派:

SUBNET_ADDRESS_PREFIX='<network address and prefix for an address space under the vnet that has at least 250 available addresses (/24 or larger subnet)>'
ALB_SUBNET_NAME='subnet-alb' # subnet name can be any non-reserved subnet name (i.e. GatewaySubnet, AzureFirewallSubnet, AzureBastionSubnet would all be invalid)
az network vnet subnet create \
  --resource-group $VNET_RESOURCE_GROUP \
  --vnet-name $VNET_NAME \
  --name $ALB_SUBNET_NAME \
  --address-prefixes $SUBNET_ADDRESS_PREFIX \
  --delegations 'Microsoft.ServiceNetworking/trafficControllers'
ALB_SUBNET_ID=$(az network vnet subnet show --name $ALB_SUBNET_NAME --resource-group $VNET_RESOURCE_GROUP --vnet-name $VNET_NAME --query '[id]' --output tsv)

將權限委派給受控識別

ALB 控制器必須能佈建適用於容器的應用程式閘道新資源,以及須加入供適用於容器的應用程式閘道關聯資源使用的子網路。

在此範例中,我們會將「適用於容器組態管理員的 AppGW」角色委派給包含受控叢集的資源群組,並將「網路參與者」角色委派給適用於容器的應用程式閘道關聯子網路所使用的子網路,包含 Microsoft.Network/virtualNetworks/subnets/join/action 權限。

如有需要,可以利用 Microsoft.Network/virtualNetworks/subnets/join/action 建立和指派自訂角色,以消除「網路參與者」角色中包含的其他權限。 深入了解管理子網路權限

IDENTITY_RESOURCE_NAME='azure-alb-identity'

MC_RESOURCE_GROUP=$(az aks show --name $AKS_NAME --resource-group $RESOURCE_GROUP --query "nodeResourceGroup" -otsv | tr -d '\r')

mcResourceGroupId=$(az group show --name $MC_RESOURCE_GROUP --query id -otsv)
principalId=$(az identity show -g $RESOURCE_GROUP -n $IDENTITY_RESOURCE_NAME --query principalId -otsv)

# Delegate AppGw for Containers Configuration Manager role to AKS Managed Cluster RG
az role assignment create --assignee-object-id $principalId --assignee-principal-type ServicePrincipal --scope $mcResourceGroupId --role "fbc52c3f-28ad-4303-a892-8a056630b8f1" 

# Delegate Network Contributor permission for join to association subnet
az role assignment create --assignee-object-id $principalId --assignee-principal-type ServicePrincipal --scope $ALB_SUBNET_ID --role "4d97b98b-1d4f-4787-a291-c67834d212e7" 

建立 ApplicationLoadBalancer Kubernetes 資源

  1. 定義 ApplicationLoadBalancer 資源的 Kubernetes 命名空間
kubectl apply -f - <<EOF
apiVersion: v1
kind: Namespace
metadata:
  name: alb-test-infra
EOF
  1. 定義 ApplicationLoadBalancer 資源,並指定適用於容器的應用程式閘道關聯資源部署目標的子網路識別碼。 該關聯會建立從適用於容器的應用程式閘道到已定義子網路的連線 (以及適用的連線網路),以將流量 Proxy 處理到定義的後端。

注意

在 ALB 控制器在 ARM 中建立適用於容器的應用程式閘道資源時,ALB 控制器會針對其資源使用下列命名慣例:

  • alb-<8 個隨機產生的字元>,以定義適用於容器的應用程式閘道資源
  • as-<8 個隨機產生的字元>,以定義關聯資源

如果您要變更在 Azure 中建立的資源名稱,請考慮遵循自備部署策略

執行下列命令,以建立適用於容器的應用程式閘道資源與關聯。

kubectl apply -f - <<EOF
apiVersion: alb.networking.azure.io/v1
kind: ApplicationLoadBalancer
metadata:
  name: alb-test
  namespace: alb-test-infra
spec:
  associations:
  - $ALB_SUBNET_ID
EOF

驗證是否建立適用於容器的應用程式閘道資源

建立 ApplicationLoadBalancer 資源之後,即可追蹤適用於容器的應用程式閘道資源的部署進度。 佈建完成時,部署會從 InProgress 轉換為 Programmed 狀態。 建立適用於容器的應用程式閘道資源可能需要 5-6 分鐘。

您可以執行下列命令來檢查 ApplicationLoadBalancer 資源的狀態:

kubectl get applicationloadbalancer alb-test -n alb-test-infra -o yaml -w

從 Kubernetes 成功佈建適用於容器的應用程式閘道資源的範例輸出。

status:
  conditions:
  - lastTransitionTime: "2023-06-19T21:03:29Z"
    message: Valid Application Gateway for Containers resource
    observedGeneration: 1
    reason: Accepted
    status: "True"
    type: Accepted
  - lastTransitionTime: "2023-06-19T21:03:29Z"
    message: alb-id=/subscriptions/xxx/resourceGroups/yyy/providers/Microsoft.ServiceNetworking/trafficControllers/alb-zzz
    observedGeneration: 1
    reason: Ready
    status: "True"
    type: Deployment

下一步

恭喜,您已在叢集上安裝 ALB 控制器,並在 Azure 中部署適用於容器的應用程式閘道資源!

操作指南針對適用於容器的應用程式閘道,示範了一些負載平衡概念,歡迎嘗試部署應用程式範例。