使用 gcloud CLI 创建 Memorystore for Memcached 实例

本快速入门将引导您完成创建 Memorystore for Memcached 实例、连接到实例、发送部分 Memcached 命令以及删除实例的步骤。

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. 在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

    转到“项目选择器”

  5. 确保您的 Google Cloud 项目已启用结算功能

  6. 如果您尚未安装 Google Cloud SDK,请先安装。

    出现提示时,请选择您在上面选择或创建的项目。

  7. 如果您已安装 Google Cloud SDK,请进行更新。

    gcloud components update
  8. 为您的项目启用 Service Networking API。
    启用 Service Networking API
  9. 为您的项目启用 Memorystore for Memcached API。
    启用 Memorystore for Memcached API

创建 Memorystore for Memcached 实例

要创建 Memorystore for Memcached 实例,请执行以下操作:

  1. 打开终端窗口。

  2. 输入以下命令,在 gcloud 中将要在其中创建实例的项目设置为默认项目:

    gcloud config set core/project project-id
    
  3. 输入以下命令,在 us-central1 区域中创建包含 3 个节点(每个节点有 4 个核心和 10GB 内存)的 Memcached 实例:

    gcloud memcache instances create myinstance --node-count=3 --node-cpu=4 \
        --node-memory=10GB --region=us-central1 --memcached-version=1.6.15
    
  4. 创建实例后,请运行以下 describe 命令,并记下其中一个节点的 IP 地址。

    gcloud memcache instances describe myinstance --region=us-central1
    

从 Compute Engine 虚拟机连接到您的实例

  1. 连接到 Linux 虚拟机。如需了解连接到 Linux 虚拟机的步骤,请参阅快速入门:使用 Linux 虚拟机。如果您按照快速入门中的说明使用了基于 Debian 的映像创建虚拟机实例,请使用 apt-get 安装 telnet

    sudo apt-get install telnet
    
  2. 在终端窗口中,使用 telnet 命令建立其中一个 Memcached 节点的 IP 地址,并将 variables 替换为适当的值。

    telnet node-ip-address 11211
    

    如果它正常运行,您应该会看到以下输出,其中 [bracketed-variables] 已替换为您项目的变量:

    Trying [node-ip-address]…
    Connected to [node-ip-address]
    
  3. 在 telnet 会话中,输入一些 Memcached 命令:

    输入:

    get greeting
    

    结果:

    END
    

    请输入:

    set greeting 1 0 11
    hello world
    

    结果:

    STORED
    

    请输入:

    get greeting
    

    Result:

    VALUE greeting 1 11
    hello world
    END
    

Memorystore for Memcached 支持标准开源 Memcached 库。

清理

为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。

  1. 为避免系统因本快速入门中使用的资源向您的 Google Cloud 账号收取费用,请通过输入以下命令删除实例:

    gcloud memcache instances delete myinstance --region=us-central1
    

    输入 Y 以确认删除实例:

    You are about to delete instance [myinstance] in [us-central1].
    Any associated data will be lost.
    Do you want to continue (Y/n)? Y
    Delete request issued for: [myinstance]
    
  2. 如果成功,gcloud 将返回以下响应:

    Deleted instance [myinstance]
    

后续步骤