簡體   English   中英

在 Azure VM 上運行的 Docker 容器中使用 Azure 托管標識

[英]Using Azure Managed Identity in a Docker container running on an Azure VM

我有一個問題,我無法通過 Azure 容器實例和 Azure 容器應用程序訪問我公司的容器注冊表,這與 .NET 和公司注冊表的私有鏈接有關,而且無法鏈接動態啟動的容器. 最終我想 go 到 Kube.netes 作為我的批處理作業工作負載的平台,但現在我必須找到一個快速的解決方案,讓它能夠至少運行一個容器,並且可能在必要時手動擴展這些容器(一個是大多數時候可能就足夠了)。

現在,我旨在實現這一目標的方法是通過簡單地啟動一個 VM(必要時可能還有幾個)並使用(Python)應用程序代碼在此 VM 上運行一個 docker 容器。

現在我想知道 Docker 容器是否以及如何使用分配給 VM 的(系統/用戶分配的)托管標識。 當分配給 ACI 容器並在啟動時分配給它一個用戶管理的身份時,我可以很容易地只使用如下代碼:

default_credential = DefaultAzureCredential()
q_client = QueueClient(
  credential=default_credential,
  queue_name='Queuename',
  accounrt_url='someurl'
)

並且能夠訪問 - 例如 - 這個隊列。 無需請求任何類型的令牌,無需指定任何類型的環境變量。

現在我懷疑這是否會在 docker 容器中運行,該容器在分配有用戶身份的 VM 中運行,因為用戶身份並未真正直接分配給 docker 容器。 有什么辦法仍然可以實現這一點,或者這是一個愚蠢的差事,我現在應該只使用環境變量嗎? 我不太喜歡后者的想法,但我還沒有找到以這種特殊方式使用托管身份的方法。

• 是的,您可以通過一種方式使用分配給部署在其上的 docker 容器上的 VM 的托管標識。 請按照下面給出的步驟進行操作,您肯定可以在 docker 容器中使用分配給 VM 的托管標識:-

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 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 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 資源進行身份驗證。

b) 要執行上述操作,您必須在 Azure 中部署 VM 並通過 RDP 連接到它,然后執行以下命令以啟動容器:-

docker run -ti -v c:\miat-helper:c:\miat-helper mcr.microsoft.com/powershell:6.2.3-nanoserver-1809

使用以下命令,您將在容器內獲得一個 PowerShell 會話,您可以在其中使用“幫助器”工具獲取訪問令牌

$access_token = Invoke-Expression "c:\miat-helper\bin\client.exe --folder c:\miat-helper --resource https://management.azure.com/"

c) 您可以使用該訪問令牌調用 Azure API 因此,要獲取有關生成的 VM 的更多信息,請確保將資源組名稱和虛擬機名稱替換為您的值,如下所示:-

 $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
  Write-Host $vmInfo

上述命令的執行應該為你提供了必要的工具來干凈地處理來自容器的 Azure 資源的身份驗證。 此外,然后在 ARM 模板部署期間將托管標識分配給 VM,並將角色分配給該托管標識以允許對 VM 進行讀取訪問 完成這些操作后, you make a 'GET' 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 '.request' file in that folder with the targeted resource as content you make a 'GET' 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 '.request' file in that folder with the targeted resource as content

d) 然后通過讀取文件並請求在同一類中創建響應文件的令牌並將令牌寫入文件的事件通知“助手”服務 然后客戶端選擇響應文件,讀取令牌,並將其寫入標准輸出 通過這種方式,我們可以在同樣非常少的設置和高穩定性的情況下想出更好的東西。

有關這方面的更多詳細信息,請參閱以下鏈接:-

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

我無意中發現了這個問題,因為原始問題“在 Azure VM 上運行的 Docker 容器中使用 Azure 托管身份

這就是我的做法。

先決條件:

  • Azure VM,上面有 linux 和 docker
  • VM 具有分配給它的托管標識

在 docker 中啟動 azure cli。Host.network 很重要,因為我們需要到達Azure 提供的本地端點。

docker run --network host -it mcr.microsoft.com/azure-cli

在容器內,您現在可以運行:

az login --identity

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM