使用 Terraform 创建 Memorystore for Redis 实例

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

本快速入门使用面向 Terraform 的 Google Cloud Platform 提供商

准备工作

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  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. 如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。

  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. 如果您要使用现有项目来完成本指南,请验证您是否拥有完成本指南所需的权限。如果您创建了新项目,则您已拥有所需的权限。

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

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

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

    gcloud components update
  12. 启用 Memorystore for Redis API。
    Memorystore for Redis
  13. 具有可向其添加 Memorystore 资源的 Terraform 文件。如需了解如何使用 Google Cloud 设置 Terraform,请参阅 Google Cloud 提供程序使用入门
  14. 所需的角色

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

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

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

创建 Memorystore for Redis 实例

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

  1. 将以下资源添加到 Terraform 配置文件中:

    resource "google_redis_instance" "my_memorystore_redis_instance" {
      name           = "myinstance"
      tier           = "BASIC"
      memory_size_gb = 2
      region         = "us-central1"
      redis_version  = "REDIS_6_X"
    }
    
  2. 将以下输出值添加到 Terraform 配置文件中,以输出实例的 IP 地址。您需要使用此地址连接到实例。

    output "host" {
     description = "The IP address of the instance."
     value = "${google_redis_instance.my_memorystore_redis_instance.host}"
    }
    
  3. 运行 terraform init 命令。

  4. 运行 terraform plan 命令,然后查看要创建的实例。

  5. 如需创建实例,请运行 terraform apply 命令。

从 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. 从 Terraform 配置文件中移除 google_redis_instance 资源。
  2. 如需删除资源,请运行 terraform initterraform planterraform apply 命令。
  3. 可选。如果您为此快速入门创建了 Compute Engine 虚拟机,请删除该虚拟机。

后续步骤