使用 gcloud CLI 创建 Memorystore for Redis 实例

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


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

操作演示


准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud新手, 请创建一个账号来评估我们的产品在 实际场景中的表现。新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create 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.

    Go to project selector

  3. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  4. Verify that billing is enabled for your Google Cloud project.

  5. Enable the Memorystore for Redis 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.

    Enable the API

  6. In the Google Cloud console, on the project selector page, select or create 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.

    Go to project selector

  7. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  8. Verify that billing is enabled for your Google Cloud project.

  9. Enable the Memorystore for Redis 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.

    Enable the API

  10. 如果您尚未安装,请安装 Google Cloud CLI

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

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

    gcloud components update

所需角色

如需获得创建 Memorystore for Redis 实例所需的权限,请让管理员向您授予项目的以下 IAM 角色:

如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

您也可以通过自定义 角色或其他预定义 角色来获取所需的权限。

创建 Memorystore for Redis 实例

在本部分中,您将创建一个 2 GB 的 Memorystore for Redis 实例,该实例位于 us-central1 区域,并且属于基本层级。如需详细了解层级,请参阅 Redis 层级功能

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

    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. 创建实例后,如需获取实例的 IP 地址和端口号,请输入以下命令:

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

    如果您可以访问该实例,则该命令会返回以下结果:

    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

  5. 记下实例的 hostport 值。您需要使用这些值来连接到实例。

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

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

  1. 如果您没有与该实例使用同一授权网络的 Compute Engine 虚拟机,请创建一个虚拟机并使用 SSH 连接到该虚拟机。为此,请按照在 Compute Engine 中创建 Linux 虚拟机实例中的步骤操作。

  2. 使用 apt-get 安装 telnet

    sudo apt-get install telnet
    
  3. 从终端通过 telnet 连接到实例的 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. 如需删除实例,请输入以下命令:

    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]

    如果您可以删除该实例,则该命令会返回以下结果:

    Deleted instance [myinstance].

  3. 可选 。如果您为此快速入门创建了 Compute Engine 虚拟机, 请将其删除

后续步骤