設定 Dataproc Hub


Dataproc Hub 是自訂的 JupyterHub 伺服器。管理員可設定及建立 Dataproc Hub 執行個體,產生單一使用者 Dataproc 叢集,用於代管 JupyterJupyterLab 筆記本環境 (請參閱「使用 Dataproc Hub」)。

為多位使用者啟動 Notebooks。您可以建立啟用 Dataproc 的 Vertex AI Workbench 執行個體,或在 VM 上安裝 Dataproc JupyterLab 外掛程式,為多位使用者提供筆記本服務。

目標

  1. 定義 Dataproc 叢集設定 (或使用預先定義的設定檔)。

  2. 設定 Dataproc Hub 執行個體環境變數。

  3. 建立 Dataproc Hub 執行個體。

事前準備

請建立 Google Cloud 專案和 Cloud Storage 值區。

  1. 設定專案

    1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
    2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

      Go to project selector

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

    4. Enable the Dataproc, Compute Engine, and Cloud Storage APIs.

      Enable the APIs

    5. Install the Google Cloud CLI.

    6. 如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

    7. 如要初始化 gcloud CLI,請執行下列指令:

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

      Go to project selector

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

    10. Enable the Dataproc, Compute Engine, and Cloud Storage APIs.

      Enable the APIs

    11. Install the Google Cloud CLI.

    12. 如果您使用外部識別資訊提供者 (IdP),請先 使用聯合身分登入 gcloud CLI

    13. 如要初始化 gcloud CLI,請執行下列指令:

      gcloud init
    14. 在專案中建立 Cloud Storage bucket,以保存本教學課程中使用的資料。

      1. In the Google Cloud console, go to the Cloud Storage Buckets page.

        Go to Buckets

      2. Click Create.
      3. On the Create a bucket page, enter your bucket information. To go to the next step, click Continue.
        1. In the Get started section, do the following:
          • Enter a globally unique name that meets the bucket naming requirements.
          • To add a bucket label, expand the Labels section (), click Add label, and specify a key and a value for your label.
        2. In the Choose where to store your data section, do the following:
          1. Select a Location type.
          2. Choose a location where your bucket's data is permanently stored from the Location type drop-down menu.
          3. To set up cross-bucket replication, select Add cross-bucket replication via Storage Transfer Service and follow these steps:

            Set up cross-bucket replication

            1. In the Bucket menu, select a bucket.
            2. In the Replication settings section, click Configure to configure settings for the replication job.

              The Configure cross-bucket replication pane appears.

              • To filter objects to replicate by object name prefix, enter a prefix that you want to include or exclude objects from, then click Add a prefix.
              • To set a storage class for the replicated objects, select a storage class from the Storage class menu. If you skip this step, the replicated objects will use the destination bucket's storage class by default.
              • Click Done.
        3. In the Choose how to store your data section, do the following:
          1. Select a default storage class for the bucket or Autoclass for automatic storage class management of your bucket's data.
          2. To enable hierarchical namespace, in the Optimize storage for data-intensive workloads section, select Enable hierarchical namespace on this bucket.
        4. In the Choose how to control access to objects section, select whether or not your bucket enforces public access prevention, and select an access control method for your bucket's objects.
        5. In the Choose how to protect object data section, do the following:
          • Select any of the options under Data protection that you want to set for your bucket.
            • To enable soft delete, click the Soft delete policy (For data recovery) checkbox, and specify the number of days you want to retain objects after deletion.
            • To set Object Versioning, click the Object versioning (For version control) checkbox, and specify the maximum number of versions per object and the number of days after which the noncurrent versions expire.
            • To enable the retention policy on objects and buckets, click the Retention (For compliance) checkbox, and then do the following:
              • To enable Object Retention Lock, click the Enable object retention checkbox.
              • To enable Bucket Lock, click the Set bucket retention policy checkbox, and choose a unit of time and a length of time for your retention period.
          • To choose how your object data will be encrypted, expand the Data encryption section (), and select a Data encryption method.
      4. Click Create.

    15. 定義叢集設定

      Dataproc Hub 執行個體會根據 YAML 叢集設定檔中的設定值建立叢集。

      叢集設定可指定 Dataproc 叢集可用的任何功能或元件 (例如機型、初始化動作和選用元件)。叢集映像檔版本必須為 1.4.13 以上。如果嘗試使用低於 1.4.13 的映像檔版本產生叢集,系統會發生錯誤並失敗。

      叢集設定檔的 YAML 範例

      clusterName: cluster-name
      config:
        softwareConfig:
          imageVersion: 2.2-ubuntu22
          optionalComponents:
          - JUPYTER
      

      每項設定都必須儲存在 Cloud Storage。 您可以建立及儲存多個設定檔,讓使用者在使用 Dataproc Hub 建立 Dataproc 叢集筆記本環境時,可以選擇要使用的設定檔。

      建立 YAML 叢集設定檔的方式有兩種:

      1. 從控制台建立 YAML 叢集設定檔

      2. 從現有叢集匯出 YAML 叢集設定檔

      從控制台建立 YAML 叢集設定檔

      1. 在 Google Cloud 控制台中開啟「建立叢集頁面,然後選取並填寫欄位,指定 Dataproc Hub 為使用者產生的叢集類型。
        1. 在左側面板底部,選取「Equivalent REST」。
        2. 複製產生的 JSON 區塊 (不含開頭的 POST 要求行),然後將 JSON 區塊貼到線上 JSON 轉 YAML 轉換器 (在線上搜尋「Convert JSON to YAML」)。
        3. 將轉換後的 YAML 複製到本機 cluster-config-filename.yaml 檔案。

      從現有叢集匯出 YAML 叢集設定檔

      1. 建立符合需求的叢集
      2. 將叢集設定匯出至本機 cluster-config-filename.yaml 檔案。
        gcloud dataproc clusters export cluster-name \
            --destination cluster-config-filename.yaml  \
            --region region
         

      將 YAML 設定檔儲存在 Cloud Storage 中

      將本機 YAML 叢集設定檔複製到 Cloud Storage 值區。

      gcloud storage cp cluster-config-filename.yaml gs://bucket-name/
      

      設定 Dataproc Hub 執行個體環境變數

      管理員可以設定下表列出的 Hub 環境變數,為 Hub 使用者產生的 Dataproc 叢集設定屬性。

      變數 說明 範例
      NOTEBOOKS_LOCATION 包含使用者筆記本的 Cloud Storage bucket 或 bucket 資料夾。`gs://` 前置字串為選用項目。預設:Dataproc 暫存 bucket gs://bucket-name/
      DATAPROC_CONFIGS 導向 YAML 叢集設定檔的 Cloud Storage 路徑字串清單 (以半形逗號分隔)。`gs://` 前置字元為選用項目。 預設值: gs://dataproc-spawner-dist/example-configs/。其中包含預先定義的 example-cluster.yamlexample-single-node.yaml gs://cluster-config-filename.yaml
      DATAPROC_LOCATIONS_LIST Dataproc Hub 執行個體所在區域的可用區尾碼。使用者可以選取其中一個區域,做為要產生 Dataproc 叢集的區域。預設值:「b」。 b,c,d
      DATAPROC_DEFAULT_SUBNET Dataproc Hub 執行個體生成 Dataproc 叢集時所在的子網路。預設值:Dataproc Hub 執行個體子網路。 https://www.googleapis.com/compute/v1/projects/project-id/regions/region/subnetworks/subnet-name
      DATAPROC_SERVICE_ACCOUNT Dataproc VM 執行的服務帳戶預設:如未設定,系統會使用預設的 Dataproc 服務帳戶 service-account@project-id.iam.gserviceaccount.com
      SPAWNER_DEFAULT_URL 根據預設,要在已生成的 Dataproc 叢集上顯示 Jupyter 還是 JupyterLab UI。預設值:「/lab」。 `/` 或 `/lab`,分別對應 Jupyter 或 JupyterLab。
      DATAPROC_ALLOW_CUSTOM_CLUSTERS 是否允許使用者自訂 Dataproc 叢集。 預設值:false。 「true」或「false」
      DATAPROC_MACHINE_TYPES_LIST 如果啟用叢集自訂功能 (DATAPROC_ALLOW_CUSTOM_CLUSTERS),這份清單會列出允許使用者為生成的 Dataproc 叢集選擇哪些機型。預設值:空白 (允許所有機器類型)。 n1-standard-4,n1-standard-8,e2-standard-4,n1-highcpu-4
      NOTEBOOKS_EXAMPLES_LOCATION 導向筆記本值區或值區資料夾的 Cloud Storage 路徑;叢集啟動時,上述值區和資料夾會下載至已生成的 Dataproc 叢集。預設值:空白。 gs://bucket-name/

      設定 Hub 環境變數

      設定 Hub 環境變數的方法有兩種:

      1. 從控制台設定 Hub 環境變數

      2. 在文字檔中設定 Hub 環境變數

      從控制台設定 Hub 環境變數

      在 Google Cloud 控制台的「Dataproc」→「Workbench」頁面中,從「User-Managed Notebooks」分頁建立 Dataproc Hub 執行個體時,您可以按一下「Populate」按鈕,開啟「Populate Dataproc Hub」表單,設定每個環境變數。

      在文字檔中設定 Hub 環境變數

      1. 建立檔案。您可以使用文字編輯器,在本機檔案中設定 Dataproc Hub 執行個體環境變數。或者,您也可以填入預留位置值,並變更或新增變數及其值,然後執行下列指令來建立檔案。

        cat <<EOF > environment-variables-file
        DATAPROC_CONFIGS=gs://bucket/cluster-config-filename.yaml
        NOTEBOOKS_LOCATION=gs://bucket/notebooks
        DATAPROC_LOCATIONS_LIST=b,c
        EOF
        

      2. 將檔案儲存到 Cloud Storage。將本機 Dataproc Hub 執行個體環境變數檔案複製到 Cloud Storage 值區。

        gcloud storage cp environment-variable-filename gs://bucket-name/folder-name/

      設定 Identity and Access Management (IAM) 角色

      Dataproc Hub 包含下列身分,並具備下列功能:

      • 管理員:建立 Dataproc Hub 執行個體
      • 資料和機器學習使用者:存取 Dataproc Hub UI
      • Dataproc Hub 服務帳戶:代表 Dataproc Hub
      • Dataproc 服務帳戶:代表 Dataproc Hub 建立的 Dataproc 叢集。

      每個身分都需要特定角色或權限,才能執行相關聯的工作。下表摘要列出各身分所需的 IAM 角色和權限。

      身分識別 類型 角色或權限
      Dataproc Hub 管理員 使用者或服務帳戶 roles/notebooks.admin
      Dataproc Hub 使用者 使用者 notebooks.instances.use, dataproc.clusters.use
      Dataproc Hub 服務帳戶 roles/dataproc.hubAgent
      Dataproc 服務帳戶 roles/dataproc.worker

      建立 Dataproc Hub 執行個體

      1. 事前準備:如要透過 Google Cloud 控制台建立 Dataproc Hub 執行個體,使用者帳戶必須具備 compute.instances.create 權限。此外,執行個體的服務帳戶 (Compute Engine 預設服務帳戶,或您在「IAM 與管理」>「服務帳戶」中列出的使用者指定服務帳戶,請參閱「Dataproc VM 服務帳戶」) 必須具備 iam.serviceAccounts.actAs 權限。

      2. 前往 Google Cloud 控制台的「Dataproc」→「Workbench」頁面,然後選取「User-Managed Notebooks」分頁標籤。

      3. 如果系統未預先選取「環境:Dataproc Hub」做為篩選器,請點選「篩選器」方塊,然後選取「環境:Dataproc Hub」。

      4. 按一下「New Notebook」(新增筆記本)→「Dataproc Hub」

      5. 在「Create a user-managed notebook」(建立由使用者管理的筆記本) 頁面中,提供下列資訊:

        1. 筆記本名稱:Dataproc Hub 執行個體名稱。
        2. 「Region」(區域):選取 Dataproc Hub 執行個體的區域。這個 Dataproc Hub 執行個體生成的 Dataproc 叢集也會在這個區域中建立。
        3. 「Zone」(區域):在所選區域中,選擇一個區域。
        4. 環境:
          1. Environment:選取「Dataproc Hub」。
          2. Select a script to run after creation (選用):您可以插入或瀏覽並選取初始化動作指令碼或執行檔,在產生的 Dataproc 叢集上執行。
          3. Populate Dataproc Hub (optional):按一下「填入」Populate,開啟表單,即可設定每個 Hub 環境變數 (如需各變數的說明,請參閱「設定 Dataproc Hub 執行個體環境變數」)。對於任何未設定的環境變數,Dataproc 會使用預設值。或者,您也可以設定「中繼資料」key:value 配對來設定環境變數 (請參閱下一個項目)。
          4. Metadata
            1. 如果您已建立包含中心環境變數設定的文字檔 (請參閱「設定中心環境變數」),請提供檔案名稱做為 key,並提供檔案的 gs://bucket-name/folder-name/environment-variable-filename Cloud Storage 位置做為 value。對於任何未設定的環境變數,Dataproc 會使用預設值。
        5. 機器設定:
          1. Machine Type:選取 Compute Engine 機器類型
          2. 設定其他機器設定選項。
        6. 其他選項
          1. 您可以展開「磁碟」、「網路」、「權限」、「安全性」和「環境升級與系統健康狀態」區段,然後設定或取代預設值。
        7. 按一下「建立」,啟動 Dataproc Hub 執行個體。
      6. 建立 Dataproc Hub 執行個體後,系統會啟用「Open JupyterLab」連結。使用者點選這個連結,即可開啟 JupyterHub 伺服器頁面,設定及建立 Dataproc JupyterLab 叢集 (請參閱「使用 Dataproc Hub」)。

清除所用資源

刪除 Dataproc Hub 執行個體

  • 如要刪除 Dataproc Hub 執行個體,請按照下列步驟操作:
    gcloud compute instances delete --project=${PROJECT} ${INSTANCE_NAME}
    

刪除值區

  • 如要刪除您在「事前準備」中建立的 Cloud Storage bucket,包括儲存在 bucket 中的資料檔案,請按照下列步驟操作:
    gcloud storage rm gs://${BUCKET_NAME} --recursive
    

後續步驟