本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
任务的 Windows IAM 角色的其他配置
重要
对于 Fargate 上使用任务角色的 Windows 容器,无需采取进一步的操作。对于 EC2 上使用任务角色的 Windows 容器,请按照以下步骤操作。
使用 Windows 功能的任务的 IAM 角色需要在 EC2 上进行额外配置,但此配置很大程度上类似于在 Linux 容器实例上配置任务的 IAM 角色。必须满足以下要求才能为 Windows 容器配置任务的 IAM 角色。
-
在启动容器实例时,您必须在容器实例用户数据脚本中设置
-EnableTaskIAMRole
选项。EnableTaskIAMRole
为任务打开任务 IAM 角色功能。例如:<powershell> Import-Module ECSTools Initialize-ECSAgent -Cluster '
windows
' -EnableTaskIAMRole </powershell> -
您必须使用任务的 IAM 角色的容器引导脚本中提供的联网命令来引导容器。
-
您必须为任务创建 IAM 角色和策略。有关更多信息,请参阅为任务创建 IAM 角色和策略:
-
您必须指定在注册任务定义时为任务创建的 IAM 角色,或者在运行任务时作为覆盖指定。有关更多信息,请参阅为任务指定 IAM 角色:
-
任务的 IAM 角色凭证提供程序在容器实例上使用端口 80。因此,如果在容器实例上配置任务的 IAM 角色,则容器无法将端口 80 用于任何端口映射中的主机端口。要在端口 80 上公开容器,建议您为这些容器配置一个使用负载平衡功能的服务。您可以在负载均衡器上使用端口 80。通过这样做,可以将流量传送到容器实例上的另一个主机端口。有关更多信息,请参阅服务负载均衡:
-
如果重新启动 Windows 实例,则必须删除代理接口并再次初始化 Amazon ECS 容器代理以备份凭证代理。
任务的 IAM 角色的容器引导脚本
必须先使用所需的联网命令引导容器,然后容器才能访问容器实例上的凭证代理来获得凭证。当容器启动时,应在容器上运行以下代码示例脚本。
注意
在 Windows 上使用 awsvpc
网络模式时,不需要运行此脚本。
如果您运行包含 Powershell 的 Windows 容器,请使用以下脚本:
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"). You may # not use this file except in compliance with the License. A copy of the # License is located at # # http://aws.amazon.com/apache2.0/ # # or in the "license" file accompanying this file. This file is distributed # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either # express or implied. See the License for the specific language governing # permissions and limitations under the License. $gateway = (Get-NetRoute | Where { $_.DestinationPrefix -eq '0.0.0.0/0' } | Sort-Object RouteMetric | Select NextHop).NextHop $ifIndex = (Get-NetAdapter -InterfaceDescription "Hyper-V Virtual Ethernet*" | Sort-Object | Select ifIndex).ifIndex New-NetRoute -DestinationPrefix 169.254.170.2/32 -InterfaceIndex $ifIndex -NextHop $gateway -PolicyStore ActiveStore # credentials API New-NetRoute -DestinationPrefix 169.254.169.254/32 -InterfaceIndex $ifIndex -NextHop $gateway -PolicyStore ActiveStore # metadata API
如果您运行的 Windows 容器只有命令 shell,请使用以下脚本:
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"). You may # not use this file except in compliance with the License. A copy of the # License is located at # # http://aws.amazon.com/apache2.0/ # # or in the "license" file accompanying this file. This file is distributed # on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either # express or implied. See the License for the specific language governing # permissions and limitations under the License. for /f "tokens=1" %i in ('netsh interface ipv4 show interfaces ^| findstr /x /r ".*vEthernet.*"') do set interface=%i for /f "tokens=3" %i in ('netsh interface ipv4 show addresses %interface% ^| findstr /x /r ".*Default.Gateway.*"') do set gateway=%i netsh interface ipv4 add route prefix=169.254.170.2/32 interface="%interface%" nexthop="%gateway%" store=active # credentials API netsh interface ipv4 add route prefix=169.254.169.254/32 interface="%interface%" nexthop="%gateway%" store=active # metadata API