共用方式為


使用提取祕密將映像從 Azure 容器登錄提取至 Kubernetes 叢集

您可以使用 Azure 容器登錄,作為具有任何 Kubernetes 叢集的容器映像來源,包括「本機」Kubernetes 叢集,例如 minikubekind。 本文說明如何使用 Azure 容器登錄的認證來建立 Kubernetes 提取祕密。 然後,使用此秘密從 Pod 部署中的 Azure 容器登錄提取映像。

此範例會使用 Microsoft Entra 服務主體認證來建立提取祕密。 您也可以使用其他 Azure 容器登錄認證來設定提取祕密,例如存放庫範圍的存取權杖

注意

雖然提取祕密是常用的,但會帶來額外的管理負荷。 如果您是使用 Azure Kubernetes Service,我們建議使用其他選項,例如使用叢集的受控識別或服務主體來安全地提取映像,而不需在每個 Pod 上進行設定額外的 imagePullSecrets 設定。

必要條件

本文假設您已建立私人 Azure 容器登錄。 您也需要使 Kubernetes 叢集執行中,且可透過 kubectl 命令列工具存取。

建立服務主體

若要建立具容器登錄存取權的服務主體,請在 Azure Cloud Shell 或本機的 Azure CLI 安裝中執行下列指令碼。 此指令碼會針對 Bash 殼層加以格式化。

執行指令碼之前,請使用容器登錄的名稱更新 ACR_NAME 變數。 SERVICE_PRINCIPAL_NAME 值在您的 Microsoft Entra 租用戶中必須是唯一的。 如果您收到「'http://acr-service-principal' already exists.」錯誤,請為服務主體指定不同的名稱。

如果您要授與不同權限,則可以選擇修改 az ad sp create-for-rbac 命令中的 --role 值。 如需角色的完整清單,請參閱 ACR 角色和權限 \(英文\)。

執行指令碼之後,請記下的服務主體的識別碼密碼。 一旦擁有其認證,便可以將您的應用程式和服務設定為以服務主體向您的容器登錄進行驗證。

#!/bin/bash
# This script requires Azure CLI version 2.25.0 or later. Check version with `az --version`.

# Modify for your environment.
# ACR_NAME: The name of your Azure Container Registry
# SERVICE_PRINCIPAL_NAME: Must be unique within your AD tenant
ACR_NAME=$containerRegistry
SERVICE_PRINCIPAL_NAME=$servicePrincipal

# Obtain the full registry ID
ACR_REGISTRY_ID=$(az acr show --name $ACR_NAME --query "id" --output tsv)
# echo $registryId

# Create the service principal with rights scoped to the registry.
# Default permissions are for docker pull access. Modify the '--role'
# argument value as desired:
# acrpull:     pull only
# acrpush:     push and pull
# owner:       push, pull, and assign roles
PASSWORD=$(az ad sp create-for-rbac --name $SERVICE_PRINCIPAL_NAME --scopes $ACR_REGISTRY_ID --role acrpull --query "password" --output tsv)
USER_NAME=$(az ad sp list --display-name $SERVICE_PRINCIPAL_NAME --query "[].appId" --output tsv)

# Output the service principal's credentials; use these in your services and
# applications to authenticate to the container registry.
echo "Service principal ID: $USER_NAME"
echo "Service principal password: $PASSWORD"

使用現有的服務主體

若要授與現有服務主體登錄存取權,您必須為服務主體指派新的角色。 如同建立新的服務主體一樣,您可以授與提取、推送和提取,以及擁有者存取權等權限。

以下指令碼使用 az role assignment create 命令,以授與您在 SERVICE_PRINCIPAL_ID 變數中所指定的服務主體提取權限。 如果您要授與不同層級的存取權,請調整 --role 值。

#!/bin/bash
# Modify for your environment. The ACR_NAME is the name of your Azure Container
# Registry, and the SERVICE_PRINCIPAL_ID is the service principal's 'appId' or
# one of its 'servicePrincipalNames' values.
ACR_NAME=$containerRegistry
SERVICE_PRINCIPAL_ID=$servicePrincipal

# Populate value required for subsequent command args
ACR_REGISTRY_ID=$(az acr show --name $ACR_NAME --query id --output tsv)

# Assign the desired role to the service principal. Modify the '--role' argument
# value as desired:
# acrpull:     pull only
# acrpush:     push and pull
# owner:       push, pull, and assign roles
az role assignment create --assignee $SERVICE_PRINCIPAL_ID --scope $ACR_REGISTRY_ID --role acrpull

如果未儲存或記住服務主體密碼,您可以使用 az ad sp credential reset 命令來將其重設:

az ad sp credential reset  --name http://<service-principal-name> --query password --output tsv

此命令會傳回服務主體的新有效密碼。

建立映像提取密碼

Kubernetes 使用映像提取秘密,來儲存驗證登錄所需的資訊。 若要建立 Azure Container Registry 的提取祕密,請提供服務主體識別碼、密碼和登錄 URL。

使用下列 kubectl 命令建立映像提取祕密:

kubectl create secret docker-registry <secret-name> \
    --namespace <namespace> \
    --docker-server=<container-registry-name>.azurecr.io \
    --docker-username=<service-principal-ID> \
    --docker-password=<service-principal-password>

其中:

Description
secret-name 映像提取密碼的名稱,例如 acr-secret
namespace 要將祕密放入其中的 Kubernetes 命名空間
僅當您想要將秘密放在預設命名空間以外的命名空間時,才需要 Kubernetes 命名空間
container-registry-name Azure Container Registry 的名稱,例如 myregistry

--docker-server 是登錄登入伺服器的完整名稱
service-principal-ID Kubernetes 會使用服務主體識別碼,來存取您的登錄作業
service-principal-password 服務主體密碼

使用映像提取秘密

建立映像提取密碼後,您可以使用該密碼,來建立 Kubernetes 的 pod 和部署。 在部署檔案中的 imagePullSecrets 下提供秘密名稱。 例如:

apiVersion: v1
kind: Pod
metadata:
  name: my-awesome-app-pod
  namespace: awesomeapps
spec:
  containers:
    - name: main-app-container
      image: myregistry.azurecr.io/my-awesome-app:v1
      imagePullPolicy: IfNotPresent
  imagePullSecrets:
    - name: acr-secret

在上述範例中,my-awesome-app:v1 是要從 Azure Container Registry 提取的映像名稱,且 acr-secret 是您所建立並用來存取登錄的提取秘密名稱。 當您部署 pod 時,Kubernetes 會自動從登錄中提取映像 (若該映像尚未存在於叢集中)。

下一步