K8s有两种更新机制:Recreate和Rolling Update,Recreate先下线所有应用,再统一部署新版本,中间服务不可用。所以k8s默认更新机制为Rolling Update。
将deployment的镜像由v1更新为v2,k8s会自动更新pod。
master $ kubectl get pods
NAME READY STATUS RESTARTS AGE
frontend-65998dcfd8-km58j 1/1 Running 0 11s
frontend-65998dcfd8-ls2hz 1/1 Running 0 35s
frontend-65998dcfd8-nw8qg 1/1 Running 0 35s
frontend-65998dcfd8-zcpnf 1/1 Running 0 10s
frontend-7965b86db7-5dsv5 1/1 Running 0 20m
frontend-7965b86db7-8frss 1/1 Terminating 0 20m
frontend-7965b86db7-c8d54 1/1 Terminating 0 20m
中间访问应用,可能新老版本都会访问到。
更新完后:
master $ kubectl get pods
NAME READY STATUS RESTARTS AGE
frontend-65998dcfd8-km58j 1/1 Running 0 2m
frontend-65998dcfd8-ls2hz 1/1 Running 0 3m
frontend-65998dcfd8-nw8qg 1/1 Running 0 3m
frontend-65998dcfd8-zcpnf 1/1 Running 0 2m
从deployment的日志中也可以看出是一个一个更新的:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 27m deployment-controller Scaled up replica set frontend-7965b86db7 to 4
Normal ScalingReplicaSet 7m deployment-controller Scaled up replica set frontend-65998dcfd8 to 1
Normal ScalingReplicaSet 7m deployment-controller Scaled down replica set frontend-7965b86db7 to 3
Normal ScalingReplicaSet 7m deployment-controller Scaled up replica set frontend-65998dcfd8 to 2
Normal ScalingReplicaSet 6m deployment-controller Scaled down replica set frontend-7965b86db7 to 2
Normal ScalingReplicaSet 6m deployment-controller Scaled up replica set frontend-65998dcfd8 to 3
Normal ScalingReplicaSet 6m deployment-controller Scaled down replica set frontend-7965b86db7 to 1
Normal ScalingReplicaSet 6m deployment-controller Scaled up replica set frontend-65998dcfd8 to 4
Normal ScalingReplicaSet 6m deployment-controller Scaled down replica set frontend-7965b86db7 to 0
将策略更新为Recreate:kubectl edit deployment frontend
此时进行更新,将镜像由v2改为v3。
k8s首先将所有pod都terminate,再进行更新:
master $ kubectl get pods
NAME READY STATUS RESTARTS AGE
frontend-65998dcfd8-km58j 1/1 Terminating 0 11m
frontend-65998dcfd8-ls2hz 1/1 Terminating 0 11m
frontend-65998dcfd8-nw8qg 1/1 Terminating 0 11m
frontend-65998dcfd8-zcpnf 1/1 Terminating 0 11m
master $ kubectl get pods
NAME READY STATUS RESTARTS AGE
frontend-5c858cd557-4g9p9 1/1 Running 0 7s
frontend-5c858cd557-k8xb6 1/1 Running 0 7s
frontend-5c858cd557-rxnsk 1/1 Running 0 7s
frontend-5c858cd557-s69gz 0/1 ContainerCreating 0 7s
deployment日志:
Normal ScalingReplicaSet 2m deployment-controller Scaled down replica set frontend-65998dcfd8 to 0
Normal ScalingReplicaSet 1m deployment-controller Scaled up replica set frontend-5c858cd557 to 4
kubectl rollout undo deployment/myapp-deployment
左图为更新后,右图为回滚后状态。