Deploy to Google Kubernetes Engine (GKE)¶
部署到 Google Kubernetes Engine (GKE)¶
GKE is Google Cloud managed Kubernetes service. It allows you to deploy and manage containerized applications using Kubernetes.
GKE 是 Google Cloud 托管的 Kubernetes 服务。它允许您使用 Kubernetes 部署和管理容器化应用程序。
To deploy your agent you will need to have a Kubernetes cluster running on GKE. You can create a cluster using the Google Cloud Console or the gcloud command line tool.
要部署您的智能体,您需要在 GKE 上运行一个 Kubernetes 集群。您可以使用 Google Cloud Console 或 gcloud 命令行工具创建集群。
In this example we will deploy a simple agent to GKE. The agent will be a FastAPI application that uses Gemini 2.0 Flash as the LLM. We can use Vertex AI or AI Studio as the LLM provider using the Environment variable GOOGLE_GENAI_USE_VERTEXAI.
在此示例中,我们将向 GKE 部署一个简单的智能体。该智能体将是一个使用 Gemini 2.0 Flash 作为 LLM 的 FastAPI 应用程序。我们可以使用 Vertex AI 或 AI Studio 作为 LLM 提供程序,使用环境变量 GOOGLE_GENAI_USE_VERTEXAI。
Environment variables¶
环境变量¶
Set your environment variables as described in the Setup and Installation guide. You also need to install the kubectl command line tool. You can find instructions to do so in the Google Kubernetes Engine Documentation.
按照设置和安装指南中的描述设置您的环境变量。您还需要安装 kubectl 命令行工具。您可以在 Google Kubernetes Engine 文档中找到相关说明。
export GOOGLE_CLOUD_PROJECT=your-project-id # Your GCP project ID
# 您的 GCP 项目 ID
export GOOGLE_CLOUD_LOCATION=us-central1 # Or your preferred location
# 或您的首选位置
export GOOGLE_GENAI_USE_VERTEXAI=true # Set to true if using Vertex AI
# 如果使用 Vertex AI,则设置为 true
export GOOGLE_CLOUD_PROJECT_NUMBER=$(gcloud projects describe --format json $GOOGLE_CLOUD_PROJECT | jq -r ".projectNumber")
If you don't have jq installed, you can use the following command to get the project number:
如果您没有安装 jq,可以使用以下命令获取项目编号:
And copy the project number from the output.
然后从输出中复制项目编号。
Enable APIs and Permissions¶
启用 API 和权限¶
Ensure you have authenticated with Google Cloud (gcloud auth login and gcloud config set project <your-project-id>).
确保您已向 Google Cloud 进行身份验证 (gcloud auth login 和 gcloud config set project <your-project-id>)。
Enable the necessary APIs for your project. You can do this using the gcloud command line tool.
为您的项目启用必要的 API。您可以使用 gcloud 命令行工具执行此操作。
gcloud services enable \
container.googleapis.com \
artifactregistry.googleapis.com \
cloudbuild.googleapis.com \
aiplatform.googleapis.com
Create a GKE Cluster¶
创建 GKE 集群¶
If you don't already have a GKE cluster, create one:
如果您还没有 GKE 集群,请创建一个:
# Create a GKE cluster
# 创建 GKE 集群
gcloud container clusters create adk-agent-cluster \
--region $GOOGLE_CLOUD_LOCATION \
--num-nodes 3 \
--machine-type e2-medium \
--scopes="https://www.googleapis.com/auth/cloud-platform"
Get the cluster credentials:
获取集群凭据:
# Get credentials for the cluster
# 获取集群的凭据
gcloud container clusters get-credentials adk-agent-cluster \
--region $GOOGLE_CLOUD_LOCATION
Prepare your Agent for Deployment¶
准备您的智能体以进行部署¶
Your agent should be structured as follows:
您的智能体应按以下方式结构化:
Your agent directory should contain:
您的智能体目录应包含:
- agent.py (your agent code)
- agent.py (您的智能体代码)
- __init__.py (package initialization)
- __init__.py (包初始化)
- requirements.txt (dependencies)
- requirements.txt (依赖项)
- Dockerfile (container build instructions)
- Dockerfile (容器构建说明)
Example Dockerfile:
示例 Dockerfile:
FROM python:3.10-slim
WORKDIR /app
# Copy requirements first for better caching
# 首先复制要求以便更好地缓存
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy the application code
# 复制应用程序代码
COPY . .
# Expose port for FastAPI
# 公开 FastAPI 的端口
EXPOSE 8080
# Run the application
# 运行应用程序
CMD ["uvicorn", "agent:root_agent", "--host", "0.0.0.0", "--port", "8080"]
Build and Push Container Image¶
构建并推送容器映像¶
Create a Docker repository in Google Artifact Registry:
在 Google Artifact Registry 中创建 Docker 仓库:
# Create a Docker repository
# 创建 Docker 仓库
gcloud artifacts repositories create adk-agent-repo \
--repository-format=docker \
--location=$GOOGLE_CLOUD_LOCATION \
--description="Repository for ADK agent images"
Build and push the container image:
构建并推送容器映像:
# Build the Docker image
# 构建 Docker 映像
docker build -t gcr.io/$GOOGLE_CLOUD_PROJECT/adk-agent-repo/capital-agent:latest .
# Push the image to Artifact Registry
# 将映像推送到 Artifact Registry
docker push gcr.io/$GOOGLE_CLOUD_PROJECT/adk-agent-repo/capital-agent:latest
Create Kubernetes Manifests¶
创建 Kubernetes 清单¶
Create a deployment manifest deployment.yaml:
创建部署清单 deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: capital-agent
spec:
replicas: 2
selector:
matchLabels:
app: capital-agent
template:
metadata:
labels:
app: capital-agent
spec:
containers:
- name: capital-agent
image: gcr.io/$GOOGLE_CLOUD_PROJECT/adk-agent-repo/capital-agent:latest
ports:
- containerPort: 8080
env:
- name: GOOGLE_CLOUD_PROJECT
value: "your-project-id"
- name: GOOGLE_CLOUD_LOCATION
value: "us-central1"
- name: GOOGLE_GENAI_USE_VERTEXAI
value: "true"
resources:
requests:
cpu: "500m"
memory: "512Mi"
limits:
cpu: "1000m"
memory: "1Gi"
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
Create a service manifest service.yaml:
创建服务清单 service.yaml:
apiVersion: v1
kind: Service
metadata:
name: capital-agent
spec:
type: LoadBalancer
selector:
app: capital-agent
ports:
- protocol: TCP
port: 80
targetPort: 8080
Deploy to GKE¶
部署到 GKE¶
Apply the Kubernetes manifests:
应用 Kubernetes 清单:
# Create the deployment
# 创建部署
kubectl apply -f deployment.yaml
# Create the service
# 创建服务
kubectl apply -f service.yaml
# Check the status
# 检查状态
kubectl get pods
kubectl get service capital-agent
Access your Deployed Agent¶
访问您部署的智能体¶
Get the external IP of your service:
获取服务的外部 IP:
Wait for the EXTERNAL-IP to be assigned (this may take a few minutes).
等待分配 EXTERNAL-IP (这可能需要几分钟)。
Once you have the external IP, you can access your agent at:
一旦您拥有外部 IP,就可以在以下位置访问您的智能体:
Monitoring and Scaling¶
监控和扩展¶
You can monitor your GKE deployment using:
您可以使用以下方法监控您的 GKE 部署:
# View logs
# 查看日志
kubectl logs -f deployment/capital-agent
# Scale the deployment
# 扩展部署
kubectl scale deployment capital-agent --replicas=5
# View pod status
# 查看 pod 状态
kubectl get pods -l app=capital-agent
Cleaning Up¶
清理¶
To delete your deployment and resources:
要删除您的部署和资源: