引导启动 Amazon ECS Windows 容器实例以传递数据 - Amazon Elastic Container Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

引导启动 Amazon ECS Windows 容器实例以传递数据

在启动 Amazon EC2 实例时,您可以将用户数据传递到 EC2 实例。数据可以用于执行常见的自动配置任务,甚至用于在实例启动时运行脚本。对于 Amazon ECS,最常见的用户数据使用案例是将配置信息传递到 Docker 进程守护程序和 Amazon ECS 容器实例。

您可以将多类用户数据传递到 Amazon EC2,其中包括云 boothook、Shell 脚本和 cloud-init 指令。有关这些和其他格式类型的更多信息,请参阅 Cloud-Init 文档

您可以在使用 Amazon EC2 启动向导时传递此用户数据。有关更多信息,请参阅 启动 Amazon ECS Linux 容器实例

原定设置 Windows 用户数据

该示例用户数据脚本显示了在使用控制台时 Windows 容器实例接收的默认用户数据。下面的脚本将执行以下操作:

  • 将集群名称设置为您输入的名称。

  • 设置任务的 IAM 角色。

  • json-file 以及 awslogs 设置为可用的日志记录驱动程序。

此外,使用 awsvpc 网络模式时,以下选项可用。

  • EnableTaskENI:此标志打开任务联网,并且在使用 awsvpc 网络模式时是必需的。

  • AwsvpcBlockIMDS:此可选标志阻止在 awsvpc 网络模式下运行的任务容器的 IMDS 访问。

  • AwsvpcAdditionalLocalRoutes:此可选标志允许您有其他路由。

    ip-address 替换为附加路由的IP地址,例如172.31.42.23/32。

您可以将该脚本用于自己的容器实例,但前提是它们是从经 Amazon ECS 优化的 Windows Server AMI 启动的。

替换 -Cluster cluster-name 行以指定您自己的集群名称。

<powershell> Initialize-ECSAgent -Cluster cluster-name -EnableTaskIAMRole -LoggingDrivers '["json-file","awslogs"]' -EnableTaskENI -AwsvpcBlockIMDS -AwsvpcAdditionalLocalRoutes '["ip-address"]' </powershell>

对于配置为使用 awslogs 日志记录驱动程序的 Windows 任务,您还必须在容器实例上设置 ECS_ENABLE_AWSLOGS_EXECUTIONROLE_OVERRIDE 环境变量。使用下面的语法。

替换 -Cluster cluster-name 行以指定您自己的集群名称。

<powershell> [Environment]::SetEnvironmentVariable("ECS_ENABLE_AWSLOGS_EXECUTIONROLE_OVERRIDE", $TRUE, "Machine") Initialize-ECSAgent -Cluster cluster-name -EnableTaskIAMRole -LoggingDrivers '["json-file","awslogs"]' </powershell>

Windows 代理安装用户数据

此示例用户数据脚本在使用 Windows_Server-2016-English-Full-Containers AMI 启动的实例上安装 Amazon ECS 容器代理。该脚本已根据 Amazon ECS 容器代理的 GitHub 存储库 README 页面中的代理安装说明进行了调整。

注意

此脚本作为示例分享。使用经 Amazon ECS 优化的 Windows AMI 的 Windows 容器更容易上手。有关更多信息,请参阅 创建 Fargate 启动类型的 Amazon ECS 集群

您可以将该脚本用于自己的容器实例(前提是它们是使用 Windows_Server-2016-English-Full-Containers AMI 版本启动的)。请记得替换 windows 行来指定自己的集群名称(如果您使用的不是名为 windows 的集群)。

<powershell> # Set up directories the agent uses New-Item -Type directory -Path ${env:ProgramFiles}\Amazon\ECS -Force New-Item -Type directory -Path ${env:ProgramData}\Amazon\ECS -Force New-Item -Type directory -Path ${env:ProgramData}\Amazon\ECS\data -Force # Set up configuration $ecsExeDir = "${env:ProgramFiles}\Amazon\ECS" [Environment]::SetEnvironmentVariable("ECS_CLUSTER", "windows", "Machine") [Environment]::SetEnvironmentVariable("ECS_LOGFILE", "${env:ProgramData}\Amazon\ECS\log\ecs-agent.log", "Machine") [Environment]::SetEnvironmentVariable("ECS_DATADIR", "${env:ProgramData}\Amazon\ECS\data", "Machine") # Download the agent $agentVersion = "latest" $agentZipUri = "https://s3.amazonaws.com/amazon-ecs-agent/ecs-agent-windows-$agentVersion.zip" $zipFile = "${env:TEMP}\ecs-agent.zip" Invoke-RestMethod -OutFile $zipFile -Uri $agentZipUri # Put the executables in the executable directory. Expand-Archive -Path $zipFile -DestinationPath $ecsExeDir -Force Set-Location ${ecsExeDir} # Set $EnableTaskIAMRoles to $true to enable task IAM roles # Note that enabling IAM roles will make port 80 unavailable for tasks. [bool]$EnableTaskIAMRoles = $false if (${EnableTaskIAMRoles}) { $HostSetupScript = Invoke-WebRequest https://raw.githubusercontent.com/aws/amazon-ecs-agent/master/misc/windows-deploy/hostsetup.ps1 Invoke-Expression $($HostSetupScript.Content) } # Install the agent service New-Service -Name "AmazonECS" ` -BinaryPathName "$ecsExeDir\amazon-ecs-agent.exe -windows-service" ` -DisplayName "Amazon ECS" ` -Description "Amazon ECS service runs the Amazon ECS agent" ` -DependsOn Docker ` -StartupType Manual sc.exe failure AmazonECS reset=300 actions=restart/5000/restart/30000/restart/60000 sc.exe failureflag AmazonECS 1 Start-Service AmazonECS </powershell>