本教學課程說明如何使用 MySQL 資料庫,在 Google Kubernetes Engine (GKE) 上設定單一備用資源 WordPress 部署項目。請使用會提供代管版本 MySQL 的 Cloud SQL,勿自行安裝 MySQL。WordPress 使用 PersistentVolumes
(PV) 和 PersistentVolumeClaims
(PVC) 儲存資料。
PV 代表叢集中的儲存空間磁碟區,由管理員佈建,或由 Kubernetes 動態佈建,以滿足 PVC 中提出的要求。PVC 是使用者提出的特定儲存空間類別儲存空間要求,可由 PV 滿足此要求。PV 和 PVC 與 Pod 生命週期無關,可以在重新啟動、重新排程或甚至是刪除 Pod 時保存資料。WordPress 使用永久磁碟做為支援 PV 的儲存空間。
背景
WordPress 是一種網誌工具,使用關聯式資料庫儲存網誌文章及其相關物件和中繼資料,並使用本機檔案系統儲存資產,例如網誌文章中的圖片。本教學課程使用 Docker Hub 中的正式 WordPress Docker 映像檔。
一般而言,容器的根檔案系統不適合儲存永久資料。您在 GKE 中執行的容器通常是可以自由使用的實體,叢集管理員可以刪除、移除或重新排程任何因節點失敗或其他原因而無法使用的容器。節點故障時,儲存至容器根檔案系統的所有資料均會遺失。
使用永久磁碟支援的 PV,可將 WordPress 平台資料儲存在容器以外。這樣一來,即使容器遭到刪除,其資料仍會持續存在。使用預設儲存空間類別時,如果 Pod 重新排程至其他節點,永久磁碟 (以及資料) 不會隨之移動。處理資料遷移作業的方法有很多種,但不在本教學課程的討論範圍內。詳情請參閱「使用永久磁碟的永久磁碟區」。
WordPress 需要 PV 才能儲存資料。在本教學課程中,您將使用預設儲存空間級別,以動態方式建立永久磁碟,並為部署作業建立 PVC。
目標
- 建立 GKE 叢集。
- 建立由永久磁碟支援的 PV 和 PVC。
- 建立 MySQL 適用的 Cloud SQL 執行個體。
- 部署 WordPress。
- 設定 WordPress 網誌。
費用
在本文件中,您會使用 Google Cloud的下列計費元件:
如要根據預測用量估算費用,請使用 Pricing Calculator。
完成本文所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱清除所用資源一節。
事前準備
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, activate Cloud Shell.
At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.
- 在 Cloud Shell 中,啟用 GKE 和 Cloud SQL Admin API:
gcloud services enable container.googleapis.com sqladmin.googleapis.com
正在設定環境
在 Cloud Shell 中,設定 Google Cloud CLI 的預設區域:
gcloud config set compute/region region
更改下列內容:
region
:選擇距離您最近的區域。 詳情請參閱地區和區域一文。
將
PROJECT_ID
環境變數設為您的Google Cloud 專案 ID (project-id)。export PROJECT_ID=project-id
從 GitHub 存放區下載應用程式資訊清單檔案:
git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples
切換至含有
wordpress-persistent-disks
檔案的目錄:cd kubernetes-engine-samples/quickstarts/wordpress-persistent-disks
設定
WORKING_DIR
環境變數:WORKING_DIR=$(pwd)
在本教學課程中,您將使用 YAML 格式的資訊清單檔案建立 Kubernetes 物件。
建立 GKE 叢集
您將建立 GKE 叢集,以託管 WordPress 應用程式容器。
在 Cloud Shell 中,建立名為
persistent-disk-tutorial
的 GKE 叢集:CLUSTER_NAME=persistent-disk-tutorial gcloud container clusters create-auto $CLUSTER_NAME
建立完成後,請連線至新叢集:
gcloud container clusters get-credentials $CLUSTER_NAME --region REGION
建立由永久磁碟支援的 PV 和 PVC
建立 PVC 做為 WordPress 所需的儲存空間。GKE 已安裝預設的 StorageClass
資源,可讓您動態佈建永久磁碟支援的 PV。您可以使用 wordpress-volumeclaim.yaml
檔案建立部署所需的 PVC。
這個資訊清單檔案說明要求 200 GB 儲存空間的 PVC。檔案中尚未定義 StorageClass
資源,因此這個 PVC 會使用預設的 StorageClass
資源,佈建由永久磁碟支援的 PV。
在 Cloud Shell 中部署資訊清單檔案:
kubectl apply -f $WORKING_DIR/wordpress-volumeclaim.yaml
佈建永久磁碟支援的 PV,並將其繫結至 PVC,最多可能需要 10 秒。您可以使用下列指令檢查狀態:
kubectl get persistentvolumeclaim
輸出內容會顯示狀態為
Pending
的 PersistentVolumeClaim,如下所示:NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE wordpress-volumeclaim Pending standard-rwo 5s
這個 PersistentVolumeClaim 會維持在
Pending
狀態,直到您在本教學課程後續使用為止。
建立 MySQL 適用的 Cloud SQL 執行個體
在 Cloud Shell 中,建立名為
mysql-wordpress-instance
的執行個體:INSTANCE_NAME=mysql-wordpress-instance gcloud sql instances create $INSTANCE_NAME
將執行個體連線名稱新增為環境變數:
export INSTANCE_CONNECTION_NAME=$(gcloud sql instances describe $INSTANCE_NAME \ --format='value(connectionName)')
為 WordPress 建立資料庫,以儲存資料:
gcloud sql databases create wordpress --instance $INSTANCE_NAME
建立名為
wordpress
的資料庫使用者和密碼,供 WordPress 驗證執行個體:CLOUD_SQL_PASSWORD=$(openssl rand -base64 18) gcloud sql users create wordpress --host=% --instance $INSTANCE_NAME \ --password $CLOUD_SQL_PASSWORD
如果關閉 Cloud Shell 工作階段,密碼就會遺失。請記下密碼,因為後續步驟會使用到該密碼。
您已完成 WordPress 新網誌資料庫的設定作業。
部署 WordPress
您必須先建立服務帳戶,才能部署 WordPress。您會建立 Kubernetes 密鑰來保存服務帳戶憑證,並建立另一個密鑰來保存資料庫憑證。
設定服務帳戶並建立密鑰
如要讓 WordPress 應用程式透過 Cloud SQL Proxy 存取 MySQL 執行個體,請建立服務帳戶:
SA_NAME=cloudsql-proxy gcloud iam service-accounts create $SA_NAME --display-name $SA_NAME
將服務帳戶電子郵件地址新增為環境變數:
SA_EMAIL=$(gcloud iam service-accounts list \ --filter=displayName:$SA_NAME \ --format='value(email)')
將
cloudsql.client
角色新增至服務帳戶:gcloud projects add-iam-policy-binding $PROJECT_ID \ --role roles/cloudsql.client \ --member serviceAccount:$SA_EMAIL
為服務帳戶建立金鑰:
gcloud iam service-accounts keys create $WORKING_DIR/key.json \ --iam-account $SA_EMAIL
這個指令會下載
key.json
檔案的副本。為 MySQL 憑證建立 Kubernetes 密鑰:
kubectl create secret generic cloudsql-db-credentials \ --from-literal username=wordpress \ --from-literal password=$CLOUD_SQL_PASSWORD
為服務帳戶憑證建立 Kubernetes 密鑰:
kubectl create secret generic cloudsql-instance-credentials \ --from-file $WORKING_DIR/key.json
部署 WordPress
下一個步驟是在 GKE 叢集中部署 WordPress 容器。
wordpress_cloudsql.yaml
資訊清單檔案說明部署,該部署會建立執行含有 WordPress 執行個體容器的單一 Pod。這個容器會讀取包含您建立的 cloudsql-db-credentials
密鑰的 WORDPRESS_DB_PASSWORD
環境變數。
這個資訊清單檔案也會設定 WordPress 容器,透過在邊車容器中執行的 Cloud SQL Proxy 與 MySQL 通訊。
主機位址值設定於 WORDPRESS_DB_HOST
環境變數中。
準備檔案,方法是取代
INSTANCE_CONNECTION_NAME
環境變數:cat $WORKING_DIR/wordpress_cloudsql.yaml.template | envsubst > \ $WORKING_DIR/wordpress_cloudsql.yaml
部署
wordpress_cloudsql.yaml
資訊清單檔案:kubectl create -f $WORKING_DIR/wordpress_cloudsql.yaml
將永久磁碟連接至運算節點時,部署這個資訊清單檔案需要幾分鐘的時間。
觀察部署作業,查看狀態是否變更為
running
:kubectl get pod -l app=wordpress --watch
如果輸出內容顯示
Running
狀態,即可繼續下一個步驟。NAME READY STATUS RESTARTS AGE wordpress-387015-02xxb 2/2 Running 0 2m47s
公開 WordPress 服務
在上一個步驟中,您部署了 WordPress 容器,但該容器目前無法從叢集外部存取,因為沒有外部 IP 位址。您可以建立並設定附加外部負載平衡器的 Kubernetes 服務,向網際網路的流量公開發布 WordPress 應用程式。如要進一步瞭解如何在 GKE 中使用 Service 公開應用程式,請參閱操作指南。
建立
type:LoadBalancer
的 Service:kubectl create -f $WORKING_DIR/wordpress-service.yaml
建立負載平衡器需要幾分鐘的時間。
觀察部署作業,等待系統為服務指派外部 IP 位址:
kubectl get svc -l app=wordpress --watch
如果輸出內容顯示外部 IP 位址,請繼續執行下一個步驟。請注意,您的外部 IP 與下列範例不同。
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE wordpress 10.51.243.233 203.0.113.3 80:32418/TCP 1m
請記下
EXTERNAL_IP
位址欄位,稍後會用到。
設定 WordPress 網誌
在本節中,您將設定 WordPress 網誌。
在瀏覽器中前往下列網址,並將 external-ip-address 替換為公開 WordPress 執行個體的服務
EXTERNAL_IP
位址:http://external-ip-address
在「WordPress installation」(WordPress 安裝) 頁面中,選取語言,然後按一下「Continue」(繼續)。
填寫「必要資訊」頁面,然後按一下「安裝 WordPress」。
按一下 [Login] (登入)。
輸入先前建立的使用者名稱和密碼。
你現在有網誌網站了。如要造訪網誌,請在瀏覽器中前往下列網址:
http://external-ip-address
清除所用資源
如要避免系統向您的 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.
刪除個別資源
如果您使用現有專案,但不想刪除專案,請刪除個別資源。
刪除服務:
kubectl delete service wordpress
等待刪除為
wordpress
服務佈建的負載平衡器。系統會在背景中非同步刪除負載平衡器。觀看刪除程序:
watch gcloud compute forwarding-rules list
看到下列輸出內容時,表示負載平衡器已刪除:
Listed 0 items.
刪除部署作業:
kubectl delete deployment wordpress
刪除 WordPress 的 PVC:
kubectl delete pvc wordpress-volumeclaim
這個指令也會自動刪除 PV 和永久磁碟。
刪除 GKE 叢集:
gcloud container clusters delete $CLUSTER_NAME
刪除 Cloud SQL 執行個體:
gcloud sql instances delete $INSTANCE_NAME
從服務帳戶中移除角色:
gcloud projects remove-iam-policy-binding $PROJECT_ID \ --role roles/cloudsql.client \ --member serviceAccount:$SA_EMAIL
刪除服務帳戶:
gcloud iam service-accounts delete $SA_EMAIL
後續步驟
- 為應用程式設定靜態 IP 和網域名稱。
探索其他 Kubernetes Engine 教學課程。
探索 Google Cloud 的參考架構、圖表和最佳做法。 歡迎瀏覽我們的雲端架構中心。