Deployment(部署)与ReplicaSet(副本集)
kubernetes Deployment 详解 更新/回滚/缩放/暂停/恢复部署操作_还是那片西瓜吗的博客-CSDN博客_k8s停止deployment
Deployment是对ReplicaSet和Pod更高级的抽象。
它使Pod拥有多副本,自愈,扩缩容、滚动升级等能力。
ReplicaSet(副本集)是一个Pod的集合。
它可以设置运行Pod的数量,确保任何时间都有指定数量的 Pod 副本在运行。
通常我们不直接使用ReplicaSet,而是在Deployment中声明。
#创建deployment,部署3个运行nginx的Pod kubectl create deployment nginx-deployment --image=nginx:1.22 --replicas=3 #查看deployment kubectl get deploy #查看replicaSet kubectl get rs #删除deployment kubectl delete deploy nginx-deployment #将副本数量调整为5 kubectl scale deployment/nginx-deployment --replicas=5 kubectl get deploy #自动缩放 kubectl autoscale deployment/nginx-auto --min=3 --max=10 --cpu-percent=75 #查看自动缩放 kubectl get hpa #删除自动缩放 kubectl delete hpa nginx-deployment #查看版本和Pod kubectl get deployment/nginx-deployment -owide kubectl get pods #更新容器镜像 kubectl set image deployment/nginx-deployment nginx=nginx:1.23 #滚动更新 kubectl rollout status deployment/nginx-deployment #查看过程 kubectl get rs --watch #查看历史版本 kubectl rollout history deployment/nginx-deployment #查看指定版本的信息 kubectl rollout history deployment/nginx-deployment --revision=2 #回滚到历史版本 kubectl rollout undo deployment/nginx-deployment --to-revision=2
创建
[root@k8s ~]# kubectl create deployment nginx-deploy --image=nginx:1.22 --replicas=3 deployment.apps/nginx-deploy created [root@k8s ~]# kubectl get deploy NAME READY UP-TO-DATE AVAILABLE AGE nginx-deploy 3/3 3 3 7s [root@k8s ~]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-deploy-855866bb46-dvx8s 1/1 Running 0 15s nginx-deploy-855866bb46-nvg4n 1/1 Running 0 15s nginx-deploy-855866bb46-ndl2z 1/1 Running 0 15s [root@k8s ~]# kubectl get rs NAME DESIRED CURRENT READY AGE nginx-deploy-855866bb46 3 3 3 23s
自愈
删除最后一个pod--ndl2z,发现自动生成一个新的6m6s2
[root@k8s ~]# kubectl delete pod nginx-deploy-855866bb46-ndl2z pod "nginx-deploy-855866bb46-ndl2z" deleted [root@k8s ~]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx-deploy-855866bb46-dvx8s 1/1 Running 0 65s nginx-deploy-855866bb46-nvg4n 1/1 Running 0 65s nginx-deploy-855866bb46-6m6s2 1/1 Running 0 4s
扩容
开两个终端,一个动态查看ReplicaSet,另一个修改replicas。
下面是修改前,replicas=3
[root@k8s ~]# kubectl get rs --watch NAME DESIRED CURRENT READY AGE nginx-deploy-855866bb46 3 3 3 85s
增加修改
[root@k8s ~]# kubectl scale deploy nginx-deploy --replicas=5 deployment.apps/nginx-deploy scaled 查看之前的终端 [root@k8s ~]# kubectl get rs --watch NAME DESIRED CURRENT READY AGE nginx-deploy-855866bb46 3 3 3 85s nginx-deploy-855866bb46 5 3 3 4m54s nginx-deploy-855866bb46 5 3 3 4m54s nginx-deploy-855866bb46 5 5 3 4m54s nginx-deploy-855866bb46 5 5 4 4m56s nginx-deploy-855866bb46 5 5 5 4m56s [root@k8s ~]# kubectl scale deploy nginx-deploy --replicas=3 deployment.apps/nginx-deploy scaled 查看之前的终端 [root@k8s ~]# kubectl get rs --watch NAME DESIRED CURRENT READY AGE nginx-deploy-855866bb46 3 3 3 85s nginx-deploy-855866bb46 5 3 3 4m54s nginx-deploy-855866bb46 5 3 3 4m54s nginx-deploy-855866bb46 5 5 3 4m54s nginx-deploy-855866bb46 5 5 4 4m56s nginx-deploy-855866bb46 5 5 5 4m56s nginx-deploy-855866bb46 3 5 5 6m6s nginx-deploy-855866bb46 3 5 5 6m7s nginx-deploy-855866bb46 3 3 3 6m7s
自动缩放
自动缩放通过增加和减少副本的数量,以保持所有 Pod 的平均 CPU 利用率不超过 75%。
自动伸缩需要声明Pod的资源限制,同时使用Metrics Server 服务(K3s默认已安装)。
#自动缩放 kubectl autoscale deployment/nginx-auto --min=3 --max=10 --cpu-percent=75 #查看自动缩放 kubectl get hpa #删除自动缩放 kubectl delete hpa nginx-deployment
滚动更新
还是两个终端,
原终端动态查看副本集
[root@k8s ~]# kubectl get rs --watch NAME DESIRED CURRENT READY AGE nginx-deploy-855866bb46 3 3 3 3m34s
新建终端,首先查看nginx版本是1.22
[root@k8s ~]# kubectl get deploy -owide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR nginx-deploy 3/3 3 3 24m nginx nginx:1.22 app=nginx-deploy
我们要实现1.22->1.23
[root@k8s ~]# kubectl set image deployment/nginx-deploy nginx=nginx:1.23 deployment.apps/nginx-deploy image updated
查看副本集变化
[root@k8s ~]# kubectl get rs --watch NAME DESIRED CURRENT READY AGE nginx-deploy-855866bb46 3 3 3 3m34s nginx-deploy-7c9c6446b7 1 0 0 0s nginx-deploy-7c9c6446b7 1 0 0 0s nginx-deploy-7c9c6446b7 1 1 0 0s nginx-deploy-7c9c6446b7 1 1 1 1s nginx-deploy-855866bb46 2 3 3 3m55s nginx-deploy-7c9c6446b7 2 1 1 1s nginx-deploy-855866bb46 2 3 3 3m55s nginx-deploy-7c9c6446b7 2 1 1 1s nginx-deploy-855866bb46 2 2 2 3m55s nginx-deploy-7c9c6446b7 2 2 1 1s nginx-deploy-7c9c6446b7 2 2 2 2s nginx-deploy-855866bb46 1 2 2 3m56s nginx-deploy-7c9c6446b7 3 2 2 2s nginx-deploy-855866bb46 1 2 2 3m56s nginx-deploy-7c9c6446b7 3 2 2 2s nginx-deploy-855866bb46 1 1 1 3m56s nginx-deploy-7c9c6446b7 3 3 2 2s nginx-deploy-7c9c6446b7 3 3 3 4s nginx-deploy-855866bb46 0 1 1 3m58s nginx-deploy-855866bb46 0 1 1 3m58s nginx-deploy-855866bb46 0 0 0 3m58s
删除,只有未执行的副本集可以删除,执行的删除会自动重建
先查看副本集 [root@k8s ~]# kubectl get rs NAME DESIRED CURRENT READY AGE nginx-deploy-7c9c6446b7 3 3 3 55s nginx-deploy-855866bb46 0 0 0 4m49s 删除两个 [root@k8s ~]# kubectl delete rs nginx-deploy-7c9c6446b7 replicaset.apps "nginx-deploy-7c9c6446b7" deleted [root@k8s ~]# kubectl delete rs nginx-deploy-855866bb46 replicaset.apps "nginx-deploy-855866bb46" deleted 发现7c的重新搭建了,85的删除后就消失 [root@k8s ~]# kubectl get rs --watch NAME DESIRED CURRENT READY AGE nginx-deploy-855866bb46 0 0 0 5m40s nginx-deploy-7c9c6446b7 3 3 3 24s nginx-deploy-7c9c6446b7 3 3 3 28s nginx-deploy-7c9c6446b7 3 0 0 0s nginx-deploy-7c9c6446b7 3 0 0 0s nginx-deploy-7c9c6446b7 3 3 0 0s nginx-deploy-7c9c6446b7 3 3 1 1s nginx-deploy-7c9c6446b7 3 3 2 1s nginx-deploy-7c9c6446b7 3 3 3 2s nginx-deploy-855866bb46 0 0 0 5m59s [root@k8s ~]# kubectl get rs NAME DESIRED CURRENT READY AGE nginx-deploy-7c9c6446b7 3 3 3 4m44s [root@k8s ~]# kubectl get rs --watch NAME DESIRED CURRENT READY AGE nginx-deploy-7c9c6446b7 3 3 3 4m47s
版本回滚
#查看历史版本 kubectl rollout history deployment/nginx-deploy #查看指定版本的信息 kubectl rollout history deployment/nginx-deploy --revision=2 #回滚到历史版本 kubectl rollout undo deployment/nginx-deploy --to-revision=2
当版本变为1.23后,我们又想回退到1.22,找到对应的历史版本rollout undo即可
[root@k8s ~]# kubectl rollout history deployment/nginx-deploy deployment.apps/nginx-deploy REVISION CHANGE-CAUSE 9 <none> 10 <none> [root@k8s ~]# kubectl rollout undo deployment/nginx-deploy --to-revision=9 deployment.apps/nginx-deploy rolled back [root@k8s ~]# kubectl get rs -owide NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR nginx-deploy-7c9c6446b7 3 3 3 23m nginx nginx:1.23 app=nginx-deploy,pod-template-hash=7c9c6446b7 nginx-deploy-855866bb46 0 0 0 11m nginx nginx:1.22 app=nginx-deploy,pod-template-hash=855866bb46 [root@k8s ~]# kubectl get rs --watch NAME DESIRED CURRENT READY AGE nginx-deploy-7c9c6446b7 3 3 3 23m nginx-deploy-855866bb46 0 0 0 12m nginx-deploy-855866bb46 0 0 0 12m nginx-deploy-855866bb46 1 0 0 12m nginx-deploy-855866bb46 1 0 0 12m nginx-deploy-855866bb46 1 1 0 12m nginx-deploy-855866bb46 1 1 1 12m nginx-deploy-7c9c6446b7 2 3 3 24m nginx-deploy-7c9c6446b7 2 3 3 24m nginx-deploy-855866bb46 2 1 1 12m nginx-deploy-7c9c6446b7 2 2 2 24m nginx-deploy-855866bb46 2 1 1 12m nginx-deploy-855866bb46 2 2 1 12m nginx-deploy-855866bb46 2 2 2 12m nginx-deploy-7c9c6446b7 1 2 2 24m nginx-deploy-855866bb46 3 2 2 12m nginx-deploy-7c9c6446b7 1 2 2 24m nginx-deploy-855866bb46 3 2 2 12m nginx-deploy-7c9c6446b7 1 1 1 24m nginx-deploy-855866bb46 3 3 2 12m nginx-deploy-855866bb46 3 3 3 12m nginx-deploy-7c9c6446b7 0 1 1 24m nginx-deploy-7c9c6446b7 0 1 1 24m nginx-deploy-7c9c6446b7 0 0 0 24m ^C[root@k8s ~]# kubectl get rs -owide NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR nginx-deploy-855866bb46 3 3 3 12m nginx nginx:1.22 app=nginx-deploy,pod-template-hash=855866bb46 nginx-deploy-7c9c6446b7 0 0 0 24m nginx nginx:1.23 app=nginx-deploy,pod-template-hash=7c9c6446b7