分享方式:


如何使用新的應用程式閘道安裝應用程式閘道輸入控制器 (AGIC)

下列指示假設應用程式閘道輸入控制器 (AGIC) 會安裝在沒有既有元件的環境中。

必要的命令列工具

我們建議針對下列所有命令列作業使用 Azure Cloud Shell。 從 shell.azure.com 啟動殼層或按一下連結:

或者,使用下列圖示從 Azure 入口網站啟動 Cloud Shell:

入口網站啟動

您的 Azure Cloud Shell 已有所有必要的工具。 如果您選擇使用其他環境,請確保已安裝下列命令列工具:

建立身分識別

請遵循下列步驟以建立 Microsoft Entra 服務主體物件。 記錄 appIdpasswordobjectId 值 - 將在下列步驟中使用這些值。

  1. 建立 AD 服務主體 (深入了解 Azure RBAC):

    az ad sp create-for-rbac --role Contributor --scopes /subscriptions/mySubscriptionID -o json > auth.json
    appId=$(jq -r ".appId" auth.json)
    password=$(jq -r ".password" auth.json)
    

    來自 JSON 輸出的 appIdpassword 值將用於下列步驟

  2. 使用來自上一個命令輸出中的 appId 來取得新服務主體的 id

    objectId=$(az ad sp show --id $appId --query "id" -o tsv)
    

    此命令的輸出為 objectId,將在下方的 Azure Resource Manager 範本中使用

  3. 建立稍後將於 Azure Resource Manager 範本部署中使用的參數檔案。

    cat <<EOF > parameters.json
    {
      "aksServicePrincipalAppId": { "value": "$appId" },
      "aksServicePrincipalClientSecret": { "value": "$password" },
      "aksServicePrincipalObjectId": { "value": "$objectId" },
      "aksEnableRBAC": { "value": false }
    }
    EOF
    

    若要部署已啟用 Kubernetes RBAC 的叢集,請將 aksEnableRBAC 欄位設定為 true

部署元件

此步驟會將下列元件新增至您的訂用帳戶:

  1. 下載 Azure Resource Manager 範本,並視需要修改範本。

    wget https://raw.githubusercontent.com/Azure/application-gateway-kubernetes-ingress/master/deploy/azuredeploy.json -O template.json
    
  2. 使用 Azure CLI 來部署 Azure Resource Manager 範本。 部署最多可能需要 5 分鐘的時間。

    resourceGroupName="MyResourceGroup"
    location="westus2"
    deploymentName="ingress-appgw"
    
    # create a resource group
    az group create -n $resourceGroupName -l $location
    
    # modify the template as needed
    az deployment group create \
            -g $resourceGroupName \
            -n $deploymentName \
            --template-file template.json \
            --parameters parameters.json
    
  3. 部署完成後,將部署輸出下載至名為 deployment-outputs.json 的檔案。

    az deployment group show -g $resourceGroupName -n $deploymentName --query "properties.outputs" -o json > deployment-outputs.json
    

設定應用程式閘道輸入控制器

使用上一節中的指示,我們已建立並設定新的 AKS 叢集和應用程式閘道。 我們現在已準備好將範例應用程式和輸入控制器部署到新的 Kubernetes 基礎結構。

設定 Kubernetes 認證

針對下列步驟,我們需要設定 kubectl 命令,我們將用來連線到新的 Kubernetes 叢集。 Cloud Shell 已安裝 kubectl。 我們將使用 az CLI 來取得 Kubernetes 的認證。

取得新部署的 AKS 的認證 (深入了解):

# use the deployment-outputs.json created after deployment to get the cluster name and resource group name
aksClusterName=$(jq -r ".aksClusterName.value" deployment-outputs.json)
resourceGroupName=$(jq -r ".resourceGroupName.value" deployment-outputs.json)

az aks get-credentials --resource-group $resourceGroupName --name $aksClusterName

安裝 Microsoft Entra Pod 身分識別

Microsoft Entra Pod 身分識別會對 Azure Resource Manager (ARM) 提供以權杖為基礎的存取。

Microsoft Entra Pod 身分識別會將下列元件新增至您的 Kubernetes 叢集:

若要將 Microsoft Entra Pod 身分識別安裝到您的叢集:

  • 已啟用 Kubernetes RBAC 的 AKS 叢集

    kubectl create -f https://raw.githubusercontent.com/Azure/aad-pod-identity/master/deploy/infra/deployment-rbac.yaml
    
  • 已停用 Kubernetes RBAC 的 AKS 叢集

    kubectl create -f https://raw.githubusercontent.com/Azure/aad-pod-identity/master/deploy/infra/deployment.yaml
    

安裝 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
      
    • 已停用 Kubernetes RBAC 的 AKS 叢集

      helm init
      
  2. 新增 AGIC Helm 存放庫:

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

安裝輸入控制器 Helm 圖表

  1. 使用以上建立的 deployment-outputs.json 檔案,並建立下列變數。

    applicationGatewayName=$(jq -r ".applicationGatewayName.value" deployment-outputs.json)
    resourceGroupName=$(jq -r ".resourceGroupName.value" deployment-outputs.json)
    subscriptionId=$(jq -r ".subscriptionId.value" deployment-outputs.json)
    identityClientId=$(jq -r ".identityClientId.value" deployment-outputs.json)
    identityResourceId=$(jq -r ".identityResourceId.value" deployment-outputs.json)
    
  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 will manage
    #
    appgw:
        subscriptionId: <subscriptionId>
        resourceGroup: <resourceGroupName>
        name: <applicationGatewayName>
    
        # Setting appgw.shared to "true" will create 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 will watch
    # Default value is "default"
    # Leaving this variable out or setting it to blank or empty string would
    # result in Ingress Controller observing all acessible namespaces.
    #
    # kubernetes:
    #   watchNamespace: <namespace>
    
    ################################################################################
    # Specify the authentication with Azure Resource Manager
    #
    # Two authentication methods are available:
    # - Option 1: AAD-Pod-Identity (https://github.com/Azure/aad-pod-identity)
    armAuth:
        type: aadPodIdentity
        identityResourceID: <identityResourceId>
        identityClientID:  <identityClientId>
    
    ## Alternatively you can use Service Principal credentials
    # armAuth:
    #    type: servicePrincipal
    #    secretJSON: <<Generate this value with: "az ad sp create-for-rbac --subscription <subscription-uuid> --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

    sed -i "s|<subscriptionId>|${subscriptionId}|g" helm-config.yaml
    sed -i "s|<resourceGroupName>|${resourceGroupName}|g" helm-config.yaml
    sed -i "s|<applicationGatewayName>|${applicationGatewayName}|g" helm-config.yaml
    sed -i "s|<identityResourceId>|${identityResourceId}|g" helm-config.yaml
    sed -i "s|<identityClientId>|${identityClientId}|g" helm-config.yaml
    

    注意

    若要部署至主權雲端 (例如 Azure Government),則須新增 appgw.environment 設定參數並設為適當的值,如下所示。

    值:

    • verbosityLevel:設定 AGIC 記錄基礎結構的詳細資訊層級。 如需可能的值,請參閱記錄層級 \(英文\)。
    • appgw.environment:設定雲端環境環境。 可能的值:AZURECHINACLOUDAZUREGERMANCLOUDAZUREPUBLICCLOUDAZUREUSGOVERNMENTCLOUD
    • appgw.subscriptionId:應用程式閘道所在的 Azure 訂用帳戶識別碼。 範例: a123b234-a3b4-557d-b2df-a0bc12de1234
    • appgw.resourceGroup:建立應用程式閘道所在 Azure 資源群組的名稱。 範例: app-gw-resource-group
    • appgw.name:應用程式閘道的名稱。 範例: applicationgatewayd0f0
    • appgw.shared:此布林值旗標應該預設為 false。 如果您需要共用應用程式閘道,請設定為 true
    • kubernetes.watchNamespace:指定 AGIC 應該監看的命名空間。 命名空間值可以是單一字串值,或命名空間以逗號分隔的清單。
    • armAuth.type:可以是 aadPodIdentityservicePrincipal
    • armAuth.identityResourceID:Azure 受控識別的資源識別碼
    • armAuth.identityClientID:身分識別的用戶端識別碼。 以下提供 identityClientID 的詳細資訊。
    • armAuth.secretJSON:只有在選擇服務主體祕密類型 (當 armAuth.type 設定為 servicePrincipal 時才需要)

    注意

    identityResourceIDidentityClientID 是在部署元件步驟期間建立的值,而且可以使用下列命令再次取得:

    az identity show -g <resource-group> -n <identity-name>
    

    上述命令中的 <resource-group> 是您應用程式閘道的資源群組。 <identity-name> 是所建立身分識別的名稱。 指定訂用帳戶的所有身分識別都可以使用此命令列出:az identity list

  4. 安裝應用程式閘道輸入控制器套件:

    helm install -f helm-config.yaml --generate-name application-gateway-kubernetes-ingress/ingress-azure
    

安裝範例應用程式

既然已安裝應用程式閘道、AKS 和 AGIC 之後,我們可以透過 Azure Cloud Shell 安裝範例應用程式:

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: aspnetapp
  labels:
    app: aspnetapp
spec:
  containers:
  - image: "mcr.microsoft.com/dotnet/samples:aspnetapp"
    name: aspnetapp-image
    ports:
    - containerPort: 8080
      protocol: TCP

---

apiVersion: v1
kind: Service
metadata:
  name: aspnetapp
spec:
  selector:
    app: aspnetapp
  ports:
  - protocol: TCP
    port: 80
    targetPort: 8080

---

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: aspnetapp
  annotations:
    kubernetes.io/ingress.class: azure/application-gateway
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Exact
        backend:
          service:
            name: aspnetapp
            port:
              number: 80
        pathType: Exact
EOF

或者,您可以:

  • 下載上述 YAML 檔案:

    curl https://raw.githubusercontent.com/Azure/application-gateway-kubernetes-ingress/master/docs/examples/aspnetapp.yaml -o aspnetapp.yaml
    
  • 套用 YAML 檔案:

    kubectl apply -f aspnetapp.yaml
    

其他範例

操作說明指南包含更多範例,說明如何使用應用程式閘道透過 HTTP 或 HTTPS 向網際網路公開 AKS 服務。