1. 程式人生 > >kubernetes學習(一) Scale應用 & 滾動更新

kubernetes學習(一) Scale應用 & 滾動更新

一、Scale應用

  預設請款下應用只會執行一個副本,可通過kubectl get deployments 檢視副本數。

~$ kubectl get deployments
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
blogtest              1         1         1            1           3d
kubernetes-bootcamp   1         1         1            1           3d

  1、使用scale增加pod數量

    執行如下命令,可將副本數增加到3個。

~$ kubectl scale deployments/blogtest --replicas=3
deployment.extensions "blogtest" scaled

    通過kubectl get pods 可以看到當前的Pod增加到了三個。

~$ kubectl get deployment
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
blogtest              3         3         3            1           3d
kubernetes-bootcamp   1         1         1            1           3d

~$
kubectl get pods NAME READY STATUS RESTARTS AGE blogtest-7f9798c5b5-drvl8 0/1 ContainerCreating 0 8s blogtest-7f9798c5b5-gwkm9 1/1 Running 0 3d blogtest-7f9798c5b5-snppq 0/1 ContainerCreating 0 8s kubernetes-bootcamp-56cdd766d-5nj9n 1/1 Running 0 3d

    每次請求傳送到不同的Pod,3個副本輪詢處理請求,這樣就可以實現負載均衡了。

  2、使用scale減少Pod數量

 

~$ kubectl scale deployments/blogtest --replicas=2
deployment.extensions "blogtest" scaled

 

 

二、滾動更新

  1、滾動更新

   當前使用版本為v1,使用以下命令將其升級到v2.

~$ curl galaxy-k8s-test-master-02:17812
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-56cdd766d-5nj9n | v=1

~$ kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2deployment.apps "kubernetes-bootcamp" image updated

~$ curl galaxy-k8s-test-master-02:17812
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7799cbcb86-7dnzp | v=2

    滾動更新過程:v1的Pod被逐個刪除,同時啟動了新的v2 Pod。

  2、回退更新

   如果要回退到v1版本,執行kubectl rollout undo 命令。

~$ kubectl get pods
NAME                                   READY     STATUS        RESTARTS   AGE
blogtest-7f9798c5b5-gwkm9              1/1       Running       0          3d
blogtest-7f9798c5b5-snppq              1/1       Running       0          48m
kubernetes-bootcamp-56cdd766d-7skbz    1/1       Terminating   0          1m
kubernetes-bootcamp-7799cbcb86-bpnpp   1/1       Running       0          22s

~$ curl galaxy-k8s-test-master-02:17812
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7799cbcb86-bpnpp | v=2

~$ kubectl rollout undo deployments/kubernetes-bootcamp
deployment.apps "kubernetes-bootcamp" 

~$ kubectl get pods
NAME                                   READY     STATUS        RESTARTS   AGE
blogtest-7f9798c5b5-gwkm9              1/1       Running       0          3d
blogtest-7f9798c5b5-snppq              1/1       Running       0          49m
kubernetes-bootcamp-56cdd766d-jjzqm    1/1       Running       0          13s
kubernetes-bootcamp-7799cbcb86-bpnpp   1/1       Terminating   0          57s

~$ curl galaxy-k8s-test-master-02:17812
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-56cdd766d-jjzqm | v=1