Deployment 为 Pod 和 ReplicaSet 提供了一个声明式定义 (declarative) 方法,用来替代以前的 ReplicationController 来方便的管理应用。在k8s集群中使用deloyment来达到pod预期的副本数
典型的用例如下:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx-deploy
image: nginx
ports:
- containerPort: 81
运行脚本如下:
[root@weizb-1 ~]# kubectl apply -f deployment.yaml
deployment.apps/nginx-deploy created
[root@weizb-1 ~]#
查看deployment,结果如下:
看到已有3个pod状态为running,且labels和设置的一样,如下:
[root@k8s-master-1 deployment]# kubectl get pod -n deployment --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-deployment-75b96b6748-5r64m 1/1 Running 0 4m53s app=nginx,pod-template-hash=75b96b6748
nginx-deployment-75b96b6748-6sxd6 1/1 Running 0 4m53s app=nginx,pod-template-hash=75b96b6748
nginx-deployment-75b96b6748-rmd6b 1/1 Running 0 4m53s app=nginx,pod-template-hash=75b96b6748
[root@k8s-master-1 deployment]#
如下查看deployment:
[root@k8s-master-1 deployment]# kubectl get deployment -n deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 6m56s
[root@k8s-master-1 deployment]#
如下查看创建rs副本:
[root@k8s-master-1 deployment]# kubectl get rs -n deployment
NAME DESIRED CURRENT READY AGE
nginx-deployment-75b96b6748 3 3 3 8m20s
[root@k8s-master-1 deployment]#
查看deloyment的信息,如下:
[root@k8s-master-1 ~]# kubectl describe deployment nginx-deployment
Name: nginx-deployment
Namespace: deployment
CreationTimestamp: Sat, 20 Feb 2021 15:31:13 +0800
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 1
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:
test-deployment:
Image: nginx:latest
Port: 80/TCP
Host Port: 0/TCP
Environment:
Mounts:
Volumes:
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets:
NewReplicaSet: nginx-deployment-75b96b6748 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 10m deployment-controller Scaled up replica set nginx-deployment-75b96b6748 to 3
查看此时deployment rollout状态,如下:
[root@k8s-master-1 ~]#
[root@k8s-master-1 ~]# kubectl rollout status deployment/nginx-deployment -n deployment
deployment "nginx-deployment" successfully rolled out
[root@k8s-master-1 ~]#
更新pod的nginx镜像的为v1.9,如下:
确认现在使用的镜像为nginx:latest,如下:
[root@k8s-master-1 ~]# kubectl get rs -n deployment -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx-deployment-75b96b6748 3 3 3 27m test-deployment nginx:latest app=nginx,pod-template-hash=75b96b6748
修改nginx镜像,如下:
[root@k8s-master-1 ~]# kubectl set image deployment nginx-deployment test-deployment=nginx:v1.9 --record -n deployment
deployment.apps/nginx-deployment image updated
可以直接编辑deployment来修改信息结果一样,如下:
[root@k8s-master-1 ~]# kubectl edit deployment/nginx-deploy
查看pod,正在删除重建,如下:
[root@k8s-master-1 ~]# kubectl get pod -n deployment -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-56d9f665d7-j7lqs 1/1 Running 0 4s 10.244.5.5 k8s-node-3
nginx-deployment-56d9f665d7-q5648 0/1 ContainerCreating 0 1s k8s-node-3
nginx-deployment-75b96b6748-5r64m 1/1 Running 0 30m 10.244.1.4 k8s-node-1
nginx-deployment-75b96b6748-6sxd6 1/1 Terminating 0 30m 10.244.5.4 k8s-node-3
nginx-deployment-75b96b6748-rmd6b 1/1 Running 0 30m 10.244.4.5 k8s-node-2
[root@k8s-master-1 ~]# kubectl get pod -n deployment -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-56d9f665d7-j7lqs 1/1 Running 0 10s 10.244.5.5 k8s-node-3
nginx-deployment-56d9f665d7-lhnbf 0/1 ContainerCreating 0 4s k8s-node-2
nginx-deployment-56d9f665d7-q5648 1/1 Running 0 7s 10.244.5.6 k8s-node-3
nginx-deployment-75b96b6748-5r64m 1/1 Running 0 30m 10.244.1.4 k8s-node-1
nginx-deployment-75b96b6748-6sxd6 0/1 Terminating 0 30m 10.244.5.4 k8s-node-3
nginx-deployment-75b96b6748-rmd6b 0/1 Terminating 0 30m 10.244.4.5 k8s-node-2
[root@k8s-master-1 ~]# kubectl get pod -n deployment -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-56d9f665d7-j7lqs 1/1 Running 0 15s 10.244.5.5 k8s-node-3
nginx-deployment-56d9f665d7-lhnbf 1/1 Running 0 9s 10.244.4.6 k8s-node-2
nginx-deployment-56d9f665d7-q5648 1/1 Running 0 12s 10.244.5.6 k8s-node-3
nginx-deployment-75b96b6748-5r64m 0/1 Terminating 0 30m 10.244.1.4 k8s-node-1
nginx-deployment-75b96b6748-6sxd6 0/1 Terminating 0 30m 10.244.5.4 k8s-node-3
nginx-deployment-75b96b6748-rmd6b 0/1 Terminating 0 30m 10.244.4.5 k8s-node-2
[root@k8s-master-1 ~]# kubectl get pod -n deployment -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-56d9f665d7-j7lqs 1/1 Running 0 2m9s 10.244.5.5 k8s-node-3
nginx-deployment-56d9f665d7-lhnbf 1/1 Running 0 2m3s 10.244.4.6 k8s-node-2
nginx-deployment-56d9f665d7-q5648 1/1 Running 0 2m6s 10.244.5.6 k8s-node-3
查看replicaset,副本镜像已经变成nginx:v1.9,如下:
[root@k8s-master-1 ~]# kubectl get rs -n deployment -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx-deployment-56d9f665d7 3 3 3 4m2s test-deployment nginx:v1.9 app=nginx,pod-template-hash=56d9f665d7
nginx-deployment-75b96b6748 0 0 0 34m test-deployment nginx:latest app=nginx,pod-template-hash=75b96b6748
查看此时deployment信息,如下:
[root@k8s-master-1 ~]# kubectl describe deployment nginx-deployment -n deployment
Name: nginx-deployment
Namespace: deployment
CreationTimestamp: Sat, 20 Feb 2021 15:31:13 +0800
Labels: app=nginx
Annotations: deployment.kubernetes.io/revision: 2
kubernetes.io/change-cause: kubectl set image deployment nginx-deployment test-deployment=nginx:v1.9 --record=true --namespace=deployment
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:
test-deployment:
Image: nginx:v1.9
Port: 80/TCP
Host Port: 0/TCP
Environment:
Mounts:
Volumes:
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets:
NewReplicaSet: nginx-deployment-56d9f665d7 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 41m deployment-controller Scaled up replica set nginx-deployment-75b96b6748 to 3
Normal ScalingReplicaSet 11m deployment-controller Scaled up replica set nginx-deployment-56d9f665d7 to 1
Normal ScalingReplicaSet 11m deployment-controller Scaled down replica set nginx-deployment-75b96b6748 to 2
Normal ScalingReplicaSet 11m deployment-controller Scaled up replica set nginx-deployment-56d9f665d7 to 2
Normal ScalingReplicaSet 11m deployment-controller Scaled down replica set nginx-deployment-75b96b6748 to 1
Normal ScalingReplicaSet 11m deployment-controller Scaled up replica set nginx-deployment-56d9f665d7 to 3
Normal ScalingReplicaSet 11m deployment-controller Scaled down replica set nginx-deployment-75b96b6748 to 0
[root@k8s-master-1 ~]#
从desribe中可以看出,第一次创建时deployment时创建了可以名为nginx-deployment-75b96b6748的rs,直接扩容副本数为3
更新deployment时,创建了一个名为nginx-deployment-56d9f665d7的rs,副本数扩展为1。
之前的rs副本数降为2,这样至少有2个pod可用,最多创建了4个pod,以此类推。
使用相同的滚动更新策略向上和向下扩展新旧rs,最后保证新的rs 3个 pod,旧的pod为0
创建 Deployment 的时候使用了–recored 参数可以记录命令,我们可以很方便的查看每次 revison 的变化。
假设又进行了几次更新,具体过程和上述一直,查看回滚历史:
[root@k8s-master-1 ~]# kubectl rollout history deployment/nginx-deployment -n deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 kubectl set image deployment nginx-deployment test-deployment=nginx:v1.9 --record=true --namespace=deployment
2 kubectl set image deployment nginx-deployment test-deployment=google/cadvisor:latest --record=true --namespace=deployment
查看此时pod的image如下:
[root@k8s-master-1 ~]# kubectl get rs -n deployment -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx-deployment-56d9f665d7 0 0 0 35m test-deployment nginx:v1.9 app=nginx,pod-template-hash=56d9f665d7
nginx-deployment-75b96b6748 0 0 0 65m test-deployment nginx:latest app=nginx,pod-template-hash=75b96b6748
nginx-deployment-b78df489c 3 3 3 44s test-deployment google/cadvisor:latest app=nginx,pod-template-hash=b78df489c
将deployment回到上一次版本,如下:
[root@k8s-master-1 ~]# kubectl rollout undo deployment/nginx-deployment -n deployment
deployment.apps/nginx-deployment rolled back
[root@k8s-master-1 ~]# kubectl get rs -n deployment -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx-deployment-56d9f665d7 3 3 2 40m test-deployment nginx:v1.9 app=nginx,pod-template-hash=56d9f665d7
nginx-deployment-75b96b6748 0 0 0 71m test-deployment nginx:latest app=nginx,pod-template-hash=75b96b6748
nginx-deployment-b78df489c 1 1 1 6m6s test-deployment google/cadvisor:latest app=nginx,pod-template-hash=b78df489c
[root@k8s-master-1 ~]# kubectl get rs -n deployment -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx-deployment-56d9f665d7 3 3 3 40m test-deployment nginx:v1.9 app=nginx,pod-template-hash=56d9f665d7
nginx-deployment-75b96b6748 0 0 0 71m test-deployment nginx:latest app=nginx,pod-template-hash=75b96b6748
nginx-deployment-b78df489c 0 0 0 6m12s test-deployment google/cadvisor:latest app=nginx,pod-template-hash=b78df489c
[root@k8s-master-1 ~]#
如果要回退到指定版本,可以指定以下参数,如下:
kubectl rollout undo deployment/nginx-deployment -n deployment --to-revision=2
--to-revision=2指定回退到哪一个版本。
[root@k8s-master-1 ~]#
[root@k8s-master-1 ~]# kubectl scale deployment/nginx-deployment --replicas=5 -n deployment
deployment.apps/nginx-deployment scaled
[root@k8s-master-1 ~]#
查看pod如下,pod数量已经变成5个
[root@k8s-master-1 ~]# kubectl get po -n deployment
NAME READY STATUS RESTARTS AGE
nginx-deployment-56d9f665d7-442m7 1/1 Running 0 8m41s
nginx-deployment-56d9f665d7-7xln2 1/1 Running 0 8m43s
nginx-deployment-56d9f665d7-npf5l 1/1 Running 0 8m38s
nginx-deployment-56d9f665d7-p4sqm 1/1 Running 0 3s
nginx-deployment-56d9f665d7-rqwl2 1/1 Running 0 2s
[root@k8s-master-1 ~]# kubectl get rs -n deployment
NAME DESIRED CURRENT READY AGE
nginx-deployment-56d9f665d7 5 5 5 49m
nginx-deployment-75b96b6748 0 0 0 79m
nginx-deployment-b78df489c 0 0 0 14m
也可以使用以下方式,效果一样,如下:
[root@k8s-master-1 ~]# kubectl edit deployment/nginx-deployment -n deployment
[root@k8s-master-1 ~]# kubectl rollout pause deployment/nginx-deployment -n deployment
deployment.apps/nginx-deployment paused
对nginx-deployment进行更新,如下:
[root@k8s-master-1 ~]# kubectl set image deployment/nginx-deployment -n deployment test-deployment=barnybug/elasticsearch:1.7.2 --record=true
deployment.apps/nginx-deployment image updated
[root@k8s-master-1 ~]#
[root@k8s-master-1 ~]# kubectl get rs -n deployment -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx-deployment-56d9f665d7 5 5 5 67m test-deployment nginx:v1.9 app=nginx,pod-template-hash=56d9f665d7
nginx-deployment-75b96b6748 0 0 0 98m test-deployment nginx:latest app=nginx,pod-template-hash=75b96b6748
nginx-deployment-b78df489c 0 0 0 33m test-deployment google/cadvisor:latest app=nginx,pod-template-hash=b78df489c
[root@k8s-master-1 ~]# kubectl rollout history deployment/nginx-deployment -n deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 kubectl set image deployment nginx-deployment nginx=nginx:v1.9 --record=true --namespace=deployment
3 kubectl set image deployment nginx-deployment test-deployment=google/cadvisor:latest --record=true --namespace=deployment
4 kubectl set image deployment nginx-deployment test-deployment=nginx:v1.9 --record=true --namespace=deployment
以上可以看到deployment并没有进行更新。
对deployment进行恢复,如下:
[root@k8s-master-1 ~]# kubectl rollout resume deployment/nginx-deployment -n deployment
deployment.apps/nginx-deployment resumed
再次查看rs看到pod的image已经变为pause时设置的镜像,如下:
[root@k8s-master-1 ~]# kubectl get rs -n deployment -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx-deployment-56d9f665d7 2 3 3 71m test-deployment nginx:v1.9 app=nginx,pod-template-hash=56d9f665d7
nginx-deployment-75b96b6748 0 0 0 101m test-deployment nginx:latest app=nginx,pod-template-hash=75b96b6748
nginx-deployment-b78df489c 0 0 0 36m test-deployment google/cadvisor:latest app=nginx,pod-template-hash=b78df489c
nginx-deployment-c8688cb9c 4 4 3 4s test-deployment barnybug/elasticsearch:1.7.2 app=nginx,pod-template-hash=c8688cb9c
再次查看deployment的rollout已经有了新的更新,如下:
[root@k8s-master-1 ~]# kubectl rollout history deployment/nginx-deployment -n deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 kubectl set image deployment nginx-deployment nginx=nginx:v1.9 --record=true --namespace=deployment
3 kubectl set image deployment nginx-deployment test-deployment=google/cadvisor:latest --record=true --namespace=deployment
4 kubectl set image deployment nginx-deployment test-deployment=nginx:v1.9 --record=true --namespace=deployment
5 kubectl set image deployment/nginx-deployment test-deployment=barnybug/elasticsearch:1.7.2 --namespace=deployment --record=true