如何安装使用新应用程序网关的应用程序网关入口控制器 (AGIC)

以下说明假设将在不包含任何现有组件的环境中安装应用程序网关入口控制器 (AGIC)。

所需的命令行工具

请确保已安装以下命令行工具:

创建标识

请按照以下步骤创建 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 资源管理器模板中将要用到此值

  3. 创建稍后要在 Azure 资源管理器模板部署中使用的参数文件。

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

    若要部署启用了“Kubernetes RBAC”的群集,请将 aksEnableRBAC 字段设置为 true

部署组件

此步骤将以下组件添加到订阅:

  1. 下载 Azure 资源管理器模板,并根据需要修改该模板。

    wget https://raw.githubusercontent.com/Azure/application-gateway-kubernetes-ingress/master/deploy/azuredeploy.json -O template.json
    
  2. 使用 Azure CLI 部署 Azure 资源管理器模板。 部署最多可能需要 5 分钟。

    resourceGroupName="MyResourceGroup"
    location="chinanorth2"
    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 群集。 我们将使用 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 Identity

Microsoft Entra Pod Identity 提供对 Azure 资源管理器 (ARM) 的基于令牌的访问。

Microsoft Entra Pod Identity 会将以下组件添加到 Kubernetes 群集:

若要将 Microsoft Entra Pod Identity 安装到群集,请执行以下操作:

  • 已启用 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 包。

  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>
        environment: AzureChinaCloud
    
        # 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
    

    注意

    要部署到主权云,必须添加 appgw.environment 配置参数并将其设置为适当的值,如下所述。

    值:

    • verbosityLevel:设置 AGIC 日志记录基础结构的详细级别。 有关可能的值,请参阅日志记录级别
    • appgw.environment:设置云环境。 可能的值:AZURECHINACLOUDAZUREGERMANCLOUDAZUREPUBLICCLOUD
    • appgw.subscriptionId:应用程序网关所在的 Azure 订阅 ID。 示例: 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 托管标识的资源 ID
    • armAuth.identityClientID:标识的客户端 ID。 下面提供了有关 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 后,接下来可以安装示例应用:

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 向 Internet 公开 AKS 服务的更多示例。