1. 简介
Deployment 背后管理着一个或多个 ReplicaSet,并通过 ReplicaSet 来管理着一组 Pod。
与 ReplicaSet 不同,Deployment 可以实现滚动更新的功能,即创建一个新的 ReplicaSet。此外,Deployment 也支持回滚,即启用旧的 ReplicaSet。
2. 用法
deploy.yaml
:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
-
创建
$ kubectl apply -f deploy.yaml deployment.apps/nginx-deployment created
-
罗列
$ kubectl get deploy NAME READY UP-TO-DATE AVAILABLE AGE nginx-deployment 3/3 3 3 118s
查看背后的 ReplicaSet:
$ kubectl get rs NAME DESIRED CURRENT READY AGE nginx-deployment-66b6c48dd5 3 3 3 2m51s
查看底层的 Pods:
$ kubectl get pod --show-labels NAME READY STATUS RESTARTS AGE LABELS nginx-deployment-66b6c48dd5-9gv9c 1/1 Running 0 4m17s app=nginx,pod-template-hash=66b6c48dd5 nginx-deployment-66b6c48dd5-fzl5q 1/1 Running 0 4m17s app=nginx,pod-template-hash=66b6c48dd5 nginx-deployment-66b6c48dd5-xzm9w 1/1 Running 0 4m17s app=nginx,pod-template-hash=66b6c48dd5
-
更新
首先修改容器镜像为
nginx:1.16.1
,然后执行变更:$ kubectl apply -f deploy.yaml deployment.apps/nginx-deployment configured
-
查看详细信息
$ kubectl describe deploy nginx-deployment Name: nginx-deployment Namespace: default CreationTimestamp: Sat, 23 Oct 2021 06:27:47 +0000 Labels: <none> Annotations: deployment.kubernetes.io/revision: 2 Selector: app=nginx Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 25% max unavailable, 25% max surge Pod Template: Labels: app=nginx Containers: nginx: Image: nginx:1.16.1 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-559d658b74 (3/3 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 8m35s deployment-controller Scaled up replica set nginx-deployment-66b6c48dd5 to 3 Normal ScalingReplicaSet 54s deployment-controller Scaled up replica set nginx-deployment-559d658b74 to 1 Normal ScalingReplicaSet 38s deployment-controller Scaled down replica set nginx-deployment-66b6c48dd5 to 2 Normal ScalingReplicaSet 38s deployment-controller Scaled up replica set nginx-deployment-559d658b74 to 2 Normal ScalingReplicaSet 34s deployment-controller Scaled down replica set nginx-deployment-66b6c48dd5 to 1 Normal ScalingReplicaSet 34s deployment-controller Scaled up replica set nginx-deployment-559d658b74 to 3 Normal ScalingReplicaSet 31s deployment-controller Scaled down replica set nginx-deployment-66b6c48dd5 to 0
查看
Events
字段可以看出,在 Deployment 执行滚动更新时,它会不断减少旧 ReplicaSet 所管理的 Pod,直到为 0,同时不断增加新 ReplicaSet 所管理的 Pod,直至到达期望状态。 -
回滚
首先查看 Deployment 的历史版本:
$ kubectl rollout history deploy nginx-deployment deployment.apps/nginx-deployment REVISION CHANGE-CAUSE 1 <none> 2 <none>
查看某个版本的具体信息:
$ kubectl rollout history deploy nginx-deployment --revision=1 deployment.apps/nginx-deployment with revision #1 Pod Template: Labels: app=nginx pod-template-hash=66b6c48dd5 Containers: nginx: Image: nginx:1.14.2 Port: 80/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none>
回滚到具体的版本:
$ kubectl rollout undo deploy nginx-deployment --to-revision=1 deployment.apps/nginx-deployment rolled back
回滚到上个版本:
$ kubectl rollout undo deploy nginx-deployment
-
暂停
$ kubectl rollout pause deploy nginx-deployment deployment.apps/nginx-deployment paused
-
恢复
$ kubectl rollout resume deploy nginx-deployment deployment.apps/nginx-deployment resumed
-
删除
$ kubectl delete -f deploy.yaml deployment.apps "nginx-deployment" deleted
连带着删除其背后的 ReplicaSet 和 Pod。