使用 gcloud CLI 创建 Filestore 实例

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

  • 创建 Filestore 实例。
  • 在 Compute Engine 客户端虚拟机上从该实例装载文件共享。
  • 在已装载的文件共享上创建文件。
  • 删除 Filestore 实例。

准备工作

  1. 选择或创建项目:

    创建项目

     gcloud projects create project-id
    

    选择项目

     gcloud config set project project-id
    

    其中,project-id 是 Google Cloud 项目的 ID。

  2. 确保您的项目已启用结算功能。 了解如何启用结算功能

  3. 安装并初始化 gcloud CLI

    如果您已安装 gcloud CLI,请运行以下命令进行更新:

    gcloud components update
    

完成本文档中描述的任务后,您可以通过删除所创建的资源来避免继续计费。如需了解详情,请参阅清理

创建一个 Compute Engine 虚拟机作为客户端

Linux

  1. 创建 Compute Engine 实例。按如下所示的方式配置实例:
    • 将实例命名为 nfs-client
    • --zone 标志设置为 us-central1-c
    • --image-project 标志设置为 debian-cloud
    • --image-family 标志设置为 debian-10
    • --tags 标志设置为 http-server,
    gcloud compute instances create nfs-client --zone=us-central1-c --image-project=debian-cloud --image-family=debian-10 --tags=http-server,

Windows

  1. 创建 Compute Engine 实例。按如下所示的方式配置实例:
    • 将实例命名为 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,

创建 Filestore 实例

  1. 创建 Filestore 实例。按如下方式配置实例:

    • 将实例命名为 nfs-server
    • --zone 标志设置为 us-central1-c
    • --tier 标志设置为 BASIC_HDD
    • --file-share 标志设置为 name="vol1",capacity=1TB
    • --network 标志设置为 name="default"
    gcloud filestore instances create nfs-server --zone=us-central1-c --tier=BASIC_HDD --file-share=name="vol1",capacity=1TB --network=name="default"
  2. 获取您创建的 Filestore 实例的相关信息:

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

    该命令返回如下内容:

    createTime: '2019-10-11T17:28:23.340943077Z'
    fileShares:
    - capacityGb: '1024'
     name: vol1
    name: projects/yourproject/locations/us-central1-c/instances/nfs-server
    networks:
    - ipAddresses:
     - 10.0.0.2
     network: default
     reservedIpRange: 10.0.0.0/29
    state: READY
    tier: BASIC_HDD
    

    复制实例的 IP 地址,以便在装载文件共享时使用。对于此快速入门,我们使用 IP 地址 10.0.0.2

nfs-client 实例上装载 Filestore 文件共享

Linux

  1. 建立与 nfs-client 实例的 SSH 连接。
    gcloud compute ssh nfs-client

  1. 通过在 nfs-client 的终端窗口上运行以下命令来安装 NFS:

    sudo apt-get -y update &&
    sudo apt-get -y install nfs-common
    
  2. nfs-client 实例上为 Filestore 文件共享创建装载目录:

    sudo mkdir /mnt/test
    
  3. 指定 Filestore 实例的 IP 地址、文件共享的名称以及要装载到的装载目录,使用 mount 命令将文件共享装载到 nfs-client 实例:

    sudo mount 10.0.0.2:/vol1 /mnt/test
    
  4. 更改权限,将文件共享设置为可访问:

    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 实例,方法是指定 Filestore 实例的 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 项目

    删除 Google Cloud 项目:

    gcloud projects delete PROJECT_ID

删除 Filestore 实例

删除 nfs-server 实例:

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

删除 Compute Engine 实例

删除实例:
gcloud compute instances delete nfs-client

后续步骤