Crea una build privata con Gemini per l'assistenza di Google Cloud

Questo tutorial mostra come utilizzare Gemini per Google Cloud, un collaboratore basato sull'AI in Google Cloud, per navigare nei log e configurare per un insieme di microservizi in Google Kubernetes Engine.

Questo tutorial è destinato a DevOps Engineer di qualsiasi livello di esperienza.

Obiettivi

  • Chiedi a Gemini una query di logging in modo da poter osservare dai pod.
  • Scopri come creare un ambiente di build privato tramite prompt Gemini con domande basate sul contesto.
  • Usa Gemini per imparare ad archiviare le immagini container privatamente e in sicurezza.

Prodotti Google Cloud utilizzati

Questo tutorial utilizza i seguenti prodotti Google Cloud fatturabili. Utilizza la Calcolatore prezzi per generare una stima dei costi basata sull'utilizzo previsto.

  • Google Kubernetes Engine (GKE) GKE è una piattaforma Kubernetes gestita che consente di eseguire il deployment e gestire le applicazioni containerizzate su larga scala.

  • Cloud Logging. Il logging è un servizio di gestione dei log in tempo reale di archiviazione, ricerca, analisi e monitoraggio.

  • Cloud Build. Cloud Build è un servizio che esegue si basa sull'infrastruttura Google Cloud. Cloud Build può importa il codice sorgente da una varietà di repository o da Cloud Storage , eseguire una build in base alle tue specifiche e produrre artefatti come come container Docker o archivi Java.

  • Artifact Registry. Artifact Registry offre una singola posizione da cui per gestire immagini container e pacchetti di linguaggio. Ti permette di gestire centralmente per archiviare artefatti e creare dipendenze come parte di un nell'utilizzo di Google Cloud.

  • Gemini. Gemini è una piattaforma collaboratore in Google Cloud che offre soluzioni basate sull'AI generativa assistenza a una vasta gamma di utenti, tra cui sviluppatori e dati gli scienziati. Per offrire un'esperienza di assistenza integrata, Gemini è integrato in molti prodotti Google Cloud.

Prima di iniziare

  1. Assicurati che Gemini sia configurato il tuo account utente e il tuo progetto Google Cloud. Inoltre, assicurati di avere ha installato il plug-in Cloud Code nel tuo IDE preferito. Se preferisci usare l'editor di Cloud Shell o Cloud Workstations come IDE, quindi Cloud Code Gemini Code Assist è già disponibile per impostazione predefinita.
  2. Attiva Google Kubernetes Engine API.

    Abilita l'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. Crea un cluster GKE utilizzando Comando gcloud container clusters create:

    gcloud container clusters create test --region us-central1
    

    L'esecuzione del comando richiede alcuni minuti. L'output è simile seguenti:

    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. Clona un repository e usa kubectl per eseguire il deployment di un set di microservizi che compongono un'app web di e-commerce nel cluster GKE:

    git clone https://github.com/GoogleCloudPlatform/microservices-demo && cd microservices-demo
    
    kubectl apply -f ./release/kubernetes-manifests.yaml
    
  6. Dopo alcuni minuti, recupera l'indirizzo IP esterno per accedere alla tua app web in un browser:

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

Informazioni sui log per i carichi di lavoro in GKE

Per l'esempio seguente, supponiamo che tu sia un DevOps Engineer con il compito con l'ereditarietà di un set esistente di microservizi in esecuzione in un cluster Kubernetes. Per comprendere l'ambiente, decidi di esaminare i log dai vari microservizi.

Nella tua cartella preferita IDE integrato con Gemini Code Assist, puoi chattare con Gemini per ricevere assistenza. Utilizzando il riquadro Gemini, inserisci i prompt, ovvero domande o affermazioni che descrivono il supporto che vuoi e Gemini restituisce le risposte.

Scopri come e quando Gemini per Google Cloud utilizza i tuoi dati.

Sai che è stato eseguito il deployment dei microservizi nello spazio dei nomi default. Per chiedere a Gemini di aiutarti a ispezionare i log, segui questi passaggi:

  1. Nella barra delle attività dell'IDE, fai clic su chat_spark Gemini Code Assist per apri il riquadro Gemini Code Assist.

  2. Nel riquadro Gemini Code Assist, inserisci il seguente prompt:

    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 restituisce una risposta simile alla seguente:

    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. Per esaminare questi log, fai clic su ogni log restituito dalla query. Su un singolo log, se fai clic su Spiega questa query, la risposta è simile a:

    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.
    

Crea un ambiente di build privato per le immagini container

Ora che hai una conoscenza più approfondita dei carichi di lavoro derivanti dall'osservazione ai log, decidi di configurare l'infrastruttura e creare il container futuro del tuo team su un insieme di worker privati. In questa sezione, chiedi Gemini per aiutarti a capire come eseguire una build in modo da poter eseguire la migrazione delle build nel progetto Google Cloud.

  1. Nella barra delle attività dell'IDE, fai clic su chat_spark Gemini Code Assist per apri il riquadro Gemini Code Assist.

  2. Nel riquadro Gemini Code Assist, inserisci il seguente prompt:

    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 restituisce una risposta simile alla seguente:

    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. I pool privati di Cloud Build sembrano un percorso possibile per il futuro, quindi chiedi Gemini per aiutarti a creare il tuo primo pool privato. Digita il seguente prompt:

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

    Gemini restituisce una risposta simile alla seguente:

    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. Dalla risposta precedente, sembra che tu debba passare di configurazione del deployment per sostituire la variabile PRIVATEPOOL_CONFIG_FILE, quindi chiedi a Gemini durante la creazione di un file di configurazione. Digita il seguente prompt:

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

    Gemini restituisce una risposta simile alla seguente:

    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. Inizierai a creare il tuo file di configurazione in cui i worker di build non hanno accesso alla rete internet pubblica. Vai alla console Google Cloud e avvia l'editor di Cloud Shell.

  6. In Cloud Shell, esegui questo comando, Da egressOption a NO_PUBLIC_EGRESS:

    cat << EOF >> cloudbuild-privatepool-config.yaml
    
    privatePoolV1Config:
      networkConfig:
        egressOption: NO_PUBLIC_EGRESS
    
    EOF
    
  7. Hai dubbi sulle altre opzioni disponibili nella configurazione quindi chiedi a Gemini di aiutarti:

    Is there a default value for any of these options?
    

    Gemini restituisce una risposta simile alla seguente:

    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. Come puoi vedere, Gemini fornisce alcuni dei valori predefiniti è già impostato nel file di configurazione. Dopo aver letto la documentazione, che il file di configurazione sia valido così com'è. In Cloud Shell, passa il comando gcloud che Gemini ti ha fornito in precedenza il file di configurazione appena creato:

    gcloud builds worker-pools create max-pool-test \
        --config-from-file cloudbuild-privatepool-config.yaml \
        --region us-central1
    
  9. Dopo aver creato i worker, ti rendi conto che i container non avrà accesso ai gestori di pacchetti pubblici, come pip. Vuoi conferma di aver compreso il funzionamento dell'hosting di pacchetti privati. Inserisci quanto segue :

    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 restituisce una risposta simile alla seguente:

    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. Decidi di procedere con la creazione di un Artifact Registry repository Git. Nel riquadro Gemini, inserisci questo prompt:

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

    Gemini restituisce una risposta simile alla seguente:

    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. Vedendo la variabile KMS_KEY, ti rendi conto di non aver creato una chiave Cloud KMS. Digita il seguente prompt:

    Do I need a KMS_KEY?
    

    Gemini restituisce una risposta simile alla seguente:

    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. Dopo aver risposto alle tue domande, in Cloud Shell eseguirai gcloud comando fornito da Gemini:

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

Con l'assistenza di Gemini, hai creato di worker e il repository privato per la creazione e l'hosting dei tuoi artefatti.

Esegui la pulizia

Per evitare che al tuo account Google Cloud vengano addebitati costi relativi alle risorse usati in questo tutorial, puoi eliminare il progetto Google Cloud per questo tutorial. In alternativa, puoi eliminare il singolo Google Cloud.

  1. Nella console Google Cloud, vai alla pagina Gestisci risorse.

    Vai a Gestisci risorse

  2. Nell'elenco dei progetti, seleziona il progetto che vuoi eliminare, quindi fai clic su Elimina.
  3. Nella finestra di dialogo, digita l'ID del progetto e fai clic su Chiudi per eliminare il progetto.

Passaggi successivi