分享方式:


快速入門:建立適用於容器的應用程式閘道 - 自備部署

本指南會假設您採取自備部署策略,即 ALB 控制器會參考預先建立於 Azure 之適用於容器的應用程式閘道資源。 此外,會假設資源生命週期是在 Azure 中管理,與 Kubernetes 中定義的資源生命周期無關。

必要條件

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

建立適用於容器的應用程式閘道資源

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

RESOURCE_GROUP='<your resource group name>'
AGFC_NAME='alb-test' # Name of the Application Gateway for Containers resource to be created
az network alb create -g $RESOURCE_GROUP -n $AGFC_NAME

建立前端資源

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

FRONTEND_NAME='test-frontend'
az network alb frontend create -g $RESOURCE_GROUP -n $FRONTEND_NAME --alb-name $AGFC_NAME

建立關聯資源

將子網路委派給關聯資源

若要建立關聯資源,必須先參考適用於容器的應用程式閘道子網路,才能建立連線。 請確定適用於容器的應用程式閘道關聯子網路至少為 C 或更大類別 (/24 或較小的 CIDR 前置詞)。 在此步驟中,您可以重複使用現有的子網路,並在其上啟用子網路委派,或者您可建立新的 VNET、子網路,並啟用子網路委派。

如果要參考現有的子網路,請執行下列命令,以在後續步驟中設定用於參考子網路的變數。

VNET_NAME='<name of the virtual network to use>'
VNET_RESOURCE_GROUP='<the resource group of your VNET>'
ALB_SUBNET_NAME='subnet-alb' # subnet name can be any non-reserved subnet name (i.e. GatewaySubnet, AzureFirewallSubnet, AzureBastionSubnet would all be invalid)

針對適用於容器的應用程式閘道服務啟用子網路委派。 適用於容器的應用程式閘道委派是由 Microsoft.ServiceNetworking/trafficControllers 資源類型所識別。

az network vnet subnet update \
    --resource-group $VNET_RESOURCE_GROUP  \
    --name $ALB_SUBNET_NAME \
    --vnet-name $VNET_NAME \
    --delegations 'Microsoft.ServiceNetworking/trafficControllers'
ALB_SUBNET_ID=$(az network vnet subnet list --resource-group $VNET_RESOURCE_GROUP --vnet-name $VNET_NAME --query "[?name=='$ALB_SUBNET_NAME'].id" --output tsv)
echo $ALB_SUBNET_ID

將權限委派給受控識別

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

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

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

IDENTITY_RESOURCE_NAME='azure-alb-identity'

resourceGroupId=$(az group show --name $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 RG containing Application Gateway for Containers resource
az role assignment create --assignee-object-id $principalId --assignee-principal-type ServicePrincipal --scope $resourceGroupId --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" 

建立關聯資源

請執行下列命令以建立關聯資源,並連線至參考的子網路。 建立適用於容器的應用程式閘道關聯可能需要 5-6 分鐘。

ASSOCIATION_NAME='association-test'
az network alb association create -g $RESOURCE_GROUP -n $ASSOCIATION_NAME --alb-name $AGFC_NAME --subnet $ALB_SUBNET_ID

下一步

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

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