Terraform でクラスタを作成してワークロードをデプロイする
Kubernetes クラスタは、仮想データセンターと同様に、アプリケーション用のコンピューティング、ストレージ、ネットワーキングなどのサービスを提供します。Kubernetes で実行されるアプリとそれに関連するサービスは、ワークロードと呼ばれます。
このチュートリアルでは、Terraform によって Google Kubernetes Engine クラスタとサンプル ワークロードを簡単に設定して動作を確認できます。その後は、 Google Cloud コンソールでワークロードを確認してから、詳細な学習プログラムに進むか、独自のプロダクション レディなクラスタの計画と作成を開始できます。このチュートリアルは、Terraform に精通していることを前提としています。
Google Cloud コンソールでサンプル クラスタとワークロードを設定する場合は、 Google Cloud コンソールでクラスタを作成するをご覧ください。
始める前に
次の手順で Kubernetes Engine API を有効にします。
- 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.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the GKE API:
gcloud services enable container.googleapis.com
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the GKE API:
gcloud services enable container.googleapis.com
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/container.admin, roles/compute.networkAdmin, roles/iam.serviceAccountUser
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
- Replace
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
環境を準備する
このチュートリアルでは、Google Cloudでホストされているリソースを Cloud Shell によって管理します。Cloud Shell には、このチュートリアルに必要なソフトウェア(Terraform、kubectl
、Google Cloud CLI など)がプリインストールされています。
Google Cloud コンソールで [Cloud Shell をアクティブにする] アイコン
をクリックして、Cloud Shell セッションを起動します。 Google Cloud コンソールの下部ペインでセッションが起動します。
この仮想マシンに関連付けられているサービス認証情報は自動的に設定されるため、サービス アカウント キーを設定したり、ダウンロードしたりする必要はありません。
コマンドを実行する前に、次のコマンドを使用して gcloud CLI でデフォルト プロジェクトを設定します。
gcloud config set project PROJECT_ID
PROJECT_ID
は、実際のプロジェクト ID に置き換えます。GitHub リポジトリのクローンを作成します。
git clone https://github.com/terraform-google-modules/terraform-docs-samples.git --single-branch
作業ディレクトリを変更します。
cd terraform-docs-samples/gke/quickstart/autopilot
Terraform ファイルを確認する
Google Cloud プロバイダは、 Google Cloud リソースの管理とプロビジョニングを Terraform によって行えるようにするプラグインです。Terraform 構成と Google Cloud API 間のブリッジとして機能し、仮想マシンやネットワークなどのインフラストラクチャ リソースを宣言的に定義できます。
このチュートリアルのクラスタとサンプルアプリは、 Google Cloud プロバイダと Kubernetes プロバイダを使用する 2 つの Terraform ファイルで指定されています。
cluster.tf
ファイルを確認します。cat cluster.tf
出力は次のようになります。
このファイルでは、次のリソースを記述しています。
google_compute_network
: 内部 IPv6 が有効になっている VPC ネットワーク。google_compute_subnetwork
: デュアルスタック サブネットワーク。google_container_cluster
:us-central1
にあるデュアルスタック Autopilot クラスタ。deletion_protection
設定は、Terraform を使用してこのクラスタを削除できるかどうかを制御します。deletion_protection
フィールドの値をfalse
に設定すると、Terraform によってクラスタを削除できます。詳しくは、google_container_cluster
のリファレンスをご覧ください。
app.tf
ファイルを確認します。cat app.tf
出力は次のようになります。
このファイルでは、次のリソースを記述しています。
- サンプル コンテナ イメージを含む Deployment。
- LoadBalancer タイプの Service。Service は、ポート 80 で Deployment を公開します。
(省略可)アプリケーションをインターネットに公開する
サンプルの Terraform ファイルでは、内部 IP アドレスを持つアプリが記述されています。このアプリには、サンプルアプリと同じ Virtual Private Cloud(VPC)からのみアクセスできます。実行中のデモアプリのウェブ インターフェースにインターネット(ノートパソコンなど)からアクセスする場合は、クラスタを作成する前に、代わりにパブリック IP アドレスを作成するように Terraform ファイルを変更します。それには、Cloud Shell で直接テキスト エディタを使用するか、Cloud Shell エディタを使用します。
デモ アプリケーションをインターネットに公開するには、次の操作を行います。
cluster.tf
で、ipv6_access_type
をINTERNAL
からEXTERNAL
に変更します。ipv6_access_type = "EXTERNAL"
app.tf
で、networking.gke.io/load-balancer-type
アノテーションを削除して外部ロードバランサを構成します。annotations = { "networking.gke.io/load-balancer-type" = "Internal" # Remove this line }
クラスタを作成してアプリケーションをデプロイする
Cloud Shell で次のコマンドを実行して、Terraform が使用可能であることを確認します。
terraform
出力例を以下に示します。
Usage: terraform [global options] <subcommand> [args] The available commands for execution are listed below. The primary workflow commands are given first, followed by less common or more advanced commands. Main commands: init Prepare your working directory for other commands validate Check whether the configuration is valid plan Show changes required by the current configuration apply Create or update infrastructure destroy Destroy previously-created infrastructure
Terraform を初期化します。
terraform init
Terraform 構成を計画します。
terraform plan
Terraform 構成を適用する
terraform apply
プロンプトが表示されたら、「
yes
」と入力して操作を確定します。このコマンドの完了までに数分かかることがあります。出力は次のようになります。Apply complete! Resources: 6 added, 0 changed, 0 destroyed.
クラスタの動作を確認する
クラスタが正しく実行されていることを確認する手順は次のとおりです。
Google Cloud コンソールの [ワークロード] ページに移動します。
example-hello-app-deployment
ワークロードをクリックします。Pod の詳細ページが表示されます。このページには、Pod に関する情報(アノテーション、Pod で実行されているコンテナ、Pod を公開している Service、CPU やメモリ、ディスクの使用量などの指標など)が表示されます。Google Cloud コンソールの [Service と Ingress] ページに移動します。
example-hello-app-loadbalancer
LoadBalancer Service をクリックします。Service の詳細ページが表示されます。このページには、Service に関連付けられた Pod や、Service が使用するポートなど、Service に関する情報が表示されます。[外部エンドポイント] セクションで、IPv4 リンクまたは IPv6 リンクをクリックして、ブラウザで Service を表示します。出力は次のようになります。
Hello, world! Version: 2.0.0 Hostname: example-hello-app-deployment-5df979c4fb-kdwgr
クリーンアップ
このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、リソースを含む Google Cloud プロジェクトを削除します。
別のチュートリアルを行う場合や、サンプルをさらに確認する場合は、すべてを終えてからこのクリーンアップ手順を実行してください。
Cloud Shell で次のコマンドを実行して、Terraform リソースを削除します。
terraform destroy --auto-approve
クリーンアップ エラーのトラブルシューティング
The network resource 'projects/PROJECT_ID/global/networks/example-network' is already being used by 'projects/PROJECT_ID/global/firewalls/example-network-yqjlfql57iydmsuzd4ot6n5v'
のようなエラー メッセージが表示されたら、次の操作を行います。
ファイアウォール ルールを削除します。
gcloud compute firewall-rules list --filter="NETWORK:example-network" --format="table[no-heading](name)" | xargs gcloud --quiet compute firewall-rules delete
Terraform コマンドを再実行します。
terraform destroy --auto-approve
次のステップ
Google Cloud コンソールでクラスタとワークロードを調べ、主なワークロードの設定とデプロイしたリソースをいくつか確認する。
GKE で Terraform を設定して使用する方法について、GKE での Terraform のサポートで詳しく確認する。
学習プログラム: スケーラブルなアプリで、さらに詳しく学ぶ。
クラスタ管理の概要で、実際のクラスタ管理を開始する方法を確認する。