Artifact Registry 심층 탐구

1. 개요

차세대 Container Registry인 Artifact Registry를 사용하면 조직이 컨테이너 이미지와 언어 패키지 (예: Maven 및 npm)를 한곳에서 관리할 수 있습니다. Google Cloud의 도구 및 런타임과 완전히 통합되며 npm 및 Maven과 같은 도구와 함께 사용할 수 있는 언어 기반 종속 항목 관리가 지원됩니다. 따라서 CI/CD 도구와 간단하게 통합하여 자동화된 파이프라인을 구축할 수 있습니다.

이 실습에서는 Artifact Registry에서 사용할 수 있는 몇 가지 기능을 살펴봅니다.

학습할 내용

이 실습의 학습 목표는 무엇인가요?

  • 컨테이너 및 언어 패키지를 위한 저장소 만들기
  • Artifact Registry로 컨테이너 이미지 관리하기
  • Artifact Registry와 Cloud Code 통합
  • Java 종속 항목용 Artifact Registry를 사용하도록 Maven 구성

2. 설정 및 요구사항

자습형 환경 설정

  1. Google Cloud Console에 로그인하여 새 프로젝트를 만들거나 기존 프로젝트를 재사용합니다. 아직 Gmail이나 Google Workspace 계정이 없는 경우 계정을 만들어야 합니다.

b35bf95b8bf3d5d8.png

a99b7ace416376c4.png

bd84a6d3004737c5.png

  • 프로젝트 이름은 이 프로젝트 참가자의 표시 이름입니다. 이는 Google API에서 사용하지 않는 문자열이며 언제든지 업데이트할 수 있습니다.
  • 프로젝트 ID는 모든 Google Cloud 프로젝트에서 고유해야 하며, 변경할 수 없습니다(설정된 후에는 변경할 수 없음). Cloud Console은 고유한 문자열을 자동으로 생성합니다. 일반적으로 신경 쓰지 않아도 됩니다. 대부분의 Codelab에서는 프로젝트 ID를 참조해야 하며(일반적으로 PROJECT_ID로 식별됨), 마음에 들지 않는 경우 임의로 다시 생성하거나 직접 지정해서 사용할 수 있는지 확인하세요. 프로젝트가 생성되면 프로젝트 ID가 '고정'됩니다.
  • 세 번째 값은 일부 API에서 사용하는 프로젝트 번호입니다. 이 세 가지 값에 대한 자세한 내용은 문서를 참조하세요.
  1. 다음으로 Cloud 리소스/API를 사용하려면 Cloud Console에서 결제를 사용 설정해야 합니다. 이 Codelab 실행에는 많은 비용이 들지 않습니다. 이 튜토리얼을 마친 후 비용이 결제되지 않도록 리소스를 종료하려면 Codelab의 끝에 있는 '삭제' 안내를 따르세요. Google Cloud 새 사용자에게는 미화 $300 상당의 무료 체험판 프로그램에 참여할 수 있는 자격이 부여됩니다.

gcloud 설정

Cloud Shell에서 프로젝트 ID와 프로젝트 번호를 설정하고 이러한 변수를 PROJECT_IDPROJECT_NUMBER 변수로 저장합니다.

export PROJECT_ID=$(gcloud config get-value project)
export PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)')

Google 서비스 사용 설정

gcloud services enable \
  cloudresourcemanager.googleapis.com \
  container.googleapis.com \
  artifactregistry.googleapis.com \
  containerregistry.googleapis.com \
  containerscanning.googleapis.com

소스 코드 가져오기

이 실습의 소스 코드는 GitHub의 GoogleCloudPlatform org에 있습니다. 아래 명령어로 클론한 후 디렉터리로 변경합니다.

git clone https://github.com/GoogleCloudPlatform/cloud-code-samples/

이 실습에서 사용하는 인프라를 프로비저닝합니다.

이 실습에서는 GKE에 코드를 배포합니다. 아래 설정 스크립트가 이 인프라를 준비합니다.

gcloud container clusters create container-dev-cluster --zone=us-central1-b

3. 컨테이너 이미지 작업

Artifact Registry에서 Docker 저장소 만들기

Artifact Registry는 컨테이너 이미지 및 언어 패키지 관리를 지원합니다. 아티팩트 유형에 따라 필요한 사양이 다릅니다. 예를 들어 Maven 종속 항목에 대한 요청은 Node 종속 항목에 대한 요청과 다릅니다.

다양한 API 사양을 지원하려면 Artifact Registry가 API 응답이 따라야 할 형식을 알아야 합니다. 이렇게 하려면 저장소를 만들고 원하는 저장소 유형을 나타내는 --repository-format 플래그를 전달합니다.

Cloud Shell에서 다음 명령어를 실행하여 Docker 이미지용 저장소를 만듭니다.

gcloud artifacts repositories create container-dev-repo --repository-format=docker \
--location=us-central1 --description="Docker repository for Container Dev Workshop"

Cloud Shell 승인 프롬프트가 표시되면 승인을 클릭합니다.

Google Cloud 콘솔 - Artifact Registry - 저장소로 이동하여 container-dev-repo라는 새로 생성된 Docker 저장소를 확인합니다. 클릭하면 지금은 비어 있는 것을 확인할 수 있습니다.

Artifact Registry에 Docker 인증 구성

Artifact Registry 사용자 인증 정보에 연결해야 액세스 권한을 제공할 수 있습니다. 별도의 사용자 인증 정보를 설정하는 대신 gcloud 사용자 인증 정보를 원활하게 사용하도록 Docker를 구성할 수 있습니다.

Cloud Shell에서 다음 명령어를 실행하여 Docker가 Google Cloud CLI를 사용하여 us-central1 리전의 Artifact Registry에 대한 요청을 인증하도록 구성합니다.

gcloud auth configure-docker us-central1-docker.pkg.dev

이 명령어는 Cloud Shell Docker 구성을 변경하라는 확인 메시지를 표시합니다. Enter 키를 누릅니다.

샘플 애플리케이션 살펴보기

샘플 애플리케이션은 이전 단계에서 클론한 git 저장소에 제공됩니다. Java 디렉터리로 변경하고 애플리케이션 코드를 검토합니다.

cd cloud-code-samples/java/java-hello-world

폴더에는 간단한 웹페이지를 렌더링하는 예시 자바 애플리케이션이 포함되어 있습니다. 이 특정 실습과 관련이 없는 다양한 파일 외에도 src 폴더 아래에 있는 소스 코드와 컨테이너 이미지를 로컬에서 빌드하는 데 사용할 Dockerfile이 포함되어 있습니다.

컨테이너 이미지 빌드

Artifact Registry에 컨테이너 이미지를 저장하려면 먼저 컨테이너 이미지를 만들어야 합니다.

다음 명령어를 실행하여 컨테이너 이미지를 빌드하고 적절하게 태그를 지정하여 다음 단계에서 저장소에 푸시합니다.

docker build -t us-central1-docker.pkg.dev/$PROJECT_ID/container-dev-repo/java-hello-world:tag1 .

Artifact Registry에 컨테이너 이미지 푸시

다음 명령어를 실행하여 컨테이너 이미지를 이전에 만든 저장소로 푸시합니다.

docker push us-central1-docker.pkg.dev/$PROJECT_ID/container-dev-repo/java-hello-world:tag1

Artifact Registry에서 이미지 검토하기

Google Cloud Console - Artifact Registry - 저장소로 이동합니다.. container-dev-repo를 클릭하고 java-hello-world 이미지가 있는지 확인합니다. 이미지를 클릭하고 tag1 태그가 지정된 이미지를 확인합니다. 취약점 스캔이 실행 중이거나 이미 완료되었으며 감지된 취약점 수를 볼 수 있습니다.

9cb46d3689b3ed2.png

취약점 수를 클릭하면 이미지에서 감지된 취약점 목록과 CVE 게시판 이름 및 심각도가 표시됩니다. 나열된 각 취약점에서 '보기'를 클릭하면 자세한 내용을 확인할 수 있습니다.

2b17e9d26d9dd7ea.png

4. Cloud Code와 통합

이 섹션에서는 Artifact Registry Docker 이미지 저장소를 Cloud Code와 함께 사용하는 방법을 알아봅니다.

Cloud Code에서 GKE 클러스터에 애플리케이션 배포

java-hello-world 폴더에서 다음 명령어를 실행하여 Cloud Shell 편집기를 열고 애플리케이션 폴더를 작업공간에 추가합니다.

cloudshell workspace .

Cloud Shell 편집기가 애플리케이션 폴더의 탐색기와 함께 열립니다.

작업공간에서 Java 프로젝트 설정 파일을 제외하라는 팝업이 나타나면 Exclude in workspace를 클릭합니다.

다음 단계에서는 Artifact Registry 저장소 위치를 입력해야 합니다. 위치 형식은 다음과 같습니다.

us-central1-docker.pkg.dev/<PROJECT_ID>/container-dev-repo

PROJECT_ID를 찾으려면 터미널에서 다음 명령어를 실행합니다.

gcloud config get project

왼쪽 하단의 Cloud Code 상태 표시줄을 클릭한 후 Run on Kubernetes를 선택합니다.

e6e2b06467228e18.png

메시지가 표시되면 Yes을 선택하여 실습을 위해 프로비저닝된 container-dev-cluster GKE 클러스터를 가리키는 kubeconfig의 현재 컨텍스트를 사용합니다.

이미지 레지스트리 프롬프트에서 <PROJECT_ID>를 대체하기 전에 찾은 주소 위치를 입력합니다. 실제 가치에 따라

us-central1-docker.pkg.dev/<PROJECT_ID>/container-dev-repo

Kubernetes에서 실행을 처음으로 실행하면 Cloud Code에서 대상 이미지 저장소 위치를 묻는 메시지를 표시합니다. 저장소 URL이 제공되면 애플리케이션 폴더에 생성되는 .vscode/launch.json 파일에 저장됩니다.

출력 창에서 애플리케이션 이미지의 빌드가 시작됩니다. java-hello-world, 이미지가 이전에 구성된 Artifact Registry 저장소에 업로드됩니다.

Cloud Console - Artifact Registry - 저장소로 이동합니다. container-dev-repo를 클릭하고 java-hello-world 이미지를 확인하고 latest 태그가 지정된 새 이미지를 기록합니다.

배포된 애플리케이션 검토

Cloud Shell 편집기로 돌아갑니다. 배포가 완료되면 Skaffold/Cloud Code가 서비스가 전달된 노출된 URL을 출력하고 '웹 미리보기 열기' 링크를 클릭합니다.

33257a43826b88ff.png

새 브라우저 창에 Hello World 앱 페이지가 표시됩니다.

d3e49693b0383a5d.png

애플리케이션 코드 업데이트

이제 애플리케이션을 업데이트하여 클러스터의 배포에 즉시 구현된 변경사항을 확인합니다.

Cloud Shell 편집기의 src/main/java/cloudcode/helloworld/web 폴더에서 HelloWorldController.java를 엽니다.

20행의 텍스트를 '실행 중'에서 "업데이트되었습니다!"로 바뀌면 빌드 및 배포 프로세스가 즉시 시작되는 것을 볼 수 있습니다.

배포가 끝나면 전달된 URL을 다시 클릭하거나 애플리케이션 브라우저 창을 새로고침하여 변경사항이 배포되었는지 확인합니다.

41787b1da54ff137.png

다시 Cloud Console - Artifact Registry - 저장소로 이동합니다. container-dev-repo를 클릭하고 java-hello-world 이미지를 확인하고 새 이미지를 기록해 둡니다.

5. 언어 패키지 사용

이 섹션에서는 Artifact Registry Java 저장소를 설정하고 여기에 패키지를 업로드하여 다양한 애플리케이션에서 활용하는 방법을 알아봅니다.

자바 패키지 저장소 만들기

Cloud Shell에서 다음 명령어를 실행하여 Java 아티팩트용 저장소를 만듭니다.

gcloud artifacts repositories create container-dev-java-repo \
    --repository-format=maven \
    --location=us-central1 \
    --description="Java package repository for Container Dev Workshop"

Cloud Shell 승인 프롬프트가 표시되면 승인을 클릭합니다.

Google Cloud 콘솔 - Artifact Registry - 저장소로 이동하여 container-dev-java-repo라는 새로 생성된 Maven 저장소를 확인합니다. 저장소를 클릭하면 지금은 비어 있는 것을 확인할 수 있습니다.

아티팩트 저장소에 대한 인증 설정

다음 명령어를 사용하여 Artifact Registry 사용자 인증 정보 도우미가 저장소에 연결할 때 이를 사용하여 인증할 수 있도록 애플리케이션 기본 사용자 인증 정보 (ADC)의 잘 알려진 위치를 사용자 계정 사용자 인증 정보로 업데이트합니다.

gcloud auth login --update-adc

Artifact Registry용 Maven 구성

다음 명령어를 실행하여 Java 프로젝트에 추가할 저장소 구성을 출력합니다.

gcloud artifacts print-settings mvn \
    --repository=container-dev-java-repo \
    --location=us-central1

Cloud Shell 편집기에서 pom.xml을 열고 반환된 설정을 파일의 해당 섹션에 추가합니다.

distributionManagement 섹션 업데이트

<distributionManagement>
   <snapshotRepository>
     <id>artifact-registry</id>
     <url>artifactregistry://us-central1-maven.pkg.dev/<PROJECT>/container-dev-java-repo</url>
   </snapshotRepository>
   <repository>
     <id>artifact-registry</id>
     <url>artifactregistry://us-central1-maven.pkg.dev/<PROJECT>/container-dev-java-repo</url>
   </repository>
</distributionManagement>

repositories 섹션 업데이트

 <repositories>
   <repository>
     <id>artifact-registry</id>
     <url>artifactregistry://us-central1-maven.pkg.dev/<PROJECT>/container-dev-java-repo</url>
     <releases>
       <enabled>true</enabled>
     </releases>
     <snapshots>
       <enabled>true</enabled>
     </snapshots>
   </repository>
 </repositories>

확장 프로그램 업데이트

<extensions>
     <extension>
       <groupId>com.google.cloud.artifactregistry</groupId>
       <artifactId>artifactregistry-maven-wagon</artifactId>
       <version>2.1.0</version>
     </extension>
   </extensions>

다음은 전체 파일의 예입니다. <PROJECT>를 프로젝트 ID로 바꿉니다.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 
 <artifactId>hello-world</artifactId>
 <packaging>jar</packaging>
 <name>Cloud Code Hello World</name>
 <description>Getting started with Cloud Code</description>
 <version>1.0.0</version>
<distributionManagement>
   <snapshotRepository>
     <id>artifact-registry</id>
     <url>artifactregistry://us-central1-maven.pkg.dev/<PROJECT>/container-dev-java-repo</url>
   </snapshotRepository>
   <repository>
     <id>artifact-registry</id>
     <url>artifactregistry://us-central1-maven.pkg.dev/<PROJECT>/container-dev-java-repo</url>
   </repository>
 </distributionManagement>
 
 <repositories>
   <repository>
     <id>artifact-registry</id>
     <url>artifactregistry://us-central1-maven.pkg.dev/<PROJECT>/container-dev-java-repo</url>
     <releases>
       <enabled>true</enabled>
     </releases>
     <snapshots>
       <enabled>true</enabled>
     </snapshots>
   </repository>
 </repositories>
 
 <parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.6.3</version>
 </parent>
 
 <properties>
   <java.version>1.8</java.version>
   <checkstyle.config.location>./checkstyle.xml</checkstyle.config.location>
 </properties>
 
 <build>
   <plugins>
     <plugin>
       <groupId>com.google.cloud.tools</groupId>
       <artifactId>jib-maven-plugin</artifactId>
       <version>3.2.0</version>
     </plugin>
     <plugin>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-maven-plugin</artifactId>
     </plugin>
     <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-checkstyle-plugin</artifactId>
       <version>3.1.2</version>
     </plugin>
   </plugins>
   <extensions>
     <extension>
       <groupId>com.google.cloud.artifactregistry</groupId>
       <artifactId>artifactregistry-maven-wagon</artifactId>
       <version>2.1.0</version>
     </extension>
   </extensions>
 </build>
 
 <!-- The Spring Cloud GCP BOM will manage spring-cloud-gcp version numbers for you. -->
 <dependencyManagement>
   <dependencies>
     <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-gcp-dependencies</artifactId>
       <version>1.2.8.RELEASE</version>
       <type>pom</type>
       <scope>import</scope>
     </dependency>
   </dependencies>
 </dependencyManagement>
  
 <dependencies>
 
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter</artifactId>
   </dependency>
 
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jetty</artifactId>
   </dependency>
 
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-webmvc</artifactId>
   </dependency>
 
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-thymeleaf</artifactId>
   </dependency>
 
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
     <scope>test</scope>
   </dependency>
 
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-gcp-starter-logging</artifactId>
   </dependency>
      
 </dependencies>
 
</project>

Artifact Registry에 자바 패키지 업로드하기

Artifact Registry를 Maven에서 구성했으므로 이제 Artifact Registry를 사용하여 조직의 다른 프로젝트에서 사용할 Java Jar를 저장할 수 있습니다.

다음 명령어를 실행하여 자바 패키지를 Artifact Registry에 업로드합니다.

mvn deploy

Artifact Registry에서 자바 패키지 확인하기

Cloud Console - Artifact Registry - 저장소로 이동하여 container-dev-java-repo를 클릭하고 hello-world 바이너리 아티팩트가 있는지 확인합니다.

e348d976ac1ac107.png

6. 축하합니다.

축하합니다. Codelab을 완료했습니다.

학습한 내용

  • 컨테이너 및 언어 패키지용 저장소 생성
  • Artifact Registry를 사용한 관리형 컨테이너 이미지
  • Cloud Code와 통합된 Artifact Registry
  • Java 종속 항목용 Artifact Registry를 사용하도록 Maven을 구성함

삭제

다음 명령어를 실행하여 프로젝트를 삭제합니다.

gcloud projects delete $PROJECT_ID