系列目錄html
這個系列分爲兩個小節,第一個小節介紹deployment滾動更新時,deployment、replicaset、pod的細節以及建立過程以及deployment版本管理的方式node
第二個小節將介紹滾動更新過程當中最大可用、liveness以及readiness等linux
咱們在阿里雲上有兩個不一樣版本的鏡像用於測試,使用docker pull
把它拉取到本地nginx
docker pull registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v1 docker pull registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v2
截至目前,咱們並無詳細介紹過docker的操做,可是目前市面上已經有不少書籍和博客介紹docker的基本操做,沒有docker操做經驗的能夠關注一些入門教程.docker
root@k8s-master:~# docker run -d -p 10080:80 nginx:v1 e88097841c5feef92e4285a2448b943934ade5d86412946bc8d86e262f80a050 root@k8s-master:~# curl http://127.0.0.1:10080 ---------- version: v1 hostname: f5189a5d3ad3
注在linux裏若是是以root用戶登錄,則命令行前一個
#
標識,這裏並非註釋的意思,還請注意api
+------------+ | deployment | +-----+------+ | | | | +--------------------------------------------------+ | | | | | | | | | | | | | | | | | | +------v------+ +------v------+ +------v------+ |replicaset:v1| |replicaset:v2| |replicaset:v3| +-------------+ +------+------+ +-------------+ | | +--------+---------+ | | | | +---v---+ +---v---+ |pod:v2 | |pod:v2 | +-------+ +-------+
咱們首先準備一個yaml文件用於測試:bash
root@k8s-master:~# more roll_update.yaml apiVersion: extensions/v1beta1 kind: Deployment metadata: name: image-deployment spec: replicas: 1 template: metadata: labels: app: image-update spec: containers: - name: nginx image: registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v1 imagePullPolicy: Always
簡單驗證一下:app
root@k8s-master:~# kubectl apply -f roll_update.yaml deployment.extensions "update-deployment" created
root@k8s-master:~# kubectl get deploy NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE update-deployment 3 3 3 3 54s root@k8s-master:~# kubectl get rs NAME DESIRED CURRENT READY AGE update-deployment-7db77f7cc6 3 3 3 56s root@k8s-master:~# kubectl get pod NAME READY STATUS RESTARTS AGE update-deployment-7db77f7cc6-7j49g 1/1 Running 0 1m update-deployment-7db77f7cc6-b75wn 1/1 Running 0 1m update-deployment-7db77f7cc6-cfnt5 1/1 Running 0 1m
deployment、replicaset、pod都已經正常啓動,下面分析一下他們的行爲:curl
deploymentide
root@k8s-master:~# kubectl describe deploy update-deployment Name: update-deployment Namespace: default ... Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 1 max unavailable, 1 max surge ... NewReplicaSet: update-deployment-7db77f7cc6 (3/3 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 1m deployment-controller Scaled up replica set update-deployment-7db77f7cc6 to 3
replicaset
root@k8s-master:~# kubectl describe rs update-deployment-7db77f7cc6 Name: update-deployment-7db77f7cc6 Namespace: default ... Controlled By: Deployment/update-deployment Replicas: 3 current / 3 desired Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed ... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulCreate 3m replicaset-controller Created pod: update-deployment-7db77f7cc6-7j49g Normal SuccessfulCreate 3m replicaset-controller Created pod: update-deployment-7db77f7cc6-b75wn Normal SuccessfulCreate 3m replicaset-controller Created pod: update-deployment-7db77f7cc6-cfnt5
replicaset建立了3個pod
pod
root@k8s-master:~# kubectl describe pod update-deployment-7db77f7cc6-7j49g Name: update-deployment-7db77f7cc6-7j49g Namespace: default ... Status: Running IP: 10.10.169.140 Controlled By: ReplicaSet/update-deployment-7db77f7cc6 ... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 9m default-scheduler Successfully assigned update-deployment-7db77f7cc6-7j49g to k8s-node2 Normal SuccessfulMountVolume 9m kubelet, k8s-node2 MountVolume.SetUp succeeded for volume "default-token-v9nkm" Normal Pulling 9m kubelet, k8s-node2 pulling image "registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v1" Normal Pulled 9m kubelet, k8s-node2 Successfully pulled image "registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v1" Normal Created 9m kubelet, k8s-node2 Created container Normal Started 9m kubelet, k8s-node2 Started container
你們可能不由會有疑問,爲何搞這麼複雜,啓動一個pod須要動用這麼多組件呢?下面用一個場景說明爲啥須要這麼多組件:
鏡像版本更新
當鏡像版本有更新時(三種方法均可以實現,參考前一篇文章:更新k8s鏡像版本的三種方式),既要保證服務可用,又要保證在線更新,流程應該是:
1) 先增長一個pod,鏡像版本爲新版本
2) pod可用以後,刪除一個老版本pod
3) 循環第一、2步,直到老版本pod所有刪除,新版本的pod所有可用
上述的這個過程就是replicaset的做用,它根據需求,自動的增長新版本pod,而後刪除老版本pod,直到老版本pod所有刪除,新版本的pod所有可用
若是此時版本須要回退,那replicaset須要把剛纔的步驟逆向更新一遍,實現版本回退
deployment的做用就是管理replicaset。deployment會保存各個版本的replicaset,一旦須要進行版本回滾,deployment會當即回滾replicaset的版本,從而控制pod狀態
下面測試一下:
使用patch命令更新鏡像版本,而且使用pause命令來觀察:
root@k8s-master:~# kubectl patch deployment update-deployment \ --patch '{"spec": {"template": {"spec": {"containers": [{"name": "nginx","image":"registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v2"}]}}}}' \ && kubectl rollout pause deployment update-deployment deployment.extensions "update-deployment" patched deployment.apps "update-deployment" paused
此時pod狀態:
root@k8s-master:~# kubectl get pod -owide NAME READY STATUS RESTARTS AGE IP NODE update-deployment-7db77f7cc6-7j49g 1/1 Running 0 1h 10.10.169.140 k8s-node2 update-deployment-7db77f7cc6-b75wn 1/1 Running 0 1h 10.10.235.211 k8s-master update-deployment-7db77f7cc6-cfnt5 1/1 Terminating 0 1h 10.10.36.126 k8s-node1 update-deployment-7fb7b4b557-6987x 1/1 Running 0 7s 10.10.36.127 k8s-node1 update-deployment-7fb7b4b557-dxdqb 1/1 Running 0 10s 10.10.169.139 k8s-node2
新增了2個pod,而刪除了1個老版本的pod
此時replicaset狀態:
root@k8s-master:~# kubectl get rs -owide NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR update-deployment-7db77f7cc6 2 2 2 1h nginx registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v1 app=roll-update,pod-template-hash=3863393772 update-deployment-7fb7b4b557 2 2 2 4m nginx registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v2 app=roll-update,pod-template-hash=3963606113
有一個新版本的replicaset建立了出來,而且需求的pod數量爲2,而原來的replicaset需求的pod數量從3降爲2
查看replicaset版本:
root@k8s-master:~# kubectl rollout history deploy update-deployment deployments "update-deployment" REVISION CHANGE-CAUSE 1 <none> 2 update version to v2
新增了一個版本2
因爲使用pause命令,更新過程到此會卡主,咱們讓更新的過程繼續下去:
root@k8s-master:~# kubectl rollout resume deployment update-deployment deployment.apps "update-deployment" resumed
查看狀態:
root@k8s-master:~# kubectl get pod NAME READY STATUS RESTARTS AGE update-deployment-7fb7b4b557-6987x 1/1 Running 0 15m update-deployment-7fb7b4b557-dxdqb 1/1 Running 0 15m update-deployment-7fb7b4b557-wg5c8 1/1 Running 0 1m root@k8s-master:~# kubectl get rs -owide NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR update-deployment-7db77f7cc6 0 0 0 1h nginx registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v1 app=roll-update,pod-template-hash=3863393772 update-deployment-7fb7b4b557 3 3 3 14m nginx registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v2 app=roll-update,pod-template-hash=3963606113
v1版本的replicaset已經沒有pod,可是歷史記錄仍是保留的,能夠經過deployment調度快速回滾