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

本頁面說明如何使用 Terraform 建立新的 Memorystore for Redis 執行個體。並說明如何使用 Telnet 連線至執行個體。

本快速入門導覽課程使用 Terraform 的 Google Cloud Platform Provider

事前準備

  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.

    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.

    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 API
    Memorystore for Redis
  9. 擁有可新增 Memorystore 資源的 Terraform 檔案。如需在 Google Cloud 中設定 Terraform 的操作說明,請參閱「開始使用 Google Cloud 供應商」。
  10. 建立 Memorystore for Redis 執行個體

    下列 Terraform 資源範例會在 us-central1 區域的 BASIC 層級中,建立 2 GiB 基本級 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 設定檔中新增下列輸出值,以便列印 Redis 執行個體的 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 VM 連線至 Redis 執行個體

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

    您可以從使用 Redis 執行個體授權網路的任何 Compute Engine VM,透過支援的 RFC 1918 IP 位址連線至 Redis 執行個體。

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

    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
      

      Enter 鍵

      SET HELLO WORLD
      

      結果:

      +OK
      

      輸入:

      GET HELLO
      

      結果:

      $5
      WORLD
      

    清除所用資源

    如要避免系統向您的 Google Cloud 帳戶收取本快速入門導覽課程所用資源的費用,請按照下列步驟刪除 Redis 執行個體:

    1. 從 Terraform 設定檔中移除 google_redis_instance 資源。
    2. 執行 Terraform initplanapply,刪除 Redis 資源。
    3. 刪除您為本快速入門導覽課程建立的任何 Compute Engine VM。

後續步驟