使用 Terraform 建立 Memorystore for Redis 執行個體
瞭解如何建立 Memorystore for Redis 執行個體、連線至執行個體,然後刪除執行個體。
本快速入門導覽課程會使用 Terraform 的 Google Cloud Platform 供應商。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
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 theserviceusage.services.enablepermission. Learn how to grant roles.-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
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 theserviceusage.services.enablepermission. Learn how to grant roles.- 如果尚未安裝 Google Cloud SDK,請安裝。
系統提示時,請選擇您選取或建立的專案。
如果您已安裝 Google Cloud SDK,請更新 SDK。
gcloud components update
-
啟用 Memorystore for Redis API。
Memorystore for Redis - 擁有可新增 Memorystore 資源的 Terraform 檔案。如需在 Google Cloud 中設定 Terraform 的操作說明,請參閱「開始使用 Google Cloud 供應商」。
必要的角色
如要取得建立 Memorystore for Redis 執行個體所需的權限,請要求管理員授予您專案的下列 IAM 角色:
- Cloud Memorystore Redis 管理員 (
roles/redis.admin) - Compute 執行個體管理員 (v1) (
roles/compute.instanceAdmin.v1) - 服務帳戶使用者 (
roles/iam.serviceAccountUser)
如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
建立 Memorystore for Redis 執行個體
在本節中,您將建立位於 us-central1 區域的 2 GB Memorystore for Redis 執行個體,並採用 Basic 級。如要進一步瞭解層級資訊,請參閱「Redis 層級功能」一文。
在 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 設定檔中新增下列輸出值,即可列印執行個體的 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 連線至 Memorystore for Redis 執行個體
您可以從任何使用執行個體授權網路的 Compute Engine VM,連線至 Memorystore for Redis 執行個體,並使用支援的 RFC 1918 IP 位址。
如果沒有與執行個體使用相同授權網路的 Compute Engine VM,請建立一個並使用 SSH 連線至 VM。如要這麼做,請按照「在 Compute Engine 中建立 Linux VM 執行個體」一文中的步驟操作。
使用
apt-get安裝telnet:sudo apt-get install telnet在終端機中,透過 telnet 連線至執行個體的 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
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。
- 從 Terraform 設定檔中移除
google_redis_instance資源。 - 如要刪除資源,請執行
terraform init、terraform plan和terraform apply指令。 - (選用) 如果您為這個快速入門導覽課程建立了 Compute Engine VM,請刪除該 VM。
後續步驟
- 進一步瞭解如何建立及管理執行個體。
- 瞭解連線至 Redis 執行個體的其他方式。
- 瞭解 Redis 執行個體的特性。