本教學課程說明如何在 Google Kubernetes Engine (GKE) 上部署 Weaviate 向量資料庫叢集。
Weaviate 是開放原始碼的向量資料庫,具有低延遲效能,且基本支援文字和圖片等不同媒體類型。支援語意搜尋、問答和分類。Weaviate 完全以 Go 語言建構,可儲存物件和向量,因此能使用向量搜尋、關鍵字搜尋,以及兩者結合的混合型搜尋。從基礎架構的角度來看,Weaviate 是雲端原生且具有容錯能力的資料庫。無領導者架構可提供容錯能力,因為資料庫叢集的每個節點都能處理讀取和寫入要求,因此不會出現單點故障。
本教學課程的適用對象為有興趣在 GKE 上部署向量資料庫叢集的雲端平台管理員和架構師、機器學習工程師,以及 MLOps (DevOps) 專業人員。
優點
Weaviate 具有下列優點:
- 提供各種程式設計語言的程式庫和開放式 API,可與其他服務整合。
- 水平調度資源。
- 在成本效益和查詢速度之間取得平衡,尤其是在處理大型資料集時。您可以選擇要將多少資料儲存在記憶體,多少資料儲存在磁碟。
目標
在本教學課程中,您將瞭解以下內容:
- 規劃及部署 Weaviate 的 GKE 基礎架構。
- 在 GKE 叢集中部署及設定 Weaviate 資料庫。
- 執行 Notebook,在資料庫中生成及儲存範例向量嵌入項目,並執行以向量為準的搜尋查詢。
費用
在本文件中,您會使用 Google Cloud的下列計費元件:
如要根據預測用量估算費用,請使用 Pricing Calculator。
完成本文所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱清除所用資源一節。
事前準備
在本教學課程中,您將使用 Cloud Shell 執行指令。Cloud Shell 是殼層環境,用於管理 Google Cloud上託管的資源。這個環境已預先安裝 Google Cloud CLI、kubectl、Helm 和 Terraform 指令列工具。如果您未使用 Cloud Shell,則必須安裝 Google Cloud CLI。
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Resource Manager, Compute Engine, GKE, and IAM Service Account Credentials APIs:
gcloud services enable cloudresourcemanager.googleapis.com
compute.googleapis.com container.googleapis.com iamcredentials.googleapis.com -
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Resource Manager, Compute Engine, GKE, and IAM Service Account Credentials APIs:
gcloud services enable cloudresourcemanager.googleapis.com
compute.googleapis.com container.googleapis.com iamcredentials.googleapis.com -
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/compute.securityAdmin, roles/compute.viewer, roles/container.clusterAdmin, roles/container.admin, roles/iam.serviceAccountAdmin, roles/iam.serviceAccountUser, roles/monitoring.viewer
gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
- Replace
PROJECT_ID
with your project ID. -
Replace
USER_IDENTIFIER
with the identifier for your user account. For example,user:myemail@example.com
. - Replace
ROLE
with each individual role.
- Replace
設定環境
如要使用 Cloud Shell 設定環境,請按照下列步驟操作:
為專案、區域和 Kubernetes 叢集資源前置字串設定環境變數:
export PROJECT_ID=PROJECT_ID export KUBERNETES_CLUSTER_PREFIX=weaviate export REGION=us-central1
將
PROJECT_ID
替換為專案 ID。 Google Cloud本教學課程使用
us-central1
地區建立部署資源。檢查 Helm 版本:
helm version
如果版本舊於 3.13,請更新版本:
curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
從 GitHub 複製程式碼範例存放區:
git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples
請前往
weaviate
目錄:cd kubernetes-engine-samples/databases/weaviate
建立叢集基礎架構
在本節中,您將執行 Terraform 指令碼,建立高可用性的地區性私人 GKE 叢集,以部署 Weaviate 資料庫。
您可以選擇使用標準或 Autopilot 叢集部署 Weaviate。各有優點,計費模式也不同。
Autopilot
下圖顯示部署在專案中的 Autopilot GKE 叢集。
如要部署叢集基礎架構,請在 Cloud Shell 中執行下列指令:
export GOOGLE_OAUTH_ACCESS_TOKEN=$(gcloud auth print-access-token)
terraform -chdir=terraform/gke-autopilot init
terraform -chdir=terraform/gke-autopilot apply \
-var project_id=${PROJECT_ID} \
-var region=${REGION} \
-var cluster_prefix=${KUBERNETES_CLUSTER_PREFIX}
GKE 會在執行階段替換下列變數:
GOOGLE_OAUTH_ACCESS_TOKEN
會使用gcloud auth print-access-token
指令擷取存取權杖,驗證與各種 Google Cloud API 的互動PROJECT_ID
、REGION
和KUBERNETES_CLUSTER_PREFIX
是在「設定環境」一節中定義的環境變數,並指派給您要建立的 Autopilot 叢集的新相關變數。
系統顯示提示訊息時,請輸入 yes
。
輸出結果會與下列內容相似:
...
Apply complete! Resources: 9 added, 0 changed, 0 destroyed.
Outputs:
kubectl_connection_command = "gcloud container clusters get-credentials weaviate-cluster --region us-central1"
Terraform 會建立下列資源:
- Kubernetes 節點的自訂虛擬私有雲網路和私有子網路。
- 透過網路位址轉譯 (NAT) 存取網際網路的 Cloud Router。
us-central1
地區的私人 GKE 叢集。- 具有叢集記錄和監控權限的
ServiceAccount
。 - Google Cloud Managed Service for Prometheus 設定,用於叢集監控和快訊。
標準
下圖顯示部署在三個不同區域的標準私人區域 GKE 叢集。
如要部署叢集基礎架構,請在 Cloud Shell 中執行下列指令:
export GOOGLE_OAUTH_ACCESS_TOKEN=$(gcloud auth print-access-token)
terraform -chdir=terraform/gke-standard init
terraform -chdir=terraform/gke-standard apply \
-var project_id=${PROJECT_ID} \
-var region=${REGION} \
-var cluster_prefix=${KUBERNETES_CLUSTER_PREFIX}
GKE 會在執行階段替換下列變數:
GOOGLE_OAUTH_ACCESS_TOKEN
會使用gcloud auth print-access-token
指令擷取存取權杖,驗證與各種 Google Cloud API 的互動。PROJECT_ID
、REGION
和KUBERNETES_CLUSTER_PREFIX
是在「設定環境」一節中定義的環境變數,並指派給您要建立的 Standard 叢集的新相關變數。
系統顯示提示訊息時,請輸入 yes
。這些指令可能需要幾分鐘才能完成,叢集也會在幾分鐘後顯示就緒狀態。
輸出結果會與下列內容相似:
...
Apply complete! Resources: 10 added, 0 changed, 0 destroyed.
Outputs:
kubectl_connection_command = "gcloud container clusters get-credentials weaviate-cluster --region us-central1"
Terraform 會建立下列資源:
- Kubernetes 節點的自訂虛擬私有雲網路和私有子網路。
- 透過網路位址轉譯 (NAT) 存取網際網路的 Cloud Router。
us-central1
地區中已啟用自動調度的私人 GKE 叢集 (每個區域有一到兩個節點)。- 具有叢集記錄和監控權限的
ServiceAccount
。 - Google Cloud Managed Service for Prometheus 設定,用於叢集監控和快訊。
連線至叢集
設定 kubectl
,以擷取憑證並與新的 GKE 叢集通訊:
gcloud container clusters get-credentials \
${KUBERNETES_CLUSTER_PREFIX}-cluster --region ${REGION}
將 Weaviate 資料庫部署至叢集
如要使用 Helm 圖表將 Weaviate 資料庫部署至 GKE 叢集,請按照下列步驟操作:
如要在 GKE 叢集上部署 Weaviate 資料庫,請先新增 Helm Chart 存放區:
helm repo add weaviate https://weaviate.github.io/weaviate-helm
為資料庫建立
weaviate
命名空間:kubectl create ns weaviate
建立密鑰來儲存 API 金鑰:
kubectl create secret generic apikeys --from-literal=AUTHENTICATION_APIKEY_ALLOWED_KEYS=$(openssl rand -base64 32) -n weaviate
部署內部負載平衡器,從虛擬網路內存取 Weaviate:
kubectl apply -n weaviate -f manifests/05-ilb/ilb.yaml
ilb.yaml
資訊清單說明負載平衡器服務:套用資訊清單來部署 Weaviate 叢集:
helm upgrade --install "weaviate" weaviate/weaviate \ --namespace "weaviate" \ --values ./manifests/01-basic-cluster/weaviate_cluster.yaml
weaviate_cluster.yaml
資訊清單會說明 Deployment。 Deployment 是 Kubernetes API 物件,可讓您執行多個 Pod 副本,並將這些副本分散到叢集中的節點:等待幾分鐘,Weaviate 叢集就會完全啟動。
檢查部署狀態:
kubectl get weaviate -n weaviate --watch
如果成功部署
weaviate
資料庫,輸出內容會類似以下內容:NAME: weaviate LAST DEPLOYED: Tue Jun 18 13:15:53 2024 NAMESPACE: weaviate STATUS: deployed REVISION: 1 TEST SUITE: None
等待 Kubernetes 啟動資源:
kubectl wait pods -l app.kubernetes.io/name=weaviate --for condition=Ready --timeout=300s -n weaviate
使用 Vertex AI Colab Enterprise 筆記本執行查詢
本節說明如何使用 Colab Enterprise 連線至 Weaviate 資料庫。您可以透過專屬的執行階段範本部署至 weaviate-vpc
,讓 Notebook 與 GKE 叢集中的資源通訊。
如要進一步瞭解 Vertex AI Colab Enterprise,請參閱 Colab Enterprise 說明文件。
建立執行階段範本
如要建立 Colab Enterprise 執行階段範本,請按照下列步驟操作:
在 Google Cloud 控制台中,前往 Colab Enterprise 的「Runtime Templates」(執行階段範本) 頁面,並確認已選取專案:
按一下「新增範本」add_box。「建立新的執行階段範本」頁面隨即顯示。
在「執行階段基本資訊」部分:
- 在「Display name」(顯示名稱) 欄位中,輸入
weaviate-connect
。 - 在「Region」(區域) 下拉式清單中選取「
us-central1
」。與 GKE 叢集位於相同地區。
- 在「Display name」(顯示名稱) 欄位中,輸入
在「設定運算」部分:
- 在「Machine type」(機器類型) 下拉式清單中,選取「
e2-standard-2
」。 - 在「Disk size」(磁碟大小) 欄位中,輸入
30
。
- 在「Machine type」(機器類型) 下拉式清單中,選取「
在「網路與安全性」部分中:
- 在「Network」(網路) 下拉式選單中,選取 GKE 叢集所在的網路。
- 在「Subnetwork」(子網路) 下拉式清單中,選取對應的子網路。
- 取消勾選「啟用公開網際網路存取權」核取方塊。
按一下「建立」,完成建立執行階段範本。執行階段範本會顯示在「執行階段範本」分頁的清單中。
建立執行階段
如要建立 Colab Enterprise 執行階段,請按照下列步驟操作:
在剛建立的範本執行階段範本清單中,按一下「動作」欄中的 more_vert,然後點選「建立執行階段」。系統隨即會顯示「Create Vertex AI Runtime」(建立 Vertex AI 執行階段) 窗格。
如要根據範本建立執行階段,請按一下「建立」。
在開啟的「執行階段」分頁中,等待狀態轉換為「正常」。
匯入筆記本
如要在 Colab Enterprise 中匯入筆記本,請按照下列步驟操作:
前往「我的筆記本」分頁,然後按一下「匯入」。「匯入筆記本」窗格隨即顯示。
在「匯入來源」中,選取「網址」。
在「筆記本網址」下方,輸入下列連結:
https://raw.githubusercontent.com/GoogleCloudPlatform/kubernetes-engine-samples/main/databases/weaviate/manifests/02-notebook/vector-database.ipynb
按一下「匯入」。
連線至執行階段並執行查詢
如要連線至執行階段並執行查詢,請按照下列步驟操作:
在筆記本中,按一下「連線」按鈕旁邊的 arrow_drop_down「其他連線選項」。 系統隨即會顯示「Connect to Vertex AI Runtime」(連線至 Vertex AI 執行階段) 窗格。
選取「連線到執行階段」,然後選取「連線至現有的執行階段」。
選取啟動的執行階段,然後按一下「連線」。
如要執行筆記本儲存格,請按一下每個程式碼儲存格旁的「Run cell」(執行儲存格) 按鈕
。
筆記本包含程式碼儲存格和說明每個程式碼區塊的文字。執行程式碼儲存格會執行其指令並顯示輸出內容。您可以依序執行儲存格,也可以視需要執行個別儲存格。
查看叢集的 Prometheus 指標
GKE 叢集已設定 Google Cloud Managed Service for Prometheus,可收集 Prometheus 格式的指標。這項服務提供全代管的監控和快訊解決方案,可收集、儲存及分析叢集和應用程式的指標。
下圖顯示 Prometheus 如何收集叢集的指標:
圖中的 GKE 私人叢集包含下列元件:
- Weaviate Pod,會在路徑
/metrics
和通訊埠2112
上公開指標。 - 以 Prometheus 為基礎的收集器,可處理來自 Weaviate Pod 的指標。
- 將指標傳送至 Cloud Monitoring 的 PodMonitoring 資源。
如要匯出及查看指標,請按照下列步驟操作:
建立
PodMonitoring
資源,以透過labelSelector
抓取指標:kubectl apply -n weaviate -f manifests/03-prometheus-metrics/pod-monitoring.yaml
pod-monitoring.yaml
資訊清單說明PodMonitoring
資源:如要匯入自訂 Cloud Monitoring 資訊主頁,並使用
dashboard.json
中定義的設定,請按照下列步驟操作:gcloud --project "${PROJECT_ID}" monitoring dashboards create --config-from-file monitoring/dashboard.json
從資訊主頁清單中開啟
Weaviate Overview
資訊主頁。系統可能需要一些時間才能收集及顯示指標。資訊主頁會顯示分片數量、向量數量和作業延遲時間
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。
刪除專案
如要避免付費,最簡單的方法就是刪除您為本教學課程建立的專案。
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
如果您已刪除專案,則清理作業完成。如果沒有刪除專案,請繼續刪除個別資源。
刪除個別資源
設定環境變數。
export PROJECT_ID=${PROJECT_ID} export KUBERNETES_CLUSTER_PREFIX=weaviate export REGION=us-central1
執行
terraform destroy
指令:export GOOGLE_OAUTH_ACCESS_TOKEN=$(gcloud auth print-access-token) terraform -chdir=terraform/FOLDER destroy \ -var project_id=${PROJECT_ID} \ -var region=${REGION} \ -var cluster_prefix=${KUBERNETES_CLUSTER_PREFIX}
根據您建立的 GKE 叢集類型,將
FOLDER
替換為gke-autopilot
或gke-standard
。系統顯示提示訊息時,請輸入
yes
。找出所有未連接的磁碟:
export disk_list=$(gcloud compute disks list --filter="-users:* AND labels.name=${KUBERNETES_CLUSTER_PREFIX}-cluster" --format "value[separator=|](name,region)")
刪除磁碟:
for i in $disk_list; do disk_name=$(echo $i| cut -d'|' -f1) disk_region=$(echo $i| cut -d'|' -f2|sed 's|.*/||') echo "Deleting $disk_name" gcloud compute disks delete $disk_name --region $disk_region --quiet done
刪除 GitHub 存放區:
rm -r ~/kubernetes-engine-samples/