1. 程式人生 > >Docker & Kubenetes 系列四:叢集,擴容,升級,回滾

Docker & Kubenetes 系列四:叢集,擴容,升級,回滾

> 本篇將會講解應用部署到Kubenetes叢集,叢集副本集檢視,叢集自愈能力演示,叢集擴容,滾動升級,以及回滾。 本篇是Docker&Kubenetes系列的第四篇,在前面的篇幅中,我們向Kubenetes中部署了單個應用例項。如果單個節點故障了,那麼服務就不可用了,這在實際環境中是不能接受的。在實際的正式環境中,我們不僅要避免單點,還要根據負載變化動態的調整節點數量。為了實現這個目的,我們可以藉助於Kubenetes的Deployment,Deployment可以建立指定數量的Pod,並有自愈能力,還能完成升級更新及回滾操作。 ## 向Kubenetes中部署一個3節點的叢集 和之前介紹的一樣部署叢集也由過yml配置檔案開始的,本例我們依然使用前面篇幅中建立的docker映象來部署叢集。Deployment配置檔案內容如下,這個配置檔案是從Kubenetes官網複製的,手寫yml檔案太容易出錯了,yml格式挺煩的。 ``` apiVersion: apps/v1 kind: Deployment metadata: name: my-first-demo-dep labels: app: my-first-demo spec: replicas: 3 selector: matchLabels: app: my-first-demo template: metadata: labels: app: my-first-demo spec: containers: - name: my-first-demo image: hellodm/my-first-demo:v1.0 ports: - containerPort: 80 ``` 配置檔案中,`replicas=3`表示要啟動3個副本,使用的映象是`hellodm/my-first-demo:v1.0`。配置完了之後,我們來啟動叢集,命令如下: ``` $ kubectl create -f dep.yml deployment.apps/my-first-demo-dep created ``` 我們來看檢視副本集情況: ``` $ kubectl get rs NAME DESIRED CURRENT READY AGE my-first-demo-dep-5647fd55f 3 3 3 3m15s ``` 在檢視一下pod: ``` $ kubectl get pods NAME READY STATUS RESTARTS AGE my-first-demo-dep-5647fd55f-4wzxs 1/1 Running 0 22s my-first-demo-dep-5647fd55f-c9lwx 1/1 Running 0 22s my-first-demo-dep-5647fd55f-nnwt6 1/1 Running 0 22s ``` 暴露叢集,使得我們可以在訪問叢集內部的應用。 ``` $ kubectl apply -f svc.yml service/my-first-demo-svc created ``` 訪問: ``` $ curl 'http://localhost:30000/'

Hello world!

``` OK,成功了。 ## Kubenetes 擴容演示 上面我們部署了3個節點的叢集,假設現在3個節點已經有點吃力了,我們準備擴充套件到4個節點。編輯上面的Deployment檔案,修改的地方如下: ``` spec: replicas: 4 ``` 其他的不變,只是`replicas`為4了。重新啟動一下看看: ``` $ kubectl apply -f dep.yml --record=true deployment.apps/my-first-demo-dep configured ``` 看下副本集,如下,變成4個了。 ``` $ kubectl get rs NAME DESIRED CURRENT READY AGE my-first-demo-dep-5647fd55f 4 4 4 10h ``` ## 檢查Kubenetes叢集的自愈性 我們來刪掉2個Pod,模擬宕機情況,執行`kubectl delete pod`命令,如下: ``` $ kubectl delete pod my-first-demo-dep-5647fd55f-4wzxs my-first-demo-dep-5647fd55f-c9lwx ``` 立馬檢視Pod是否恢復: ``` $ kubectl get pods NAME READY STATUS RESTARTS AGE my-first-demo-dep-5647fd55f-bxpn7 1/1 Running 0 25s my-first-demo-dep-5647fd55f-nnwt6 1/1 Running 0 5m8s my-first-demo-dep-5647fd55f-vwxgz 1/1 Running 0 25s ``` 可以看到還是3臺,從NAME和AGE可以看出,多了兩個新的。OK,至此,我們演示了擴容和自愈性。 ## Kubenetes滾動升級 為了演示滾動升級,我們先製作v2.0版本的映象,修改index的內容為: ```html

Hello worldls , this is Version 2.

``` 這裡我故意拼錯了一個單詞,別急,後面有妙用。下面我們來 製作v2.0的映象: ``` $ docker build . -t my-frist-demo:v2.0 Sending build context to Docker daemon 6.144kB Step 1/2 : FROM nginx ---> 602e111c06b6 Step 2/2 : COPY index.html /usr/share/nginx/html ---> 7484909c0df2 Successfully built 7484909c0df2 Successfully tagged my-frist-demo:v2.0 ``` 釋出映象: ``` $ docker tag my-frist-demo:v2.0 hellodm/my-first-demo:v2.0 $ docker push hellodm/my-first-demo:v2.0 The push refers to repository [docker.io/hellodm/my-first-demo] a3e37d09f192: Preparing b3003aac411c: Preparing 216cf33c0a28: Preparing c2adabaecedb: Layer already exists denied: requested access to the resource is denied ``` 修改deployment的配置檔案,改用v2.0的映象,如下: ``` apiVersion: apps/v1 kind: Deployment metadata: name: my-first-demo-dep labels: app: my-first-demo spec: replicas: 3 selector: matchLabels: app: my-first-demo template: metadata: labels: app: my-first-demo spec: containers: - name: my-first-demo image: hellodm/my-first-demo:v2.0 ports: - containerPort: 80 ``` 執行釋出,這次附帶了一個引數`--record=true`讓 Kubernetes 把這行命令記到釋出歷史中方便後面檢視。 ``` $ kubectl apply -f dep.yml --record=true deployment.apps/my-first-demo-dep configured ``` 趕緊檢視一下pod狀態如下,可以發現已經在滾動釋出了,`ContainerCreating`狀態的表示新版本的容器正在啟動,`Running`的是新版本的已經執行起來了,`Terminating`的這個是老版本正在停止。 ``` $ kubectl get pods NAME READY STATUS RESTARTS AGE my-first-demo-dep-54596847d8-6m4n9 0/1 ContainerCreating 0 2s my-first-demo-dep-54596847d8-jrm8g 1/1 Running 0 4s my-first-demo-dep-5647fd55f-cbcrz 1/1 Running 0 56s my-first-demo-dep-5647fd55f-ll7tt 0/1 Terminating 0 56s my-first-demo-dep-5647fd55f-s8d6l 1/1 Running 0 56s ``` 再趕快檢視一下滾動釋出的狀態: ``` $ kubectl rollout status deployment my-first-demo-dep Waiting for rollout to finish: 1 old replicas are pending termination... deployment "my-first-demo-dep" successfully rolled out ``` 如上命令所表示,隨著最後一個`old replicas` 終止,新版本的deployment成功釋出了`successfully rolled out`。 簡直是太激動了,驗證一下是不是3個pod,如下,沒問題,3個Running狀態。 ``` $ kubectl get pods NAME READY STATUS RESTARTS AGE my-first-demo-dep-54596847d8-6m4n9 1/1 Running 0 36s my-first-demo-dep-54596847d8-jrm8g 1/1 Running 0 38s my-first-demo-dep-54596847d8-nqgmn 1/1 Running 0 34s ``` 訪問一下看看: ``` $ curl 'http://localhost:30000/'

Hello worldls , this is Version 2.

``` 已經是**新版本**了,可惜啊,新版本有問題啊!單詞拼錯了,`world`寫成了`worldls`,我們來趕緊回滾一下。 ##Kubenetes應用叢集回滾 上面我們使用滾動釋出,將網站升級為version2了。但是有錯誤,我們來檢視一下發布歷史: ``` $ kubectl rollout history deployment my-first-demo-dep deployment.extensions/my-first-demo-dep REVISION CHANGE-CAUSE 1 kubectl apply --filename=dep.yml --record=true 2 kubectl apply --filename=dep.yml --record=true ``` 可以看到,有兩個版本,我們回滾到版本1,執行如下命令: ``` $ kubectl rollout undo deployment my-first-demo-dep --to-revision=1 deployment.extensions/my-first-demo-dep rolled back ``` 查看回滾過程狀態: ```text $ kubectl rollout status deployment my-first-demo-dep Waiting for deployment "my-first-demo-dep" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "my-first-demo-dep" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "my-first-demo-dep" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "my-first-demo-dep" rollout to finish: 1 old replicas are pending termination... Waiting for deployment "my-first-demo-dep" rollout to finish: 1 old replicas are pending termination... deployment "my-first-demo-dep" successfully rolled out ``` 上面的日誌顯示了完整的回滾過程,看到最後一行`successfully rolled out`輸出的時候,說明回滾完成了。這時候響應的應該就是` Hello world!`,我們來驗證一下回滾效果: ``` $ curl 'http://localhost:30000/'

Hello world!

``` OK,成功了,回滾到了沒有錯誤的原始版本。 ## 總結 本篇演示了Kubenetes叢集釋出,叢集副本集檢視,叢集自愈能力演示,叢集擴容,滾動升級,以及回滾。然而這一切來得是如此的簡單,我們只需要修改一下配置,執行一個命令,剩下的工作Kubenetes都幫我們做了。當我在回顧我幾年前所在的某家公司的時候,程序升級上線都是由運維手工完成,雖然也完成了任務,但是那種原始的操作方式,在時下如果還有人用的話,那簡直是不敢想象。 [Docker & k8s 系列一:快速上手docker](https://www.cnblogs.com/demingblog/p/12905545.html) [Docker & k8s 系列二:本機k8s環境搭建](https://www.cnblogs.com/demingblog/p/12905563.html) [Docker & k8s 系列三:在k8s中部署單個服務例項](https://www.cnblogs.com/demingblog/p/12905569.html) ![alt 逃離沙漠公眾號](https://images.cnblogs.com/cnblogs_com/demingblog/1407293/o_taolisha