一、k8s版本更新
滚动更新是一次只更新一小部分副本,成功后,再更新更多的副本,最终完成所有副本的更新。滚动更新的最大的好处是零停机,整个更新过程始终有副本在运行,从而保证了业务的连续性。
实践:部署三个副本应用,初始镜像为httpd:v1,然后将其更新到httpd:v3
1、httpd:v1的配置文件如下:
[root@ren7 yaml]# cat httpdv1.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: httpdv1
spec:
replicas: 3
template:
metadata:
labels:
name: httpdv1
run: httpd
spec:
containers:
- name: httpd-appv1
image: reg.yunwei.com/learn/httpd:v1
ports:
- containerPort: 80
2、部署应用并查看
[root@ren7 yaml]# kubectl apply -f httpdv1.yaml
deployment.extensions/httpdv1 created
[root@ren7 yaml]# kubectl get deployment -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpdv1 3/3 3 3 19s httpd-appv1 reg.yunwei.com/learn/httpd:v1 name=httpdv1,run=httpd
[root@ren7 yaml]# kubectl get replicaset -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
httpdv1-676f9f5d8f 3 3 3 46s httpd-appv1 reg.yunwei.com/learn/httpd:v1 name=httpdv1,pod-template-hash=676f9f5d8f,run=httpd
[root@ren7 yaml]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
httpdv1-676f9f5d8f-9jmxx 1/1 Running 0 59s 172.20.33.97 192.168.11.6 <none> <none>
httpdv1-676f9f5d8f-ldmbk 1/1 Running 0 59s 172.20.72.187 192.168.11.5 <none> <none>
httpdv1-676f9f5d8f-m8w82 1/1 Running 0 59s 172.20.72.188 192.168.11.5 <none> <none>
部署过程如下:
创建 Deployment httpdv1
创建 ReplicaSet httpdv1-676f9f5d8f
创建三个 Pod
当前镜像为 reg.yunwei.com/learn/httpd:v1
3、将配置文件中 reg.yunwei.com/learn/httpd:v1 替换为 reg.yunwei.com/learn/httpd:v2,再次执行 kubectl apply。
[root@ren7 yaml]# vim httpdv1.yaml
[root@ren7 yaml]# kubectl apply -f httpdv1.yaml
deployment.extensions/httpdv1 configured
[root@ren7 yaml]# kubectl get deployment -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpdv1 2/3 3 2 4m17s httpd-appv1 reg.yunwei.com/learn/httpd:v2 name=httpdv1,run=httpd
[root@ren7 yaml]# kubectl get replicaset -o wide #这一步要稍微等几分钟才会切换到v2版本
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
httpdv1-676f9f5d8f 0 0 0 4m56s httpd-appv1 reg.yunwei.com/learn/httpd:v1 name=httpdv1,pod-template-hash=676f9f5d8f,run=httpd
httpdv1-75f7c775db 3 3 3 43s httpd-appv1 reg.yunwei.com/learn/httpd:v2 name=httpdv1,pod-template-hash=75f7c775db,run=httpd
[root@ren7 yaml]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
httpdv1-75f7c775db-44kpj 1/1 Running 0 49s 172.20.72.189 192.168.11.5 <none> <none>
httpdv1-75f7c775db-dx2dn 1/1 Running 0 46s 172.20.33.99 192.168.11.6 <none> <none>
httpdv1-75f7c775db-m9dnc 1/1 Running 0 49s 172.20.33.98 192.168.11.6 <none> <none>
我们发现了如下变化:
Deployment httpdv1 的镜像更新为 reg.yunwei.com/learn/httpd:v2
新创建了 ReplicaSet httpdv1-75f7c775db ,镜像为 reg.yunwei.com/learn/httpd:v2,并且管理了三个新的 Pod。
之前的 ReplicaSet httpdv1-676f9f5d8f 里面已经没有任何 Pod。
结论是:ReplicaSet httpdv1-676f9f5d8f 的三个 reg.yunwei.com/learn/httpd:v1 Pod 已经被 ReplicaSet httpdv1-75f7c775db 的三个 reg.yunwei.com/learn/httpd:v2 Pod 替换了。
第四步:具体过程可以通过 kubectl describe deployment httpd 查看。
[root@ren7 yaml]# kubectl describe deployment httpdv1
Name: httpdv1
Namespace: default
CreationTimestamp: Sat, 26 Oct 2019 09:44:08 +0800
Labels: name=httpdv1
run=httpd
Annotations: deployment.kubernetes.io/revision: 2
kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"extensions/v1beta1","kind":"Deployment","metadata":{"annotations":{},"name":"httpdv1","namespace":"default"},"spec":{"repli...
Selector: name=httpdv1,run=httpd
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
Pod Template:
Labels: name=httpdv1
run=httpd
Containers:
httpd-appv1:
Image: reg.yunwei.com/learn/httpd:v2
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: httpdv1-75f7c775db (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 12m deployment-controller Scaled up replica set httpdv1-676f9f5d8f to 3
Normal ScalingReplicaSet 8m39s deployment-controller Scaled up replica set httpdv1-75f7c775db to 1
Normal ScalingReplicaSet 8m39s deployment-controller Scaled down replica set httpdv1-676f9f5d8f to 2
Normal ScalingReplicaSet 8m39s deployment-controller Scaled up replica set httpdv1-75f7c775db to 2
Normal ScalingReplicaSet 8m36s deployment-controller Scaled down replica set httpdv1-676f9f5d8f to 1
Normal ScalingReplicaSet 8m36s deployment-controller Scaled up replica set httpdv1-75f7c775db to 3
Normal ScalingReplicaSet 8m32s deployment-controller Scaled down replica set httpdv1-676f9f5d8f to 0
每次只更新替换一个 Pod:
ReplicaSet httpdv1-75f7c775db 增加一个 Pod,总数为 1。
ReplicaSet httpdv1-676f9f5d8f 减少一个 Pod,总数为 2。
ReplicaSet httpdv1-75f7c775db 增加一个 Pod,总数为 2。
ReplicaSet httpdv1-676f9f5d8f 减少一个 Pod,总数为 1。
ReplicaSet httpdv1-75f7c775db 增加一个 Pod,总数为 3。
ReplicaSet httpdv1-676f9f5d8f 减少一个 Pod,总数为 0。
每次替换的 Pod 数量是可以定制的。Kubernetes 提供了两个参数 maxSurge 和 maxUnavailable 来精细控制 Pod 的替换数量。
二、更新回滚
kubectl apply 每次更新应用时 Kubernetes 都会记录下当前的配置,保存为一个 revision(版次),这样就可以回滚到某个特定 revision。
默认配置下,Kubernetes 只会保留最近的几个 revision,可以在 Deployment 配置文件中通过 revisionHistoryLimit 属性增加 revision 数量。
实践:回滚功能
1、应用有如下三个配置文件httpdv1.yaml,httpdv2.yaml 和 httpdv3.yaml,分别对应不同的httpd的镜像v1,v2和v3:
[root@ren7 yaml]# cat httpdv1.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: httpd
spec:
revisionHistoryLimit: 10
replicas: 3
template:
metadata:
labels:
name: httpd
run: httpd
spec:
containers:
- name: httpd-app
image: reg.yunwei.com/learn/httpd:v1
ports:
- containerPort: 80
[root@ren7 yaml]# cat httpdv2.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: httpd
spec:
revisionHistoryLimit: 10
replicas: 3
template:
metadata:
labels:
name: httpd
run: httpd
spec:
containers:
- name: httpd-app
image: reg.yunwei.com/learn/httpd:v2
ports:
- containerPort: 80
# nodeSelector:
# disktype: ssd
[root@ren7 yaml]# cat httpdv3.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: httpd
spec:
revisionHistoryLimit: 10
replicas: 3
template:
metadata:
labels:
name: httpd
run: httpd
spec:
containers:
- name: httpd-app
image: reg.yunwei.com/learn/httpd:v3
ports:
- containerPort: 80
2、部署应用并更新
后面一个部署的应用,会覆盖掉前面的(名称相同)
[root@ren7 yaml]# kubectl apply -f httpdv1.yaml --record
deployment.extensions/httpd created
[root@ren7 yaml]# kubectl get deployment httpd -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 3/3 3 3 33s httpd-app reg.yunwei.com/learn/httpd:v1 name=httpd,run=httpd
[root@ren7 yaml]# kubectl apply -f httpdv2.yaml --record
deployment.extensions/httpd configured
[root@ren7 yaml]# kubectl get deployment httpd -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 2/3 3 2 45s httpd-app reg.yunwei.com/learn/httpd:v2 name=httpd,run=httpd
[root@ren7 yaml]# kubectl apply -f httpdv3.yaml --record
deployment.extensions/httpd configured
[root@ren7 yaml]# kubectl get deployment httpd -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 2/3 2 2 54s httpd-app reg.yunwei.com/learn/httpd:v3 name=httpd,run=httpd
--record 的作用是将当前命令记录到 revision 记录中,这样我们就可以知道每个 revison 对应的是哪个配置文件。
3、通过 kubectl rollout history deployment httpd 查看 revison 历史记录。
[root@ren7 yaml]# kubectl rollout history deployment httpd
deployment.extensions/httpd
REVISION CHANGE-CAUSE
1 kubectl apply --filename=httpdv1.yaml --record=true
2 kubectl apply --filename=httpdv2.yaml --record=true
3 kubectl apply --filename=httpdv3.yaml --record=true
CHANGE-CAUSE 就是 --record 的结果。
4、如果要回滚到某个版本,比如 revision 1,可以执行命令 kubectl rollout undo deployment httpd --to-revision=1:
[root@ren7 yaml]# kubectl rollout undo deployment httpd --to-revision=1
deployment.extensions/httpd rolled back
[root@ren7 yaml]# kubectl get deployment httpd -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
httpd 3/3 3 3 4m31s httpd-app reg.yunwei.com/learn/httpd:v1 name=httpd,run=httpd
5、此时,revison 历史记录也会发生相应变化。
[root@ren7 yaml]# kubectl rollout history deployment httpd
deployment.extensions/httpd
REVISION CHANGE-CAUSE
2 kubectl apply --filename=httpdv2.yaml --record=true
3 kubectl apply --filename=httpdv3.yaml --record=true
4 kubectl apply --filename=httpdv1.yaml --record=true
revison 1 变成了 revison 4。不过我们可以通过 CHANGE-CAUSE 知道每个 revison 的具体含义。所以一定要在执行 kubectl apply 时加上 --record参数。