Create and modify a GKE cluster with Duet AI assistance

This tutorial shows you how to use Duet AI, an AI-powered collaborator in Google Cloud, to create, test, and modify a Google Kubernetes Engine (GKE) cluster in Autopilot. You'll see how Duet AI can help deploy a simple app to the cluster and create a daily maintenance window for the app. This tutorial is intended for engineers of any experience level.

Objectives

  • Explore various Google services that you can use to deploy an app to GKE by asking Duet AI context-based questions.
  • Prompt Duet AI to provide commands that you can use to deploy a basic app to a GKE cluster.
  • Create, explore, and modify the GKE cluster by using Duet AI to explain and generate the shell commands.

Google Cloud products used

This tutorial uses the following billable Google Cloud products. Use the Pricing Calculator to generate a cost estimate based on your projected usage.

  • GKE. GKE is a managed Kubernetes service that lets you deploy and manage containerized applications at scale. For pricing information, see GKE pricing.

  • Duet AI. Duet AI is an always-on collaborator in Google Cloud that offers generative AI-powered assistance to a wide range of users, including developers and data scientists. To provide an integrated assistance experience, Duet AI is embedded in many Google Cloud products.

Before you begin

  1. Enable the GKE API.
  2. Ensure that Duet AI is set up for your Google Cloud user account and project.

Explore Kubernetes in Google Cloud

For the following example, consider that you're an infrastructure administrator who is responsible for setting up infrastructure for a team developing a web application. The organization at large has standardized using containers and Kubernetes, so the team wants to understand how to run their web application on Kubernetes in Google Cloud. The team also wants as little infrastructure management overhead as possible.

In the Google Cloud console, you can chat with Duet AI to get help. Using the Duet AI pane, you enter prompts, which are questions or statements that describe the help you want, and Duet AI returns responses. Duet AI doesn't use your prompts or its responses as data to train its model. For more information, see How Duet AI in Google Cloud uses your data.

For more information on writing prompts to generate good responses, see Write better prompts for Duet AI.

To prompt Duet AI to help you decide how to run Kubernetes in Google Cloud, follow these steps:

  1. In the Google Cloud console toolbar, click chat_spark Open Duet AI.

  2. In the Duet AI pane, enter the following prompt based on your requirements and preferences, and then click Send:

    How can I run Kubernetes on Google Cloud without having to own
    management of nodes and the control plane?
    

    Duet AI's response may look like the following:

    To run Kubernetes on Google Cloud without having to own management of
    nodes and the control plane, you can use Google Kubernetes Engine
    (Google Kubernetes Engine (GKE)) in Autopilot. In Autopilot mode, Google manages the
    control plane and system components for you, and also manages your
    worker nodes.
    
  3. Enter a follow-up question. For example, if you want to standardize creation of GKE clusters in Autopilot mode using Terraform, enter the following prompt:

    Can you use Terraform to provision GKE clusters in Autopilot mode?
    

    Duet AI's response may look like the following:

    Yes, you can use Terraform to provision GKE clusters in Autopilot
    mode. Terraform is a tool that can be used to create and manage
    infrastructure on Google Cloud Platform. Find more information
    for your question here:
    https://cloud.google.com/blog/products/containers-kubernetes/partner-devops-and-security-solutions-for-gke-autopilot
    
  4. Optional: If your chat history is not relevant to what you're going to ask next, then reset the chat history: in the Duet AI pane, click the delete icon, and then select Reset chat.

Create a GKE cluster in Autopilot mode

Consider that you are unfamiliar with running Kubernetes using GKE in Autopilot mode. Before you provision a cluster for your developers, you decide to test GKE in Autopilot mode first. In this section, you will prompt Duet AI to assist you in creating and running a test web app in a GKE cluster in Autopilot mode.

  1. In the Duet AI pane, enter the following prompt, and then click Send:

    How do I create a GKE Autopilot cluster?
    

    Duet AI responds with the instructions for creating a cluster using the Google Cloud console and the Google Cloud CLI.

  2. When you see a response that includes placeholder values such as CLUSTER_NAME, REGION, and PROJECT_ID, adding that information to the prompt could lead to even more useful responses. Refine the prompt again with more detail:

    What is the command to create a GKE Autopilot cluster in my current
    project named duet-ai-demo in the us-central region using the gcloud CLI?
    

    Duet AI returns a response similar to the following:

    gcloud container clusters create-auto duet-ai-demo --region us-central1
    

    To use the command provided by Duet AI, open Cloud Shell and run the preceding gcloud command in the response.

    After a few minutes, your GKE Autopilot cluster will be ready for use.

Deploy a sample web application

Now that you have a GKE cluster in Autopilot mode created, you would like to test running a sample web application similar to the application your team will run on this infrastructure. Internally, you see the following container image from Google bookmarked for testing a containerized web application: us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0.

  1. In the Duet AI pane, enter the following prompt, and then click Send:

    What is the kubectl command to create a deployment called
    hello-server for the image us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0?
    

    Duet AI returns a response similar to:

    To create a deployment called hello-server for the image
    us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0, you
    can use the following kubectl command:
    
    kubectl create deployment hello-server --image=us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
    

    Run the preceding kubectl command in the Cloud Shell.

  2. With the web server now created, you decide to test out provisioning a load balancer in front of the web server to expose it to the internet.

    In the Duet AI pane, enter the following prompt, and then click Send:

    What is the kubectl command to expose this deployment on port 80 with
    a load balancer?
    

    Duet AI returns a response similar to the following:

    To expose the hello-server deployment on port 80 with a load
    balancer, you can use the following kubectl expose command:
    
    kubectl expose deployment hello-server \
      --type LoadBalancer \
      --port 80 \
      --target-port 8080
    

    Running this command will create a Compute Engine load balancer for your container.

    While providing more context is always helpful, notice how Duet AI was able to pull the deployment name hello-server from the conversation history without it being included in the prompt.

  3. Now you want to see if the web server is running and serving requests properly. To view your deployed application, you need to retrieve the external IP address from the Kubernetes service you created.

    Enter the following prompt:

    What is the kubectl command to see the external IP address
    associated with this load balancer?
    

    Duet AI returns a response similar to the following:

    To see the external IP associated with a load balancer, you can use
    the following kubectl command:
    
    kubectl get service hello-server -w
    
    This will output the name of the service, the type of service, the
    cluster IP address, the external IP address, and the ports that the
    service is listening on.
    

    After a few minutes, running the preceding kubectl command will generate a result similar to:

    NAME           TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)        AGE
    hello-server   LoadBalancer   34.118.227.57   35.225.15.112   80:31473/TCP   6m18s
    

    You can now access your web application running on GKE using the value returned for EXTERNAL-IP.

Plan a cluster upgrade strategy

You have now tested your team's primary use case in a GKE cluster in Autopilot mode. However, before you proceed with having the team use GKE Autopilot, you want to configure a time for cluster maintenance or upgrades.

You know that the traffic patterns of the users for your team's web application is diurnal. Given that Duet AI has communicated that GKE Autopilot handles maintenance of the cluster for you, you want to dictate that this maintenance happens outside your web application's busiest hours.

  1. Enter the following prompt:

    How can I configure periods of time where GKE Autopilot won't
    perform maintenance or upgrades on my cluster nodes?
    

    Duet AI returns a response similar to the following:

    To configure periods of time where GKE Autopilot won't perform
    maintenance or upgrades on your cluster nodes, you can use
    maintenance windows and exclusions.
    
    Maintenance windows are periods of time when GKE Autopilot is allowed
    to perform maintenance and upgrades on your cluster nodes. You can
    specify a maintenance window for your cluster when you create it, or
    you can update an existing cluster to specify a maintenance
    window.
    
    Maintenance exclusions are periods of time when GKE Autopilot is not
    allowed to perform maintenance or upgrades on your cluster nodes. You
    can create a maintenance exclusion for your cluster when you create
    it, or you can update an existing cluster to create a maintenance
    exclusion.
    
    To create a maintenance window or a maintenance exclusion, you can
    use the Google Cloud console, the gcloud CLI, or the Kubernetes
    API.
    
  2. You decide to create a daily maintenance window during your web application's off-hours using gcloud. Enter the following prompt:

    How can I create a daily maintenance window from 22:00 to 02:00 using
    `gcloud` for my GKE Autopilot cluster named duet-ai-demo?
    

    Duet AI returns a response similar to the following:

    To create a daily maintenance window using gcloud CLI for your
    duet-ai-demo GKE Autopilot cluster that goes from 22:00 to 02:00, you
    can use the following command:
    
    gcloud container clusters update duet-ai-demo \
      --maintenance-window-start 22:00 \
      --maintenance-window-end 02:00 \
      --maintenance-window-recurrence "FREQ=DAILY"
    
    This command will create a daily maintenance window that runs from
    22:00 to 02:00 UTC time zone.
    

    Running this command ensures that the infrastructure you provision for your team will undergo maintenance during the least busy times for your users. You are ready to move forward with recommending GKE Autopilot to your web application team!

Clean up

To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, you can delete the Google Cloud project that you created for this tutorial. Alternatively, you can delete the individual resources.

  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.

What's next