使用 Terraform 创建 Memorystore for Redis Cluster 实例

本快速入门介绍了如何使用 Terraform 创建新的 Memorystore for Redis 集群实例。此外,本文还介绍了如何使用 telnet 连接到实例。

本快速入门指南假定您要开始使用新的 Terraform 文件,并包含用于设置 Google Cloud 提供商的 Terraform 资源。不过,它省略了使用 Google Cloud 提供商对 Terraform 文件进行身份验证的步骤。

如需了解如何使用 Google Cloud 提供程序对 Terraform 进行身份验证,请参阅 Google Cloud 提供程序使用入门

准备工作

  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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

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

  4. 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 (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

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

  6. 安装 Google Cloud SDK(如果您尚未安装)。

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

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

    gcloud components update
  8. 启用 Memorystore for Redis Cluster API
    Memorystore for Redis Cluster
  9. 为 Memorystore 配置 Terraform

    本部分展示了创建 Memorystore for Redis 集群实例所需的五个 Terraform 资源示例:

    • Google Cloud 提供方配置。

    • Memorystore 实例配置。在本指南中,这是一个 39 GB 的集群,在 us-central1 区域中包含三个 redis-highmem-medium 节点。

    • Private Service Connect 服务连接政策

    • 实例的子网。

    • 实例的网络。

    添加这些 Terraform 资源的说明如下:

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

      
      provider "google" {
        project = "PROJECT_ID"
      }
      
      resource "google_redis_cluster" "cluster-ha" {
        name           = "CLUSTER_ID"
        shard_count    = 3
        psc_configs {
          network = google_compute_network.producer_net.id
        }
        region = "us-central1"
        replica_count = 1
        depends_on = [
          google_network_connectivity_service_connection_policy.default
        ]
      
      }
      
      resource "google_network_connectivity_service_connection_policy" "default" {
        name = "POLICY_NAME"
        location = "us-central1"
        service_class = "gcp-memorystore-redis"
        description   = "my basic service connection policy"
        network = google_compute_network.producer_net.id
        psc_config {
          subnetworks = [google_compute_subnetwork.producer_subnet.id]
        }
      }
      
      resource "google_compute_subnetwork" "producer_subnet" {
        name          = "SUBNET_ID"
        ip_cidr_range = "10.0.0.248/29"
        region        = "us-central1"
        network       = google_compute_network.producer_net.id
      }
      
      resource "google_compute_network" "producer_net" {
        name                    = "NETWORK_ID"
        auto_create_subnetworks = false
      }
      
      

      替换以下内容:

      • PROJECT_ID 是您的实例的项目 ID。
      • CLUSTER_ID 是您为要创建的 Memorystore for Redis Cluster 实例选择的 ID。ID 必须包含 1 到 63 个字符,且只能使用小写字母、数字或连字符。并且必须以小写字母开头并以小写字母或数字结尾。
      • POLICY_NAME 是您为项目的 Private Service Connect 服务连接政策选择的名称。如需详细了解服务连接政策,请参阅网络
      • SUBNET_ID 是您在此示例中创建的子网的所选 ID,供 Memorystore 集群使用。
      • NETWORK_ID 是您在此示例中创建的网络的所选 ID,供 Memorystore 集群使用。

    部署 Terraform 配置文件

    1. 运行 terraform init

    2. 运行 terraform plan

    3. 运行 terraform apply

    从 Compute Engine 虚拟机连接到实例

    接下来,连接到新创建的 Memorystore for Redis Cluster 实例。

    您可以从任何使用该实例的授权网络的 Compute Engine 虚拟机连接到该实例。

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

    2. 使用 apt-get 安装 telnet

      sudo apt-get install telnet
      
    3. 从终端使用 telnet 连接到实例的 IP 地址,并将 VARIABLES 替换为适当的值。

      telnet CLUSTER_DISCOVERY_ENDPOINT_IP_ADDRESS CLUSTER_DISCOVERY_ENDPOINT_PORT_NUMBER
      
    4. 在 telnet 会话中,输入一些 Redis 命令:

      请输入:

      PING
      

      结果:

      PONG
      

    清理

    为避免因本快速入门中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤删除 Redis 实例:

    1. 从 Terraform 配置文件中移除 google_redis_instance 资源。
    2. 运行 Terraform initplanapply 以销毁 Redis 资源。
    3. 删除您为本快速入门创建的任何 Compute Engine 虚拟机。

    配置 Terraform 以设置多个 VPC 网络

    本页面的各个部分展示了在 Virtual Private Cloud (VPC) 网络中创建 Memorystore for Redis 集群实例并设置连接的示例。如需查找有关使用 Terraform 在多个 VPC 网络中设置连接的说明和示例,请参阅 google_redis_cluster_user_created_connections 页面。

后续步骤