为什么通过API在Windows容器/任务内获取AWS凭证会超时?

时间:2019-06-05 20:23:58

标签: c# .net amazon-web-services containers credentials

根据文档,您应该能够使用以下URL从正在运行的任务(容器)中获取分配给任务定义的角色的凭据:http://169.254.170.2'AWS_CONTAINER_CREDENTIALS_RELATIVE_URI',其中AWS_CONTAINER_CREDENTIALS_RELATIVE_URI解析为一部分网址。

在我的日志文件中,我可以看到它解析为: http://169.254.170.2/v2/credentials/063b6cc6-0dc7-486e-ba0a-843a308b222d

但是调用API会导致超时。端点未监听的原因可能是什么?

容器正在运行Windows-server-2019的ECS_OPTIMIZED映像

private static async Task<string> GetCredentials(EnvironmentOptions opts)
{
    try
    {
        using (var httpClient = new HttpClient())
        {
                using (var request = new HttpRequestMessage(new HttpMethod("GET"), $"http://169.254.170.2{opts.CredentailsUrl}"))
                {
                    var response = await httpClient.SendAsync(request);
                    return await response.Content.ReadAsStringAsync();
                }
            }
        }
        catch (Exception ex)
        {
            return $"{ex.Message} {ex.StackTrace}";
        }
    }

opts.CredentailsUrl ='/ v2 / credentials / 063b6cc6-0dc7-486e-ba0a-843a308b222d',例如,它是从环境变量AWS_CONTAINER_CREDENTIALS_RELATIVE_URI获取的

我收到的错误消息是:连接尝试失败,因为一段时间后连接方未正确响应,或者由于连接的主机未能响应,建立的连接失败

我真的应该找回像这样的json类:

{
    "AccessKeyId": "ACCESS_KEY_ID",
    "Expiration": "EXPIRATION_DATE",
    "RoleArn": "TASK_ROLE_ARN",
    "SecretAccessKey": "SECRET_ACCESS_KEY",
    "Token": "SECURITY_TOKEN_STRING"
}

在容器实例上,我从powershell运行以下两个命令:

Import-Module ECSTools
Initialize-ECSAgent -Cluster 'txp-dev-windows' -EnableTaskIAMRole -Version "latest"

这导致最后一行实际上从未继续(=挂起)

019-06-07T10:15:06Z - [INFO]:Runtime is already installed.
2019-06-07T10:15:06Z - [INFO]:Docker version 18.09.4, build c3516c43ef
2019-06-07T10:15:06Z - [INFO]:Configuring ECS Host...
2019-06-07T10:15:06Z - [INFO]:Checking Hyper-V Network adapter
2019-06-07T10:15:08Z - [INFO]:Default vEthernet adapter found for nat. Using this adapter.
2019-06-07T10:15:08Z - [INFO]:VMNetwork adapter found with mac: 00-15-5D-2B-E1-89
2019-06-07T10:15:08Z - [INFO]:Checking for network adatper with mac: 00-15-5D-2B-E1-89
2019-06-07T10:15:08Z - [INFO]:Network adapter found.
2019-06-07T10:15:08Z - [INFO]:Network adapter found with mac 00-15-5D-2B-E1-89 on interface 2
2019-06-07T10:15:08Z - [INFO]:Getting subnet info from docker...
2019-06-07T10:15:08Z - [INFO]:Docker subnet: 0.0.0.0/0
2019-06-07T10:15:08Z - [INFO]:Docker gateway:
WARNING: Waiting for service 'Docker Engine (docker)' to stop...
WARNING: Waiting for service 'Docker Engine (docker)' to stop...
WARNING: Waiting for service 'Docker Engine (docker)' to stop...
WARNING: Waiting for service 'Docker Engine (docker)' to stop...
WARNING: Waiting for service 'Docker Engine (docker)' to stop...
2019-06-07T10:15:22Z - [INFO]:Docker subnet: 172.31.16.0/20
2019-06-07T10:15:22Z - [INFO]:Docker gateway: 172.31.16.1
2019-06-07T10:15:24Z - [INFO]:Getting net ip address
2019-06-07T10:15:25Z - [INFO]:IP address not found.
Name                           Value
----                           -----
PrefixLength                   32
IPAddress                      169.254.170.2
InterfaceIndex                 2



2019-06-07T10:15:25Z - [INFO]:Creating new virtual network adapter ip...
New-NetIPAddress : Element not found.
At C:\Program Files\WindowsPowerShell\Modules\ECSTools\ECSTools.psm1:1370 char:28
+             $newIpOutput = New-NetIPAddress @IPAddrParams
+                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (MSFT_NetIPAddress:ROOT/StandardCimv2/MSFT_NetIPAddress) [New-NetIPAddress], CimException
    + FullyQualifiedErrorId : Windows System Error 1168,New-NetIPAddress

2019-06-07T10:15:25Z - [INFO]:Virtual network adapter ip created:
2019-06-07T10:15:25Z - [INFO]:Waiting for it to become available on the device...

然后我在AWS文档中找到了这一点:任务容器引导脚本的IAM角色 在容器可以访问容器实例上的凭据代理以获取凭据之前,必须使用所需的联网命令来引导容器。

因此,我将启动脚本添加到了容器程序启动时:

                string script = @"
$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
";
                using (PowerShell PowerShellInstance = PowerShell.Create())
                {
                    PowerShellInstance.AddScript(script);
                    PowerShellInstance.Invoke();
                }

但这并没有改变。

1 个答案:

答案 0 :(得分:0)

失败的确切原因尚不清楚。必须执行某些操作以确保未为ECS群集正确配置EC2实例。它是手动创建的,并且初始化数据的powershell脚本将其连接到(空)集群。之所以这样,是因为群集创建向导仅允许使用Server 2016 AMI。但是令我惊讶的是,今天它还允许使用Server 2019 AMI。用向导来做。容器代码开始起作用。