雲端容器化技術與資源調度
認識 Kubernetes Deployment
先前我們透過了 Google Kubernetes Engine 提供的圖形化介面,完成了 hello-site
的部署作業。然而在使用 K8S 的思維當中,其實大部分的管理者通常會使用 YAML 描述檔來部署資源,而不是靠圖形介面。
刪除透過圖形介面產生的資源
- 工作負載:
hello-site-app
- Service 與 Ingress:
hello-site-app-service
準備使用 kubectl 指令來呼叫 K8S API
- 在叢集清單找到剛建立的 standard-cluster-1,並按下
連結
按鈕 - 選擇
在 Cloud Shell 中執行
# 先透過 gcloud 指令會讓 cloud shell 自動設定與 k8s 叢集的連結
$ gcloud container clusters get-credentials standard-cluster-1 --zone us-central1-a --project <your-project-id>
撰寫 Deployment YAML 設定檔
在 Cloud Shell 輸入以下指令
cd ~
vim hello-site.deployment.yml
在文字編輯器中,輸入以下內容後存檔:
apiVersion: "apps/v1"
kind: "Deployment"
metadata:
name: "hello-site-app"
namespace: "default"
labels:
app: "hello-site-app"
spec:
replicas: 1
selector:
matchLabels:
app: "hello-site-app"
template:
metadata:
labels:
app: "hello-site-app"
spec:
containers:
- name: "hello-site-container"
image: "gcr.io/cloud-container-255813/hello-site:v1"
將 Deployment YAML 檔送入 K8S Master 中
$ kubectl create -f hello-site.deployment.yml
K8S Deployment 資源概觀
- 一個
Deployment
資源下轄1~多個ReplicaSet
- 一個
ReplicaSet
資源下轄1~多個Pod
(這些 pod 的 Image 是同一個版本) - 一個
Pod
通常是一個容器 (但其實一個Pod
內可以有多個容器)