今天進入到 Deployment 他是一個控制 pod & replicaset 的設定方式,前一篇有講到,基本上都是不建議單獨使用 pod 來做部署的動作,接下來我們來看看吧。
首先,不囉唆直接先來一個標準的 Deployment範例,把下面的文字存成 nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3 # 同時建立 3 個 nginx 的 pod
# replicaset 的效果套用在帶有 app=nginx 的 pod 上
# 必須要與下面的 pod label 有相符合
selector:
matchLabels:
app: nginx
# .spec.template 其實就是 pod 的定義
template:
# pod metadata
metadata:
# 設定給 pod 的 label 資訊
labels:
app: nginx
spec:
# 這邊可以看到,其實跟昨天的 nginx 是一樣的。
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
接著下 kubectl --kubeconfig ~/.kube/k3s.yaml apply -f nginx-deployment.yaml
接著做檢查看看 kubectl --kubeconfig ~/.kube/k3s.yaml get pods
就會看到下面回應
NAME READY STATUS RESTARTS AGE
nginx-deployment-d46f5678b-ghjsx 1/1 Running 0 50s
nginx-deployment-d46f5678b-zxprl 1/1 Running 0 50s
nginx-deployment-d46f5678b-zwrdk 1/1 Running 0 50s
也可以下 kubectl --kubeconfig ~/.kube/k3s.yaml get deployment
#READY 就緒幾個/總共幾個
#UP-TO-DATE 有幾個 pod 副本已經 onboard
#AVAILABLE 目前有多少pod 副本可以運作
#Age pod運行時間
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 115s
我們來嘗試看看,怎麼透過 Deployment 去做 scale
# 設定要擴增10個
$ kubectl --kubeconfig ~/.kube/k3s.yaml scale deployment/nginx-deployment --replicas=10
deployment.apps/nginx-deployment scaled
# 檢查pod 數量
$ kubectl --kubeconfig ~/.kube/k3s.yaml get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-d46f5678b-ghjsx 1/1 Running 0 8m51s
nginx-deployment-d46f5678b-zxprl 1/1 Running 0 8m51s
nginx-deployment-d46f5678b-zwrdk 1/1 Running 0 8m51s
nginx-deployment-d46f5678b-qwrgt 1/1 Running 0 53s
nginx-deployment-d46f5678b-n84gc 1/1 Running 0 53s
nginx-deployment-d46f5678b-5w8jg 1/1 Running 0 53s
nginx-deployment-d46f5678b-2rv8x 1/1 Running 0 53s
nginx-deployment-d46f5678b-9ljzr 1/1 Running 0 53s
nginx-deployment-d46f5678b-kdvlg 1/1 Running 0 53s
nginx-deployment-d46f5678b-88xpz 1/1 Running 0 53s
# 更新一個不存在的版本
$ kubectl --kubeconfig ~/.kube/k3s.yaml set image deployment/nginx-deployment nginx=nginx1.9.2
deployment.extensions/nginx-deployment image updated
# 因為版本不存在,所以會卡住,需要強制中斷
$ kubectl rollout status deployments nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 5 out of 10 new replicas have been updated...
^C
# 可以下這句就可以回復上一個動作。 接下來再重新執行上一句就可以看到正常的狀態。
$ kubectl --kubeconfig ~/.kube/k3s.yaml rollout undo deployment/nginx-deployment
deployment.apps/nginx-deployment rolled back
透過以上介紹,我們知道了一些基礎 Deployment 的教學。明天我們來介紹一些實務上 Deployment 的特別設定。