Share via


利用現有的 Azure 應用程式閘道來安裝 Azure 應用程式閘道入口控制器 (AGIC)

應用程式閘道輸入控制器 (AGIC) 是 Azure Kubernetes Service (AKS) 叢集內的 Pod。 AGIC 會監視 Kubernetes 入口資源,並根據 Kubernetes 叢集的狀態來建立並套用 Azure 應用程式閘道設定。

提示

另請參閱什麼是適用於容器的應用程式閘道?,其目前處於公開預覽狀態。

大綱

必要條件

本文件假設您已安裝以下工具與基礎結構:

安裝 AGIC 之前,請「備份應用程式閘道設定」

  1. Azure 入口網站中,導覽至您的應用程式閘道執行個體。
  2. 在 [自動化] 區段下方,選取 [匯出範本],然後選取 [下載]

您所下載的 zip 檔案包含可用來還原應用程式閘道 (需要時) 的 JSON 範本、bash 和 PowerShell 指令碼

安裝 Helm

Helm 是 Kubernetes 的套件管理員,用來安裝 application-gateway-kubernetes-ingress 套件。

注意

如果您使用 Cloud Shell,則不需要安裝 Helm。 Azure Cloud Shell 隨附 Helm 第 3 版。 略過第一個步驟,只新增 AGIC Helm 存放庫。

  1. 安裝 Helm 並執行以下指令新增 application-gateway-kubernetes-ingresshelm 套件:

    • 已啟用 Kubernetes RBAC 的 AKS 叢集
    kubectl create serviceaccount --namespace kube-system tiller-sa
    kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller-sa
    helm init --tiller-namespace kube-system --service-account tiller-sa
    
  2. 新增 AGIC Helm 存放庫:

    helm repo add application-gateway-kubernetes-ingress https://appgwingress.blob.core.windows.net/ingress-azure-helm-package/
    helm repo update
    

Azure Resource Manager 驗證

AGIC 會與 Kubernetes API 伺服器及 Azure Resource Manager 進行通訊。 需要身分識別才能存取這些 API。

設定 Microsoft Entra 工作負載識別碼

Microsoft Entra 工作負載識別碼是您指派給軟體工作負載的身分識別,用來驗證與存取其他服務和資源。 此身分識別可讓您的 AKS Pod 使用此身分識別,並向其他 Azure 資源進行驗證。 針對此設定,需要 AGIC Pod 的授權,才能向 ARM 發出 HTTP 要求。

  1. 使用 Azure CLI az account set 命令,將特定訂閱設定為目前使用中訂閱。 然後使用 az identity create 命令來建立受控識別。 身分識別需要在節點資源群組中建立。 預設會為節點資源群組指派 MC_myResourceGroup_myAKSCluster_eastus 這類名稱。

    az account set --subscription "subscriptionID"
    
    az identity create --name "userAssignedIdentityName" --resource-group "resourceGroupName" --location "location" --subscription "subscriptionID"
    
  2. 針對角色指派,執行下列命令來識別新建身分識別的 principalId

    $resourceGroup="resource-group-name"
    $identityName="identity-name"
    az identity list -g $resourceGroup --query "[?name == '$identityName'].principalId | [0]" -o tsv
    
  3. 將應用程式閘道的「參與者」存取權授與身份識別。 您需要應用程式閘道識別碼,如下所示:/subscriptions/A/resourceGroups/B/providers/Microsoft.Network/applicationGateways/C。 首先,執行下列命令來取得訂用帳戶中的應用程式閘道識別碼清單:

    az network application-gateway list --query '[].id'
    

    若要將「參與者」存取權指派給身分識別,請執行下列命令:

    $resourceGroup="resource-group-name"
    $identityName="identity-Name"
    # Get the Application Gateway ID
    $AppGatewayID=$(az network application-gateway list --query '[].id' -o tsv)
    $role="contributor"
    # Get the principal ID for the User assigned identity
    $principalId=$(az identity list -g $resourceGroup --query "[?name == '$identityName'].principalId | [0]" -o tsv)
    az role assignment create --assignee $principalId --role $role --scope $AppGatewayID
    
  4. 將應用程式閘道資源群組的「讀者」存取權授與身份識別。 資源群組識別碼如下:/subscriptions/A/resourceGroups/B。 您可利用以下方式取得所有資源群組:az group list --query '[].id'

    $resourceGroup="resource-group-name"
    $identityName="identity-Name"
    # Get the Application Gateway resource group
    $AppGatewayResourceGroup=$(az network application-gateway list --query '[].resourceGroup' -o tsv)
    # Get the Application Gateway resource group ID
    $AppGatewayResourceGroupID=$(az group show --name $AppGatewayResourceGroup --query id -o tsv)
    $role="Reader"
    # Get the principal ID for the User assigned identity
    $principalId=$(az identity list -g $resourceGroup --query "[?name == '$identityName'].principalId | [0]" -o tsv)
    # Assign the Reader role to the User assigned identity at the resource group scope
    az role assignment create --role $role --assignee $principalId  --scope $AppGatewayResourceGroupID
    

注意

請確定 AGIC 所使用的身分識別具有委派給應用程式閘道部署所在子網路的 Microsoft.Network/virtualNetworks/subnets/join/action 權限。 如果自訂角色未定義此權限,則您可以使用內建「網路參與者」角色,其包含 Microsoft.Network/virtualNetworks/subnets/join/action 權限。

利用服務主體

也可以使用 Kubernetes 祕密來提供 AGIC 對 ARM 的存取權。

  1. 建立 Active Directory 服務主體,並利用 base64 編碼。 若要儲存 JSON BLOB 至 Kubernetes,必須利用 base64 編碼。

    az ad sp create-for-rbac --role Contributor --sdk-auth | base64 -w0
    
  2. 新增 base64 編碼的 JSON blob 至helm-config.yaml檔案。 如需進一步資訊了解helm-config.yaml,請參考下一章節。

    armAuth:
        type: servicePrincipal
        secretJSON: <Base64-Encoded-Credentials>
    

部署 Azure 應用程式閘道輸入控制器附加元件

建立輸入控制器部署資訊清單

---
# file: pet-supplies-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: pet-supplies-ingress
  annotations:
    kubernetes.io/ingress.class: azure/application-gateway

spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: store-front
            port:
              number: 80
      - path: /order-service
        pathType: Prefix
        backend:
          service:
            name: order-service
            port:
              number: 3000
      - path: /product-service
        pathType: Prefix
        backend:
          service:
            name: product-service
            port:
              number: 3002

部署輸入控制器

$namespace="namespace"
$file="pet-supplies-ingress.yaml"
kubectl apply -f $file -n $namespace

安裝入口控制器為 Helm 圖表

在前幾個步驟,我們會在您的 Kubernetes 叢集安裝 Helm’s Tiller。 利用 Cloud Shell 來安裝 AGIC Helm 套件:

  1. 新增 application-gateway-kubernetes-ingress helm 存放庫並執行 helm 更新

    helm repo add application-gateway-kubernetes-ingress https://appgwingress.blob.core.windows.net/ingress-azure-helm-package/
    helm repo update
    
  2. 下載 helm-config.yaml,其可設定 AGIC:

    wget https://raw.githubusercontent.com/Azure/application-gateway-kubernetes-ingress/master/docs/examples/sample-helm-config.yaml -O helm-config.yaml
    

    或者,複製下列 YAML 檔案:

    # This file contains the essential configs for the ingress controller helm chart
    
    # Verbosity level of the App Gateway Ingress Controller
    verbosityLevel: 3
    
    ################################################################################
    # Specify which application gateway the ingress controller must manage
    #
    appgw:
        subscriptionId: <subscriptionId>
        resourceGroup: <resourceGroupName>
        name: <applicationGatewayName>
    
        # Setting appgw.shared to "true" creates an AzureIngressProhibitedTarget CRD.
        # This prohibits AGIC from applying config for any host/path.
        # Use "kubectl get AzureIngressProhibitedTargets" to view and change this.
        shared: false
    
    ################################################################################
    # Specify which kubernetes namespace the ingress controller must watch
    # Default value is "default"
    # Leaving this variable out or setting it to blank or empty string would
    # result in Ingress Controller observing all accessible namespaces.
    #
    # kubernetes:
    #   watchNamespace: <namespace>
    
    ################################################################################
    # Specify the authentication with Azure Resource Manager
    #
    # Two authentication methods are available:
    # - Option 1: Azure-AD-workload-identity
    armAuth:
        type: workloadIdentity
        identityClientID:  <identityClientId>
    
    ## Alternatively you can use Service Principal credentials
    # armAuth:
    #    type: servicePrincipal
    #    secretJSON: <<Generate this value with: "az ad sp create-for-rbac --role Contributor --sdk-auth | base64 -w0" >>
    
    ################################################################################
    # Specify if the cluster is Kubernetes RBAC enabled or not
    rbac:
        enabled: false # true/false
    
    # Specify aks cluster related information. THIS IS BEING DEPRECATED.
    aksClusterConfiguration:
        apiServerAddress: <aks-api-server-address>
    
  3. 編輯 helm-config.yaml 並填滿 appgwarmAuth 的值。

    注意

    <identity-client-id> 是您在上節中設定的 Microsoft Entra 工作負載識別碼屬性。 您可以執行下列命令來擷取這項資訊:az identity show -g <resourcegroup> -n <identity-name>,其中 <resourcegroup> 是裝載 AKS 叢集、應用程式閘道和受控識別相關基礎結構資源的資源群組。

  4. 利用前一步驟的 helm-config.yaml 設定來安裝 Helm 圖表 application-gateway-kubernetes-ingress

    helm install -f <helm-config.yaml> application-gateway-kubernetes-ingress/ingress-azure
    

    或者,您可將 helm-config.yaml 與 Helm 指令結合為單一步驟:

    helm install ./helm/ingress-azure \
         --name ingress-azure \
         --namespace default \
         --debug \
         --set appgw.name=applicationgatewayABCD \
         --set appgw.resourceGroup=your-resource-group \
         --set appgw.subscriptionId=subscription-uuid \
         --set appgw.shared=false \
         --set armAuth.type=servicePrincipal \
         --set armAuth.secretJSON=$(az ad sp create-for-rbac --role Contributor --sdk-auth | base64 -w0) \
         --set rbac.enabled=true \
         --set verbosityLevel=3 \
         --set kubernetes.watchNamespace=default \
         --set aksClusterConfiguration.apiServerAddress=aks-abcdefg.hcp.westus2.azmk8s.io
    
  5. 檢查新建立 Pod 的記錄,確認其是否正確啟動

請參閱本操作指南,了解如何利用 Azure Azure 應用程式閘道,透過 HTTP 或 HTTPS 向網際網路公開 AKS 服務。

共用應用程式閘道

根據預設,AGIC 假設其所連結的應用程式閘道擁有完全所有權。 AGIC 0.8.0 及更高版本可跟其他 Azure 元件共用同一 Azure 應用程式閘道。 例如,我們可以將相同的應用程式閘道用於虛擬機器擴展集和 AKS 叢集上所裝載的應用程式。

啟用此設定之前,請「備份應用程式閘道設定」

  1. Azure 入口網站中,導覽至您的 Application Gateway 執行個體
  2. 在 [自動化] 區段下方,選取 [匯出範本],然後選取 [下載]

您所下載的 zip 檔案包含可用來還原應用程式閘道的 JSON 範本、bash 和 PowerShell 指令碼

範例案例

我們來看看虛構 Azure 應用程式閘道,這管理兩個網站的流量:

  • dev.contoso.com - 使用應用程式閘道和 AGIC,以在新 AKS 叢集上託管
  • prod.contoso.com - 由 Azure 虛擬機器擴展集託管

根據預設設定,AGIC 會假設其所指向的應用程式閘道擁有 100% 所有權。 AGIC 會覆寫所有 Azure 應用程式閘道的設定。 如果您手動建立 prod.contoso.com 的接聽程式 (在應用程式閘道上),但未在 Kubernetes 輸入加以定義,則 AGIC 會在幾秒內刪除 prod.contoso.com 設定。

若要安裝 AGIC 並從虛擬機器擴展集機器提供 prod.contoso.com,我們必須限制 AGIC 僅設定 dev.contoso.com。 具現化下方的 CRD 可提供輔助:

cat <<EOF | kubectl apply -f -
apiVersion: "appgw.ingress.k8s.io/v1"
kind: AzureIngressProhibitedTarget
metadata:
  name: prod-contoso-com
spec:
  hostname: prod.contoso.com
EOF

上述命令隨即建立 AzureIngressProhibitedTarget 物件。 這使得 AGIC (0.8.0 及更高版本) 注意到 Azure 應用程式閘道 prod.contoso.com 設定存在,並明確指示其避免變更與該主機名稱有關的任何設定。

利用新 AGIC 安裝來啟用

若要限制 AGIC (0.8.0 和更高版本) 僅用於應用程式閘道設定子集,請修改 helm-config.yaml 範本。 在 appgw: 區段下方,新增 shared 索引鍵,然後將其設定為 true

appgw:
    subscriptionId: <subscriptionId>    # existing field
    resourceGroup: <resourceGroupName>  # existing field
    name: <applicationGatewayName>      # existing field
    shared: true                        # <<<<< Add this field to enable shared Application Gateway >>>>>

套用 Helm 變更:

  1. 確保 AzureIngressProhibitedTarget CRD 安裝了以下內容:

    kubectl apply -f https://raw.githubusercontent.com/Azure/application-gateway-kubernetes-ingress/7b55ad194e7582c47589eb9e78615042e00babf3/crds/AzureIngressProhibitedTarget-v1-CRD-v1.yaml
    
  2. 更新 Helm :

    helm upgrade \
        --recreate-pods \
        -f helm-config.yaml \
        ingress-azure application-gateway-kubernetes-ingress/ingress-azure
    

因此,您的 AKS 叢集具有稱為 prohibit-all-targets 的新 AzureIngressProhibitedTarget 執行個體:

kubectl get AzureIngressProhibitedTargets prohibit-all-targets -o yaml

顧名思義,物件 prohibit-all-targets 禁止 AGIC 變更任何主機與路徑設定。 具有 appgw.shared=true 的 Helm 安裝會部署 AGIC,但不會對應用程式閘道進行任何變更。

延伸權限

因為具備 appgw.shared=true 的 Helm 和預設 prohibit-all-targets 會封鎖 AGIC 使其無法套用設定,所以請加寬 AGIC 權限:

  1. 使用下列包含您特定設定的程式碼片段,建立名為 AzureIngressProhibitedTarget 的新 YAML 檔案:

    cat <<EOF | kubectl apply -f -
    apiVersion: "appgw.ingress.k8s.io/v1"
    kind: AzureIngressProhibitedTarget
    metadata:
      name: your-custom-prohibitions
    spec:
      hostname: your.own-hostname.com
    EOF
    
  2. 僅有在建立自己的自訂禁止之後,才能刪除過於廣泛的預設禁止:

    kubectl delete AzureIngressProhibitedTarget prohibit-all-targets
    

啟用現有 AGIC 安裝

假設叢集已有運作中 AKS 叢集、應用程式閘道和已設定的 AGIC。 我們有 prod.contoso.com 輸入,並且成功從叢集為其提供流量。 我們想要在現有 Azure 應用程式閘道新增 staging.contoso.com,但需要將其託管於 VM。 我們將重複利用現有 Azure 應用程式閘道,並手動設定 staging.contoso.com 的接聽程式與後端集區。 不過,手動調整 Azure 應用程式閘道設定 (使用入口網站ARM APITerraform) 與 AGIC 對完全所有權的假設發生衝突。 在我們套用變更後不久,AGIC 就會將其覆寫或刪除。

我們可禁止 AGIC 變更設定子集。

  1. 建立名為 AzureIngressProhibitedTarget 且含有下列程式碼片段的新 YAML 檔案:

    cat <<EOF | kubectl apply -f -
    apiVersion: "appgw.ingress.k8s.io/v1"
    kind: AzureIngressProhibitedTarget
    metadata:
      name: manually-configured-staging-environment
    spec:
      hostname: staging.contoso.com
    EOF
    
  2. 檢視新建立物件:

    kubectl get AzureIngressProhibitedTargets
    
  3. 從 Azure 入口網站修改應用程式閘道設定 - 新增接聽程式、路由規則、後端等。我們所建立的新物件 (manually-configured-staging-environment) 禁止 AGIC 覆寫與 staging.contoso.com 相關的應用程式閘道設定。