使用 Gemini 创建和修改 GKE 集群,以获得 Google Cloud 方面的帮助

本教程介绍了如何使用 Gemini for Google Cloud(Google Cloud 中依托 AI 技术的协作工具)在 Autopilot 中创建、测试和修改 Google Kubernetes Engine (GKE) 集群。您将了解 Gemini 如何帮助将简单的应用部署到集群并为其创建每日维护窗口。本教程面向任何经验水平的工程师。

目标

  • 通过询问 Gemini 上下文相关问题,探索可用于将应用部署到 GKE 的各种 Google 服务。
  • 提示 Gemini 提供可用于将基本应用部署到 GKE 集群的命令。
  • 使用 Gemini 说明和生成 shell 命令来创建、探索和修改 GKE 集群。

使用的 Google Cloud 产品

本教程使用以下可计费的 Google Cloud 产品。您可使用价格计算器根据您的预计使用情况来估算费用。

  • GKE。GKE 是一种代管式 Kubernetes 服务,可让您大规模部署和管理容器化应用。如需了解价格信息,请参阅 GKE 价格

  • 双子座。Gemini 是 Google Cloud 中始终在线的协作工具,为广大用户(包括开发者和数据科学家)提供由生成式 AI 赋能的协助。为了提供集成式协助体验,Gemini 已嵌入到许多 Google Cloud 产品中。

准备工作

  1. 启用 GKE API
  2. 确保已为您的 Google Cloud 用户帐号和项目设置 Gemini。

探索 Google Cloud 中的 Kubernetes

在以下示例中,假设您是一位基础架构管理员,负责为 Web 应用的开发团队设置基础架构。整个组织已经对容器和 Kubernetes 进行了标准化,因此该团队希望了解如何在 Google Cloud 中的 Kubernetes 上运行他们的 Web 应用。该团队还希望尽可能减少基础架构管理开销。

在 Google Cloud 控制台中,您可以与 Gemini 聊天以获取帮助。在 Gemini 窗格中输入提示(即描述您所需要的帮助的问题或语句),Gemini 会返回“回复”。Gemini 不会将您的提示或其回答用作数据来训练其模型。如需了解详情,请参阅 Gemini for Google Cloud 如何使用您的数据

如需详细了解如何撰写提示以生成优质回复,请参阅为 Gemini 撰写更优质的提示

如需提示 Gemini 帮助您决定如何在 Google Cloud 中运行 Kubernetes,请按以下步骤操作:

  1. 在 Google Cloud 控制台工具栏中,点击 spark 打开 Gemini

  2. Gemini 窗格中,根据您的要求和偏好设置输入以下提示,然后点击发送

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

    Gemini 的回答可能如下所示:

    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. 输入后续问题。例如,如果要使用 Terraform 在 Autopilot 模式下以标准化方式创建 GKE 集群,请输入以下提示:

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

    Gemini 的回答可能如下所示:

    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. 可选:如果您的聊天记录与您接下来要询问的内容不相关,请重置聊天记录:在 Gemini 窗格中,点击删除图标,然后选择重置聊天

在 Autopilot 模式下创建 GKE 集群

请注意,您不熟悉如何在 Autopilot 模式下使用 GKE 运行 Kubernetes。在为开发者预配集群之前,您决定先在 Autopilot 模式下测试 GKE。在本部分中,您将提示 Gemini 帮助您以 Autopilot 模式在 GKE 集群中创建和运行测试 Web 应用。

  1. Gemini 窗格中,输入以下提示,然后点击 Send

    How do I create a GKE Autopilot cluster?
    

    Gemini 会返回有关使用 Google Cloud 控制台和 Google Cloud CLI 创建集群的说明。

  2. 当您看到包含占位值(如 CLUSTER_NAMEREGIONPROJECT_ID)的响应时,将此信息添加到提示中可以得到更实用的响应。再次优化提示,提供更详细的信息:

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

    Gemini 给出的回答可能类似于以下内容:

    gcloud container clusters create-auto gemini-demo --region us-central1
    

    如需使用 Gemini 提供的命令,请打开 Cloud Shell 并在响应中运行上述 gcloud 命令。

    几分钟后,您的 GKE Autopilot 集群即准备就绪,可供使用。

部署示例 Web 应用

现在您已创建了 Autopilot 模式下的 GKE 集群,您希望测试运行的示例 Web 应用(类似于您的团队将在此基础架构上运行的应用)。在内部,您可以看到 Google 为测试容器化 Web 应用添加了书签的以下容器映像:us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0

  1. Gemini 窗格中,输入以下提示,然后点击 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?
    

    Gemini 会返回类似于以下内容的响应:

    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
    

    在 Cloud Shell 中运行上述 kubectl 命令。

  2. 创建 Web 服务器后,您决定在 Web 服务器前端预配负载均衡器以将其公开给互联网。

    Gemini 窗格中,输入以下提示,然后点击 Send

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

    Gemini 给出的回答可能类似于以下内容:

    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
    

    运行此命令将为您的容器创建一个 Compute Engine 负载均衡器。

    虽然提供更多上下文总是很有帮助,但请注意 Gemini 如何能够从对话记录中提取部署名称 hello-server,而无需在提示中包含该名称。

  3. 现在,您需要了解 Web 服务器是否正在运行并正确处理请求。如需查看已部署的应用,您需要从您创建的 Kubernetes 服务中检索外部 IP 地址。

    输入以下提示:

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

    Gemini 给出的回答可能类似于以下内容:

    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.
    

    几分钟后,运行上述 kubectl 命令会生成类似于以下内容的结果:

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

    您现在可以使用为 EXTERNAL-IP 返回的值访问在 GKE 上运行的 Web 应用。

规划集群升级策略

现在,您已经在 Autopilot 模式下在 GKE 集群中测试了团队的主要使用场景。但是,在继续让团队使用 GKE Autopilot 之前,您需要配置集群维护或升级的时间。

您知道团队 Web 应用的用户流量模式是日夜的。鉴于 Gemini 已告知 GKE Autopilot 为您处理集群的维护,您需要指示此维护发生在 Web 应用的最繁忙时段之外。

  1. 输入以下提示:

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

    Gemini 给出的回答可能类似于以下内容:

    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. 您决定使用 gcloud 在 Web 应用的关闭时间创建每日维护窗口。输入以下提示:

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

    Gemini 给出的回答可能类似于以下内容:

    To create a daily maintenance window using gcloud CLI for your
    gemini-demo GKE Autopilot cluster that goes from 22:00 to 02:00, you
    can use the following command:
    
    gcloud container clusters update gemini-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.
    

    运行此命令可确保您为团队预配的基础架构会在用户最繁忙的时段进行维护。您现在可以向 Web 应用团队推荐 GKE Autopilot 了!

清理

为避免系统因本教程中使用的资源向您的 Google Cloud 帐号收取费用,您可以删除为本教程创建的 Google Cloud 项目。或者,您也可以删除各个资源。

  1. 在 Google Cloud 控制台中,进入管理资源页面。

    转到“管理资源”

  2. 在项目列表中,选择要删除的项目,然后点击删除
  3. 在对话框中输入项目 ID,然后点击关闭以删除项目。

后续步骤