Terraform を使用して、Compute Engine VM に GKE on Bare Metal ハイブリッド クラスタを作成する

このドキュメントでは、Terraform を使用して Compute Engine に VM を設定し、GKE on Bare Metal を高可用性(HA)モードでインストールして試用する方法について説明します。このために Google Cloud CLI を使用する方法については、Compute Engine VM で GKE on Bare Metal を試すをご覧ください。

GKE on Bare Metal は、ハードウェアを準備せずにすぐに試すことができます。用意されている Terraform スクリプトによって Compute Engine に VM のネットワークが作成され、それを使用して GKE on Bare Metal を実行できます。このチュートリアルでは、ハイブリッド クラスタ デプロイ モデルを使用します。

以下の手順でサンプル クラスタを実行します。

  1. Terraform スクリプトを実行して、Compute Engine で VM のネットワークを設定する
  2. ハイブリッド クラスタをデプロイする
  3. クラスタを確認する

始める前に

デプロイに必要なリソースは次のとおりです。

Compute Engine に VM ネットワークを設定する

このセクションでは、anthos-samples リポジトリの Terraform スクリプトを使用します。このスクリプトは、次のリソースを使用して Compute Engine を構成します。

  • ハイブリッド クラスタをデプロイする 6 つの VM。
    • 1 つの管理 VM で、他のマシンにハイブリッド クラスタをデプロイする。
    • 3 つの VM は、ハイブリッド クラスタ コントロール プレーンの実行に必要な 3 つのコントロール プレーン ノード用。
    • 2 つの VM は、ハイブリッド クラスタでワークロードを実行するために必要な 2 つのワーカーノード用。
  • L2 接続をエミュレートするすべてのノード間の VxLAN オーバーレイ ネットワーク。
  • 管理 VM からコントロール プレーンワーカーノードへの SSH アクセス権。

Compute Engine VM を使用する Google Cloud 上のベアメタル インフラストラクチャ

クラスタ内のノード数は、新しいノード名を instance_count Terraform 変数に追加することで変更できます。

###################################################################################
# The recommended instance count for High Availability (HA) is 3 for Control plane
# and 2 for Worker nodes.
###################################################################################
variable "instance_count" {
  description = "Number of instances to provision per layer (Control plane and Worker nodes) of the cluster"
  type        = map(any)
  default = {
    "controlplane" : 3
    "worker" : 2
  }
}

  1. anthos-bm-gcp-terraform サンプルの Terraform スクリプトをダウンロードします。

    git clone https://github.com/GoogleCloudPlatform/anthos-samples
    cd anthos-samples/anthos-bm-gcp-terraform
    
  2. terraform.tfvars.sample ファイルを更新して、環境固有の変数を設定します。

    project_id       = "PROJECT_ID"
    region           = "GOOGLE_CLOUD_REGION"
    zone             = "GOOGLE_CLOUD_ZONE"
    credentials_file = "PATH_TO_GOOGLE_CLOUD_SERVICE_ACCOUNT_KEY_FILE"
    
  3. terraform.tfvars.sample ファイルの名前を、terraform で使用されるデフォルトの変数ファイル名に変更します。

    mv terraform.tfvars.sample terraform.tfvars
    
  4. サンプル ディレクトリを Terraform 作業ディレクトリとして初期化します。これにより、必要な Terraform 状態管理の構成が git init と同じ様に設定されます。

    terraform init
    
  5. Terraform 実行プランを作成します。このステップでは、リソースの状態を比較してスクリプトを検証し、実行プランを作成します。

    terraform plan
    
  6. Terraform スクリプトに記述された変更を適用します。このステップでは、指定されたプロバイダ(この場合は Google Cloud)でプランを実行して、リソースを目的の状態にします。

    terraform apply  # when prompted to confirm the Terraform plan, type 'Yes' and enter
    

ハイブリッド クラスタのデプロイ

Terraform の実行が完了すると、ハイブリッド クラスタをデプロイする準備が完了します。

  1. SSH を使用して管理ホストに接続します。

    gcloud compute ssh tfadmin@cluster1-abm-ws0-001 --project=PROJECT_ID --zone=GOOGLE_CLOUD_ZONE
    

    VM の更新に関するメッセージは無視して、このチュートリアルを完了してください。VM をテスト環境として維持する場合は、Ubuntu のドキュメントに記載されているように、OS を更新するか次のリリースにアップグレードすることをおすすめします。

  2. 次のコードブロックを実行して、構成された Compute Engine VM で cluster1 ハイブリッド クラスタを作成します。

    sudo ./run_initialization_checks.sh && \
    sudo bmctl create config -c cluster1 && \
    sudo cp ~/cluster1.yaml bmctl-workspace/cluster1 && \
    sudo bmctl create cluster -c cluster1
    

bmctl コマンドを実行すると、新しいハイブリッド クラスタの設定が開始されます。これには、ノードでのプリフライト チェック、管理クラスタとユーザー クラスタの作成、Connect を使用した Google Cloud へのクラスタの登録が含まれます。セットアップには 15 分ほどかかります。クラスタを作成すると、次の出力が表示されます。

    Created config: bmctl-workspace/cluster1/cluster1.yaml
    Creating bootstrap cluster... OK
    Installing dependency components... OK
    Waiting for preflight check job to finish... OK
    - Validation Category: machines and network
            - [PASSED] 10.200.0.3
            - [PASSED] 10.200.0.4
            - [PASSED] 10.200.0.5
            - [PASSED] 10.200.0.6
            - [PASSED] 10.200.0.7
            - [PASSED] gcp
            - [PASSED] node-network
    Flushing logs... OK
    Applying resources for new cluster
    Waiting for cluster to become ready OK
    Writing kubeconfig file
    kubeconfig of created cluster is at bmctl-workspace/cluster1/cluster1-kubeconfig, please run
    kubectl --kubeconfig bmctl-workspace/cluster1/cluster1-kubeconfig get nodes
    to get cluster node status.
    Please restrict access to this file as it contains authentication credentials of your cluster.
    Waiting for node pools to become ready OK
    Moving admin cluster resources to the created admin cluster
    Flushing logs... OK
    Deleting bootstrap cluster... OK

クラスタの確認と操作

クラスタの kubeconfig ファイルは、管理マシンの bmctl-workspace ディレクトリにあります。デプロイを確認するには次の手順を行います。

  1. 管理ホストから切断している場合は、SSH を使用してホストに接続します。

    # You can copy the command from the output of the Terraform execution above
    gcloud compute ssh tfadmin@cluster1-abm-ws0-001 --project=PROJECT_ID --zone=GOOGLE_CLOUD_ZONE
    
  2. KUBECONFIG 環境変数にクラスタの構成ファイルへのパスを設定し、クラスタで kubectl コマンドを実行します。

    export CLUSTER_ID=cluster1
    export KUBECONFIG=$HOME/bmctl-workspace/$CLUSTER_ID/$CLUSTER_ID-kubeconfig
    kubectl get nodes
    

    クラスタのノードが、次のように表示されます。

    NAME          STATUS   ROLES    AGE   VERSION
    cluster1-abm-cp1-001   Ready    master   17m   v1.18.6-gke.6600
    cluster1-abm-cp2-001   Ready    master   16m   v1.18.6-gke.6600
    cluster1-abm-cp3-001   Ready    master   16m   v1.18.6-gke.6600
    cluster1-abm-w1-001    Ready    <none>   14m   v1.18.6-gke.6600
    cluster1-abm-w2-001    Ready    <none>   14m   v1.18.6-gke.6600
    

Google Cloud コンソールからクラスタにログインする

Google Cloud コンソールでワークロードをモニタリングするには、クラスタにログインする必要があります。

クラスタへのログインの手順と詳細についてはGoogle Cloud コンソールからクラスタにログインするをご覧ください。

クリーンアップ

クラスタの設定をクリーンアップするには、2 つの方法があります。

コンソール

Terraform

  • Terraform によって作成されたすべてのリソースを削除する前に、クラスタの登録を解除します。
# Use SSH to connect to the admin host
gcloud compute ssh tfadmin@cluster1-abm-ws0-001 --project=PROJECT_ID --zone=GOOGLE_CLOUD_ZONE

# Reset the cluster
export CLUSTER_ID=cluster1
export KUBECONFIG=$HOME/bmctl-workspace/$CLUSTER_ID/$CLUSTER_ID-kubeconfig
sudo bmctl reset --cluster $CLUSTER_ID

# log out of the admin host
exit
  • Terraform を使用してすべてのリソースを削除します。
terraform destroy --auto-approve