一、概述
Deployment对象,是用于部署应用的对象,它是K8S中最常用的一个对象,它为ReplicaSet和Pod的创建提供了一种声明式的定义方法,从而无需手动创建RS和pod 对象,使用Deployment而不直接创建RS是因为Deployment对象拥有许多RS没有的特性,如滚动升级和回滚等。
场景的应用场景:
- 定义Deployment来创建Pod和ReplicaSet
- 滚动升级和回滚应用
- 扩容和缩容
- 暂停和继续Deployment
(二)、Deployment的创建和更新
最简单的Deployment应用如下:
[root@k8s-master deployment]# cat 2.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2 # tells deployment to run 2 pods matching the template
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
[root@k8s-master deployment]# kubectl apply -f 2.yml
deployment.apps/nginx-deployment created
[root@k8s-master deployment]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 2/2 2 2 55s
root@k8s-master deployment]# kubectl describe nginx-deployment
error: the server doesn't have a resource type "nginx-deployment"
[root@k8s-master deployment]# kubectl describe deployment/nginx-deployment
Name: nginx-deployment
Namespace: default
CreationTimestamp: Tue, 28 Dec 2021 02:57:29 -0500
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx:1.14.2
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-deployment-66b6c48dd5 (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 99s deployment-controller Scaled up replica set nginx-deployment-66b6c48dd5 to 2
[root@k8s-master deployment]# kubectl get pods,rs -l app=nginx
NAME READY STATUS RESTARTS AGE
pod/nginx-5vxzv 1/1 Running 0 8d
pod/nginx-6m64m 1/1 Running 0 8d
pod/nginx-9f8n8 1/1 Running 0 8d
pod/nginx-deployment-66b6c48dd5-cpd7h 1/1 Running 0 14m
pod/nginx-deployment-66b6c48dd5-hxnl2 1/1 Running 0 14m
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deployment-66b6c48dd5 2 2 2 14m
(三)、Deployment的更新
可以通过更新一个新的 YAML 文件来更新 Deployment。下面的 YAML 文件指定该 Deployment 镜像更新为 nginx 1.16.1。
3.1、通过更新yml文件进行更新
[root@k8s-master deployment]# cat 2.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 3 # tells deployment to run 2 pods matching the template
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16.1
ports:
- containerPort: 80
[root@k8s-master deployment]# kubectl apply -f 2.yml
deployment.apps/nginx-deployment configured
[root@k8s-master deployment]# kubectl get pods,rs -l app=nginx
NAME READY STATUS RESTARTS AGE
pod/nginx-5vxzv 1/1 Running 0 8d
pod/nginx-6m64m 1/1 Running 0 8d
pod/nginx-9f8n8 1/1 Running 0 8d
pod/nginx-deployment-559d658b74-49qvd 1/1 Running 0 5m52s
pod/nginx-deployment-559d658b74-b9g49 1/1 Running 0 5m52s
pod/nginx-deployment-559d658b74-kgnr6 1/1 Running 0 6m24s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deployment-559d658b74 3 3 3 6m45s
replicaset.apps/nginx-deployment-5d7bb69767 0 0 0 3m32s
replicaset.apps/nginx-deployment-66b6c48dd5 0 0 0 24m
3.2、通过命令进行更新,一种通过set,另一种通过edit命令进行编辑
[root@k8s-master deployment]# kubectl set image deployment/nginx-deployment nginx=1.9.1
deployment.apps/nginx-deployment image updated
[root@k8s-master deployment]# kubectl edit deployment/nginx-deployment
deployment.apps/nginx-deployment edited
[root@k8s-master deployment]# kubectl rollout status deployment/nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 4 new replicas have been updated...
[root@k8s-master deployment]# kubectset image deployment/nginx-deployment nginx=nginx:1.15.1
deployment.apps/nginx-deployment image updated
####查看更新的进度
[root@k8s-master deployment]# kubectl rollout status deployment/nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 3 of 4 updated replicas are available...
deployment "nginx-deployment" successfully rolled out
(四)、Deployment的回滚
如果发现nginx的1.16.1版本不稳定,可以回滚到1.14.1版本中,此时不需要手动更改deployment文件,而可以利用deployment的rollout回滚功能。
4.1、查看历史版本
[root@k8s-master deployment]# kubectl rollout history deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
3 <none>
4 <none>
5 <none>
7 <none>
8 <none>
9 <none>
10 <none>
由于创建的时候没有添加--record参数来记录命令,不是很容易查查每次revison的变化,因此可以通过describe或rollout history来查看下详细信息
4.2、查看版本的详细信息
[root@k8s-master deployment]# kubectl rollout history deployment/nginx-deployment --revision=4
deployment.apps/nginx-deployment with revision #4
Pod Template:
Labels: app=nginx
pod-template-hash=559d658b74
Containers:
nginx:
Image: nginx:1.16.1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
[root@k8s-master deployment]# kubectl rollout history deployment/nginx-deployment --revision=7
deployment.apps/nginx-deployment with revision #7
Pod Template:
Labels: app=nginx
pod-template-hash=79dcf65f46
Containers:
nginx:
Image: 1.14.1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
发下版本7的nginx是1.14.1,可以通过rollout undo来回滚到指定的版本中
4.3、回滚指定的版本
[root@k8s-master deployment]# kubectl rollout undo deployment/nginx-deployment --to-revision=7
deployment.apps/nginx-deployment rolled back
[root@k8s-master deployment]# kubectl describe deployment/nginx-deployment
Name: nginx-deployment
Namespace: default
CreationTimestamp: Tue, 28 Dec 2021 02:57:29 -0500
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 11
Selector: app=nginx
Replicas: 4 desired | 2 updated | 5 total | 3 available | 2 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: 1.14.1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True ReplicaSetUpdated
OldReplicaSets: nginx-deployment-69cc985499 (3/3 replicas created)
NewReplicaSet: nginx-deployment-79dcf65f46 (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 54m deployment-controller Scaled up replica set nginx-deployment-66b6c48dd5 to 2
Normal ScalingReplicaSet 37m deployment-controller Scaled up replica set nginx-deployment-559d658b74 to 1
Normal ScalingReplicaSet 36m deployment-controller Scaled down replica set nginx-deployment-66b6c48dd5 to 1
Normal ScalingReplicaSet 36m deployment-controller Scaled up replica set nginx-deployment-559d658b74 to 2
Normal ScalingReplicaSet 36m deployment-controller Scaled down replica set nginx-deployment-66b6c48dd5 to 0
Normal ScalingReplicaSet 33m deployment-controller Scaled up replica set nginx-deployment-5d7bb69767 to 1
Normal ScalingReplicaSet 30m deployment-controller Scaled down replica set nginx-deployment-5d7bb69767 to 0
Normal ScalingReplicaSet 20m deployment-controller Scaled up replica set nginx-deployment-698694d5d4 to 1
Normal ScalingReplicaSet 20m (x2 over 33m) deployment-controller Scaled down replica set nginx-deployment-559d658b74 to 3
Normal ScalingReplicaSet 20m (x2 over 36m) deployment-controller Scaled up replica set nginx-deployment-559d658b74 to 4
Normal ScalingReplicaSet 18m deployment-controller Scaled up replica set nginx-deployment-59dd995d49 to 2
Normal ScalingReplicaSet 17m (x15 over 20m) deployment-controller (combined from similar events): Scaled down replica set nginx-deployment-559d658b74 to 0
Normal ScalingReplicaSet 32s deployment-controller Scaled up replica set nginx-deployment-79dcf65f46 to 1
Normal ScalingReplicaSet 32s deployment-controller Scaled down replica set nginx-deployment-69cc985499 to 3
Normal ScalingReplicaSet 32s deployment-controller Scaled up replica set nginx-deployment-79dcf65f46 to 2
可以通过设置 .spec.revisonHistoryLimit 项来指定 deployment 最多保留多少 revison 历史记录。默认的会保留所有的 revision;如果将该项设置为 0,Deployment 就不允许回退了。
总结:
rollout命令的更多用法:
- history(查看历史版本)
- pause(暂停Deployment)
- resume(恢复暂停的Deployment)
- status(查看资源状态)
- undo(回滚版本) 滚动更新相关配置:
- .spec.minReadySeconds: 新创建的Pod状态为Ready持续的时间至少为.spec.minReadySeconds才认为Pod Available(Ready)。
- .spec.strategy.rollingUpdate.maxSurge: specifies the maximum number of Pods that can be created over the desired number of Pods. The value cannot be 0 if MaxUnavailable is 0. 可以为整数或者百分比,默认为desired Pods数的25%. Scale Up新的ReplicaSet时,按照比例计算出允许的MaxSurge,计算时向上取整(比如3.4,取4)。
- .spec.strategy.rollingUpdate.maxUnavailable: specifies the maximum number of Pods that can be unavailable during the update process. The value cannot be 0 if maxSurge is 0.可以为整数或者百分比,默认为desired Pods数的25%. Scale Down旧的ReplicaSet时,按照比例计算出允许的maxUnavailable,计算时向下取整(比如3.6,取3)。 因此,在Deployment rollout时,需要保证Available(Ready) Pods数不低于 desired pods number - maxUnavailable; 保证所有的Pods数不多于 desired pods number + maxSurge。