빠른 시작: Cloud 감사 로그를 사용하여 이벤트 수신(Google Cloud CLI)

이 빠른 시작에서는 Eventarc를 사용하여 Cloud Run for Anthos가 Cloud Storage에서 이벤트를 수신하도록 설정하는 방법을 보여줍니다.

이 빠른 시작에서는 다음을 수행합니다.

  1. 이벤트 소스로 사용할 Cloud Storage 버킷을 만듭니다.
  2. Google Kubernetes Engine(GKE) 클러스터를 만들고 Cloud Run for Anthos를 사용 설정합니다.
  3. 대상으로 이벤트를 전달하는 이벤트 전달자 구성요소를 사용하여 Pub/Sub에서 이벤트를 가져오도록 서비스 계정을 설정합니다.
  4. 이벤트를 수신하는 Cloud Run for Anthos 서비스를 배포합니다.
  5. Eventarc에서 Cloud Run for Anthos 대상을 초기화합니다.
  6. 이벤트를 Cloud Storage에서 Cloud Run for Anthos 서비스로 전송하는 Eventarc GKE 트리거를 만듭니다.
  7. 파일을 Cloud Storage 버킷에 업로드하여 이벤트를 생성하고 Cloud Run for Anthos 로그에서 이벤트를 확인합니다.

시작하기 전에

  1. Google Cloud 계정에 로그인합니다. Google Cloud를 처음 사용하는 경우 계정을 만들고 Google 제품의 실제 성능을 평가해 보세요. 신규 고객에게는 워크로드를 실행, 테스트, 배포하는 데 사용할 수 있는 $300의 무료 크레딧이 제공됩니다.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Google Cloud 프로젝트에 결제가 사용 설정되어 있는지 확인합니다.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Google Cloud 프로젝트에 결제가 사용 설정되어 있는지 확인합니다.

  6. Google Cloud CLI를 설치하고 초기화합니다. 기본 컴퓨팅 리전을 구성하라는 메시지가 표시되면 n을 입력합니다.
  7. gcloud 구성요소를 업데이트합니다.
    gcloud components update
  8. Google Cloud, Cloud Build, Resource Manager, Google Kubernetes Engine API, Container Registry, Eventarc API를 사용 설정합니다.
    gcloud services enable cloudapis.googleapis.com
    gcloud services enable cloudbuild.googleapis.com
    gcloud services enable cloudresourcemanager.googleapis.com
    gcloud services enable container.googleapis.com
    gcloud services enable containerregistry.googleapis.com
    gcloud services enable eventarc.googleapis.com
  9. 이 빠른 시작에서 사용되는 구성 변수를 설정합니다.
    gcloud config set project PROJECT_ID
    gcloud config set run/cluster events-cluster
    gcloud config set run/cluster_location us-central1
    gcloud config set run/platform gke
    gcloud config set eventarc/location us-central1
    여기서 PROJECT_ID는 Google Cloud 프로젝트 ID입니다.
  10. 선택사항: 다음을 입력하여 Google Cloud CLI를 사용해 구성 설정을 확인할 수 있습니다.
    gcloud config list
    출력은 다음과 비슷하게 표시됩니다.
    [eventarc]
    location = us-central1
    [run]
    cluster = events-cluster
    cluster_location = us-central1
    platform = gke
  11. Cloud Storage에서 Cloud Audit Logging 관리자 읽기, 데이터 읽기, 데이터 쓰기 로그 유형을 사용 설정하세요.
    1. 프로젝트의 IAM 정책을 읽고 파일에 저장합니다.
       gcloud projects get-iam-policy PROJECT_ID > /tmp/policy.yaml
      
    2. /tmp/policy.yaml에서 정책을 수정하되, 데이터 액세스 감사 로그 구성 추가하거나 변경합니다.

      auditConfigs:
      - auditLogConfigs:
        - logType: ADMIN_READ
        - logType: DATA_WRITE
        - logType: DATA_READ
        service: storage.googleapis.com
      bindings:
      - members:
        - user:EMAIL_ADDRESS
        role: roles/owner
      etag: BwW_bHKTV5U=
      version: 1
      EMAIL_ADDRESS를 이메일 주소로 바꿉니다.
    3. 다음과 같이 새 IAM 정책을 씁니다.

      gcloud projects set-iam-policy PROJECT_ID /tmp/policy.yaml
      앞의 명령어가 다른 변경사항과의 충돌을 보고할 경우 이 단계를 반복하면서 프로젝트의 IAM 정책 읽기를 시작합니다.

Cloud Storage 버킷 만들기

이 빠른 시작에서는 Cloud Storage를 이벤트 소스로 사용합니다. 스토리지 버킷을 만들려면 다음을 실행하세요.

gsutil mb -l us-central1 gs://events-quickstart-$(gcloud config get-value project)/

이벤트 소스가 생성되면 Cloud Run에 이벤트 수신자 서비스를 배포할 수 있습니다.

Cloud Run for Anthos가 사용 설정된 GKE 클러스터 만들기

CloudRun, HttpLoadBalancing, HorizontalPodAutoscaling 부가기능이 포함된 Cloud Run for Anthos용 GKE 클러스터를 만듭니다. GKE 내에서 실행되는 애플리케이션에서 Google Cloud 서비스에 액세스하려면 워크로드 아이덴티티를 사용 설정합니다.

PROJECT_ID=$(gcloud config get-value project)

gcloud beta container clusters create events-cluster \
  --addons=HttpLoadBalancing,HorizontalPodAutoscaling,CloudRun \
  --machine-type=n1-standard-4 \
  --enable-autoscaling --min-nodes=2 --max-nodes=10 \
  --no-issue-client-certificate --num-nodes=2  \
  --enable-stackdriver-kubernetes \
  --scopes=cloud-platform,logging-write,monitoring-write,pubsub \
  --zone us-central1 \
  --release-channel=rapid \
  --workload-pool=$PROJECT_ID.svc.id.goog

클러스터 만들기가 완료될 때까지 기다립니다. 만드는 과정에서 표시되는 경고는 무시해도 됩니다. 클러스터가 만들어지면 다음과 유사한 출력이 표시됩니다.

Creating cluster events-cluster...done.
Created [https://container.googleapis.com/v1beta1/projects/my-project/zones/us-central1/clusters/events-cluster].

여기서 my-project는 Google Cloud 프로젝트 ID입니다.

그러면 my-projectevents-cluster라는 GKE 클러스터가 생성됩니다.

Google 서비스 계정 설정

이벤트 전달자 구성요소가 Pub/Sub에서 이벤트를 가져와 대상으로 전달할 수 있도록 사용자 제공 서비스 계정을 설정하고 해당 계정에 특정 역할을 부여합니다.

  1. 트리거를 만드는 데 사용되는 TRIGGER_GSA라는 서비스 계정을 만듭니다.

    TRIGGER_GSA=eventarc-crfa-triggers
    gcloud iam service-accounts create $TRIGGER_GSA
  2. 서비스 계정에 pubsub.subscriber, monitoring.metricWriter, eventarc.eventReceiver 역할을 부여합니다.

    gcloud projects add-iam-policy-binding $PROJECT_ID \
      --member "serviceAccount:$TRIGGER_GSA@$PROJECT_ID.iam.gserviceaccount.com" \
      --role "roles/pubsub.subscriber"
    
    gcloud projects add-iam-policy-binding $PROJECT_ID \
      --member "serviceAccount:$TRIGGER_GSA@$PROJECT_ID.iam.gserviceaccount.com" \
      --role "roles/monitoring.metricWriter"
    
    gcloud projects add-iam-policy-binding $PROJECT_ID \
     --member "serviceAccount:$TRIGGER_GSA@$PROJECT_ID.iam.gserviceaccount.com" \
     --role "roles/eventarc.eventReceiver"

Cloud Run for Anthos 서비스 배포

사전 빌드된 이미지 gcr.io/cloudrun/hello를 사용하여 이벤트를 수신 및 로깅하는 Cloud Run for Anthos 서비스를 배포합니다.

gcloud run deploy hello \
  --platform=gke \
  --cluster=events-cluster \
  --cluster-location=us-central1 \
  --namespace=default \
  --image=gcr.io/cloudrun/hello

GKE 대상 사용 설정

Eventarc는 Cloud Run for Anthos 서비스를 타겟팅하는 트리거별로 Pub/Sub에서 이벤트를 가져와 대상으로 전달하는 이벤트 전달자 구성요소를 만듭니다. GKE 클러스터에서 구성요소를 만들고 리소스를 관리하려면 Eventarc 서비스 계정에 권한을 부여합니다.

  1. Eventarc에 GKE 대상을 사용 설정합니다.

    gcloud eventarc gke-destinations init
  2. 필요한 역할을 결합하라는 메시지가 표시되면 y를 입력합니다.

    다음 역할이 서비스 계정에 결합됩니다.

    • compute.viewer
    • container.developer
    • iam.serviceAccountAdmin

Cloud 감사 로그 트리거 만들기

Cloud Storage에 파일을 업로드하면 Eventarc 트리거가 Cloud Storage의 이벤트를 hello Cloud Run for Anthos 서비스로 전송합니다.

  1. Cloud 감사 로그 트리거를 만듭니다.

    gcloud eventarc triggers create my-gke-trigger \
      --location=$TRIGGER_LOCATION \
      --destination-gke-cluster="events-cluster" \
      --destination-gke-location="us-central1" \
      --destination-gke-namespace="default" \
      --destination-gke-service="hello" \
      --destination-gke-path="/" \
      --event-filters="type=google.cloud.audit.log.v1.written" \
      --event-filters="serviceName=storage.googleapis.com" \
      --event-filters="methodName=storage.objects.create" \
      --service-account=$TRIGGER_GSA@$PROJECT_ID.iam.gserviceaccount.com
    

    그러면 my-gke-trigger라는 트리거가 생성됩니다.

  2. 트리거가 성공적으로 생성되었는지 확인하세요.

    gcloud eventarc triggers list

    출력은 다음과 비슷하게 표시됩니다.

    NAME             TYPE                               DESTINATION_RUN_SERVICE  DESTINATION_RUN_PATH  ACTIVE
    my-gke-trigger   google.cloud.audit.log.v1.written                                                 Yes
    

이벤트 생성 및 확인

텍스트 파일을 Cloud Storage에 업로드하여 이벤트를 생성하고 Cloud Run for Anthos 서비스를 트리거합니다. 그런 다음 Cloud Run for Anthos 서비스 로그에서 이벤트 메시지를 볼 수 있습니다.

  1. 텍스트 파일을 Cloud Storage에 업로드합니다.

    echo "Hello World" > random.txt
    gsutil cp random.txt gs://events-quickstart-$(gcloud config get-value project)/random.txt

    업로드 시 이벤트가 생성되고 Cloud Run for Anthos에서 이벤트의 메시지를 기록합니다.

  2. 이벤트 메시지를 보려면 Cloud Run for Anthos 서비스 로그로 이동합니다.

    1. Cloud Run for Anthos 페이지로 이동합니다.

      Cloud Run for Anthos로 이동

    2. hello 서비스를 클릭합니다.
    3. 로그 탭을 선택합니다.
  3. 다음과 같은 로그 항목을 찾습니다.

    Hello from Cloud Run! The container started successfully and is listening for HTTP requests on $PORT
    Received event of type google.cloud.audit.log.v1.written.
    

삭제

Cloud Run에서는 서비스를 사용하지 않을 때 비용이 청구되지 않지만 Container Registry에 컨테이너 이미지를 저장하는 것이나 Eventarc 리소스, Pub/Sub 메시지, GKE 클러스터에 대해 요금이 부과될 수 있습니다.

이미지 삭제, 스토리지 버킷 삭제, GKE 클러스터 삭제를 수행할 수 있습니다.

Eventarc 트리거를 삭제하려면 다음 안내를 따르세요.

gcloud eventarc triggers delete my-gke-trigger

또는 Google Cloud 프로젝트를 삭제하여 비용 청구를 방지할 수 있습니다. 클라우드 프로젝트를 삭제하면 해당 프로젝트 내에서 사용되는 모든 리소스에 대한 청구가 중단됩니다.

gcloud projects delete PROJECT_ID_OR_NUMBER

PROJECT_ID_OR_NUMBER를 프로젝트 ID 또는 번호로 바꿉니다.

다음 단계