使用 gcloud CLI 创建 Memorystore for Redis 实例

了解如何创建新的 Memorystore for Redis 实例、连接该实例以及将其删除。


如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示

操作演示


准备工作

  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 CLI(如果您尚未安装)。

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

  7. 如果您已安装 Google Cloud CLI,请更新它。

    gcloud components update

创建一个 Memorystore for Redis 实例

如需创建 Memorystore for Redis 实例,请执行以下操作:

  1. 打开终端窗口。
  2. 输入以下命令,在 gcloud 中将要在其中创建实例的项目设置为默认项目:

    gcloud config set core/project PROJECT_ID
  3. 输入以下命令,在 us-central1 区域中创建 2 GiB 基本层级 Redis 实例:

    gcloud redis instances create myinstance --size=2 --region=us-central1 \
        --redis-version=redis_6_x
  4. 创建实例后,输入 describe 命令以获取实例的 IP 地址和端口:

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

    如果成功,gcloud 将返回以下内容:

    authorizedNetwork: projects/my-project/global/networks/default
    createTime: '2018-04-09T21:47:56.824081Z'
    currentLocationId: us-central1-a
    host: 10.0.0.27
    locationId: us-central1-a
    memorySizeGb: 2
    name: projects/my-project/locations/us-central1/instances/myinstance
    networkThroughputGbps: 2
    port: 6379
    redisVersion: REDIS_6_X
    reservedIpRange: 10.0.0.24/29
    state: READY
    tier: BASIC

    您将使用 hostport 值连接到实例。

从 Compute Engine 虚拟机连接到 Redis 实例

您可以从任何具有支持的 RFC 1918 IP 地址且使用 Redis 实例的授权网络的 Compute Engine 虚拟机连接到 Redis 实例。

  1. 如果您还没有与 Redis 实例使用同一授权网络的 Compute Engine 虚拟机,请创建一个虚拟机并按照 Linux 虚拟机使用快速入门中的说明连接到该虚拟机。

  2. 使用 apt-get 安装 telnet

    sudo apt-get install telnet
    
  3. 从该终端通过 telnet 连接到 Redis 实例的 IP 地址,并将 variables 替换为适当的值。

    telnet instance-ip-address 6379
    

    如果成功,该命令将返回以下结果:

    Trying instance-ip-address…
    Connected to instance-ip-address
    
  4. 在 telnet 会话中,输入一些 Redis 命令:

    输入:

    PING
    

    结果:

    PONG
    

    输入

    SET HELLO WORLD
    

    结果:

    +OK
    

    请输入:

    GET HELLO
    

    结果:

    $5
    WORLD
    

清理

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

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

    gcloud redis instances delete myinstance --region=us-central1
  2. 输入 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]

    如果成功,gcloud 将返回以下响应:

    Deleted instance [myinstance].

后续步骤