在 Google Cloud 专用 Gemini 的帮助下创建和修改 GKE 集群

本教程介绍如何使用 Google Cloud 专用 Gemini(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。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 不会将您的提示或回答用作数据来训练其模型。如需了解详情,请参阅 Google Cloud 专用 Gemini 如何使用您的数据

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

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

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

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

    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 帮助您在 GKE 集群中以 Autopilot 模式创建和运行测试 Web 应用。

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

    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 窗格中,输入以下提示,然后点击发送

    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 窗格中,输入以下提示,然后点击发送

    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,然后点击关闭以删除项目。

后续步骤