0%

K8S|6.deployment常用操作

定义

参考:https://kubernetes.io/zh-cn/docs/concepts/workloads/controllers/deployment/

创建

vim nginx-deployment.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx-deployment
template:
metadata:
name: nginx-deployment
labels:
app: nginx-deployment
spec:
containers:
- name: nginx-deployment
image: registry.cn-hangzhou.aliyuncs.com/alxq/nginx:stable-alpine3.17
imagePullPolicy: IfNotPresent
restartPolicy: Always
1
kubectl apply -f nginx-deployment.yml

查看

1
2
3
4
5
6
7
8
9
10
11
12
13
kubectl get deployment nginx-deployment  		##或者可以写作kubectl get deployment/nginx-deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 5h31m


在检查集群中的 Deployment 时,所显示的字段有:

NAME 列出了名字空间中 Deployment 的名称。
READY 显示应用程序的可用的“副本”数。显示的模式是“就绪个数/期望个数”。
UP-TO-DATE 显示为了达到期望状态已经更新的副本数。
AVAILABLE 显示应用可供用户使用的副本数。
AGE 显示应用程序运行的时间。
请注意期望副本数是根据 .spec.replicas 字段设置 3。
1
2
#查看deployment详细描述信息
kubectl describe deployment nginx-deployment

扩/缩容副本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-58d666ff55-2z556 1/1 Running 0 4m9s
nginx-deployment-58d666ff55-gs2kl 1/1 Running 0 4m20s
nginx-deployment-58d666ff55-hn9s8 1/1 Running 0 4m18s

kubectl scale deployment nginx-deployment --replicas=5

kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-58d666ff55-2z556 1/1 Running 0 4m37s
nginx-deployment-58d666ff55-c84mh 1/1 Running 0 23s
nginx-deployment-58d666ff55-gs2kl 1/1 Running 0 4m48s
nginx-deployment-58d666ff55-hn9s8 1/1 Running 0 4m46s
nginx-deployment-58d666ff55-jpvzc 1/1 Running 0 24s

回滚

Deployment 的 Pod 模板(.spec.template)发生更改时,才会创建新修订版本 – 例如,模板的标签或容器镜像发生变化。 其他更新,如 Deployment 的扩缩容操作不会创建 Deployment 修订版本。 这是为了方便同时执行手动缩放或自动缩放。 换言之,当你回滚到较早的修订版本时,只有 Deployment 的 Pod 模板部分会被回滚。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 查看 Deployment 上线状态
kubectl rollout status deployment nginx-deployment

# 检查 Deployment 修订历史
kubectl rollout history deployment/nginx-deployment

# 查看修订历史的详细信息
kubectl rollout history deployment/nginx-deployment --revision=2

# 回滚到之前的修订版本
kubectl rollout undo deployment/nginx-deployment #回滚到上个版本
kubectl rollout undo deployment/nginx-deployment --to-revision=2 #回滚到指定版本

# 重新部署当前版本
kubectl rollout restart deployment/nginx-deployment

暂停、更新、恢复

在你更新一个 Deployment 的时候,你可以在触发一个或多个更新之前暂停 Deployment 的上线过程。 当你准备应用这些变更时,你可以重新恢复 Deployment 上线过程。 这样做使得你能够在暂停和恢复执行之间应用多个修补程序,而不会触发不必要的上线操作。

1
2
3
4
5
6
7
8
9
10
11
12
#在做这个之前,我们可以监控pod(或者rs)的状态变化,暂停deploy后进行更新,pod没有触发更新,在恢复后进行一次变更
kubectl get po -l app=nginx-deployment -w

# 暂停deployment
kubectl rollout pause deployment/nginx-deployment

# 更新deployment
kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1
kubectl set resources deployment/nginx-deployment -c=nginx-deployment --limits=cpu=200m,memory=512Mi

# 恢复deployment
kubectl rollout resume deployment/nginx-deployment

删除

1
2
3
4
5
6
# 删除deployment
kubectl delete deployment nginx-deployment
kubectl delete -f nginx-deployment.yml #建议使用指定配置文件删除

#删除命名空间下所有资源
kubectl delete all --all [-n namespace-name]