天天看點

【k8s學習系列】第2篇,規模和更新部署k8s學習系列前言規模和更新部署總結

k8s學習系列

第零篇 k8s環境搭建

第壹篇 部署第一個應用程式

第貳篇 規模和更新部署

文章目錄

  • k8s學習系列
  • 前言
  • 規模和更新部署
    • 1. 使用副本縮放應用程式
    • 2. 更新和復原應用程式
    • 3. 删除應用程式
  • 總結

前言

本人目前換工作即将從事雲原生相關的工作,把k8s技能再重新拾起來。整個系列文章基于IBM Kubernetes 101 content

提示:此系列文章所用作業系統及版本号:

  • 作業系統:mac OS Catalina(10.15.4)
  • 【k8s學習系列】第2篇,規模和更新部署k8s學習系列前言規模和更新部署總結

規模和更新部署

在這個實驗室中,您将學習如何更新部署中的應用數量,以及如何在 Kubernetes 上安全地推出應用程式的更新。

對于這個,您需要從前一個實驗室運作 guestbook 應用程式的部署。如果你需要建立它,運作:

kubectl create deployment guestbook --image=ibmcom/guestbook:v1
           

1. 使用副本縮放應用程式

副本(replica)是包含正在運作的服務的 pod 的副本。通過擁有一個 pod 的多個副本,您可以確定部署(deployment)具有可用資源來處理應用程式上不斷增加的負載。

1.1 Kubectl 提供了一個 scale 子指令來更改現有部署的大小。

讓我們将容量從一個單獨運作的 guestbook 執行個體增加到10個執行個體:

kubectl scale --replicas=10 deployment guestbook
           

Kubernetes現在将試圖啟動9個新的和第一個相同配置的pods,以達到期望的10個副本。

1.2 要檢視您的更改是否已經擴充,您可以運作:

kubectl rollout status deployment guestbook
           

擴充可能發生得太快,以至于以下消息可能無法顯示:

$ kubectl rollout status deployment guestbook
Waiting for rollout to finish: 1 of 10 updated replicas are available...
Waiting for rollout to finish: 2 of 10 updated replicas are available...
Waiting for rollout to finish: 3 of 10 updated replicas are available...
Waiting for rollout to finish: 4 of 10 updated replicas are available...
Waiting for rollout to finish: 5 of 10 updated replicas are available...
Waiting for rollout to finish: 6 of 10 updated replicas are available...
Waiting for rollout to finish: 7 of 10 updated replicas are available...
Waiting for rollout to finish: 8 of 10 updated replicas are available...
Waiting for rollout to finish: 9 of 10 updated replicas are available...
deployment "guestbook" successfully rolled out
           

1.3 一旦擴充完成,使用以下方法確定你的pods正常運作,你應該看到10個部署副本的輸出清單:

$ kubectl get pods
NAME                        READY     STATUS    RESTARTS   AGE
guestbook-562211614-1tqm7   1/1       Running   0          1d
guestbook-562211614-1zqn4   1/1       Running   0          2m
guestbook-562211614-5htdz   1/1       Running   0          2m
guestbook-562211614-6h04h   1/1       Running   0          2m
guestbook-562211614-ds9hb   1/1       Running   0          2m
guestbook-562211614-nb5qp   1/1       Running   0          2m
guestbook-562211614-vtfp2   1/1       Running   0          2m
guestbook-562211614-vz5qw   1/1       Running   0          2m
guestbook-562211614-zksw3   1/1       Running   0          2m
guestbook-562211614-zsp0j   1/1       Running   0          2m
           
提示: 提高可用性的另一種方法是在部署中添加叢集和區域,如下圖所示:
【k8s學習系列】第2篇,規模和更新部署k8s學習系列前言規模和更新部署總結

2. 更新和復原應用程式

Kubernetes 允許您将應用程式滾動更新到新的容器映像。這使您可以輕松地更新正在運作的映像,而且如果在部署期間或之後發現了問題,還可以輕松地撤消擴充。

在之前的實驗中,我們使用了一個帶有 v1标簽的圖像。對于我們的更新,我們将使用帶有 v2标記的圖像。

更新和復原:

2.1 使用 kubectl,您現在可以更新您的部署以使用 v2映像。Kubectl 允許您使用 set 子指令更改現有資源的詳細資訊。我們可以使用它來改變正在使用的圖像。

kubectl set image deployment/guestbook guestbook=ibmcom/guestbook:v2
           
請注意,一個 pod 可以有多個容器,每個容器都有自己的名稱。每個鏡像可以單獨或一次改變參考的名稱。對于我們的 guestbook Deployment,容器名稱也是 guestbook。可以同時更新多個容器。

2.2 若要檢查滾動的狀态,請運作:

kubectl rollout status deployment/guestbook
           

進展可能發生得太快,以至于以下消息可能無法顯示:

$ kubectl rollout status deployment/guestbook
Waiting for rollout to finish: 2 out of 10 new replicas have been updated...
Waiting for rollout to finish: 3 out of 10 new replicas have been updated...
Waiting for rollout to finish: 3 out of 10 new replicas have been updated...
Waiting for rollout to finish: 3 out of 10 new replicas have been updated...
Waiting for rollout to finish: 4 out of 10 new replicas have been updated...
Waiting for rollout to finish: 4 out of 10 new replicas have been updated...
Waiting for rollout to finish: 4 out of 10 new replicas have been updated...
Waiting for rollout to finish: 4 out of 10 new replicas have been updated...
Waiting for rollout to finish: 4 out of 10 new replicas have been updated...
Waiting for rollout to finish: 5 out of 10 new replicas have been updated...
Waiting for rollout to finish: 5 out of 10 new replicas have been updated...
Waiting for rollout to finish: 5 out of 10 new replicas have been updated...
Waiting for rollout to finish: 6 out of 10 new replicas have been updated...
Waiting for rollout to finish: 6 out of 10 new replicas have been updated...
Waiting for rollout to finish: 6 out of 10 new replicas have been updated...
Waiting for rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for rollout to finish: 8 out of 10 new replicas have been updated...
Waiting for rollout to finish: 8 out of 10 new replicas have been updated...
Waiting for rollout to finish: 8 out of 10 new replicas have been updated...
Waiting for rollout to finish: 8 out of 10 new replicas have been updated...
Waiting for rollout to finish: 9 out of 10 new replicas have been updated...
Waiting for rollout to finish: 9 out of 10 new replicas have been updated...
Waiting for rollout to finish: 9 out of 10 new replicas have been updated...
Waiting for rollout to finish: 1 old replicas are pending termination...
Waiting for rollout to finish: 1 old replicas are pending termination...
Waiting for rollout to finish: 1 old replicas are pending termination...
Waiting for rollout to finish: 9 of 10 updated replicas are available...
Waiting for rollout to finish: 9 of 10 updated replicas are available...
Waiting for rollout to finish: 9 of 10 updated replicas are available...
deployment "guestbook" successfully rolled out
           

2.3 像以前一樣測試應用程式,通過在浏覽器中通路 < public-ip > : < nodeport > 來确認您的新代碼處于活動狀态。

記住,要獲得“ nodeport”和“ public-ip” ,請使用以下指令。如果沒有設定叢集環境變量,則用叢集名稱替換 $CLUSTER _ name:

kubectl describe service guestbook
           

kubectl get nodes -o wide
           

要驗證您正在運作 Guestbook 的“ v2” ,請檢視頁面的标題,現在應該是 Guestbook-v2。如果您使用的是浏覽器,請確定您強制重新整理(使緩存失效)。

【k8s學習系列】第2篇,規模和更新部署k8s學習系列前言規模和更新部署總結

打開Kubernetes Dashboard檢視:

【k8s學習系列】第2篇,規模和更新部署k8s學習系列前言規模和更新部署總結
【k8s學習系列】第2篇,規模和更新部署k8s學習系列前言規模和更新部署總結

可以發現pods都成功更新到鏡像為v2。

2.4 如果要撤消最新的首次部署,請使用以下指令:

kubectl rollout undo deployment guestbook
           

然後你可以使用這個指令檢視狀态:

kubectl rollout status deployment/guestbook
           

打開Kubernetes Dashboard檢視發現,Replica Sets 已經成功復原:

【k8s學習系列】第2篇,規模和更新部署k8s學習系列前言規模和更新部署總結
【k8s學習系列】第2篇,規模和更新部署k8s學習系列前言規模和更新部署總結

2.5 在執行 rollout 時,您會看到對舊副本和新副本的引用(old replicas and new replicas)。舊的副本是我們擴充應用程式時最初部署的10個pods。新的副本來自新建立的帶有不同鏡像的pods。所有這些pods都屬于 Deployment。部署使用一個名為 ReplicaSet 的資源管理這兩組pods。我們可以通過以下方式檢視留言簿副本(ReplicaSets):

$ kubectl get replicasets -l app=guestbook
NAME                   DESIRED   CURRENT   READY     AGE
guestbook-5f5548d4f    10        10        10        21m
guestbook-768cc55c78   0         0         0         3h
           

3. 删除應用程式

在我們繼續之前,讓我們先删除這個應用程式,這樣我們就可以學習一種不同的方法來獲得相同的結果:

若要删除部署(deployment),請使用

kubectl delete deployment guestbook
           

要删除該服務,請使用:

kubectl delete service guestbook
           

祝賀你!您部署了應用程式的第二個版本。2号實驗室已經完成。繼續到本課程的下一個實驗室。

總結

通過本文,我們學習了

  1. 擴充一個部署
  2. 滾動更新服務
  3. 已經部署成功的應用,如何撤銷復原
  4. 删除應用程式