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

In dieser Anleitung erfahren Sie, wie Sie Gemini für Google Cloud, ein KI-gestützter Mitarbeiter in Google Cloud, um Logs zu durchsuchen und einen Build einzurichten für eine Reihe von Mikrodiensten in Google Kubernetes Engine.

Diese Anleitung richtet sich an DevOps-Entwickler mit unterschiedlichen Kenntnissen.

Lernziele

  • Fordern Sie mit Gemini eine Logging-Abfrage an, damit Sie Logs von Pods.
  • Mit Prompts eine private Build-Umgebung einrichten Gemini mit kontextbasierten Fragen.
  • Mit Gemini lernen, wie Container-Images gespeichert werden geschützt werden.

Verwendete Google Cloud-Produkte

In dieser Anleitung werden die folgenden kostenpflichtigen Google Cloud-Produkte verwendet. Verwenden Sie die Methode Preisrechner um eine Kostenschätzung auf Basis Ihrer voraussichtlichen Nutzung zu erstellen.

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

  • Cloud Logging: Mit Logging verwalten Sie Logs in Echtzeit. mit Unterstützung für Speicher, Suche, Analyse und Monitoring.

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

  • Artifact Registry Artifact Registry ist ein zentraler Ort, an dem Sie Container-Images und Sprachpakete verwalten. Damit können Sie zentral Speichern Sie Artefakte und erstellen Sie Abhängigkeiten Google Cloud-Erfahrung

  • Gemini: Gemini ist immer aktiv Mitarbeitende in Google Cloud, die auf generativer KI basieren Unterstützung für eine Vielzahl von Nutzenden, einschließlich Entwickelnden und Daten Wissenschaftlern. Um eine integrierte Unterstützung zu bieten, Gemini ist in viele Google Cloud-Produkte eingebettet.

Hinweise

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

    Aktivieren Sie die API

  3. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  4. GKE-Cluster mithilfe der gcloud container clusters create-Befehl:

    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. Repository klonen und mit kubectl eine Reihe von Mikrodiensten bereitstellen aus einer E-Commerce-Webanwendung im GKE-Cluster:

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

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

Logs für Arbeitslasten in GKE verstehen

Nehmen wir für das folgende Beispiel an, dass Sie als DevOps-Entwickler mit der Aufgabe, mit der Übernahme eines vorhandenen Satzes von Mikrodiensten, die in einer GKE Cluster. Sie möchten die Logs prüfen, um die Umgebung zu verstehen aus den verschiedenen Mikrodiensten.

In der gewünschten In Gemini Code Assist integrierte IDE können Sie mit Gemini chatten, um Hilfe zu erhalten. Über den Bereich Gemini: geben Sie Aufforderungen ein, d. h. Fragen oder Aussagen, die die Hilfe beschreiben. und Gemini gibt Antworten zurück.

Wie und wann Gemini funktioniert für Google Cloud nutzt Ihre Daten.

Sie wissen, dass die Mikrodienste im Namespace default bereitgestellt wurden. So bitten Sie Gemini, Ihnen bei der Prüfung von Protokollen zu helfen:

  1. Klicken Sie in der Aktivitätsleiste Ihrer IDE auf chat_spark Gemini Code Assist für öffnen Sie den Bereich Gemini Code Assist.

  2. Geben Sie im Bereich Gemini Code Assist den folgenden Prompt 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 eine Antwort aus, die ungefähr so aussieht:

    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 auf die einzelnen Logs, die von der Abfrage zurückgegeben werden, um sie zu prüfen. Auf einer Protokoll: Wenn Sie auf Diese Abfrage erklären klicken, erhalten Sie eine ähnliche Antwort. zu:

    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

Jetzt, da Sie ein tieferes Verständnis der Arbeitslasten haben, Logs möchten Sie die Infrastruktur einrichten und den zukünftigen Container Ihres Teams aufbauen. Images auf einer Reihe privater Worker. In diesem Abschnitt fordern Sie Gemini, das Ihnen hilft, zu verstehen, wie ein Build ausgeführt wird um Ihre Builds in Ihr Google Cloud-Projekt zu migrieren.

  1. Klicken Sie in der Aktivitätsleiste Ihrer IDE auf chat_spark Gemini Code Assist für öffnen Sie den Bereich Gemini Code Assist.

  2. Geben Sie im Bereich Gemini Code Assist den folgenden Prompt 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 eine Antwort aus, die ungefähr so aussieht:

    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 klingt nach einem praktikablen Weg in die Zukunft Gemini unterstützt Sie 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 eine Antwort aus, die ungefähr so aussieht:

    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 geht hervor, dass Sie eine Konfigurationsdatei als Ersatz für die Variable PRIVATEPOOL_CONFIG_FILE, also fordern Sie Gemini an, die Sie beim Erstellen einer Konfigurationsdatei unterstützen. Geben Sie den folgenden Prompt ein:

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

    Gemini gibt eine Antwort aus, die ungefähr so aussieht:

    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 mit dem Erstellen einer eigenen Konfigurationsdatei, in der die Build-Worker keinen Zugriff auf das öffentliche Internet haben. Google Cloud Console aufrufen und Starten Sie den Cloud Shell-Editor.

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

    cat << EOF >> cloudbuild-privatepool-config.yaml
    
    privatePoolV1Config:
      networkConfig:
        egressOption: NO_PUBLIC_EGRESS
    
    EOF
    
  7. Sie sind sich nicht sicher, welche weiteren Optionen in der Konfiguration verfügbar sind. -Datei, sodass Sie Gemini um Unterstützung bitten:

    Is there a default value for any of these options?
    

    Gemini gibt eine Antwort aus, die ungefähr so aussieht:

    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 bereitstellt bereits in der Konfigurationsdatei festgelegt ist. Wenn Sie die Dokumentation lesen, ob Ihre Konfigurationsdatei so wie sie ist. Übergeben Sie in Cloud Shell den gcloud-Befehl, den Gemini zuvor für Sie bereitgestellt hat Ihre neu erstellte 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, hat keinen Zugriff auf öffentliche Paketmanager wie pip. Sie möchten um Ihr Wissen über das Hosten privater Pakete zu bestätigen. Geben Sie Folgendes ein: Eingabeaufforderung:

    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 eine Antwort aus, die ungefähr so aussieht:

    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 der Erstellung einer Artifact Registry fortzufahren zu erstellen. Geben Sie im Bereich Gemini den folgenden Prompt ein:

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

    Gemini gibt eine Antwort aus, die ungefähr so aussieht:

    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. Sie sehen die Variable KMS_KEY und stellen fest, dass Sie kein Cloud KMS-Schlüssel. Geben Sie den folgenden Prompt ein:

    Do I need a KMS_KEY?
    

    Gemini gibt eine Antwort aus, die ungefähr so aussieht:

    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. Nachdem Ihre Fragen beantwortet wurden, führen Sie in Cloud Shell den gcloud aus. Befehl, 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-Unterstützung hast du den privaten Eintrag erfolgreich erstellt und das private Repository zum Erstellen und Hosten Ihrer Artefakte.

Bereinigen

Um zu vermeiden, dass Ihrem Google Cloud-Konto die Ressourcen in Rechnung gestellt werden aus dieser Anleitung können Sie das Google Cloud-Projekt löschen, die für diese Anleitung erstellt wurden. 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