1. 簡介
總覽
Cloud Run 最近新增了 GPU 支援功能。目前僅開放公開測試,且須先加入候補名單。如有興趣試用這項功能,請填寫這份表單,加入候補名單。Cloud Run 是 Google Cloud 的容器平台,可讓您輕鬆在容器中執行程式碼,不必管理叢集。
目前我們提供的 GPU 是 Nvidia L4 GPU,具有 24 GB 的 vRAM。每個 Cloud Run 執行個體都有一個 GPU,且 Cloud Run 自動調度資源功能仍適用。包括最多擴充至 5 個執行個體 (可申請提高配額),以及在沒有任何要求時縮減至零個執行個體。
GPU 的用途之一是執行您自己的開放式大型語言模型 (LLM)。本教學課程會逐步引導您部署執行 LLM 的服務。
在本程式碼研究室中,您將部署多容器服務,使用 Open WebUI 做為前端傳入容器,並在 Sidecar 中使用 Ollama,提供儲存在 Google Cloud Storage 值區中的 Gemma 2 2B 模型。
課程內容
- 如何在 Cloud Run 中建立多容器服務
- 如何將 Ollama 部署為 Sidecar,提供 Gemma 2 2B 模型
- 如何將 Open WebUI 部署為前端 Ingress 容器
2. 設定環境變數並啟用 API
升級 gcloud CLI
首先,您必須安裝最新版的 gcloud CLI。如要更新 CLI,請執行下列指令:
gcloud components update
設定環境變數
您可以設定環境變數,供本程式碼研究室使用。
PROJECT_ID=<YOUR_PROJECT_ID> REGION=us-central1 gcloud config set project $PROJECT_ID
啟用 API
開始進行本程式碼研究室之前,請先啟用數個 API。本程式碼研究室需要使用下列 API。執行下列指令即可啟用這些 API:
gcloud services enable run.googleapis.com \
cloudbuild.googleapis.com \
storage.googleapis.com \
artifactregistry.googleapis.com
為本程式碼研究室建立目錄。
mkdir ollama-sidecar-codelab cd ollama-sidecar-codelab
3. 建立 GCS bucket 來儲存 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 值區。
首先,請建立 bucket。
gcloud storage buckets create gs://$PROJECT_ID-gemma2-2b-codelab
現在,請將 models 資料夾移至 GCS。
gsutil cp -r /home/$USER/.ollama/models gs://$PROJECT_ID-gemma2-2b-codelab
5. 建立 Ollama 映像檔
建立含有以下內容的 Dockerfile
FROM --platform=linux/amd64 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
建立 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 Sidecar 映像檔
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 建立前端 Ingress 容器。
使用 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
env:
- name: OLLAMA_MODELS
value: /root/.ollama/models
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. 恭喜!
恭喜您完成本程式碼研究室!
建議您參閱 Cloud Run 函式說明文件
涵蓋內容
- 如何在 Cloud Run 中建立多容器服務
- 如何將 Ollama 部署為 Sidecar,提供 Gemma 2 2B 模型
- 如何將 Open WebUI 部署為前端 Ingress 容器
9. 清除所用資源
為避免產生意外費用 (例如,Cloud Run 服務意外叫用次數超過免費層級的每月 Cloud Run 叫用配額),您可以刪除 Cloud Run 或在步驟 2 中建立的專案。
如要刪除 Cloud Run 函式,請前往 Cloud Run Cloud 控制台 (https://console.cloud.google.com/run),然後刪除 ollama-sidecar-codelab 服務。
如要刪除整個專案,請前往 https://console.cloud.google.com/cloud-resource-manager,選取您在步驟 2 中建立的專案,然後選擇「刪除」。刪除專案後,您必須在 Cloud SDK 中變更專案。如要查看所有可用專案的清單,請執行 gcloud projects list。