Google Kubernetes Engine에서 Spark 작업 실행

이 문서에서는 GKE 기반 Managed Service for Apache Spark 가상 클러스터를 만들고 클러스터에서 Spark 작업을 실행하는 방법을 보여줍니다.

옵션 개요

GKE 기반 Managed Service for Apache Spark는 컨테이너화된 환경을 강력하게 제어할 수 있지만 Google Cloud 는 운영을 간소화하고 개발을 가속화할 수 있는 완전 관리형 서버리스 옵션도 제공합니다. Spark Managed Service for Apache Spark 배포 옵션 비교는 최적의 Spark 서비스 결정을 참고하세요.

시작하기 전에

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

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that you have the permissions required to complete this guide.

  4. Verify that billing is enabled for your Google Cloud project.

  5. Enable the Dataproc API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  6. Google Cloud CLI를 설치합니다.

  7. 외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.

  8. gcloud CLI를 초기화하려면, 다음 명령어를 실행합니다.

    gcloud init
  9. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  10. Verify that you have the permissions required to complete this guide.

  11. Verify that billing is enabled for your Google Cloud project.

  12. Enable the Dataproc API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  13. Google Cloud CLI를 설치합니다.

  14. 외부 ID 공급업체(IdP)를 사용하는 경우 먼저 제휴 ID로 gcloud CLI에 로그인해야 합니다.

  15. gcloud CLI를 초기화하려면, 다음 명령어를 실행합니다.

    gcloud init
  16. 클러스터에 워크로드 아이덴티티가 사용 설정된 표준 (Autopilot 아님) Google Kubernetes Engine (GKE) 영역 또는 리전 클러스터를 만들어야 합니다.

필요한 역할

이 페이지의 예시를 실행하려면 특정 IAM 역할이 필요합니다. 조직 정책에 따라 이러한 역할이 이미 부여되었을 수 있습니다. 역할 부여를 확인하려면 역할을 부여해야 하나요?를 참고하세요.

역할 부여에 대한 상세 설명은 프로젝트, 폴더, 조직에 대한 액세스 관리를 참고하세요.

사용자 역할

Managed Service for Apache Spark 클러스터를 만드는 데 필요한 권한을 얻으려면 관리자에게 다음 IAM 역할을 부여해 달라고 요청하세요.

서비스 계정 역할

Compute Engine 기본 서비스 계정에 Managed Service for Apache Spark 클러스터를 만드는 데 필요한 권한이 있는지 확인하려면 관리자에게 Compute Engine 기본 서비스 계정에 프로젝트에 대한 Dataproc 작업자 (roles/dataproc.worker) IAM 역할을 부여해 달라고 요청하세요.

가상 클러스터 만들기

GKE 기반 Managed Service for Apache Spark 가상 클러스터는 Managed Service for Apache Spark 구성요소의 배포 플랫폼으로 생성됩니다. 가상 리소스로서 Compute Engine 기반 Managed Service for Apache Spark 클러스터와 달리 별도의 Managed Service for Apache Spark 마스터 및 작업자 VM을 포함하지 않습니다.

  • GKE 기반 Managed Service for Apache Spark에서는 GKE 클러스터 내에 노드 풀을 GKE 기반 Managed Service for Apache Spark 가상 클러스터를 만들 때 만듭니다.

  • GKE 기반 Managed Service for Apache Spark 작업은 이 노드 풀에서 포드로 실행됩니다. 노드 풀 및 노드 풀의 포드 예약은 GKE에서 관리됩니다.

  • 여러 개의 가상 클러스터를 만듭니다. GKE 클러스터에서 여러 가상 클러스터를 만들고 실행하여 가상 클러스터에서 노드 풀을 공유하면 개선된 리소스 사용률을 얻을 수 있습니다.

    • 각 가상 클러스터:
      • Spark 엔진 버전 및 워크로드 아이덴티티를 포함한 별도의 속성으로 생성됩니다.
      • GKE 클러스터의 별도 GKE 네임스페이스 내에 격리됩니다.

콘솔

콘솔에서는 GKE 기반 Managed Service for Apache Spark 클러스터 만들기가 지원되지 않습니다. Google Cloud

gcloud

로컬 또는 Cloud Shell에서 환경 변수를 설정한 후 gcloud dataproc clusters gke create 명령어를 실행하여 GKE 기반 Managed Service for Apache Spark 클러스터를 만듭니다.

  1. 환경 변수를 설정합니다.

    DP_CLUSTER=Managed Service for Apache Spark on GKE  cluster-name \
      REGION=region \
      GKE_CLUSTER=GKE cluster-name \
      BUCKET=Cloud Storage bucket-name \
      DP_POOLNAME=node pool-name
      PHS_CLUSTER=Managed Service for Apache Spark PHS server name
    
    참고:

    • DP_CLUSTER: Managed Service for Apache Spark 가상 클러스터 이름을 설정합니다. 이름은 소문자로 시작하고 이어서 최대 54자의 소문자, 숫자 또는 하이픈이 와야 합니다. 하이픈으로 끝날 수 없습니다.
    • REGION: region은 GKE 클러스터가 위치한 리전과 같아야 합니다.
    • GKE_CLUSTER: 기존 GKE 클러스터의 이름입니다.
    • BUCKET: (선택사항) Managed Service for Apache Spark가 아티팩트를 스테이징하는 데 사용할 Cloud Storage 버킷의 이름을 지정할 수 있습니다. 버킷을 지정하지 않으면 GKE 기반 Managed Service for Apache Spark에서 스테이징 버킷을 만듭니다.
    • DP_POOLNAME: GKE 클러스터에 만들려는 노드 풀 의 이름입니다.
    • PHS_CLUSTER: (선택사항) Managed Service for Apache Spark PHS 서버 를 사용하여 활성 및 삭제된 GKE 기반 Managed Service for Apache Spark 클러스터에 대한 Spark 작업 기록을 확인합니다. PHS 클러스터가 GKE 기반 Managed Service for Apache Spark 가상 클러스터와 동일한 리전에 있어야 합니다.
  2. 명령어를 실행합니다.

    gcloud dataproc clusters gke create ${DP_CLUSTER} \
        --region=${REGION} \
        --gke-cluster=${GKE_CLUSTER} \
        --spark-engine-version=latest \
        --staging-bucket=${BUCKET} \
        --pools="name=${DP_POOLNAME},roles=default" \
        --setup-workload-identity \
        --history-server-cluster=${PHS_CLUSTER}
    
    참고:

    • --spark-engine-version: Managed Service for Apache Spark 클러스터에서 사용되는 Spark 이미지 버전입니다. `3`, `3.1` 또는 `latest`와 같은 식별자를 사용하거나 `3.1-dataproc-5`와 같은 전체 하위 부 버전을 지정할 수 있습니다.
    • --staging-bucket: GKE 기반 Managed Service for Apache Spark에서 스테이징 버킷을 만들도록 하려면 이 플래그를 삭제합니다.
    • --pools: 이 플래그는 Managed Service for Apache Spark가 워크로드를 수행하기 위해 만들거나 사용할 신규 또는 기존 노드 풀을 지정하는 데 사용됩니다. GKE 기반 Managed Service for Apache Spark 노드 풀 설정을 쉼표로 구분하여 나열합니다. 예:
      --pools=name=dp-default,roles=default,machineType=e2-standard-4,min=0,max=10
      
      노드 풀 namerole을 지정해야 합니다. 다른 노드 풀 설정은 선택사항입니다. 여러 --pools 플래그를 사용하여 여러 노드 풀을 지정할 수 있습니다. 노드 풀 최소 하나 이상에 default 역할이 있어야 합니다. 모든 노드 풀에 동일한 위치가 있어야 합니다.
    • --setup-workload-identity: 이 플래그는 워크로드 아이덴티티 바인딩을 사용 설정합니다. 이러한 바인딩을 통해 Kubernetes 서비스 계정 (KSA) 은 기본 Managed Service for Apache Spark VM 서비스 계정 (데이터 영역 ID) 의 가상 클러스터 역할을 할 수 있습니다.

REST

virtualClusterConfigcluster.create API 요청의 일부로 작성합니다.

요청 데이터를 사용하기 전에 다음을 바꿉니다.

  • PROJECT: Google Cloud 프로젝트 ID
  • REGION: Dataproc 가상 클러스터 리전(기존 GKE 클러스터 리전과 동일한 리전)
  • DP_CLUSTER: Dataproc 클러스터 이름
  • GKE_CLUSTER: GKE 클러스터 이름
  • NODE_POOL: 노드 풀 이름
  • PHS_CLUSTER: 영구 기록 서버(PHS) 클러스터 이름
  • BUCKET: (선택사항) 스테이징 버킷 이름. GKE 기반 Managed Service for Apache Spark에서 스테이징 버킷을 만들도록 하려면 비워둡니다.

HTTP 메서드 및 URL:

POST https://dataproc.googleapis.com/v1/projects/project-id/regions/region/clusters

JSON 요청 본문:

{
  "clusterName":"DP_CLUSTER",
  "projectId":"PROJECT",
  "virtualClusterConfig":{
    "auxiliaryServicesConfig":{
      "sparkHistoryServerConfig":{
        "dataprocCluster":"projects/PROJECT/regions/REGION/clusters/PHS_CLUSTER"
      }
    },
    "kubernetesClusterConfig":{
      "gkeClusterConfig":{
        "gkeClusterTarget":"projects/PROJECT/locations/REGION/clusters/GKE_CLUSTER",
        "nodePoolTarget":[
          {
"nodePool":"projects/PROJECT/locations/REGION/clusters/GKE_CLUSTER/nodePools/NODE_POOL",
            "roles":[
              "DEFAULT"
            ]
          }
        ]
      },
      "kubernetesSoftwareConfig":{
        "componentVersion":{
          "SPARK":"latest"
        }
      }
    },
    "stagingBucket":"BUCKET"
  }
}

요청을 보내려면 다음 옵션 중 하나를 펼칩니다.

다음과 비슷한 JSON 응답이 표시됩니다.

{
  "projectId":"PROJECT",
  "clusterName":"DP_CLUSTER",
  "status":{
    "state":"RUNNING",
    "stateStartTime":"2022-04-01T19:16:39.865716Z"
  },
  "clusterUuid":"98060b77-...",
  "statusHistory":[
    {
      "state":"CREATING",
      "stateStartTime":"2022-04-01T19:14:27.340544Z"
    }
  ],
  "labels":{
    "goog-dataproc-cluster-name":"DP_CLUSTER",
    "goog-dataproc-cluster-uuid":"98060b77-...",
    "goog-dataproc-location":"REGION",
    "goog-dataproc-environment":"prod"
  },
  "virtualClusterConfig":{
    "stagingBucket":"BUCKET",
    "kubernetesClusterConfig":{
      "kubernetesNamespace":"dp-cluster",
      "gkeClusterConfig":{
"gkeClusterTarget":"projects/PROJECT/locations/REGION/clusters/GKE_CLUSTER",
        "nodePoolTarget":[
          {
"nodePool":"projects/PROJECT/locations/REGION/clusters/GKE_CLUSTER/nodePools/NODE_POOL",
            "roles":[
              "DEFAULT"
            ]
          }
        ]
      },
      "kubernetesSoftwareConfig":{
        "componentVersion":{
          "SPARK":"3.1-..."
        },
        "properties":{
          "dpgke:dpgke.unstable.outputOnly.endpoints.sparkHistoryServer":"https://...",
          "spark:spark.eventLog.dir":"gs://BUCKET/.../spark-job-history",
          "spark:spark.eventLog.enabled":"true"
        }
      }
    },
    "auxiliaryServicesConfig":{
      "sparkHistoryServerConfig":{
        "dataprocCluster":"projects/PROJECT/regions/REGION/clusters/PHS_CLUSTER"
      }
    }
  }

Spark 작업 제출

GKE 기반 Managed Service for Apache Spark 가상 클러스터가 실행된 후, 콘솔, gcloud CLI 또는 Managed Service for Apache Spark jobs.submit API (직접 HTTP 요청 또는 Cloud 클라이언트 라이브러리 사용)를 사용하여 Spark 작업을 제출합니다. Google Cloud

**gcloud CLI Spark 작업 예시:**

gcloud dataproc jobs submit spark \
    --region=${REGION} \
    --cluster=${DP_CLUSTER} \
    --class=org.apache.spark.examples.SparkPi \
    --jars=local:///usr/lib/spark/examples/jars/spark-examples.jar \
    -- 1000

**gcloud CLI PySpark 작업 예시:**

gcloud dataproc jobs submit pyspark \
    --region=${REGION} \
    --cluster=${DP_CLUSTER} \
    local:///usr/lib/spark/examples/src/main/python/pi.py \
    -- 10

**gcloud CLI SparkR 작업 예시:**

gcloud dataproc jobs submit spark-r \
    --region=${REGION} \
    --cluster=${DP_CLUSTER} \
    local:///usr/lib/spark/examples/src/main/r/dataframe.R

정리