使用 Terraform 建立 Memorystore for Redis Cluster 執行個體

本快速入門導覽課程說明如何使用 Terraform 建立新的 Memorystore for Redis Cluster 執行個體。此外,還會說明如何使用 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

    本節提供五個 Terraform 資源的範例,這些資源用於建立 Memorystore for Redis Cluster 執行個體:

    • 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 VM 連線至執行個體

    接著,連線至新建立的 Memorystore for Redis Cluster 執行個體。

    您可以從使用執行個體授權網路的任何 Compute Engine VM 連線至執行個體。

    1. Compute Engine VM 必須與執行個體使用相同授權網路,如果不存在這樣的 VM,請按照快速入門導覽課程:使用 Linux VM 來建立及連線至 VM。

    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 VM。

    設定 Terraform 來建立多個虛擬私有雲網路

    本頁面的各節會顯示範例,說明如何在虛擬私有雲 (VPC) 網路中建立 Memorystore for Redis Cluster 執行個體並設定連線。如需使用 Terraform 在多個 VPC 網路中設定連線的操作說明和範例,請參閱 google_redis_cluster_user_created_connections 頁面。

後續步驟