昨天跟各位介紹完一些關於 Deployment 的觀念後,今天不免俗的要再來跟各位介紹一些實作的技巧啦!
那我們廢話不多說,馬上開始我們今天的介紹吧!
首先我們一樣先建立一個屬於這個 Deployment 的 YAML 檔:
#test_deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: ubuntu-deployment
labels:
app: test
spec:
replicas: 3 # 建立三個有 ubuntu image 的 pod
selector:
matchLabels:
app: test
template:
metadata:
labels:
app: test
spec:
containers:
- name: ubuntu
image: ubuntu:20.04
args: [bash, -c, 'for ((i = 0; ; i++)); do echo "$i: $(date)"; sleep 100; done']
建立完 YAML 檔後,一樣將它建立於 Kubernetes 中:
kubectl apply -f test_deployment.yaml
deployment.apps/ubuntu-deployment created
這時我們可以來看看是否成功建立:
kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
ubuntu-deployment 3/3 3 3 16s
kubectl get pod
NAME READY STATUS RESTARTS AGE
ubuntu-deployment-55ffcc9677-85g9g 1/1 Running 0 5s
ubuntu-deployment-55ffcc9677-9bqgw 1/1 Running 0 5s
ubuntu-deployment-55ffcc9677-fpsvv 1/1 Running 0 5s
READY : 就緒個數/總數。
UP-TO-DATE : 有幾個 Pod 副本已經 onboard。
AVAILABLE : 目前有多少 Pod 副本可以運作。
Age : pod運行時間。
正常來說,出現以上畫面就代表你已經成功建立了!
我們在前一天有說過,他還有一個功能是滾動升級和回滾應用,這邊我們可以來嘗試看看:
kubectl set image deployment/ubuntu-deployment ubuntu=ubuntu:22.04
deployment.apps/ubuntu-deployment image updated
這段跑完後,他會顯示它已經更新了,這邊我們來驗證看看是否成功將 ubuntu 升級到 22.04 的版本。
# 確認已全部完成更新
ubuntu@ubuntu:~$ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
ubuntu-deployment 3/3 3 3 91m
# 查詢 Pod 的資訊
ubuntu@ubuntu:~$ kubectl describe deployment
Name: ubuntu-deployment
Namespace: default
CreationTimestamp: Tue, 13 Sep 2022 09:00:52 +0000
Labels: app=test
Annotations: deployment.kubernetes.io/revision: 2
Selector: app=test
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=test
Containers:
ubuntu:
Image: ubuntu:22.04
Port: <none>
Host Port: <none>
Args:
bash
-c
for ((i = 0; ; i++)); do echo "$i: $(date)"; sleep 100; done
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: ubuntu-deployment-869bd6f4c (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 57s deployment-controller Scaled up replica set ubuntu-deployment-869bd6f4c to 1
Normal ScalingReplicaSet 46s deployment-controller Scaled down replica set ubuntu-deployment-55ffcc9677 to 2
Normal ScalingReplicaSet 46s deployment-controller Scaled up replica set ubuntu-deployment-869bd6f4c to 2
Normal ScalingReplicaSet 45s deployment-controller Scaled down replica set ubuntu-deployment-55ffcc9677 to 1
Normal ScalingReplicaSet 45s deployment-controller Scaled up replica set ubuntu-deployment-869bd6f4c to 3
Normal ScalingReplicaSet 44s deployment-controller Scaled down replica set ubuntu-deployment-55ffcc9677 to 0
這邊我們觀察 Pod Templete 裡面的 Image 會發現已成功變成 ubuntu:22.04,所以已成功的更新了。
可是如果我們在運作時,發現此版本相比上一版太不穩定,想回到上一版的話也可以,可以用以下的指令:
ubuntu@ubuntu:~$ kubectl rollout undo deployment/ubuntu-deployment
deployment.apps/ubuntu-deployment rolled back
# 再確認一次是否回到原本的20.04
ubuntu@ubuntu:~$ kubectl describe deployments
Name: ubuntu-deployment
Namespace: default
CreationTimestamp: Tue, 13 Sep 2022 09:00:52 +0000
Labels: app=test
Annotations: deployment.kubernetes.io/revision: 3
Selector: app=test
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=test
Containers:
ubuntu:
Image: ubuntu:20.04
Port: <none>
Host Port: <none>
Args:
bash
-c
for ((i = 0; ; i++)); do echo "$i: $(date)"; sleep 100; done
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: ubuntu-deployment-55ffcc9677 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 15m deployment-controller Scaled up replica set ubuntu-deployment-869bd6f4c to 1
Normal ScalingReplicaSet 15m deployment-controller Scaled down replica set ubuntu-deployment-55ffcc9677 to 2
Normal ScalingReplicaSet 15m deployment-controller Scaled up replica set ubuntu-deployment-869bd6f4c to 2
Normal ScalingReplicaSet 15m deployment-controller Scaled down replica set ubuntu-deployment-55ffcc9677 to 1
Normal ScalingReplicaSet 15m deployment-controller Scaled up replica set ubuntu-deployment-869bd6f4c to 3
Normal ScalingReplicaSet 15m deployment-controller Scaled down replica set ubuntu-deployment-55ffcc9677 to 0
Normal ScalingReplicaSet 4s deployment-controller Scaled up replica set ubuntu-deployment-55ffcc9677 to 1
Normal ScalingReplicaSet 3s deployment-controller Scaled down replica set ubuntu-deployment-869bd6f4c to 2
Normal ScalingReplicaSet 3s deployment-controller Scaled up replica set ubuntu-deployment-55ffcc9677 to 2
Normal ScalingReplicaSet 2s (x2 over 106m) deployment-controller Scaled up replica set ubuntu-deployment-55ffcc9677 to 3
Normal ScalingReplicaSet 2s deployment-controller Scaled down replica set ubuntu-deployment-869bd6f4c to 1
Normal ScalingReplicaSet 1s deployment-controller Scaled down replica set ubuntu-deployment-869bd6f4c to 0
這邊我們會發現 image 已成功回到了 20.04了。
還有當我們可能需要的 Pod 數量可能不夠或因為太多有點佔用到資源的話,我們也可以進行擴容和縮容,現在就讓我來為你們介紹。
如果希望 Pod 數可以增加的話,可以使用以下的指令:
ubuntu@ubuntu:~$ kubectl scale deployment ubuntu-deployment --replicas 5
deployment.apps/ubuntu-deployment scaled
# 查詢 Deployment 裡面含有的 Pod 數
ubuntu@ubuntu:~$ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
ubuntu-deployment 5/5 5 5 135m
這邊我們看到已成功擴充了 Pod 的數量了。
相對的,如果有縮減的話,就是直接修改後面 replicas 的參數就 ok 了。
到這邊,已經差不多把一些基本的 Deployment 的操作介紹完了,讀者們可以試著自己做做看,依照自己的需求來配置自己的 Deployment。
那我們今天就先介紹到這邊吧,大家掰掰!