在 Azure VM 上运行的 Docker 容器中使用 Azure 托管标识

Tim*_*Tim 5 virtual-machine azure docker azure-managed-identity azure-vm

我遇到一个问题,无法通过 Azure 容器实例和 Azure 容器应用程序访问公司的容器注册表,这与 VNET 和公司注册表的私有链接以及无法链接动态启动的容器有关。最终我想使用 Kubernetes 作为我的批处理作业工作负载的平台,但现在我必须找到一种快速解决方案,使其能够运行至少一个容器,并且可能在必要时手动扩展这些容器(一个可能就足够了)大部分的时间)。

现在,我想要实现这一目标的方法是简单地启动一个虚拟机(必要时可能还会启动更多虚拟机)并使用(Python)应用程序代码在此虚拟机上运行 Docker 容器。

现在我想知道 Docker 容器是否可以使用分配给虚拟机的托管身份(系统/用户分配的),如果可以,如何使用。当分配给 ACI 容器并在启动时分配用户管理的标识时,我可以轻松地使用如下代码:

default_credential = DefaultAzureCredential()
q_client = QueueClient(
  credential=default_credential,
  queue_name='Queuename',
  accounrt_url='someurl'
)
Run Code Online (Sandbox Code Playgroud)

并能够访问 - 例如 - 这个队列。无需请求任何类型的令牌,无需指定任何类型的环境变量。

现在我怀疑这是否适用于在分配了用户身份的虚拟机中运行的 docker 容器,因为用户身份并未真正直接分配给 docker 容器。有什么办法仍然可以实现这一目标,还是这是一个愚蠢的差事,我现在应该只使用环境变量吗?我不太喜欢后者的想法,但我还没有找到一种以这种特殊方式使用托管身份的方法。

小智 3

\xe2\x80\xa2 是的,有一种方法可以在部署于其上的 docker 容器上使用分配给虚拟机的托管标识。请按照下面给出的步骤操作,您一定可以在 docker 容器中使用分配给 VM 的托管标识:-

\n

a) 因此,到get an access token to authenticate a request to an Azure resource using a managed identity, you have to call a special URL: - https://169.254.169.254/metadata/identity/oauth2/token . Also, to ensure that the authentication token and regarding networking works in a container, you will have to use a tool which is intended to be run as Windows service on the container host and uses file-based communication to wait for requests by monitoring a folder. The container puts a request file in that folder, the service requests a token and responds to the container with a response file。然后,容器可以使用该令牌对您需要的任何 Azure 资源进行身份验证。

\n

b) 要执行上述操作,您必须在 Azure 中部署 VM 并通过 RDP 连接到它,然后执行以下命令来启动容器:-

\n
docker run -ti -v c:\\miat-helper:c:\\miat-helper mcr.microsoft.com/powershell:6.2.3-nanoserver-1809\n
Run Code Online (Sandbox Code Playgroud)\n

使用以下命令,您将在容器内获得一个PowerShell 会话,您可以在其中使用 \xe2\x80\x98helper\xe2\x80\x99 工具来获取访问令牌。

\n
$access_token = Invoke-Expression "c:\\miat-helper\\bin\\client.exe --folder c:\\miat-helper --resource https://management.azure.com/"\n
Run Code Online (Sandbox Code Playgroud)\n

c) 您可以使用该访问令牌调用 Azure API。因此,要获取有关生成的 VM 的更多信息,请确保将资源组名称和虚拟机名称替换为您的值,如下所示:-

\n
 $vmInfo = (Invoke-WebRequest -Uri \'https://management.azure.com/subscriptions/ 94670b10-08d0-4d17-bcfe-e01f701be9ff/resourceGroups/<resource group name>/providers/Microsoft.Compute/ virtualMachines/<virtual machine name>?api-version=2017-12-01\' -Method GET -ContentType "application/json" -Headers @{ Authorization ="Bearer $access_token"}).content\n  Write-Host $vmInfo\n
Run Code Online (Sandbox Code Playgroud)\n

上述命令的执行应该为您提供必要的工具来干净地处理针对容器中的 Azure 资源的身份验证。此外,然后在 ARM 模板部署期间向 VM 分配托管标识,并向该托管标识分配角色以允许对 VM 进行读取访问。一旦这些事情都完成了you make a \xe2\x80\x98GET\xe2\x80\x99 request to the token endpoint from the VM and you get an access token. Then, the helper tool will run as a Windows service and is configured to listen on a particular folder post which the client creates a \xe2\x80\x98.request\xe2\x80\x99 file in that folder with the targeted resource as content

\n

d) 然后通过一个事件通知 \xe2\x80\x98helper\xe2\x80\x99 服务,该事件读取文件并请求在同一类中创建响应文件的令牌,并将令牌写入文件。然后,客户端选择响应文件,读取令牌,并将其写入标准输出。通过这种方式,我们可以想到更好的东西,同时具有同样非常低的设置量和高稳定性。

\n

有关此内容的更多详细信息,请参阅以下链接:-

\n

https://tobiasfenster.io/using-azure-management-identities-in-containers

\n