使用 Terraform 建立 Memorystore for Redis 執行個體
本頁面說明如何使用 Terraform 建立新的 Memorystore for Redis 執行個體。並說明如何使用 Telnet 連線至執行個體。
本快速入門導覽課程使用 Terraform 的 Google Cloud Platform Provider。
事前準備
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Verify that billing is enabled for your Google Cloud project.
- 如果尚未安裝 Google Cloud SDK,請安裝。
系統提示時,請選擇在上方選取或建立的專案。
如果您已安裝 Google Cloud SDK,請更新。
gcloud components update
-
啟用 Memorystore for Redis API
Memorystore for Redis - 擁有可新增 Memorystore 資源的 Terraform 檔案。如需在 Google Cloud 中設定 Terraform 的操作說明,請參閱「開始使用 Google Cloud 供應商」。
在 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" }
(選用) 在 Terraform 設定檔中新增下列輸出值,以便列印 Redis 執行個體的 IP 位址,本快速入門指南稍後會用到這個位址:
output "host" { description = "The IP address of the instance." value = "${google_redis_instance.my_memorystore_redis_instance.host}" }
執行
terraform init
。執行
terraform plan
,並檢查要建立的資源。執行
terraform apply
即可建立。Compute Engine VM 必須與 Redis 執行個體使用相同授權網路,如果不存在這樣的 VM,請按照快速入門導覽課程:使用 Linux VM 來建立及連線至 VM。
使用
apt-get
安裝telnet
:sudo apt-get install telnet
在終端機中,透過 telnet 連線至 Redis 執行個體的 IP 位址,並將 variables 替換為適當的值。
telnet instance-ip-address 6379
如果執行成功,指令會傳回下列結果:
Trying instance-ip-address… Connected to instance-ip-address
在 Telnet 工作階段中,輸入一些 Redis 指令:
輸入:
PING
結果:
PONG
Enter 鍵
SET HELLO WORLD
結果:
+OK
輸入:
GET HELLO
結果:
$5 WORLD
- 從 Terraform 設定檔中移除
google_redis_instance
資源。 - 執行 Terraform
init
、plan
和apply
,刪除 Redis 資源。 - 刪除您為本快速入門導覽課程建立的任何 Compute Engine VM。
建立 Memorystore for Redis 執行個體
下列 Terraform 資源範例會在 us-central1
區域的 BASIC 層級中,建立 2 GiB 基本級 Redis 執行個體。如要進一步瞭解層級資訊,請參閱「Redis 層級功能」一文。
從 Compute Engine VM 連線至 Redis 執行個體
接著,連線至新建立的 Redis 執行個體。
您可以從使用 Redis 執行個體授權網路的任何 Compute Engine VM,透過支援的 RFC 1918 IP 位址連線至 Redis 執行個體。
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本快速入門導覽課程所用資源的費用,請按照下列步驟刪除 Redis 執行個體:
後續步驟
- 進一步瞭解如何建立及管理執行個體。
- 瞭解連線至 Redis 執行個體的其他方式。
- 瞭解 Redis 執行個體的特性。