Share via


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

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

提示

另請參閱什麼是容器 應用程式閘道。

必要的命令行工具

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

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

Portal launch

您的 Azure Cloud Shell 已經有所有必要的工具。 如果您選擇使用另一個環境,請確定已安裝下列命令列工具:

建立身分識別

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

  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)
    

    appId JSON 輸出中的 和 password 值將會用於下列步驟

  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 的叢集,請將 字段設定 aksEnableRBACtrue

部署元件

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

  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-ingress helm 套件:

    • 已啟用 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:設定雲端環境。 可能的值:AZURECHINACLOUD、、AZUREGERMANCLOUDAZUREPUBLICCLOUDAZUREUSGOVERNMENTCLOUD
    • 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 服務,並搭配 應用程式閘道。