Compute Engine VM 上にハイブリッド クラスタを作成する

このページでは、Compute Engine で実行されている仮想マシン(VM)を使用して、高可用性(HA)モードでハイブリッド クラスタを設定する方法について説明します。

Google Distributed Cloud ソフトウェアのみは、ハードウェアを準備することなくすぐに試すことができます。このページの手順を完了すると、Compute Engine で実行されているテスト環境が機能するようになります。

Compute Engine VM で Google Distributed Cloud ソフトウェアのみを試すには、次の手順を行います。

  1. Compute Engine で 6 つの VM を作成する
  2. すべての VM 間に L2 接続で vxlan ネットワークを作成する
  3. Google Distributed Cloud の前提条件をインストールする
  4. ハイブリッド クラスタをデプロイする
  5. クラスタを確認する

始める前に

  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.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure that billing is enabled for your Google Cloud project.

  6. このページのスクリプトとコマンドで使用する環境変数を設定する際に必要となるため、プロジェクト ID をメモしておきます。既存のプロジェクトを選択した場合は、ご自身がプロジェクト オーナーまたはプロジェクト編集者であることを確認します。
  7. Linux ワークステーションに、最新の Google Cloud CLI( Google Cloudを操作するためのコマンドライン ツール)がインストールされていることを確認します。gcloud CLI がすでにインストールされている場合は、次のコマンドを実行してコンポーネントを更新します。
    gcloud components update

    gcloud CLI のインストール方法によっては、次のメッセージが表示されることがあります。

    "You cannot perform this action because the Google Cloud CLI component manager is disabled for this installation. You can run the following command to achieve the same result for this installation:"

    手順に沿ってコマンドをコピーして貼り付け、コンポーネントを更新します。

  8. このガイドの手順は、anthos-samples リポジトリのインストール スクリプトから取ったものです。よくある質問セクションには、このスクリプトをカスタマイズして、一般的なバリエーションを処理する方法の詳細が記載されています。

    Compute Engine で 6 つの VM を作成する

    以下の VM を作成する手順は次のとおりです。

    • 管理ワークステーション用の 1 つの VM。管理ワークステーションは、インストール中にクラスタをプロビジョニングするためのコマンドライン インターフェース(CLI)ツールと構成ファイルをホストします。また、インストール後に、プロビジョニングされたクラスタとやり取りするための CLI ツールもホストします。管理ワークステーションは、SSH を使用してクラスタ内の他のすべてのノードにアクセスできます。
    • Google Distributed Cloud コントロール プレーンを実行するために必要な、3 つのコントロール プレーン ノード用の 3 つの VM。
    • Google Distributed Cloud クラスタのワークロードを実行するために必要な、2 つのワーカーノード用の 2 つの VM。
    1. 環境変数を設定します。

      export PROJECT_ID=PROJECT_ID
      export ZONE=ZONE
      export CLUSTER_NAME=CLUSTER_NAME
      export BMCTL_VERSION=1.32.200-gke.104
      

      ZONE については、us-central1-a または他の任意の Compute Engine ゾーンを使用できます。

    2. 次のコマンドを実行して、Google アカウントでログインし、プロジェクトをデフォルトとして設定します。

      gcloud auth login
      gcloud config set project $PROJECT_ID
      gcloud config set compute/zone $ZONE
      
    3. baremetal-gcr サービス アカウントとキーを作成します。

      gcloud iam service-accounts create baremetal-gcr
      
      gcloud iam service-accounts keys create bm-gcr.json \
          --iam-account=baremetal-gcr@"${PROJECT_ID}".iam.gserviceaccount.com
    4. Google Cloud API とサービスを有効にする

      gcloud services enable \
          anthos.googleapis.com \
          anthosaudit.googleapis.com \
          anthosgke.googleapis.com \
          cloudresourcemanager.googleapis.com \
          connectgateway.googleapis.com \
          container.googleapis.com \
          gkeconnect.googleapis.com \
          gkehub.googleapis.com \
          serviceusage.googleapis.com \
          stackdriver.googleapis.com \
          monitoring.googleapis.com \
          logging.googleapis.com \
          opsconfigmonitoring.googleapis.com \
          compute.googleapis.com \
          gkeonprem.googleapis.com \
          iam.googleapis.com \
          kubernetesmetadata.googleapis.com
    5. 複数の API とサービスに複数のサービス アカウントが必要とならないように、baremetal-gcr サービス アカウントに追加の権限を付与します。

      gcloud projects add-iam-policy-binding "$PROJECT_ID" \
        --member="serviceAccount:baremetal-gcr@$PROJECT_ID.iam.gserviceaccount.com" \
        --role="roles/gkehub.connect" \
        --no-user-output-enabled
      
      gcloud projects add-iam-policy-binding "$PROJECT_ID" \
        --member="serviceAccount:baremetal-gcr@$PROJECT_ID.iam.gserviceaccount.com" \
        --role="roles/gkehub.admin" \
        --no-user-output-enabled
      
      gcloud projects add-iam-policy-binding "$PROJECT_ID" \
        --member="serviceAccount:baremetal-gcr@$PROJECT_ID.iam.gserviceaccount.com" \
        --role="roles/logging.logWriter" \
        --no-user-output-enabled
      
      gcloud projects add-iam-policy-binding "$PROJECT_ID" \
        --member="serviceAccount:baremetal-gcr@$PROJECT_ID.iam.gserviceaccount.com" \
        --role="roles/monitoring.metricWriter" \
        --no-user-output-enabled
      
      gcloud projects add-iam-policy-binding "$PROJECT_ID" \
        --member="serviceAccount:baremetal-gcr@$PROJECT_ID.iam.gserviceaccount.com" \
        --role="roles/monitoring.dashboardEditor" \
        --no-user-output-enabled
      
      gcloud projects add-iam-policy-binding "$PROJECT_ID" \
        --member="serviceAccount:baremetal-gcr@$PROJECT_ID.iam.gserviceaccount.com" \
        --role="roles/stackdriver.resourceMetadata.writer" \
        --no-user-output-enabled
      
      gcloud projects add-iam-policy-binding "$PROJECT_ID" \
        --member="serviceAccount:baremetal-gcr@$PROJECT_ID.iam.gserviceaccount.com" \
        --role="roles/opsconfigmonitoring.resourceMetadata.writer" \
        --no-user-output-enabled
      
      gcloud projects add-iam-policy-binding "$PROJECT_ID" \
          --member="serviceAccount:baremetal-gcr@$PROJECT_ID.iam.gserviceaccount.com" \
          --role="roles/kubernetesmetadata.publisher" \
          --no-user-output-enabled
      
      gcloud projects add-iam-policy-binding "$PROJECT_ID" \
        --member="serviceAccount:baremetal-gcr@$PROJECT_ID.iam.gserviceaccount.com" \
        --role="roles/monitoring.viewer" \
        --no-user-output-enabled
      
      gcloud projects add-iam-policy-binding "$PROJECT_ID" \
        --member="serviceAccount:baremetal-gcr@$PROJECT_ID.iam.gserviceaccount.com" \
        --role="roles/serviceusage.serviceUsageViewer" \
        --no-user-output-enabled
    6. このページのすべてのコマンドに必要な変数と配列を作成します。

      MACHINE_TYPE=n1-standard-8
      VM_PREFIX=abm
      VM_WS=$VM_PREFIX-ws
      VM_CP1=$VM_PREFIX-cp1
      VM_CP2=$VM_PREFIX-cp2
      VM_CP3=$VM_PREFIX-cp3
      VM_W1=$VM_PREFIX-w1
      VM_W2=$VM_PREFIX-w2
      declare -a VMs=("$VM_WS" "$VM_CP1" "$VM_CP2" "$VM_CP3" "$VM_W1" "$VM_W2")
      declare -a IPs=()
    7. 次のループを使用して 6 つの VM を作成します。

      for vm in "${VMs[@]}"
      do
          gcloud compute instances create "$vm" \
            --image-family=ubuntu-2004-lts --image-project=ubuntu-os-cloud \
            --zone="${ZONE}" \
            --boot-disk-size 200G \
            --boot-disk-type pd-ssd \
            --can-ip-forward \
            --network default \
            --tags http-server,https-server \
            --min-cpu-platform "Intel Haswell" \
            --enable-nested-virtualization \
            --scopes cloud-platform \
            --machine-type "$MACHINE_TYPE" \
            --metadata "cluster_id=${CLUSTER_NAME},bmctl_version=${BMCTL_VERSION}"
          IP=$(gcloud compute instances describe "$vm" --zone "${ZONE}" \
               --format='get(networkInterfaces[0].networkIP)')
          IPs+=("$IP")
      done

      このコマンドは、次の名前の VM インスタンスを作成します。

      • abm-ws: 管理ワークステーション用の VM。
      • abm-cp1abm-cp2abm-cp3: コントロール プレーン ノード用の VM。
      • abm-w1abm-w2: ワークロードを実行するノード用の VM。
    8. 次のループを使用して、すべての VM で SSH を使用できることを確認します。

      for vm in "${VMs[@]}"
      do
          while ! gcloud compute ssh root@"$vm" --zone "${ZONE}" --command "printf 'SSH to $vm succeeded\n'"
          do
              printf "Trying to SSH into %s failed. Sleeping for 5 seconds. zzzZZzzZZ" "$vm"
              sleep  5
          done
      done

    VM 間の L2 接続を使用して vxlan ネットワークを作成する

    Linux の標準 vxlan 機能を使用して、L2 接続ですべての VM を接続するネットワークを作成します。

    以下のコマンドには、次のアクションを行う 2 つのループが含まれています。

    1. SSH を使用して各 VM にアクセスします。
    2. 必要なパッケージの更新とインストール。
    3. 必要なコマンドを実行して、vxlan でネットワークを構成します。

      i=2 # We start from 10.200.0.2/24
      for vm in "${VMs[@]}"
      do
          gcloud compute ssh root@"$vm" --zone "${ZONE}" << EOF
              apt-get -qq update > /dev/null
              apt-get -qq install -y jq > /dev/null
              set -x
              ip link add vxlan0 type vxlan id 42 dev ens4 dstport 0
              current_ip=\$(ip --json a show dev ens4 | jq '.[0].addr_info[0].local' -r)
              printf "VM IP address is: \$current_ip"
              for ip in ${IPs[@]}; do
                  if [ "\$ip" != "\$current_ip" ]; then
                      bridge fdb append to 00:00:00:00:00:00 dst \$ip dev vxlan0
                  fi
              done
              ip addr add 10.200.0.$i/24 dev vxlan0
              ip link set up dev vxlan0
      
      EOF
          i=$((i+1))
      done

    これで、10.200.0.0/24 ネットワーク内に L2 接続が確立されました。VM には次の IP アドレスが割り振られます。

    • 管理ワークステーション VM: 10.200.0.2
    • コントロール プレーン ノードを実行する VM:
      • 10.200.0.3
      • 10.200.0.4
      • 10.200.0.5
    • ワーカーノードを実行する VM:
      • 10.200.0.6
      • 10.200.0.7

    Google Distributed Cloud の前提条件をインストールする

    Google Distributed Cloud をインストールする前に、管理ワークステーションに次のツールをインストールする必要があります。

    • bmctl
    • kubectl
    • Docker

    ツールをインストールして Google Distributed Cloud のインストールの準備をするには:

    1. 次のコマンドを実行して、管理ワークステーションにサービス アカウント キーをダウンロードし、必要なツールをインストールします。

      gcloud compute ssh root@$VM_WS --zone "${ZONE}" << EOF
      set -x
      
      export PROJECT_ID=\$(gcloud config get-value project)
      BMCTL_VERSION=\$(curl http://metadata.google.internal/computeMetadata/v1/instance/attributes/bmctl_version -H "Metadata-Flavor: Google")
      export BMCTL_VERSION
      
      gcloud iam service-accounts keys create bm-gcr.json \
        --iam-account=baremetal-gcr@\${PROJECT_ID}.iam.gserviceaccount.com
      
      curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl"
      
      chmod +x kubectl
      mv kubectl /usr/local/sbin/
      mkdir baremetal && cd baremetal
      gsutil cp gs://anthos-baremetal-release/bmctl/$BMCTL_VERSION/linux-amd64/bmctl .
      chmod a+x bmctl
      mv bmctl /usr/local/sbin/
      
      cd ~
      printf "Installing docker"
      curl -fsSL https://get.docker.com -o get-docker.sh
      sh get-docker.sh
      EOF
    2. 次のコマンドを実行して、root@10.200.0.x が動作することを確認します。このコマンドは、以下のタスクを実行します。

      1. 管理マシンで新しい SSH 認証鍵を生成します。
      2. デプロイメント内の他のすべての VM に公開鍵を追加します。
      gcloud compute ssh root@$VM_WS --zone "${ZONE}" << EOF
      set -x
      ssh-keygen -t rsa -N "" -f /root/.ssh/id_rsa
      sed 's/ssh-rsa/root:ssh-rsa/' ~/.ssh/id_rsa.pub > ssh-metadata
      for vm in ${VMs[@]}
      do
          gcloud compute instances add-metadata \$vm --zone ${ZONE} --metadata-from-file ssh-keys=ssh-metadata
      done
      EOF

    ハイブリッド クラスタをデプロイする

    次のコードブロックには、次のタスクを完了するために必要なすべてのコマンドと構成が含まれています。

    1. 必要なハイブリッド クラスタ用の構成ファイルを作成します。
    2. プリフライト チェックを実行します。
    3. クラスタをデプロイします。
    gcloud compute ssh root@$VM_WS --zone "${ZONE}" <<EOF
    set -x
    export PROJECT_ID=$(gcloud config get-value project)
    CLUSTER_NAME=\$(curl http://metadata.google.internal/computeMetadata/v1/instance/attributes/cluster_id -H "Metadata-Flavor: Google")
    BMCTL_VERSION=\$(curl http://metadata.google.internal/computeMetadata/v1/instance/attributes/bmctl_version -H "Metadata-Flavor: Google")
    export CLUSTER_NAME
    export BMCTL_VERSION
    bmctl create config -c \$CLUSTER_NAME
    cat > bmctl-workspace/\$CLUSTER_NAME/\$CLUSTER_NAME.yaml << EOB
    ---
    gcrKeyPath: /root/bm-gcr.json
    sshPrivateKeyPath: /root/.ssh/id_rsa
    gkeConnectAgentServiceAccountKeyPath: /root/bm-gcr.json
    gkeConnectRegisterServiceAccountKeyPath: /root/bm-gcr.json
    cloudOperationsServiceAccountKeyPath: /root/bm-gcr.json
    ---
    apiVersion: v1
    kind: Namespace
    metadata:
      name: cluster-\$CLUSTER_NAME
    ---
    apiVersion: baremetal.cluster.gke.io/v1
    kind: Cluster
    metadata:
      name: \$CLUSTER_NAME
      namespace: cluster-\$CLUSTER_NAME
    spec:
      type: hybrid
      anthosBareMetalVersion: \$BMCTL_VERSION
      gkeConnect:
        projectID: \$PROJECT_ID
      controlPlane:
        nodePoolSpec:
          clusterName: \$CLUSTER_NAME
          nodes:
          - address: 10.200.0.3
          - address: 10.200.0.4
          - address: 10.200.0.5
      clusterNetwork:
        pods:
          cidrBlocks:
          - 192.168.0.0/16
        services:
          cidrBlocks:
          - 172.26.232.0/24
      loadBalancer:
        mode: bundled
        ports:
          controlPlaneLBPort: 443
        vips:
          controlPlaneVIP: 10.200.0.49
          ingressVIP: 10.200.0.50
        addressPools:
        - name: pool1
          addresses:
          - 10.200.0.50-10.200.0.70
      clusterOperations:
        # might need to be this location
        location: us-central1
        projectID: \$PROJECT_ID
      storage:
        lvpNodeMounts:
          path: /mnt/localpv-disk
          storageClassName: node-disk
        lvpShare:
          numPVUnderSharedPath: 5
          path: /mnt/localpv-share
          storageClassName: local-shared
      nodeConfig:
        podDensity:
          maxPodsPerNode: 250
    ---
    apiVersion: baremetal.cluster.gke.io/v1
    kind: NodePool
    metadata:
      name: node-pool-1
      namespace: cluster-\$CLUSTER_NAME
    spec:
      clusterName: \$CLUSTER_NAME
      nodes:
      - address: 10.200.0.6
      - address: 10.200.0.7
    EOB
    
    bmctl create cluster -c \$CLUSTER_NAME
    EOF

    クラスタを確認する

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

    1. SSH を使用して、root として管理ワークステーションにアクセスします。

      gcloud compute ssh root@abm-ws --zone ${ZONE}
      

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

    2. KUBECONFIG 環境変数にクラスタの構成ファイルへのパスを設定し、クラスタで kubectl コマンドを実行します。

      export clusterid=CLUSTER_NAME
      export KUBECONFIG=$HOME/bmctl-workspace/$clusterid/$clusterid-kubeconfig
      kubectl get nodes
      
    3. 現在のコンテキストを環境変数に設定します。

      export CONTEXT="$(kubectl config current-context)"
      
    4. 次の gcloud CLI コマンドを実行します。このコマンドは以下を行います。

      • ユーザー アカウントにクラスタに対する Kubernetes clusterrole/cluster-admin ロールを付与します。
      • 管理ワークステーションに SSH 接続せずにローカル コンピュータで kubectl コマンドを実行できるようにクラスタを構成します。

      GOOGLE_ACCOUNT_EMAIL は、 Google Cloud アカウントに関連付けられているメールアドレスに置き換えます。例: --users=alex@example.com

      gcloud container fleet memberships generate-gateway-rbac  \
          --membership=CLUSTER_NAME \
          --role=clusterrole/cluster-admin \
          --users=GOOGLE_ACCOUNT_EMAIL \
          --project=PROJECT_ID \
          --kubeconfig=$KUBECONFIG \
          --context=$CONTEXT\
          --apply
      

      このコマンドの出力は次のようになります(読みやすくするために省略しています)。

      Validating input arguments.
      Specified Cluster Role is: clusterrole/cluster-admin
      Generated RBAC policy is:
      --------------------------------------------
      ...
      
      Applying the generate RBAC policy to cluster with kubeconfig: /root/bmctl-workspace/CLUSTER_NAME/CLUSTER_NAME-kubeconfig, context: CLUSTER_NAME-admin@CLUSTER_NAME
      Writing RBAC policy for user: GOOGLE_ACCOUNT_EMAIL to cluster.
      Successfully applied the RBAC policy to cluster.
      
    5. 完了したら、「exit」と入力して管理ワークステーションからログアウトします。

    6. Connect ゲートウェイを介して、クラスタにアクセスできる kubeconfig エントリを取得します。

      gcloud container fleet memberships get-credentials CLUSTER_NAME
      

      出力は次のようになります。

      Starting to build Gateway kubeconfig...
      Current project_id: PROJECT_ID
      A new kubeconfig entry "connectgateway_PROJECT_ID_global_CLUSTER_NAME" has been generated and set as the current context.
      
    7. これで、Connect Gateway を介して kubectl コマンドを実行できるようになりました。

      kubectl get nodes
      kubectl get namespaces
      

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

    Google Cloud コンソールでクラスタのワークロードを確認するには、クラスタにログインする必要があります。コンソールに初めてログインする前に、認証方法を構成する必要があります。構成する最も簡単な認証方法は、Google ID です。この認証方法では、Google Cloud アカウントに関連付けられているメールアドレスを使用してログインできます。

    前のセクションで実行した gcloud container fleet memberships generate-gateway-rbac コマンドは、Google ID でログインできるようにクラスタを構成します。

    1. Google Cloud コンソールで、[GKE クラスタ] ページに移動します。

      GKE クラスタに移動

    2. 登録済みクラスタの横にある [アクション] をクリックし、[ログイン] をクリックします。

    3. [Google ID を使用してログインします] を選択します。

    4. [ログイン] をクリックします。

    クリーンアップ

    1. 管理ワークステーションに接続して、クラスタ VM をインストール前の状態にリセットし、 Google Cloudプロジェクトからクラスタの登録を解除します。

      gcloud compute ssh root@abm-ws --zone ${ZONE} << EOF
      set -x
      export clusterid=CLUSTER_NAME
      bmctl reset -c \$clusterid
      EOF
      
    2. 名前に abm を含むすべての VM を一覧表示します。

      gcloud compute instances list | grep 'abm'
      
    3. 名前に abm を含むすべての VM を削除しても問題がないことを確認します。

      確認後、次のコマンドを実行して abm VM を削除できます。

      gcloud compute instances list --format="value(name)" | grep 'abm'  | xargs gcloud \
          --quiet compute instances delete