1. 简介
概览
Cloud Run 最近添加了对 GPU 的支持。此功能目前处于公开预览版阶段,需要加入等候名单才能使用。如果您有兴趣试用此功能,请填写此表单加入等候名单。Cloud Run 是 Google Cloud 上的一个容器平台,让您可以轻松在容器中运行代码,而无需管理集群。
目前,我们推出的 GPU 是具有 24 GB vRAM 的 Nvidia L4 GPU。每个 Cloud Run 实例都有一个 GPU,并且 Cloud Run 自动扩缩功能仍然适用。这包括最多扩容到 5 个实例(可申请配额增加),以及在没有请求时缩减到 0 个实例。
GPU 的一个用例是运行您自己的开放式大语言模型 (LLM)。本教程将介绍如何部署运行 LLM 的服务。
在本 Codelab 中,您将部署一个多容器服务,该服务使用 Open WebUI 作为前端入站容器,并在边车中使用 Ollama 来提供存储在 Google Cloud Storage 存储桶中的 Gemma 2 2B 模型。
学习内容
- 如何在 Cloud Run 中创建多个容器服务
- 如何将 Ollama 部署为用于提供 Gemma 2 2B 模型的边车
- 如何将 Open WebUI 部署为前端入站流量容器
2. 设置环境变量和启用 API
升级 gcloud CLI
首先,您需要安装较新版本的 gcloud CLI。您可以通过运行以下命令来更新 CLI:
gcloud components update
设置环境变量
您可以设置要在整个 Codelab 中使用的环境变量。
PROJECT_ID=<YOUR_PROJECT_ID> REGION=us-central1 gcloud config set project $PROJECT_ID
启用 API
在开始使用此 Codelab 之前,您需要先启用多个 API。此 Codelab 需要使用以下 API。您可以通过运行以下命令来启用这些 API:
gcloud services enable run.googleapis.com \ cloudbuild.googleapis.com \ storage.googleapis.com \ artifactregistry.googleapis.com
为此 Codelab 创建一个目录。
mkdir ollama-sidecar-codelab cd ollama-sidecar-codelab
3. 创建一个 GCS 存储分区来存储 Gemma 2 2B 模型
首先,您需要安装 Ollama 才能下载模型。这会将模型下载到 /home/$USER/.ollama/models
curl -fsSL https://ollama.com/install.sh | sh
现在运行 ollama
ollama serve
Ollama 开始监听端口 11434。
打开第二个终端窗口,将 Gemma 2 2B 模型拉取
ollama pull gemma2:2b
(可选)您可以从命令行与 Gemma 交互,方法是
ollama run gemma2:2b
与 Gemma 聊天结束后,你可以输入
/bye
4. 创建存储桶
现在,模型已下载完毕,您可以将其移至 GCS 存储桶。
首先,创建存储桶。
gcloud storage buckets create gs://$PROJECT_ID-gemma2-2b-codelab
现在,将模型文件夹移动到 GCS。
gsutil cp -r /home/$USER/.ollama/models gs://$PROJECT_ID-gemma2-2b-codelab
5. 创建 Ollama 图片
创建一个包含以下内容的 Dockerfile
FROM ollama/ollama # Listen on all interfaces, port 11434 ENV OLLAMA_HOST 0.0.0.0:11434 # Store model weight files in /models ENV OLLAMA_MODELS /models # Reduce logging verbosity ENV OLLAMA_DEBUG false # Never unload model weights from the GPU ENV OLLAMA_KEEP_ALIVE -1 # Store the model weights in the container image ENV MODEL gemma2:2b RUN ollama serve & sleep 5 && ollama pull $MODEL # Start Ollama ENTRYPOINT ["ollama", "serve"]
创建 Artifact Registry 代码库以存储您的服务映像。
gcloud artifacts repositories create ollama-sidecar-codelab-repo --repository-format=docker \
--location=us-central1 --description="Ollama + OpenWebUI website demo" \
--project=$PROJECT_ID
构建 ollama 边车映像
gcloud builds submit \
--tag us-central1-docker.pkg.dev/$PROJECT_ID/ollama-sidecar-codelab-repo/ollama-gemma-2b \
--machine-type e2-highcpu-32
6. 创建 Open WebUI 前端映像
在本部分中,您将使用 Open WebUI 创建前端入站容器。
使用 docker 拉取 Open WebUI 映像。
docker pull ghcr.io/open-webui/open-webui:main
然后,将 Docker 配置为使用您的 Google Cloud 凭据对 Artifact Registry 进行身份验证。这样,您就可以使用 Docker 将映像推送到 Artifact Registry 代码库。
gcloud auth configure-docker us-central1-docker.pkg.dev
为映像添加标记,然后推送到 Artifact Registry。
docker tag ghcr.io/open-webui/open-webui:main us-central1-docker.pkg.dev/$PROJECT_ID/ollama-sidecar-codelab-repo/openwebui docker push us-central1-docker.pkg.dev/$PROJECT_ID/ollama-sidecar-codelab-repo/openwebui
7. 将多容器服务部署到 Cloud Run
使用 yaml 文件部署多容器服务
使用以下内容创建 service.yaml
。
apiVersion: serving.knative.dev/v1 kind: Service metadata: name: ollama-sidecar-codelab labels: cloud.googleapis.com/location: us-central1 spec: template: metadata: annotations: autoscaling.knative.dev/maxScale: '5' run.googleapis.com/cpu-throttling: 'false' run.googleapis.com/startup-cpu-boost: 'true' run.googleapis.com/container-dependencies: '{"openwebui":["ollama-sidecar"]}' spec: containerConcurrency: 80 timeoutSeconds: 300 containers: - name: openwebui image: us-central1-docker.pkg.dev/YOUR_PROJECT_ID/ollama-sidecar-codelab/openwebui ports: - name: http1 containerPort: 8080 env: - name: OLLAMA_BASE_URL value: http://localhost:11434 - name: WEBUI_AUTH value: 'false' resources: limits: memory: 1Gi cpu: 2000m volumeMounts: - name: in-memory-1 mountPath: /app/backend/data startupProbe: timeoutSeconds: 240 periodSeconds: 240 failureThreshold: 1 tcpSocket: port: 8080 - name: ollama-sidecar image: us-central1-docker.pkg.dev/YOUR_PROJECT_ID/ollama-sidecar-codelab/ollama-gemma-2b resources: limits: cpu: '6' nvidia.com/gpu: '1' memory: 16Gi volumeMounts: - name: gcs-1 mountPath: /root/.ollama startupProbe: timeoutSeconds: 1 periodSeconds: 10 failureThreshold: 3 tcpSocket: port: 11434 volumes: - name: gcs-1 csi: driver: gcsfuse.run.googleapis.com volumeAttributes: bucketName: YOUR_PROJECT_ID-gemma2-2b-codelab - name: in-memory-1 emptyDir: medium: Memory sizeLimit: 10Gi nodeSelector: run.googleapis.com/accelerator: nvidia-l4
更新 service.yaml,将 PROJECT_ID 替换为您的项目 ID:
sed -i "s/YOUR_PROJECT_ID/${PROJECT_ID}/g" service.yaml
使用以下命令部署到 Cloud Run。
gcloud beta run services replace service.yaml
测试 Cloud Run 服务
现在,在网络浏览器中打开服务网址。
界面加载完成后,在选择模型下,选择 Gemma 2 2B。
现在,向 Gemma 提问,例如“为什么天空是蓝色的?”
8. 恭喜!
恭喜您完成此 Codelab!
建议您查看有关 Cloud Run 函数的文档
所学内容
- 如何在 Cloud Run 中创建多容器服务
- 如何将 Ollama 部署为用于提供 Gemma 2 2B 模型的边车
- 如何将 Open WebUI 部署为前端入站流量容器
9. 清理
为避免意外产生费用(例如,如果 Cloud Run 服务的意外调用次数超过了免费层级的 Cloud Run 调用配额),您可以删除 Cloud Run 或删除您在第 2 步中创建的项目。
如需删除 Cloud Run 函数,请前往 https://console.cloud.google.com/run 前往 Cloud Run Cloud 控制台,然后删除 ollama-sidecar-codelab
服务。
如果您选择删除整个项目,可以前往 https://console.cloud.google.com/cloud-resource-manager,选择您在第 2 步中创建的项目,然后选择“删除”。如果删除项目,则需要在 Cloud SDK 中更改项目。您可以通过运行 gcloud projects list
来查看所有可用项目的列表。