使用 gcloud CLI 建立 Memorystore for Redis 執行個體

瞭解如何建立 Memorystore for Redis 執行個體、連線至該執行個體,然後刪除執行個體。


如要直接在 Google Cloud 控制台中,按照這項工作的逐步指南操作,請按一下「Guide me」(逐步引導)

「Guide me」(逐步引導)


事前準備

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

    Go to project selector

  3. 如要使用現有專案進行本指南中的操作,請確認您具有完成本指南所需的權限。如果您建立新專案,則已具備必要權限。

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

  5. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  6. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. 如要使用現有專案進行本指南中的操作,請確認您具有完成本指南所需的權限。如果您建立新專案,則已具備必要權限。

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

  9. 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 the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  10. 如果尚未安裝,請安裝 Google Cloud CLI

    系統提示時,請選擇您選取或建立的專案。

  11. 如果您已安裝 Google Cloud CLI,請更新。

    gcloud components update
  12. 必要的角色

    如要取得建立 Memorystore for Redis 執行個體所需的權限,請要求管理員在專案中授予您下列 IAM 角色:

    如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。

    您或許也能透過自訂角色或其他預先定義的角色,取得必要權限。

建立 Memorystore for Redis 執行個體

在本節中,您將在 us-central1 區域建立 2 GB 的 Memorystore for Redis 執行個體,並使用基本級方案。如要進一步瞭解層級資訊,請參閱「Redis 層級功能」。

  1. 開啟終端機視窗。
  2. 如要將您要建立執行個體的專案設為預設專案,請輸入下列指令:

    gcloud config set core/project PROJECT_ID
    
  3. 如要在 us-central1 區域建立 2 GiB 基本級 Redis 執行個體,請輸入下列指令:

    gcloud redis instances create myinstance --size=2 --region=us-central1 \
    --redis-version=redis_6_x
    
  4. 建立執行個體後,如要取得執行個體的 IP 位址和連接埠號碼,請輸入下列指令:

    gcloud redis instances describe myinstance --region=us-central1
    

    如果可以存取執行個體,指令會傳回下列結果:

    authorizedNetwork: projects/my-project/global/networks/default
    createTime: '2018-04-09T21:47:56.824081Z'
    currentLocationId: us-central1-a
    host: 10.0.0.27
    locationId: us-central1-a
    memorySizeGb: 2
    name: projects/my-project/locations/us-central1/instances/myinstance
    networkThroughputGbps: 2
    port: 6379
    redisVersion: REDIS_6_X
    reservedIpRange: 10.0.0.24/29
    state: READY
    tier: BASIC

  5. 請記下執行個體的 hostport 值。您需要這些值才能連線至執行個體。

從 Compute Engine VM 連線至 Memorystore for Redis 執行個體

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

  1. 如果沒有與執行個體使用相同授權網路的 Compute Engine VM,請建立一個並使用 SSH 連線至 VM。如要這麼做,請按照「在 Compute Engine 中建立 Linux VM 執行個體」一文中的步驟操作。

  2. 使用 apt-get 安裝 telnet

    sudo apt-get install telnet
    
  3. 在終端機中,透過 telnet 連線至執行個體的 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 帳戶收取本頁面所用資源的費用,請按照下列步驟操作。

  1. 如要刪除執行個體,請輸入下列指令:

    gcloud redis instances delete myinstance --region=us-central1
  2. 如要確認刪除,請輸入 Y

    You are about to delete instance [myinstance] in [us-central1].
    Any associated data will be lost.
    Do you want to continue (Y/n)? Y
    Delete request issued for: [myinstance]

    如果可以刪除執行個體,指令會傳回下列結果:

    Deleted instance [myinstance].

  3. (選用) 如果您為這個快速入門導覽課程建立了 Compute Engine VM,請刪除該 VM。

後續步驟