本教程将介绍如何使用 Google Cloud 专用 Gemini,由 AI 赋能 创建、测试和修改 Autopilot 中的 Google Kubernetes Engine (GKE) 集群。您将看到 Gemini 可以帮助您向集群部署一个简单的应用, 应用的每日维护窗口本辅导手册适用于 经验丰富。
目标
- 探索可用来部署应用的各种 Google 服务 向 Gemini 询问基于上下文的问题,即可构建 GKE。
- 提示 Gemini 提供可用于部署基础模型的命令, 部署到 GKE 集群
- 使用 Gemini 创建、探索和修改 GKE 集群 并生成 shell 命令。
使用的 Google Cloud 产品
本教程使用以下可计费的 Google Cloud 产品。您可使用价格计算器根据您的预计使用情况来估算费用。
GKE。GKE 是一种代管式 Kubernetes 服务 大规模管理容器化应用有关价格信息,请参阅 GKE 价格。
Gemini。Gemini 是 Google Cloud 中始终在线的协作工具, 为广大用户提供依托生成式 AI 的辅助服务,包括 开发者和数据科学家提供综合协助 Gemini 已嵌入许多 Google Cloud 产品中。
准备工作
探索 Google Cloud 中的 Kubernetes
在以下示例中,假设您是一位基础架构管理员, 负责为 Web 应用。整个组织都使用容器和 因此该团队想了解如何在 Google Web 应用上的 在 Google Cloud 中使用 Kubernetes。该团队还希望能尽量减少基础架构负载, 尽可能降低管理开销
在 Google Cloud 控制台中,您可以与 Gemini 聊天以获取帮助。使用 Gemini 窗格,请在其中输入提示,也就是问题或陈述, 描述你需要的帮助,Gemini 会返回回答。Gemini 不使用 作为数据来训练模型。如需更多信息 请参阅 Google Cloud 专用 Gemini 如何使用您的数据。
如需详细了解如何撰写提示以生成优质回答,请参阅 针对 Gemini 撰写更好的提示。
为了让 Gemini 帮您决定如何在 Google Cloud 中运行 Kubernetes, 请按以下步骤操作:
在 Google Cloud 控制台工具栏中,点击 spark 打开 Gemini。
在 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.
输入后续问题。例如,如果您希望将 使用 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
可选:如果你的聊天记录与你要询问的内容不相关 然后重置聊天记录:在 Gemini 窗格中,点击 删除图标, 然后选择重置聊天。
在 Autopilot 模式下创建 GKE 集群
请注意,您不熟悉如何在 Autopilot 中使用 GKE 运行 Kubernetes 模式。在为开发者预配集群之前,您决定测试 GKE 在 Autopilot 模式下运行。在本部分中,您需要提示 Gemini 为您提供帮助 如何在 GKE 集群中以 Autopilot 模式创建和运行测试 Web 应用。
在 Gemini 窗格中,输入以下提示,然后点击 发送:
How do I create a GKE Autopilot cluster?
Gemini 会提供有关如何使用 Google Cloud 控制台和 Google Cloud CLI。
当您看到包含占位值(例如 CLUSTER_NAME、REGION 和 PROJECT_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 应用(与您的团队将要的应用类似的应用)
在该基础架构上运行在内部,您会看到以下容器映像
的以下 Web 应用:
us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
。
在 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
命令。现在创建了 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
” 历史记录。现在,您想要查看 Web 服务器是否正在运行和处理请求 正确。要查看已部署的应用,您需要检索 外部 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 返回。
规划集群升级策略
您现已在 Autopilot 中的 GKE 集群中测试了团队的主要用例 模式。不过,在继续让团队使用 GKE Autopilot 之前, 配置集群维护或升级时间。
您知道团队的 Web 应用用户流量模式 是白天的鉴于 Gemini 已经告知 GKE Autopilot 会处理 指定此维护操作 发生在 Web 应用的繁忙时段以外。
输入以下提示:
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.
您决定在网络运行期间创建一个每日维护窗口 使用
gcloud
设置应用关闭时间。输入以下提示: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 账号产生费用 则可以删除为 Cloud Storage 存储分区创建的 Google Cloud 项目, 本教程。或者,您也可以删除各个资源。
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
后续步骤
- 已读 Google Cloud 专用 Gemini 概览。
- 了解 Gemini 配额和限制。
- 了解 Gemini 的发布位置。