Deployments
一个 Deployment 为 Pod 和 ReplicaSet 提供声明式的更新能力。
你负责描述 Deployment 中的目标状态 ,而 Deployment 控制器(Controller) 以受控速率更改实际状态, 使其变为期望状态。你可以定义 Deployment 以创建新的 ReplicaSet,或删除现有 Deployment, 并通过新的 Deployment 收养其资源。
下面是一个 Deployment 示例。其中创建了一个 ReplicaSet,负责启动三个 nginx
Pod:
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
查看deployment的状态:
powershell
root@k8s-master:~# kubectl get deploy nginx-deployment -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx-deployment 2/3 3 2 71s nginx nginx:1.14.2 app=nginx
要查看 Deployment 上线状态:
powershell
root@k8s-master:~# kubectl rollout status deploy nginx-deployment
deployment "nginx-deployment" successfully rolled out
更新 Deployment
-
先来更新 nginx Pod 以使用
nginx:1.16.1
镜像,而不是nginx:1.14.2
镜像。powershellroot@k8s-master:~# kubectl get deploy nginx-deployment -o wide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR nginx-deployment 3/3 3 3 10m nginx nginx:1.14.2 app=nginx #更新 root@k8s-master:~# kubectl set image deploy nginx-deployment nginx=nginx:1.16.1 deployment.apps/nginx-deployment image updated
-
查看上线状态:
powershellroot@k8s-master:~# kubectl rollout status deployment nginx-deployment deployment "nginx-deployment" successfully rolled out root@k8s-master:~# kubectl get deployment nginx-deployment -o wide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR nginx-deployment 3/3 3 3 13m nginx nginx:1.16.1 app=nginx #看到版本已经变更为1.16.1
回滚 Deployment
有时,你可能想要回滚 Deployment;例如,当 Deployment 不稳定时(例如进入反复崩溃状态)。 默认情况下,Deployment 的所有上线记录都保留在系统中,以便可以随时回滚 (你可以通过修改修订历史记录限制来更改这一约束)。
-
假设你在更新 Deployment 时犯了一个拼写错误,将镜像名称命名设置为
nginx:1.161
而不是nginx:1.16.1
:powershellroot@k8s-master:~# kubectl set image deployment/nginx-deployment nginx=nginx:1.161 deployment.apps/nginx-deployment image updated
-
查看状态
powershellroot@k8s-master:~# kubectl rollout status deployment nginx-deployment Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
检查 Deployment 上线历史
首先,检查 Deployment 修订历史:
powershell
root@k8s-master:~# kubectl rollout history deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
2 <none>
3 <none>
CHANGE-CAUSE
的内容是从 Deployment 的 kubernetes.io/change-cause
注解复制过来的。 复制动作发生在修订版本创建时。你可以通过以下方式设置 CHANGE-CAUSE
消息:
- 使用
kubectl annotate deployment/nginx-deployment kubernetes.io/change-cause="image updated to 1.16.1"
为 Deployment 添加注解。 - 手动编辑资源的清单。
要查看修订历史的详细信息,运行:
powershell
kubectl rollout history deployment/nginx-deployment --revision=2
powershell
root@k8s-master:~# kubectl rollout history deployment/nginx-deployment --revision=2
deployment.apps/nginx-deployment with revision #2
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>
回滚到之前的修订版本
按照下面给出的步骤将 Deployment 从当前版本回滚到以前的版本(即版本 2)。
假定现在你已决定撤消当前上线并回滚到以前的修订版本:
powershell
root@k8s-master:~# kubectl rollout undo deployment/nginx-deployment
deployment.apps/nginx-deployment rolled back
检查回滚是否成功以及 Deployment 是否正在运行,运行:
powershell
root@k8s-master:~# kubectl get deployment nginx-deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 13m
获取 Deployment 描述信息:
powershell
root@k8s-master:~# kubectl describe deployment nginx-deployment
Name: nginx-deployment
Namespace: default
CreationTimestamp: Mon, 23 Oct 2023 16:17:23 +0800
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 4
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 17m deployment-controller Scaled up replica set nginx-deployment-66b6c48dd5 to 3
Normal ScalingReplicaSet 14m deployment-controller Scaled up replica set nginx-deployment-559d658b74 to 1
Normal ScalingReplicaSet 14m deployment-controller Scaled down replica set nginx-deployment-66b6c48dd5 to 2
Normal ScalingReplicaSet 14m deployment-controller Scaled up replica set nginx-deployment-559d658b74 to 2
Normal ScalingReplicaSet 14m deployment-controller Scaled down replica set nginx-deployment-66b6c48dd5 to 1
Normal ScalingReplicaSet 14m deployment-controller Scaled up replica set nginx-deployment-559d658b74 to 3
Normal ScalingReplicaSet 14m deployment-controller Scaled down replica set nginx-deployment-66b6c48dd5 to 0
Normal ScalingReplicaSet 12m deployment-controller Scaled up replica set nginx-deployment-66bc5d6c8 to 1
Normal ScalingReplicaSet 7m46s deployment-controller Scaled down replica set nginx-deployment-66bc5d6c8 to 0
版本已经回退到1.16.1
缩放 Deployment
你可以使用如下指令缩放 Deployment:
powershell
root@k8s-master:~# kubectl scale deployment nginx-deployment --replicas=10
deployment.apps/nginx-deployment scaled
查看
powershell
root@k8s-master:~# kubectl get deploy nginx-deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 10/10 10 10 38m
暂停、恢复 Deployment 的上线过程
在你更新一个 Deployment 的时候,或者计划更新它的时候, 你可以在触发一个或多个更新之前暂停 Deployment 的上线过程。 当你准备应用这些变更时,你可以重新恢复 Deployment 上线过程。 这样做使得你能够在暂停和恢复执行之间应用多个修补程序,而不会触发不必要的上线操作。
powershell
#获取deploy信息
root@k8s-master:~# kubectl get deploy nginx-deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 10/10 10 10 38m
使用如下指令暂停上线:
powershell
root@k8s-master:~# kubectl rollout pause deployment/nginx-deployment
deployment.apps/nginx-deployment paused
root@k8s-master:~# kubectl get deployment nginx-deployment -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx-deployment 10/10 10 10 78m nginx nginx:1.16.1 app=nginx
查看变更历史:
powershell
root@k8s-master:~# kubectl rollout history deployment nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
3 <none>
4 <none>
更新镜像:
powershell
kubectl set image deployment/nginx-deployment nginx=nginx:1.16.2
查看历史(新上线没有被触发):
powershell
root@k8s-master:~# kubectl rollout history deployment nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
3 <none>
4 <none>
后续操作,如设置资源:
powershell
kubectl set resources deployment/nginx-deployment -c=nginx --limits=cpu=200m,memory=512Mi
最终,恢复 Deployment 上线并观察新的 ReplicaSet 的创建过程,其中包含了所应用的所有更新:
powershell
root@k8s-master:~# kubectl rollout resume deployment/nginx-deployment
deployment.apps/nginx-deployment resumed
powershell
root@k8s-master:~# kubectl get deployments nginx-deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 8/10 5 8 91m
powershell
root@k8s-master:~# kubectl rollout history deployment nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
3 <none>
4 <none>
5 <none>
#发布历史又出现了新的一条