Privaten Build mit Gemini für Google Cloud-Unterstützung erstellen

In dieser Anleitung erfahren Sie, wie Sie mit Gemini für Google Cloud, einer KI-basierten Unterstützung in Google Cloud, Logs aufrufen und eine Build-Umgebung für eine Reihe von Mikrodiensten in Google Kubernetes Engine einrichten.

Diese Anleitung richtet sich an DevOps-Entwickler jeder Erfahrungsstufe.

Lernziele

  • Fordern Sie Gemini eine Logging-Abfrage auf, damit Sie Logs von Pods beobachten können.
  • Erfahren Sie, wie Sie eine private Build-Umgebung einrichten, indem Sie Gemini kontextbasierte Fragen stellen.
  • Mit Gemini erfahren, wie Sie Container-Images privat und sicher speichern.

Verwendete Google Cloud-Produkte

In dieser Anleitung werden die folgenden kostenpflichtigen Google Cloud-Produkte verwendet. Sie können mithilfe des Preisrechners eine Kostenschätzung für Ihre voraussichtliche Nutzung erstellen.

  • Google Kubernetes Engine (GKE) GKE ist ein verwalteter Kubernetes-Dienst, mit dem Sie Containeranwendungen in großem Maßstab bereitstellen und verwalten können.

  • Cloud Logging: Logging ist ein System zur Logverwaltung in Echtzeit, das Speicher, Suche, Analyse und Monitoring unterstützt.

  • Cloud Build Cloud Build ist ein Dienst, der Ihre Builds in der Google Cloud-Infrastruktur ausführt. Cloud Build kann Quellcode aus einer Vielzahl von Repositories oder Cloud Storage-Bereichen importieren, einen Build nach Ihren Vorgaben ausführen und Artefakte wie Docker-Container oder Java-Archive erstellen.

  • Artifact Registry Artifact Registry ist ein zentraler Ort, an dem Sie Container-Images und Sprachpakete verwalten können. Sie können damit Artefakte zentral speichern und Abhängigkeiten im Rahmen einer integrierten Google Cloud-Umgebung erstellen.

  • Gemini: Gemini ist ein ständiger Mitarbeiter in Google Cloud, der einer breiten Palette von Nutzern, einschließlich Entwicklern und Data Scientists, auf Basis generativer KI Unterstützung bietet. Für eine integrierte Unterstützung ist Gemini in viele Google Cloud-Produkte eingebettet.

Hinweise

  1. Gemini muss für Ihr Google Cloud-Nutzerkonto und Ihr Projekt eingerichtet sein. Prüfen Sie außerdem, ob das Cloud Code-Plug-in in Ihrer bevorzugten IDE installiert ist. Wenn Sie den Cloud Shell Editor oder Cloud Workstations als IDE verwenden möchten, sind Cloud Code und Gemini Code Assist bereits standardmäßig verfügbar.
  2. Google Kubernetes Engine API aktivieren.

    Aktivieren Sie die API

  3. Aktivieren Sie Cloud Shell in der Google Cloud Console.

    Cloud Shell aktivieren

    Unten in der Google Cloud Console wird eine Cloud Shell-Sitzung gestartet und eine Eingabeaufforderung angezeigt. Cloud Shell ist eine Shell-Umgebung, in der das Google Cloud CLI bereits installiert ist und Werte für Ihr aktuelles Projekt bereits festgelegt sind. Das Initialisieren der Sitzung kann einige Sekunden dauern.

  4. Erstellen Sie einen GKE-Cluster mit dem Befehl gcloud container clusters create:

    gcloud container clusters create test --region us-central1
    

    Die Ausführung des Befehls dauert einige Minuten. Die Ausgabe sieht etwa so aus:

    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. Klonen Sie ein Repository und stellen Sie mit kubectl eine Reihe von Mikrodiensten bereit, aus denen eine E-Commerce-Webanwendung im GKE-Cluster besteht:

    git clone https://github.com/GoogleCloudPlatform/microservices-demo && cd microservices-demo
    
    kubectl apply -f ./release/kubernetes-manifests.yaml
    
  6. Rufen Sie die externe IP-Adresse nach einigen Minuten ab, um in einem Browser auf die Webanwendung zuzugreifen:

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

Logs für Arbeitslasten in GKE verstehen

Im folgenden Beispiel sind Sie ein DevOps-Entwickler mit der Aufgabe, einen vorhandenen Satz von Mikrodiensten zu übernehmen, die in einem GKE-Cluster ausgeführt werden. Zum besseren Verständnis der Umgebung beschließen Sie, die Logs der verschiedenen Mikrodienste zu prüfen.

In Ihrer bevorzugten IDE, in die Code Assist integriert ist, können Sie mit Gemini chatten, um Hilfe zu erhalten. Im Bereich Gemini geben Sie Aufforderungen ein. Dies sind Fragen oder Anweisungen, die die gewünschte Hilfe beschreiben. Gemini gibt dann Antworten zurück.

Gemini verwendet Ihre Prompts und seine Antworten nicht als Daten zum Trainieren seines Modells. Weitere Informationen finden Sie unter So nutzt Gemini für Google Cloud Ihre Daten.

Sie wissen, dass die Mikrodienste im Namespace default bereitgestellt wurden. Gehen Sie so vor, um Gemini dazu aufzufordern, Sie beim Prüfen von Logs zu unterstützen:

  1. Klicken Sie in der Aktivitätsleiste Ihrer IDE auf chat_spark Gemini Code Assist, um den Bereich Gemini Code Assist zu öffnen.

  2. Geben Sie im Bereich Gemini Code Assist die folgende Eingabeaufforderung ein:

    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 gibt in etwa folgende Antwort zurück:

    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. Klicken Sie zum Prüfen dieser Logs auf jedes von der Abfrage zurückgegebene Log. Wenn Sie bei einem einzelnen Log auf Diese Abfrage erklären klicken, sieht die Antwort in etwa so aus:

    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.
    

Private Build-Umgebung für Container-Images erstellen

Da Sie nun ein besseres Verständnis für die Arbeitslasten haben, die sich aus dem Beobachten der Logs ergeben, entscheiden Sie sich, eine Infrastruktur einzurichten und die zukünftigen Container-Images Ihres Teams auf einer Gruppe privater Worker zu erstellen. In diesem Abschnitt fordern Sie Gemini dazu auf, Ihnen zu zeigen, wie Sie eine Build-Umgebung ausführen, damit Sie Ihre Builds zu Ihrem Google Cloud-Projekt migrieren können.

  1. Klicken Sie in der Aktivitätsleiste Ihrer IDE auf chat_spark Gemini Code Assist, um den Bereich Gemini Code Assist zu öffnen.

  2. Geben Sie im Bereich Gemini Code Assist die folgende Eingabeaufforderung ein:

    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 gibt in etwa folgende Antwort zurück:

    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. Private Cloud Build-Pools klingen wie eine praktikable Lösung für die Zukunft. Daher bitten Sie Gemini um Unterstützung beim Erstellen Ihres ersten privaten Pools. Geben Sie den folgenden Prompt ein:

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

    Gemini gibt in etwa folgende Antwort zurück:

    Here's how you can create your first private worker pool using the gcloud
    command:
    
    1. Define your pool configuration:
    
    First, you need to define the configuration of your private pool. This
    includes details like the pool name, region, machine type, disk size, and
    network settings. You can do this by creating a JSON file named
    workerpool.json with the following structure:
    
    {
      "name": "my-first-privatepool",
      "location": "us-central1",
      "machineType": "e2-standard-2",
      "diskSizeGb": 800,
      "network": "projects/your-project-id/global/networks/default",
      "assignExternalIP": true
    }
    
    2. Create the pool using gcloud:
    
    Once you have the configuration file ready, you can use the following gcloud
    command to create the private worker pool:
    
    gcloud builds worker-pools create \
      --project=gsm-project-382212 \
      --location=us-central1 \
      --worker-pool-id=my-first-privatepool \
      --file=workerpool.json
    
    Explanation of the command:
    
    - gcloud builds worker-pools create : This is the command to create a new worker pool.
    - --project=gsm-project-382212 : This specifies the project ID where you want to create the pool.
    - --location=us-central1 : This specifies the region where you want to create the pool.
    - --worker-pool-id=my-first-privatepool : This specifies the ID for your private pool.
    - --file=workerpool.json : This specifies the path to your configuration file.
    
    3. Verify the pool creation:
    
    After running the command, you can verify that the pool has been created by
    running the following command:
    
    gcloud builds worker-pools list
    
    Additional notes:
    
    - You can customize the configuration file to meet your specific needs. For
    example, you can change the machine type, disk size, or network settings.
    - You can also create private worker pools using the Cloud Build API or th
     Cloud Console.
    
    I hope this helps! Let me know if you have any other questions.
    
  4. Aus der vorherigen Antwort lässt sich ableiten, dass Sie eine Konfigurationsdatei übergeben müssen, um die Variable PRIVATEPOOL_CONFIG_FILE zu ersetzen. Daher fordern Sie Gemini an, Sie beim Erstellen einer Konfigurationsdatei zu unterstützen. Geben Sie den folgenden Prompt ein:

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

    Gemini gibt in etwa folgende Antwort zurück:

    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. Sie beginnen damit, eine eigene Konfigurationsdatei zu erstellen, in der die Build-Worker keinen Zugriff auf das öffentliche Internet haben. Rufen Sie die Google Cloud Console auf und starten Sie den Cloud Shell-Editor.

  6. Führen Sie in Cloud Shell den folgenden Befehl aus und setzen Sie egressOption auf NO_PUBLIC_EGRESS:

    cat << EOF >> cloudbuild-privatepool-config.yaml
    
    privatePoolV1Config:
      networkConfig:
        egressOption: NO_PUBLIC_EGRESS
    
    EOF
    
  7. Sie sind sich bei den übrigen in der Konfigurationsdatei verfügbaren Optionen nicht sicher. Daher fordern Sie Gemini um Unterstützung auf:

    Is there a default value for any of these options?
    

    Gemini gibt in etwa folgende Antwort zurück:

    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.
    
  8. Sie sehen, dass Gemini einige der Standardwerte enthält, die bereits in der Konfigurationsdatei festgelegt sind. Nach dem Lesen der Dokumentation entscheiden Sie, dass Ihre Konfigurationsdatei unverändert ist. Übergeben Sie in Cloud Shell den Befehl gcloud, den Gemini zuvor für Sie bereitgestellt hat, in der neu erstellten Konfigurationsdatei:

    gcloud builds worker-pools create max-pool-test \
        --config-from-file cloudbuild-privatepool-config.yaml \
        --region us-central1
    
  9. Nachdem die Worker erstellt wurden, stellen Sie fest, dass Container keinen Zugriff auf öffentliche Paketmanager wie pip haben. Sie möchten Ihr Wissen zum Hosten privater Pakete bestätigen. Geben Sie die folgende Eingabeaufforderung ein:

    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 gibt in etwa folgende Antwort zurück:

    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.
    
  10. Sie beschließen, mit dem Erstellen eines Artifact Registry-Repositorys fortzufahren. Geben Sie im Bereich Gemini die folgende Eingabeaufforderung ein:

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

    Gemini gibt in etwa folgende Antwort zurück:

    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.
    

  11. Wenn Sie die Variable KMS_KEY sehen, stellen Sie fest, dass Sie noch keinen Cloud KMS-Schlüssel erstellt haben. Geben Sie den folgenden Prompt ein:

    Do I need a KMS_KEY?
    

    Gemini gibt in etwa folgende Antwort zurück:

    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.
    
  12. Wenn Ihre Fragen beantwortet wurden, führen Sie in Cloud Shell den Befehl gcloud aus, den Gemini für Sie bereitgestellt hat:

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

Mit Gemini haben Sie erfolgreich die private Gruppe von Workern und das private Repository zum Erstellen und Hosten Ihrer Artefakte erstellt.

Bereinigen

Damit Ihrem Google Cloud-Konto die in dieser Anleitung verwendeten Ressourcen nicht in Rechnung gestellt werden, können Sie das Google Cloud-Projekt löschen, das Sie für diese Anleitung erstellt haben. Alternativ haben Sie die Möglichkeit, die einzelnen Ressourcen zu löschen.

  1. Wechseln Sie in der Google Cloud Console zur Seite Ressourcen verwalten.

    Zur Seite „Ressourcen verwalten“

  2. Wählen Sie in der Projektliste das Projekt aus, das Sie löschen möchten, und klicken Sie dann auf Löschen.
  3. Geben Sie im Dialogfeld die Projekt-ID ein und klicken Sie auf Shut down (Beenden), um das Projekt zu löschen.

Nächste Schritte