使用 gcloud CLI 创建 Filestore 实例

本快速入门介绍了如何使用 Google Cloud CLI 快速启动和运行 Identity and Access Management。在本快速入门中,您将了解如何执行以下操作:

  • 创建 IAM 实例。
  • 在 Compute Engine 客户端虚拟机上从该实例装载文件共享。
  • 在已装载的文件共享上创建文件。
  • 删除 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 虚拟机

    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 中,通过指定 IAM 实例的 IP 地址、文件共享的名称以及要装载到的驱动器盘符,使用 mount 命令将文件共享装载到 nfs-client 实例:

      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

    后续步骤