本篇將會講解應用部署到Kubenetes集羣,集羣副本集查看,集羣自愈能力演示,集羣擴容,滾動升級,以及回滾。html
本篇是Docker&Kubenetes系列的第四篇,在前面的篇幅中,咱們向Kubenetes中部署了單個應用實例。若是單個節點故障了,那麼服務就不可用了,這在實際環境中是不能接受的。在實際的正式環境中,咱們不只要避免單點,還要根據負載變化動態的調整節點數量。爲了實現這個目的,咱們能夠藉助於Kubenetes的Deployment,Deployment能夠建立指定數量的Pod,並有自愈能力,還能完成升級更新及回滾操做。nginx
和以前介紹的同樣部署集羣也由過yml配置文件開始的,本例咱們依然使用前面篇幅中建立的docker鏡像來部署集羣。Deployment配置文件內容以下,這個配置文件是從Kubenetes官網複製的,手寫yml文件太容易出錯了,yml格式挺煩的。docker
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
。配置完了以後,咱們來啓動集羣,命令以下:api
$ kubectl create -f dep.yml deployment.apps/my-first-demo-dep created
咱們來看查看副本集狀況:app
$ 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
暴露集羣,使得咱們能夠在訪問集羣內部的應用。curl
$ kubectl apply -f svc.yml service/my-first-demo-svc created
訪問:網站
$ curl 'http://localhost:30000/' <h1>Hello world! <h1>
OK,成功了。ui
上面咱們部署了3個節點的集羣,假設如今3個節點已經有點吃力了,咱們準備擴展到4個節點。編輯上面的Deployment文件,修改的地方以下:this
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
咱們來刪掉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,至此,咱們演示了擴容和自愈性。
爲了演示滾動升級,咱們先製做v2.0版本的鏡像,修改index的內容爲:
<h1>Hello worldls , this is Version 2.</h1>
這裏我故意拼錯了一個單詞,別急,後面有妙用。下面咱們來
製做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/' <h1>Hello worldls , this is Version 2.</h1>
已是新版本了,惋惜啊,新版本有問題啊!單詞拼錯了,world
寫成了worldls
,咱們來趕忙回滾一下。
上面咱們使用滾動發佈,將網站升級爲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
查看回滾過程狀態:
$ 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/' <h1>Hello world! <h1>
OK,成功了,回滾到了沒有錯誤的原始版本。
本篇演示了Kubenetes集羣發佈,集羣副本集查看,集羣自愈能力演示,集羣擴容,滾動升級,以及回滾。然而這一切來得是如此的簡單,咱們只須要修改一下配置,執行一個命令,剩下的工做Kubenetes都幫咱們作了。當我在回顧我幾年前所在的某家公司的時候,程序升級上線都是由運維手工完成,雖然也完成了任務,可是那種原始的操做方式,在時下若是還有人用的話,那簡直是不敢想象。
Docker & k8s 系列一:快速上手docker
Docker & k8s 系列二:本機k8s環境搭建
Docker & k8s 系列三:在k8s中部署單個服務實例