Gemini for Google Cloud アシスタンスを使用してプライベート ビルドを作成する

このチュートリアルでは、Google Cloud の AI を活用したコラボレーターである Gemini for Google Cloud を使用して、ログをナビゲートし、Google Kubernetes Engine 内のマイクロサービスのビルド環境を設定する方法について説明します。

このチュートリアルは、あらゆる経験レベルの DevOps エンジニアを対象としています。

目標

  • Pod のログを観察できるように、クエリをロギングするよう Gemini に指示します。
  • Gemini にコンテキスト ベースの質問を行い、プライベート ビルド環境を確立する方法を理解します。
  • Gemini を使用して、コンテナ イメージを非公開で安全に保存する方法を学習します。

使用する Google Cloud プロダクト

このチュートリアルでは、課金対象である次の Google Cloud プロダクトを使用します。料金計算ツールを使うと、予想使用量に基づいて費用の見積もりを出すことができます。

  • Google Kubernetes Engine(GKE)。GKE は、コンテナ化されたアプリケーションを大規模にデプロイして管理できるマネージド Kubernetes サービスです。 料金について詳しくは、GKE の料金をご覧ください。

  • Cloud Logging。Logging は、ストレージ、検索、分析、モニタリングをサポートするリアルタイムのログ管理システムです。

  • Cloud Build。Cloud Build は、Google Cloud インフラストラクチャでビルドを行うサービスです。Cloud Build では、さまざまなリポジトリやクラウド ストレージ スペースからソースコードをインポートし、仕様に合わせてビルドを実行し、Docker コンテナや Java アーカイブなどのアーティファクトを生成できます。

  • Artifact Registry Artifact Registry は、コンテナ イメージと言語パッケージを 1 か所で管理できる場所です。統合された Google Cloud エクスペリエンスの一部として、一元的にアーティファクトを保存し、依存関係をビルドできます。

  • Gemini。Gemini は、Google Cloud の常時接続のコラボレーターで、デベロッパーやデータ サイエンティストなどの幅広いユーザーに、生成 AI を活用したアシスタンスを提供します。統合されたアシスタンスのエクスペリエンスを提供するために、Gemini は多くの Google Cloud プロダクトに組み込まれています。

始める前に

  1. Gemini が Google Cloud ユーザー アカウントとプロジェクト用に設定されていることを確認します。
  2. Google Kubernetes Engine API を有効にします。

    API を有効にする

  3. Google Cloud コンソールで、「Cloud Shell をアクティブにする」をクリックします。

    Cloud Shell をアクティブにする

    Google Cloud コンソールの下部で Cloud Shell セッションが開始し、コマンドライン プロンプトが表示されます。Cloud Shell はシェル環境です。Google Cloud CLI がすでにインストールされており、現在のプロジェクトの値もすでに設定されています。セッションが初期化されるまで数秒かかることがあります。

  4. gcloud container clusters create コマンドを使用して、GKE クラスタを作成します。

    gcloud container clusters create test --region us-central1
    

    このスクリプトの実行には数分かかります。出力は次のようになります。

    Creating cluster test in us-central1... Cluster is being health-checked (master is healthy)...done.
    
    Created https://container.googleapis.com/v1/projects/agmsb-gke-lab/zones/us-central1/clusters/test.
    
    To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/us-central1/test?project=agmsb-gke-lab
    kubeconfig entry generated for test.
    
    NAME: test
    LOCATION: us-central1
    MASTER_VERSION: 1.27.3-gke.100
    MASTER_IP: 34.72.99.149
    MACHINE_TYPE: e2-medium
    NODE_VERSION: 1.27.3-gke.100
    NUM_NODES: 9
    STATUS: RUNNING
    
  5. リポジトリのクローンを作成し、kubectl を使用して、e コマース ウェブアプリを構成する一連のマイクロサービスを GKE クラスタにデプロイします。

    git clone https://github.com/GoogleCloudPlatform/microservices-demo && cd microservices-demo
    
    kubectl apply -f ./release/kubernetes-manifests.yaml
    
  6. 数分後、外部 IP アドレスを取得して、ブラウザでウェブアプリにアクセスします。

    kubectl get service frontend-external | awk '{print $4}'
    

GKE のワークロードのログについて理解する

次の例では、あなたは、DevOps エンジニアとして、GKE クラスタで実行中の既存の一連のマイクロサービスを継承することを担当しています。環境を把握するため、あなたはさまざまなマイクロサービスのログを調べたいと思っています。

Google Cloud コンソールで、Gemini とチャットしてヘルプを受けることができます。[Gemini] ペインを使用してプロンプトを入力します。プロンプトは、必要なヘルプを説明する質問やステートメントです。Gemini によってレスポンスが返されます。

Gemini がプロンプトやそのレスポンスをデータとして使用してモデルをトレーニングすることはありせん。詳細については、Google Cloud の Gemini がデータを使用する方法をご覧ください。

マイクロサービスが default 名前空間にデプロイされていることがわかっています。 ログの検査を支援するよう Gemini に指示するには、次の手順を行います。

  1. Google Cloud コンソールのツールバーで、spark [Gemini を開く] をクリックします。
  2. [Gemini] ペインで、次のプロンプトを入力します。

    How can I write a Cloud Logging query to search for logs from Pods in
    a namespace called "default" in a Google Kubernetes Engine cluster named "test"?
    

    Gemini から次のようなレスポンスが返されます。

    To write a Cloud Logging query to search for logs from Pods in a
    namespace called "default" in GKE cluster called "test", you
    can use the following query:
    
    resource.type="k8s_container"
    resource.labels.namespace_name="default"
    resource.labels.cluster_name="test"
    
    This query will search for all logs from Pods in the "default"
    namespace in the "test" GKE cluster. You can also use the
    Cloud Logging console to search for logs from Pods in a specific
    namespace and cluster. To do this, follow these steps:
    
    1. Go to the Cloud Logging console.
    2. In the Query builder pane, click
       Resource.
    3. In the Resource type list, select Kubernetes
       Container.
    4. In the Namespace name field, enter default.
    5. In the Cluster name field, enter test.
    6. Click Add.
    7. In the Query results pane, you will see a list of logs
       from Pods in the "default" namespace in the "test" GKE
       cluster.
    
  3. これらのログを調べるには、クエリから返された各ログをクリックします。個々のログで、[このクエリを説明] をクリックすると、レスポンスは次のようになります。

    Sure. Here are the bullet points that explain the log entry:
    
    • The log entry is about a GET request to the /_healthz path.
    • The request was successful and returned a 200 status code.
    • The request took 0 milliseconds to complete.
    • The request was made by the x-liveness-probe session.
    • The request was made from the frontend-755cdc7957-pv8x5 pod
      in the default namespace.
    

コンテナ イメージ用のプライベート ビルド環境を作成する

ログの観察によってワークロードをより深く理解したので、あなたは、一連のプライベート ワーカー上でインフラストラクチャを設定し、チームの将来のコンテナ イメージを構築することにしました。このセクションでは、Google Cloud プロジェクトにビルドを移行できるように、ビルド環境を実行する方法を理解することについて、Gemini に支援するよう指示します。

  1. Google Cloud コンソールのツールバーで、spark [Gemini を開く] をクリックします。
  2. [Gemini] ペインで、次のプロンプトを入力します。

    How can I build a private build environment that is not connected to
    the internet in Google Cloud? I want to be able to use Docker to
    build my container images in this environment.
    

    Gemini から次のようなレスポンスが返されます。

    To build a private build environment that is not connected to the internet
    in Google Cloud, you can use a Cloud Build private pool. A private pool
    is a group of build workers that are not connected to the public internet.
    You can create a private pool in a Virtual Private Cloud (VPC) network,
    which provides an additional layer of security for your build environment.
    
    To create a private pool, you will need to:
    
    1. Create a VPC network.
    2. Create a private pool in the VPC network.
    3. Configure your build config file to use the private pool.
    
    Once you have created a private pool, you can use it to build your
    container images using Docker. To do this, you will need to:
    
    1. Create a Dockerfile.
    2. Create a build config file.
    3. Run the `gcloud builds submit` command.
    
    The `gcloud builds submit` command will build your container image
    using the private pool.
    
    Find more information for your question here:
    
    • https://cloud.google.com/build/docs/private-pools/create-manage-private-pools
    
  3. Cloud Build のプライベート プールは実行可能なパスのように思えます。そのため、最初のプライベート プールの作成を支援することについて Gemini に支援を依頼します。 次のプロンプトを入力します。

    Can you give me a gcloud command to create my first Cloud Build
    private pool?
    

    Gemini から次のようなレスポンスが返されます。

    Sure. To create a private pool, you can use the following gcloud
    command:
    
    gcloud builds worker-pools create PRIVATEPOOL_ID --config-from-file
    PRIVATEPOOL_CONFIG_FILE --region REGION
    
    Where:
    
    • PRIVATEPOOL_ID is a unique identifier for your private pool.
    • PRIVATEPOOL_CONFIG_FILE is the name of your private pool
      config file.
    • REGION is the region where you want to create your private
      pool.
    
    For more information, see the Cloud Build documentation.
    
    Source:
    
    https://cloud.google.com/build/docs/private-pools/create-manage-private-pools
    
  4. 上記のレスポンスから、変数 PRIVATEPOOL_CONFIG_FILE を置き換えるために構成ファイルを渡す必要があるため、構成ファイルの作成について Gemini の支援をリクエストします。次のプロンプトを入力します。

    Can you show me an example of a Cloud Build private pool config file?
    

    Gemini から次のようなレスポンスが返されます。

    Sure. Here is an example of a Cloud Build private pool config file:
    
    privatePoolV1Config:
       networkConfig:
         egressOption: NO_PUBLIC_EGRESS<br>
         peeredNetwork: 'projects/my-project/global/networks/my-network'
         peeredNetworkIpRange: '10.1.2.0/24'
       workerConfig:
         diskSizeGb: '100'
         machineType: 'e2-standard-4'
    
    This config file creates a private pool with the following
    properties:
    
    • The pool is not connected to the public internet.
    • The pool is connected to the `my-network` VPC network.
    • The pool's workers have a `100GB` boot disk and a
      `e2-standard-4` machine type.
    
    For more information, see the Cloud Build documentation.
    
  5. ビルドワーカーが公共のインターネットにアクセスできない独自の構成ファイルの作成を開始します。Cloud Shell で次のコマンドを実行し、egressOptionNO_PUBLIC_EGRESS に設定します。

    cat << EOF >> cloudbuild-privatepool-config.yaml
    
    privatePoolV1Config:
      networkConfig:
        egressOption: NO_PUBLIC_EGRESS
    
    EOF
    
  6. 構成ファイルの残りのオプションが不明なため Gemini に支援を依頼します。

    Is there a default value for any of these options?
    

    Gemini から次のようなレスポンスが返されます。

    Yes, there are default values for some of the options in the private
    pool config file. For example, the default value for egressOption is
    ALLOW_PUBLIC_EGRESS, which means that the private pool will have
    access to the public internet. The default value for diskSizeGb is
    100, and the default value for machineType is e2-medium.
    
    For a full list of the default values, see the Cloud Build documentation.
    
  7. Gemini は、構成ファイルにすでに設定されているデフォルト値の一部を提供していることがわかりました。ドキュメントを読んだ結果、あなたは構成ファイルをそのまま使用することにします。Cloud Shell で、先ほど作成した構成ファイルで Gemini から提供された gcloud コマンドを渡します。

    gcloud builds worker-pools create max-pool-test \
        --config-from-file cloudbuild-privatepool-config.yaml \
        --region us-central1
    
  8. ワーカーが作成された後、あなたは、コンテナが pip などの公開パッケージ マネージャーにアクセスできないことに気づきました。あなたは、プライベート パッケージのホスティングについて理解していることを確認したいと思っています。次のプロンプトを入力します。

    If I create a private pool with NO_PUBLIC_EGRESS enabled, can I use
    Artifact Registry to host my packages privately and access them from
    a private pool?
    

    Gemini から次のようなレスポンスが返されます。

    Yes, you can use Artifact Registry to host your packages privately
    and access them from a private pool. To do this, you will need to
    create a private pool with NO_PUBLIC_EGRESS enabled and then
    configure your Artifact Registry repository to use the private pool.
    For more information, see the Cloud Build documentation.
    
  9. あなたは、Artifact Registry リポジトリの作成に進むことにしました。[Gemini] ペインで、次のプロンプトを入力します。

    How can I use gcloud to create a private Docker repository for
    container images in Artifact Registry?
    

    Gemini から次のようなレスポンスが返されます。

    To create a private Docker repository for container images in
    Artifact Registry, you can use the following command:
    
    gcloud artifacts repositories create REPOSITORY_NAME \\\
        --repository-format=docker \\\
        --location=LOCATION \\\
        --description="DESCRIPTION" \\\
        --kms-key=KMS_KEY
    
    Where:
    
    • REPOSITORY_NAME is the name of your repository.
    • LOCATION is the location of your repository.
    • DESCRIPTION is a description of your repository.
    • KMS_KEY is the name of your Cloud Key Management Service key that
      you want to use to encrypt your repository data.
    
    For more information, see the gcloud artifacts repositories create
    documentation.
    

  10. KMS_KEY 変数を見ると、Cloud KMS 鍵がまだ作成されていないことがわかりました。次のプロンプトを入力します。

    Do I need a KMS_KEY?
    

    Gemini から次のようなレスポンスが返されます。

    No, you don't need a KMS_KEY to create a private Docker repository
    for container images in Artifact Registry. However, if you want to
    encrypt your container images, you can use a Cloud Key Management Service key. For more
    information, see the Artifact Registry CMEK documentation.
    
  11. 質問に回答されたので、Cloud Shell で、Gemini から提供された gcloud コマンドを実行します。

    gcloud artifacts repositories create max-docker-repo-test \
        --repository-format=docker \
        --location=us-central1 \
        --description="test container repository"
    

Gemini のアシスタンスによって、アーティファクトのビルドとホストを行うための非公開のワーカーセットと非公開リポジトリが正常に作成されました。

クリーンアップ

このチュートリアルで使用したリソースについて、Google Cloud アカウントに課金されないようにするには、このチュートリアルで作成した Google Cloud プロジェクトを削除します。また、リソースを個別に削除することもできます。

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

次のステップ