Share via


끌어오기 비밀을 사용하여 Azure Container Registry에서 Kubernetes 클러스터로 이미지 끌어오기

minikubekind와 같은 ‘로컬’ Kubernetes 클러스터를 포함하여 모든 Kubernetes 클러스터에서 컨테이너 이미지의 원본으로 Azure Container Registry를 사용할 수 있습니다. 이 문서에서는 Azure Container Registry에 대한 자격 증명을 사용하여 Kubernetes 끌어오기 비밀을 만드는 방법을 보여 줍니다. 그런 다음, 비밀을 사용하여 Pod 배포의 Azure Container Registry에서 이미지를 끌어올 수 있습니다.

이 예에서는 Microsoft Entra 서비스 주체 자격 증명을 사용하여 끌어오기 비밀을 만듭니다. 리포지토리 범위 액세스 토큰과 같은 다른 Azure Container Registry 자격 증명을 사용하여 끌어오기 비밀을 구성할 수도 있습니다.

참고 항목

끌어오기 비밀은 일반적으로 사용되지만 추가 관리 오버헤드가 발생합니다. Azure Kubernetes Service를 사용하는 경우 클러스터의 관리 ID 또는 서비스 주체를 사용하는 것과 같은 다른 옵션을 사용하여 각 Pod에서 추가 imagePullSecrets 설정 없이 이미지를 안전하게 끌어오는 것이 좋습니다.

필수 조건

이 문서에서는 프라이빗 Azure Container Registry를 이미 만들었다고 가정합니다. 또한 kubectl 명령줄 도구를 통해 Kubernetes 클러스터를 실행하고 액세스할 수 있어야 합니다.

서비스 주체 만들기

컨테이너 레지스트리에 대한 액세스 권한을 사용하여 서비스 주체를 만들려면 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 역할 및 권한을 참조하세요.

스크립트를 실행한 후 서비스 주체의 ID암호를 기록해 둡니다. 자격 증명이 있으면 컨테이너 레지스트리를 서비스 주체로 인증하도록 애플리케이션과 서비스를 구성할 수 있습니다.

#!/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"

기존 서비스 주체 사용

기존 서비스 주체에 레지스트리 액세스 권한을 부여하려면 서비스 주체에 새 역할을 할당해야 합니다. 새 서비스 주체를 만들 때처럼 풀, 푸시 및 풀, 소유자 액세스 권한을 부여할 수 있습니다.

다음 스크립트는 SERVICE_PRINCIPAL_ID 변수에 지정한 서비스 주체에게 az role assignment create 명령을 사용하여 권한을 부여합니다. 다른 수준의 액세스 권한을 부여하려면 --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에 대한 끌어오기 비밀을 만들려면 서비스 주체 ID, 암호 및 레지스트리 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>

여기서

설명
secret-name 이미지 끌어오기 비밀의 이름(예: acr-secret)
namespace 비밀을 넣을 Kubernetes 네임스페이스
기본 네임스페이스가 아닌 네임스페이스에 비밀을 배치하려는 경우에만 필요
container-registry-name Azure Container Registry의 이름(예: myregistry)

--docker-server는 레지스트리 로그인 서버의 정규화된 이름임
service-principal-ID Kubernetes에서 레지스트리에 액세스하는 데 사용할 서비스 주체의 ID
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는 아직 클러스터에 없는 경우 레지스트리에서 이미지를 자동으로 끌어옵니다.

다음 단계