使用受控識別從 Azure Container Registry 部署至 Azure 容器執行個體

Azure Container Registry (ACR) (部分機器翻譯) 是 Azure 型的受控容器登錄服務,可用來儲存私人 Docker 容器映像。 本文說明如何在部署至 Azure 容器執行個體的容器群組時,提取儲存於 Azure Container Registry 的容器映像。 設定登錄存取的其中一種方式,是建立 Microsoft Entra 受控識別。

使用私人端點限制對 Azure Container Registry (ACR) 的存取時,使用受控識別可讓部署至虛擬網路中的 Azure 容器執行個體能夠透過私人端點存取容器登錄。

必要條件

Azure Container Registry:您需要至少一個映像的進階 SKU Azure 容器登錄。 如果您需要建立登錄,請參閱使用 Azure CLI 建立容器登錄 (部分機器翻譯)。 請務必記下登錄的 idloginServer

Azure CLI:本文中的命令列範例使用 Azure CLI,並使用 Bash 殼層適用的格式。 您可以在本機安裝 Azure CLI,或使用 Azure Cloud Shell

限制

設定登錄驗證

您的容器登錄必須已啟用受信任的服務。 若要尋找如何啟用受信任服務的指示,請參閱 允許受信任的服務安全地存取受網路限制的容器登錄 (部分機器翻譯)。

建立身分識別

使用 az identity create 命令,在您的訂用帳戶中建立身分識別。 您可以使用先前用來建立容器登錄的相同資源群組,或使用不同的資源群組。

az identity create --resource-group myResourceGroup --name myACRId

若要在接下來的步驟中設定身分識別,請使用 az identity show 命令,以將身分識別的資源識別碼與服務主體識別碼儲存在變數中。

若要在未來的步驟中正確設定身分識別,請使用 az 身分識別示範 (部分機器翻譯) 來取得身分識別的資源識別碼和服務主體識別碼,並將其儲存在變數中。

# Get resource ID of the user-assigned identity
USERID=$(az identity show --resource-group myResourceGroup --name myACRId --query id --output tsv)
# Get service principal ID of the user-assigned identity
SPID=$(az identity show --resource-group myResourceGroup --name myACRId --query principalId --output tsv)

您需要身分識別的資源識別碼,才能從虛擬機器登入 CLI。 若要顯示值:

echo $USERID

資源識別碼的格式為:

/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myACRId

您也需要服務主體識別碼,才能將受控識別存取權授與容器登錄。 若要顯示值:

echo $SPID

服務主體識別碼的格式如下:

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx

授與身分識別角色指派

為了讓您的身分識別能存取容器登錄,您必須將角色指派授與它。 使用下列命令將 acrpull 角色授與您剛才建立的身分識別,請務必提供您的登錄識別碼和我們先前取得的服務主體:

az role assignment create --assignee $SPID --scope <registry-id> --role acrpull

使用 Azure Resource Manager (ARM) 範本部署

一開始先將下列 JSON 複製到名為 azuredeploy.json 的新檔案中。 在 Azure Cloud Shell 中,您可以使用 Visual Studio Code,在工作目錄中建立檔案:

code azuredeploy.json

您可以在容器群組定義中加入 imageRegistryCredentials 屬性,以在 ARM 範本中指定 Azure Container Registry 的屬性。 例如,您可以直接指定登錄認證:

注意

這不是完整的 ARM 範本,而是完整範本的 resources 區段的外觀範例。

{
    "type": "Microsoft.ContainerInstance/containerGroups",
    "apiVersion": "2021-09-01",
    "name": "myContainerGroup",
    "location": "norwayeast",
    "identity": {
      "type": "UserAssigned",
      "userAssignedIdentities": {
        "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myACRId": {}
        }
    },
    "properties": {
      "containers": [
        {
          "name": "mycontainer",
          "properties": {
            "image": "myacr.azurecr.io/hello-world:latest",
            "ports": [
              {
                "port": 80,
                "protocol": "TCP"
              }
            ],
            "resources": {
              "requests": {
                "cpu": 1,
                "memoryInGB": 1
              }
            }
        }
        }
      ],
      "imageRegistryCredentials": [
        {
            "server":"myacr.azurecr.io",
            "identity":"/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myACRId"
        }
      ],
      "ipAddress": {
        "ports": [
          {
            "port": 80,
            "protocol": "TCP"
          }
        ],
        "type": "public"
      },
      "osType": "Linux"
    }
  }

部署範本

使用下列命令部署您的 Resource Manager 範本:

az deployment group create --resource-group myResourceGroup --template-file azuredeploy.json

使用 Azure CLI 進行部署

若要使用受控識別來部署容器群組,以透過 Azure CLI 驗證映像提取,請使用下列命令,確定您的 <dns-label> 是全域唯一的:

az container create --name my-containergroup --resource-group myResourceGroup --image <loginServer>/hello-world:v1 --acr-identity $USERID --assign-identity $USERID --ports 80 --dns-name-label <dns-label>

使用 Azure CLI 部署在虛擬網路中

若要使用受控識別將容器群組部署到虛擬網路,以透過 Azure CLI 驗證從執行於私人端點後方的 ACR 進行的映像提取,請使用下列命令:

az container create --name my-containergroup --resource-group myResourceGroup --image <loginServer>/hello-world:v1 --acr-identity $USERID --assign-identity $USERID --vnet "/subscriptions/$SUBSCRIPTION_ID/resourceGroups/"/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/myVNetResourceGroup/providers/ --subnet mySubnetName

如需如何部署至虛擬網路的詳細資訊,請參閱將容器執行個體部署到 Azure 虛擬網路中

使用 YAML 和 Azure CLI 在虛擬網路中部署多容器群組

若要使用受控識別將多容器群組部署到虛擬網路,以透過 Azure CLI 驗證從執行於私人端點後方的 ACR 進行的映像提取,您可以在 YAML 檔案中指定容器群組設定。 然後將 YAML 檔案以參數形式傳遞給命令。

apiVersion: '2021-10-01'
location: eastus
type: Microsoft.ContainerInstance/containerGroups
identity: 
  type: UserAssigned
  userAssignedIdentities: {
    '/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myACRId': {}
    }
properties:
  osType: Linux
  imageRegistryCredentials:
  - server: myacr.azurecr.io
    identity: '/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myACRId'
  subnetIds:
  - id: '/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/myVNetResourceGroup/providers/Microsoft.Network/virtualNetworks/myVNetName/subnets/mySubnetName'
    name: mySubnetName
  containers:
  - name: myContainer-1
    properties:
      resources:
        requests:
          cpu: '.4'
          memoryInGb: '1'
      environmentVariables:
        - name: CONTAINER
          value: 1
      image: 'myacr.azurecr.io/myimage:latest'
  - name: myContainer-2
    properties:
      resources:
        requests:
          cpu: '.4'
          memoryInGb: '1'
      environmentVariables:
        - name: CONTAINER
          value: 2
      image: 'myacr.azurecr.io/myimage:latest'
az container create --name my-containergroup --resource-group myResourceGroup --file my-YAML-file.yaml

如需如何部署至多容器群組的詳細資訊,請參閱部署多容器群組

清除資源

若要從您的 Azure 訂閱中移除所有資源,請刪除資源群組:

az group delete --name myResourceGroup

後續步驟