gcloud CLI を使用して Memorystore for Redis インスタンスを作成する
Memorystore for Redis インスタンスを作成し、接続して削除する方法を学習します。
このタスクを Google Cloud コンソールで直接行う際の順を追ったガイダンスについては、「ガイドを表示」をクリックしてください。
始める前に
- アカウントにログインします。 Google Cloud を初めて使用する場合は、 アカウントを作成して、実際のシナリオで Google プロダクトのパフォーマンスを評価してください。 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 CLI をインストールします。
プロンプトが表示されたら、選択または作成したプロジェクトを選択します。
Google Cloud CLI がすでにインストールされている場合は、更新します。
gcloud components update
必要なロール
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 インスタンスを作成します。階層の詳細については、Redis の階層の機能をご覧ください。
- ターミナル ウィンドウを開きます。
インスタンスを作成するプロジェクトをデフォルト プロジェクトとして設定するには、次のコマンドを入力します。
gcloud config set core/project PROJECT_ID
us-central1リージョンに 2 GiB ベーシック ティア Redis インスタンスを作成するには、次のコマンドを入力します。gcloud redis instances create myinstance --size=2 --region=us-central1 \ --redis-version=redis_6_x
インスタンスを作成したら、インスタンスの 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
インスタンスの
host値とport値をメモします。インスタンスに接続するには、これらの値が必要です。
Compute Engine VM から Memorystore for Redis インスタンスに接続する
Memorystore for Redis インスタンスには、インスタンスの承認済みネットワークを使用する任意の Compute Engine VM から サポートされている RFC 1918 IP アドレスを使用して接続できます。
インスタンスと同じ承認済みネットワークを使用する Compute Engine VM がない場合は、作成して SSH を使用して VM に接続します。これを行うには、Compute Engine で Linux VM インスタンスを作成するの手順に沿って操作します。
apt-getを使用してtelnetをインストールします。sudo apt-get install telnetターミナルから、インスタンスの IP アドレスに telnet で接続します。 VARIABLES は適切な値に置き換えます。
telnet INSTANCE_IP_ADDRESS 6379
インスタンスに接続できる場合、コマンドは次の結果を返します。
Trying INSTANCE_IP_ADDRESS… Connected to INSTANCE_IP_ADDRESS
telnet セッションで、次のようにいくつかの Redis コマンドを入力します。
次のように入力します。
PING
結果:
PONG
入力
SET HELLO WORLD
結果:
+OK
次のように入力します。
GET HELLO
結果:
$5 WORLD
クリーンアップ
このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、 次の手順を実施します。
インスタンスを削除するには、次のコマンドを入力します。
gcloud redis instances delete myinstance --region=us-central1
削除を確認するには、
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].
省略可 。このクイックスタート用に Compute Engine VM を作成した場合は、 次に 削除します。
次のステップ
- インスタンスの作成と管理について学習する。
- Redis インスタンスに接続するその他の方法について学習する。
- Redis インスタンスの特性について学習する。