本快速入门介绍如何使用 gcloud
命令行工具快速启动和运行 Filestore。在本快速入门中,您将学习如何完成以下操作:
- 创建 Filestore 实例。
- 在 Compute Engine 客户端虚拟机上从该实例装载文件共享。
- 在已装载的文件共享上创建文件。
- 删除 Filestore 实例。
准备工作
选择或创建 GCP 项目:
创建项目
gcloud projects create project-id
选择项目
gcloud config set project project-id
其中,project-id 是 Google Cloud 项目的 ID。
确保您的 Google Cloud Platform 项目已启用结算功能。 了解如何启用结算功能
-
如果您已安装 Cloud SDK,请运行以下命令进行更新:
gcloud components update
完成本教程后,您可以删除所创建的资源以避免继续计费。如需了解详情,请参阅清理。
创建一个 Compute Engine 虚拟机作为客户端
Linux
-
创建 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
-
创建 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 实例
创建 Filestore 实例。按如下方式配置实例:
- 将实例命名为
nfs-server
。 - 将
--zone
标志设置为us-central1-c
。 - 将
--tier
标志设置为BASIC_HDD
。 - 将
--file-share
标志设置为name="vol1",capacity=1TB
。 - 将
--network
标志设置为name="default"
。
gcloud beta filestore instances create nfs-server --zone=us-central1-c --tier=BASIC_HDD --file-share=name="vol1",capacity=1TB --network=name="default"
- 将实例命名为
获取刚创建的 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
-
建立与
nfs-client
实例的 SSH 连接。gcloud compute ssh nfs-client
通过在
nfs-client
的终端窗口上运行以下命令来安装 NFS:sudo apt-get -y update && sudo apt-get -y install nfs-common
在
nfs-client
实例上为 Filestore 文件共享创建装载目录:sudo mkdir /mnt/test
通过指定 Filestore 实例的 IP 地址、文件共享的名称以及要装载到的装载目录,使用
mount
命令将文件共享装载到nfs-client
实例:sudo mount 10.0.0.2:/vol1 /mnt/test
更改权限,将文件共享设置为可访问:
sudo chmod go+rw /mnt/test
Windows
登录到 nfs-client
实例并以管理员身份打开命令提示符
创建一个帐号并为
nfs-client
实例设置一个初始密码:gcloud compute reset-windows-password nfs-client
将您的实例配置为允许连接到串行端口:
gcloud compute instances add-metadata nfs-client --metadata=serial-port-enable=1
进入交互式会话:
gcloud compute connect-to-serial-port nfs-client --port=2
在
SAC>
提示符下,创建一个新频道:cmd
已创建名为
Cmd0001
的频道。连接到该频道:
ch -sn Cmd0001
输入
nfs-client
实例的用户名和密码,并将Domain
字段留空。这会让您连接到nfs-client
实例的Command Prompt
接口。
安装 NFS 客户端
在
nfs-client
的Command Prompt
中,切换到Windows PowerShell
:powershell
安装
NFS
客户端Install-WindowsFeature -Name NFS-Client
在系统提示时重启
nfs-client
实例:restart-computer
在
SAC>
提示符处,等待以下通知显示:EVENT: The CMD command is now available.
然后,按照之前的说明运行
cmd
和ch -sn
命令以登录并重新连接到nfs-client
实例。
配置 NFS 客户端使用的用户 ID
- 在命令提示符中,运行
powershell
以切换到 Windows PowerShell。 在
PowerShell
中,运行以下命令以创建两个新的注册表项:AnonymousUid
和AnonymousGid
: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
重启 NFS 客户端服务:
nfsadmin client stop
nfsadmin client start
将 vol1
文件共享映射到 nfs-client
实例
退出
PowerShell
:exit
从
Command Prompt
开始,将文件共享vol1
映射到nfs-server
的云端硬盘Z:
:net use z: \\10.0.0.2\vol1
在已装载的文件共享上创建文件
Linux
在
nfs-client
终端窗口中,通过运行以下命令创建名为testfile
的文件:echo 'This is a test' > /mnt/test/testfile
确认文件已创建,方法是运行以下命令并验证
testfile
是否在返回的目录内容中:ls /mnt/test
Windows
在
nfs-client
实例的“命令提示符”窗口中,创建名为testfile
的文件:echo 'This is a test' > Z:\testfile
运行以下命令,确认文件已创建:
dir Z:
并验证
testfile
是否在返回的目录内容中。
清理
为避免系统因本快速入门中使用的资源向您的 Google Cloud 帐号收取费用,请按照以下步骤操作。
删除 Google Cloud 项目
- 在 Cloud Console 中,转到管理资源页面。
- 在项目列表中,选择要删除的项目,然后点击删除。
- 在对话框中输入项目 ID,然后点击关闭以删除项目。
删除 Filestore 实例
删除 nfs-server
实例:
gcloud filestore instances delete nfs-server --zone=us-central1-c
删除 Compute Engine 实例
删除实例:gcloud compute instances delete nfs-client
后续步骤
- 阅读 Filestore 概览以详细了解 Filestore 的基础知识。
- 按照创建实例中的说明自行设置 Filestore 实例。
- 请阅读访问控制,了解如何控制对 Filestore 操作的访问以及实例上的资源。