在叢集上安裝及執行 Jupyter 筆記本

目標

本教學課程說明如何在新的叢集上安裝 Managed Service for Apache Spark Jupyter 元件,然後使用 Managed Service for Apache Spark 元件閘道,從本機瀏覽器連線至叢集上執行的 Jupyter 筆記本 UI。

費用

在本文件中,您會使用下列 Google Cloud計費元件:

您可以使用 Pricing Calculator,根據預測用量估算費用。

初次使用 Google Cloud 的使用者可能符合免費試用期資格。

事前準備

請建立 Google Cloud 專案和 Cloud Storage bucket。

  1. 設定專案

    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 billing is enabled for your Google Cloud project.

    4. Enable the Dataproc, Compute Engine, and Cloud Storage APIs, if any are not already enabled.

      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 APIs

    5. Install the Google Cloud CLI.

    6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    7. To initialize the gcloud CLI, run the following command:

      gcloud init
    8. 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

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

    10. Enable the Dataproc, Compute Engine, and Cloud Storage APIs, if any are not already enabled.

      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 APIs

    11. Install the Google Cloud CLI.

    12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    13. To initialize the gcloud CLI, run the following command:

      gcloud init

  2. 在專案中建立 Cloud Storage bucket,儲存您在本教學課程中建立的筆記本。

    1. 前往 Google Cloud 控制台的「Cloud Storage bucket」頁面。

      前往「Buckets」(值區) 頁面

    2. 點選 「Create」(建立)。
    3. 在「Create a bucket」(建立 bucket) 頁面中,輸入 bucket 資訊。如要前往下一個步驟,請按一下「Continue」(繼續)。
      1. 在「Get started」(開始使用) 部分,執行下列操作:
        • 輸入符合 bucket 命名規定的全域不重複名稱。
        • 如要新增值區標籤,請展開「Labels」(標籤) 專區 (),按一下「Add label」(新增標籤) ,然後為標籤指定key和value。
      2. 在「Choose where to store your data」(選擇資料的儲存位置) 專區中,執行下列操作:
        1. 選取「位置類型」。
        2. 從「Location type」下拉式選單中,選擇永久儲存 bucket 資料的位置。
          • 如果選取「雙區域」位置類型,也可以勾選相關核取方塊,啟用強化型複製功能。
        3. 如要設定跨 bucket 複製,請選取「Add cross-bucket replication via Storage Transfer Service」(新增透過 Storage 移轉服務執行的跨 bucket 複製作業),然後按照下列步驟操作:

          設定跨 bucket 複製作業

          1. 在「Bucket」選單中選取 bucket。
          2. 在「Replication settings」(複製設定) 專區中,按一下「Configure」(設定),即可調整複製工作的設定。

            「Configure cross-bucket replication」(設定跨 bucket 複製作業) 窗格隨即顯示。

            • 如要依物件名稱前置字串篩選要複製的物件,請輸入要納入或排除物件的前置字串,然後按一下「 新增前置字串」。
            • 如要為複製的物件設定儲存空間級別,請從「Storage class」(儲存空間級別) 選單中選取儲存空間級別。如果略過這個步驟,複製的物件預設會使用目的地 bucket 的儲存空間級別。
            • 按一下「完成」。
      3. 在「Choose how to store your data」(選擇資料儲存方式) 部分,執行下列操作:
        1. 選取 bucket 的預設儲存空間級別,或選取「Autoclass」,讓系統自動管理 bucket 資料的儲存空間級別。
        2. 如要啟用階層命名空間,請在「Optimize storage for data-intensive workloads」(為資料密集型工作負載提供最理想的儲存空間) 區段中,選取「Enable hierarchical namespace on this bucket」(為這個 bucket 啟用階層命名空間)。
      4. 在「Choose how to control access to objects」(選取如何控制物件的存取權) 專區中,選取 bucket 是否強制執行禁止公開存取功能,並為 bucket 的物件選取存取控管方法。
      5. 在「Choose how to protect object data」(選擇保護物件資料的方式) 部分,執行下列操作:
        • 在「資料保護」下方,選取要為 bucket 設定的選項。
          • 如要啟用虛刪除功能,請勾選「Soft delete policy (For data recovery)」(虛刪除政策 (用於資料復原)) 核取方塊,並指定物件刪除後要保留的天數。
          • 如要設定物件版本管理,請勾選「Object versioning (For version control)」(物件版本管理 (用於版本管控)) 核取方塊,並指定每個物件的版本數量上限,以及非現行版本的到期天數。
          • 如要啟用物件和 bucket 的資料保留政策,請勾選「資料保留 (法規遵循)」核取方塊,然後執行下列操作:
            • 如要啟用物件保留鎖定功能,請勾選「Enable object retention」(啟用物件保留功能) 核取方塊。
            • 如要啟用值區鎖定功能,請勾選「設定值區資料保留政策」核取方塊,然後選擇保留期限的時間單位和長度。
        • 如要選擇物件資料的加密方式,請展開「Data encryption」(資料加密) 專區 (),然後選取「Data encryption」(資料加密) 方法。
    4. 點選「建立」。
    5. 筆記本將儲存在 Cloud Storage 的 gs://bucket-name/notebooks/jupyter 中。

建立叢集並安裝 Jupyter 元件

建立已安裝 Jupyter 元件的叢集。

開啟 Jupyter 和 JupyterLab UI

在 Google Cloud 控制台中,按一下Google Cloud 控制台的元件閘道連結,即可開啟在叢集主要節點執行的 Jupyter 筆記本或 JupyterLab UI。

Jupyter 執行個體顯示的頂層目錄是虛擬目錄,可讓您查看 Cloud Storage bucket 或本機檔案系統的內容。如要選擇位置,請按一下 Cloud Storage 的「GCS」連結,或叢集主要節點本機檔案系統的「Local Disk」(本機磁碟)。

  1. 按一下「GCS」連結。Jupyter 筆記本網頁 UI 會顯示儲存在 Cloud Storage bucket 中的筆記本,包括您在本教學課程中建立的筆記本。

清除所用資源

完成教學課程後,您可以清除所建立的資源,這樣資源就不會繼續使用配額,也不會產生費用。下列各節將說明如何刪除或關閉這些資源。

刪除專案

如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。

刪除專案的方法如下:

  1. 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。

    前往「Manage resources」(管理資源)

  2. 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
  3. 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。

刪除叢集

  • 刪除叢集的方法如下:
    gcloud dataproc clusters delete cluster-name \
        --region=${REGION}
    

刪除 bucket

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

後續步驟