你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

将基于规则的路由与 Azure 容器应用配合使用

本文介绍如何将基于规则的路由与 Azure 容器应用配合使用。 使用基于规则的路由,可以在容器应用环境中创建完全限定的域名(FQDN)。 然后,根据每个请求的路径,使用规则将此 FQDN 的请求路由到不同的容器应用。

先决条件

设置

  1. 运行以下命令,以便从 CLI 登录到 Azure。

    az login
    
  2. 为了确保运行最新版本的 CLI,请运行升级命令。

    az upgrade
    

    忽略有关当前正在使用的模块的任何警告。

  3. 现在已安装当前扩展或模块,接下来请注册 Microsoft.AppMicrosoft.OperationalInsights 命名空间。

    az provider register --namespace Microsoft.App
    
    az provider register --namespace Microsoft.OperationalInsights
    

创建环境变量

创建以下环境变量。

CONTAINER_APP_1_NAME="my-container-app-1"
CONTAINER_APP_1_IMAGE="mcr.microsoft.com/k8se/quickstart:latest"
CONTAINER_APP_1_TARGET_PORT="80"
CONTAINER_APP_2_NAME="my-container-app-2"
CONTAINER_APP_2_IMAGE="mcr.microsoft.com/dotnet/samples:aspnetapp"
CONTAINER_APP_2_TARGET_PORT="8080"
LOCATION="eastus"
RESOURCE_GROUP="my-container-apps"
ENVIRONMENT_NAME="my-container-apps-env"
ROUTE_CONFIG_NAME="my-route-config"

创建容器应用

  1. 运行以下命令以创建第一个容器应用。 此容器应用使用容器应用快速入门镜像。

    az containerapp up \
      --name $CONTAINER_APP_1_NAME \
      --resource-group $RESOURCE_GROUP \
      --location $LOCATION \
      --environment $ENVIRONMENT_NAME \
      --image $CONTAINER_APP_1_IMAGE \
      --target-port $CONTAINER_APP_1_TARGET_PORT \
      --ingress external \
      --query properties.configuration.ingress.fqdn
    
  2. 运行以下命令以创建第二个容器应用。 此容器应用使用 ASP.NET 快速入门映像。

    az containerapp up \
      --name $CONTAINER_APP_2_NAME \
      --resource-group $RESOURCE_GROUP \
      --location $LOCATION \
      --environment $ENVIRONMENT_NAME \
      --image $CONTAINER_APP_2_IMAGE \
      --target-port $CONTAINER_APP_2_TARGET_PORT \
      --ingress external \
      --query properties.configuration.ingress.fqdn
    
  3. 创建 HTTP 路由配置。

    创建以下文件并将其另存为 routing.yml

    rules:
      - description: App 1 rule
        routes:
          - match:
              prefix: /app1
            action:
              prefixRewrite: /
        targets:
          - containerApp: my-container-app-1
      - description: App 2 rule
        routes:
          - match:
              path: /app2
            action:
              prefixRewrite: /
          - match:
              path: /
        targets:
          - containerApp: my-container-app-2
    

    此配置定义 HTTP 流量的两个路由规则。

    房产 DESCRIPTION
    description 规则的人类可读标签
    routes.match.prefix 要匹配的 URL 路径前缀。 例如,/api
    routes.action.prefixRewrite 在转发之前,将匹配的前缀替换为什么。
    targets.containerApp 发送匹配路由请求的容器应用的名称。

    这些规则允许域上的不同路径路由到不同的容器应用,同时在请求路径到达目标应用之前修改请求路径。

    未列出的可能影响路由的其他属性包括以下内容。

    房产 DESCRIPTION
    route.match.path 完全匹配路径定义。
    route.match.pathSeparatedPrefix 匹配“/”边界上的路由,而不是任何文本。 例如,如果将值设置为/product,则它将匹配/product/1,但不匹配/product1
    route.match.caseSensitive 控制路由模式是否与区分大小写匹配。
    target.label 路由到容器应用中的特定标记修订。
    target.revision 路由到容器应用中的特定标记修订。
  4. 运行以下命令以创建 HTTP 路由配置。

    az containerapp env http-route-config create \
      --http-route-config-name $ROUTE_CONFIG_NAME \
      --resource-group $RESOURCE_GROUP \
      --name $ENVIRONMENT_NAME \
      --yaml routing.yml \
      --query properties.fqdn
    

    HTTP 路由配置的完全限定域名(FQDN)如以下示例所示: my-route-config.ambitiouspebble-11ba6155.eastus.azurecontainerapps.io

  1. 确保两个容器应用已存在。

  2. 创建以下 Bicep 文件并将其另存为 routing.bicep

    resource containerAppsEnvironment 'Microsoft.App/managedEnvironments@2024-10-02-preview' = {
      name: 'my-container-apps-env'
      location: 'eastus'
      tags: {}
      properties: {
        workloadProfiles: [
            {
                workloadProfileType: 'Consumption'
                name: 'Consumption'
            }
        ]
      }
    }
    
    resource httpRouteConfig 'Microsoft.App/managedEnvironments/httpRouteConfigs@2024-10-02-preview' = {
      parent: containerAppsEnvironment
      name: 'my-route-config'
      location: 'eastus'
      properties: {
        rules: [
            {
                description: 'App 1 rule'
                routes: [
                    {
                        match: {
                            prefix: '/app1'
                        }
                        action: {
                            prefixRewrite: '/'
                        }
                    }
                ]
                targets: [
                    {
                        containerApp: 'my-container-app-1'
                    }
                ]
            }
            {
                description: 'App 2 rule'
                routes: [
                    {
                        match: {
                            path: '/app2'
                        }
                        action: {
                            prefixRewrite: '/'
                        }
                    }
                    {
                        match: {
                            path: '/'
                        }
                    }
                ]
                targets: [
                    {
                        containerApp: 'my-container-app-2'
                    }
                ]
            }
        ]
      }
    }
    
    output fqdn string = httpRouteConfig.properties.fqdn
    
  3. 使用以下命令部署 Bicep 文件:

    az deployment group create `
      --name $ROUTE_CONFIG_NAME `
      --resource-group $RESOURCE_GROUP `
      --template-file routing.bicep
    
  4. 在输出中查找 outputs,它包含 HTTP 路由配置的完全限定域名 (FQDN)。 例如:

        "outputs": {
          "fqdn": {
            "type": "String",
            "value": "my-route-config.ambitiouspebble-11ba6155.eastus.azurecontainerapps.io"
          }
        },
    

验证 HTTP 路由配置

  1. 使用路径 /app1 浏览到 HTTP 路由配置 FQDN。

    例如: my-route-config.ambitiouspebble-11ba6155.eastus.azurecontainerapps.io/app1

    你会看到容器应用快速入门映像。

  2. 使用路径 /app2 浏览到 HTTP 路由配置 FQDN。

    例如: my-route-config.ambitiouspebble-11ba6155.eastus.azurecontainerapps.io/app2

    你将看到 ASP.NET 快速入门映像。

清理资源

如果你不打算继续使用此应用程序,请运行以下命令以删除资源组以及本快速入门中创建的所有资源。

谨慎

以下命令删除指定的资源组及其包含的所有资源。 如果本快速入门范围之外的资源存在于指定的资源组中,则这些资源也会被删除。

az group delete --name my-container-apps