使用 gcloud CLI 建立 Filestore 執行個體

本快速入門導覽課程說明如何使用 Google Cloud CLI,快速開始及執行 Identity and Access Management。在本快速入門導覽課程中,您將瞭解如何執行下列操作:

  • 建立 IAM 執行個體。
  • 在 Compute Engine 用戶端 VM 上,掛接該執行個體的檔案共用區。
  • 在已掛接的檔案共用區中建立檔案。
  • 刪除 IAM 執行個體。

事前準備

  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. Install the Google Cloud CLI.

  3. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

  4. 執行下列指令,初始化 gcloud CLI:

    gcloud init
  5. Create or select 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.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  6. 如要使用現有專案進行本指南中的操作,請確認您具有完成本指南所需的權限。如果您建立新專案,則已具備必要權限。

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

  8. Enable the Filestore API:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable file.googleapis.com
  9. Install the Google Cloud CLI.

  10. 若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

  11. 執行下列指令,初始化 gcloud CLI:

    gcloud init
  12. Create or select 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.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  13. 如要使用現有專案進行本指南中的操作,請確認您具有完成本指南所需的權限。如果您建立新專案,則已具備必要權限。

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

  15. Enable the Filestore API:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable file.googleapis.com
  16. 必要的角色

    如要取得完成本快速入門導覽課程所需的權限,請要求管理員在專案中授予您下列 IAM 角色:

    如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

    您或許也能透過自訂角色或其他預先定義的角色,取得必要權限。

    完成本文所述工作後,您可以刪除建立的資源,避免繼續計費,詳情請參閱「清除所用資源」一節。

    建立 Compute Engine VM

    Linux

    1. Create a Compute Engine instance. Configure the instance as follows:
      • Name the instance nfs-client.
      • --zone 旗標設為 us-central1-c
      • --image-project 旗標設為 debian-cloud
      • --image-family 旗標設為 debian-11
      • --tags 旗標設為 http-server,
      • gcloud compute instances create nfs-client --zone=us-central1-c --image-project=debian-cloud --image-family=debian-11 --tags=http-server,

    Windows

    1. Create a Compute Engine instance. Configure the instance as follows:
      • Name the instance nfs-client.
      • --zone 旗標設為 us-central1-c
      • --image-project 旗標設為 windows-cloud
      • --image-family 旗標設為 windows-2012-r2
      • --tags 旗標設為 http-server,http-server,
      • gcloud compute instances create nfs-client --zone=us-central1-c --image-project=windows-cloud --image-family=windows-2012-r2 --tags=http-server,http-server,

    建立 IAM 執行個體

    本快速入門導覽課程說明如何在啟用自訂效能的區域服務層級中建立執行個體。如要瞭解如何建立執行個體,請參閱建立執行個體

    1. 建立 IAM 執行個體。如下所示建立執行個體:

      • 為執行個體 nfs-server 命名。
      • --region 旗標設為 us-central1
      • --tier 旗標設為 REGIONAL
      • --performance 旗標設為 max-iops-per-tb=17000

      • --file-share 旗標設為 name="vol1",capacity=1TB

      • --network 旗標設為 name="default"

        gcloud filestore instances create nfs-server --region=us-central1 --tier=REGIONAL --performance=max-iops-per-tb=17000 --file-share=name="vol1",capacity=1TB --network=name="default"
    2. 取得您建立的 IAM 執行個體相關資訊:

        gcloud filestore instances describe nfs-server --region=us-central1 

      指令會傳回類似下列內容:

          createTime: '2025-02-12T09:15:08.163246004Z'
          customPerformanceSupported: true
          fileShares:
           -capacityGb: '1024'
              name: vol1
          name: projects/yourproject/locations/us-central1/instances/nfs-server
          networks:
          -connectMode: DIRECT_PEERING
            ipAddresses:
             - 10.0.0.2
            network: default
            reservedIpRange: 10.0.0.2/26
          performanceConfig:
            iopsPerTb:
              maxIopsPerTb: '17000'
          performanceLimits:
            maxIops: '17000'
            maxReadIops: '17000'
            maxReadThroughputBps: '417792000'
            maxWriteIops: '5100'
            maxWriteThroughputBps: '139264000'
          protocol: NFS_V3
          state: READY
          tier: REGIONAL

    複製執行個體的 IP 位址,供掛接檔案共用區時使用。在本快速入門導覽課程中,我們使用 IP 位址 10.0.0.2

    nfs-client 執行個體中掛接 IAM 檔案共用區

    Linux

    1. Establish an SSH connection to the nfs-client 執行個體:
      gcloud compute ssh nfs-client

    1. nfs-client 的終端機視窗中執行下列指令,安裝 NFS:

      sudo apt-get -y update &&
      sudo apt-get -y install nfs-common
      
    2. nfs-client 執行個體上,為 IAM 檔案共用區建立掛接目錄:

      sudo mkdir /mnt/test
      
    3. 指定 IAM 執行個體的 IP 位址、檔案共用區名稱,以及要掛接的掛接目錄,即可使用 mount 指令將檔案共用區掛接到 nfs-client 執行個體:

      sudo mount MOUNT-POINT-DIRECTORY /mnt/test
      

      其中:

      MOUNT-POINT-DIRECTORY 是掛接 Filestore 檔案共用區的所在目錄路徑。例如:10.0.0.2:/vol1

    4. 選用:確認已掛接 Filestore 檔案共用區:

      df -h --type=nfs

    5. 變更設定,開放檔案共用區的存取權限:

      sudo chmod go+rw /mnt/test
      

    Windows

    登入 nfs-client 執行個體,然後以系統管理員身分開啟命令提示字元

    1. nfs-client 執行個體建立帳戶並設定初始密碼:

      gcloud compute reset-windows-password nfs-client
      
    2. 將執行個體設定為啟用序列埠連線:

      gcloud compute instances add-metadata nfs-client --metadata=serial-port-enable=1
      
    3. 輸入互動工作階段:

      gcloud compute connect-to-serial-port nfs-client --port=2
      
    4. SAC> 提示下,建立新通道:

      cmd
      

      系統會建立名為 Cmd0001 的管道。

    5. 連線至管道:

      ch -sn Cmd0001
      
    6. 輸入 nfs-client 執行個體的使用者名稱和密碼,並將 Domain 欄位保留空白。您已連線至 nfs-client 執行個體的 Command Prompt 介面。

    安裝 NFS 用戶端

    1. nfs-clientCommand Prompt 中,切換至 Windows PowerShell

      powershell
      
    2. 安裝 NFS 用戶端:

      Install-WindowsFeature -Name NFS-Client
      
    3. 系統出現提示時,重新啟動 nfs-client 執行個體:

      restart-computer
      
    4. SAC> 提示中,等待顯示下列通知:

      EVENT: The CMD command is now available.

      然後,按照先前的指示執行 cmdch -sn 指令,登入並重新連線至 nfs-client 執行個體。

    設定 NFS 用戶端所用的使用者 ID

    1. 在命令提示字元中執行 powershell,切換至 Windows PowerShell。
    2. PowerShell 中執行下列指令,建立兩個新的登錄項目 AnonymousUidAnonymousGid

      New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Default" `
          -Name "AnonymousUid" -Value "0" -PropertyType DWORD
      
      New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\ClientForNFS\CurrentVersion\Default" `
          -Name "AnonymousGid" -Value "0" -PropertyType DWORD
      
    3. 重新啟動 NFS 用戶端服務:

      nfsadmin client stop
      
      nfsadmin client start
      

    vol1 檔案共用區對應至 nfs-client 執行個體

    1. 離開 PowerShell

      exit
      
    2. Command Prompt,使用 mount 指令將檔案共用區掛接到 nfs-client 執行個體,並指定 IAM 執行個體的 IP 位址、檔案共用區名稱,以及要掛接的磁碟機代號:

      mount -o mtype=hard 10.0.0.2:/vol1 z:
      

    在已掛接的檔案共用區中建立檔案

    Linux

    1. nfs-client 終端機視窗中執行下列指令,建立名為 testfile 的檔案:

      echo 'This is a test' > /mnt/test/testfile
      
    2. 執行下列指令並確認 testfile 位於傳回的目錄內容中,確認檔案已建立:

      ls /mnt/test
      

    Windows

    1. nfs-client 執行個體的命令提示字元視窗中,建立名為 testfile 的檔案:

      echo 'This is a test' > Z:\testfile
      
    2. 執行下列指令,確認檔案已建立:

      dir Z:
      

      並確認 testfile 位於傳回的目錄內容中。

    清除所用資源

    為了避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請刪除含有這些資源的 Google Cloud 專案。

    刪除 Google Cloud 專案

      Delete a Google Cloud project:

      gcloud projects delete PROJECT_ID

    刪除 IAM 執行個體

    刪除 nfs-server 執行個體:

    gcloud filestore instances delete nfs-server --zone=us-central1-c
    

    刪除 Compute Engine 執行個體

    刪除執行個體:
    gcloud compute instances delete nfs-client

    後續步驟