20--Deployment常规操作

一、Deployment滚动更新

1、更新配置清单

# 滚动更新:先生成新pod,后删除旧pod
[root@k8s-m-01 k8s]# vim deploy-nginx.yml
kind: Deployment
apiVersion: apps/v1
metadata:
  name: test1-deploy
spec:
  selector:
    matchLabels:
      app: deployment
  template:
    metadata:
      labels:
        app: deployment
    spec:
      containers:
        - name: nginx
          image: nginx
          
[root@k8s-m-01 k8s]# kubectl get pods
NAME                               READY   STATUS    RESTARTS   AGE
test1-deploy-549486578f-xntvw      1/1     Running   0          5m36s

[root@k8s-m-01 k8s]# vim deploy-nginx.yml
... ...
      containers:
        - name: nginx
          image: nginx:1.18      #修改版本1.18

[root@k8s-m-01 k8s]# kubectl apply -f deploy-nginx.yml 
deployment.apps/test1-deploy unchanged

[root@k8s-m-01 k8s]# kubectl get pods
NAME                               READY   STATUS    RESTARTS   AGE
test1-deploy-c7779f765-gxgp5       1/1     Running   0          8m13s

2、设置镜像 kubectl set image

[root@k8s-m-01 k8s]# kubectl set image deployment/test1-deploy nginx=nginx:latest

[root@k8s-m-01 k8s]# kubectl set image deployment/test1-deploy nginx=nginx:latest
deployment.apps/test1-deploy image updated

[root@k8s-m-01 k8s]# kubectl get pods -o wide
NAME                               READY   STATUS    RESTARTS   AGE   IP             NODE 
test1-deploy-76684b764-htc9n       1/1     Running   0          26s   10.244.2.100   k8s-n-02   <none>           <none>     #名字不一样了,已更新

3、打标签 kubectl patch

[root@k8s-m-01 k8s]# kubectl patch deployments.apps test1-deploy -p '{"spec":{"template":{"spec":{"containers":[{"name":"nginx","image":"nginx:1.18"}]}}}}'
deployment.apps/test1-deploy patched

[root@k8s-m-01 k8s]# kubectl get pods -o wide
NAME                               READY   STATUS        RESTARTS   AGE    IP             
test1-deploy-76684b764-htc9n       0/1     Terminating   0          9m1s   10.244.2.100   k8s-n-02   <none>           <none>     #旧的即将删除
test1-deploy-c7779f765-xsbf8       1/1     Running       0          24s    10.244.2.101   k8s-n-02   <none>           <none>

4、修改资源 kubectl edit

[root@k8s-m-01 k8s]# kubectl edit deployments.apps test1-deploy
... ...template:
    metadata:
      creationTimestamp: null
      labels:
        app: deployment
    spec:
      containers:
      - image: nginx:latest #修改成需要的版本
        imagePullPolicy: Always
        name: nginx
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
[root@k8s-m-01 k8s]# kubectl edit deployments.apps test1-deploy
deployment.apps/test1-deploy edited

[root@k8s-m-01 k8s]# kubectl get pods -o wide
NAME                               READY   STATUS    RESTARTS   AGE     IP             
test1-deploy-76684b764-zwmx2       1/1     Running   0          2m49s   10.244.2.102   k8s-n-02   <none>           <none>

二、Deployment应用回滚

1、查看更新的版本

#为了实验清楚,先删掉
[root@k8s-m-01 k8s]# kubectl delete -f /k8s/deploy-nginx.yml 
deployment.apps "test1-deploy" deleted

#再去创建,当前是1.18版本
[root@k8s-m-01 k8s]# kubectl apply -f deploy-nginx.yml 
deployment.apps/test1-deploy created

#去更新版本
[root@k8s-m-01 k8s]# kubectl patch deployments.apps test1-deploy -p '{"spec":{"template":{"spec":{"containers":[{"name":"nginx","image":"nginx:latest"}]}}}}'
deployment.apps/test1-deploy patched

#再进行更新
[root@k8s-m-01 k8s]# kubectl patch deployments.apps test1-deploy -p '{"spec":{"template":{"spec":{"containers":[{"name":"nginx","image":"nginx:1.19"}]}}}}'
deployment.apps/test1-deploy patched

#查看更新的版本
[root@k8s-m-01 k8s]# kubectl rollout history deployment test1-deploy
deployment.apps/test1-deploy 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
3         <none>      #共3个版本

2、根据版本回滚

2种方式

1、回滚到上一个版本
[root@k8s-m-01 k8s]# kubectl rollout undo deployment test1-deploy
deployment.apps/test1-deploy rolled back
[root@k8s-m-01 k8s]# kubectl rollout history deployment test1-deploy
deployment.apps/test1-deploy 
REVISION  CHANGE-CAUSE
1         <none>
3         <none>
4         <none> #回滚后,会变成上面的2版本,而重新生成一个4版本

#再来回滚一次
[root@k8s-m-01 k8s]# kubectl rollout undo deployment test1-deploy
deployment.apps/test1-deploy rolled back
[root@k8s-m-01 k8s]# kubectl rollout history deployment test1-deploy
deployment.apps/test1-deploy 
REVISION  CHANGE-CAUSE
1         <none>
4         <none>
5         <none>
2、回滚到指定版本
[root@k8s-m-01 k8s]# kubectl rollout undo deployment test1-deploy --to-revision=1
deployment.apps/test1-deploy rolled back

[root@k8s-m-01 k8s]# kubectl rollout history deployment test1-deploy
deployment.apps/test1-deploy 
REVISION  CHANGE-CAUSE
4         <none>
5         <none>
6         <none>	#6=1	 

三、Deployment弹性扩容

1、编辑资源

[root@k8s-m-01 k8s]# kubectl edit deployments.apps test1-deploy
... ...spec:
  progressDeadlineSeconds: 600
  replicas: 1   #修改成2
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: deployment
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
... ...
[root@k8s-m-01 k8s]# kubectl edit deployments.apps test1-deploy
deployment.apps/test1-deploy edited

[root@k8s-m-01 k8s]# kubectl get pods
NAME                               READY   STATUS    RESTARTS 
test1-deploy-776d6f99f5-87fvz      1/1     Running   0          3m59s
test1-deploy-776d6f99f5-fjq5k      1/1     Running   0          36s  

2、打标签

[root@k8s-m-01 k8s]# kubectl patch deployments.apps test1-deploy -p '{"spec":{"replicas":1}}'
deployment.apps/test1-deploy patched

[root@k8s-m-01 k8s]# kubectl get pods
NAME                               READY   STATUS    RESTARTS 
test1-deploy-776d6f99f5-87fvz      1/1     Running   0          8m3s   #此时缩容成1个

3、scale

[root@k8s-m-01 k8s]# kubectl scale deployment test1-deploy --replicas=3
deployment.apps/test1-deploy scaled
[root@k8s-m-01 k8s]# kubectl get pods
NAME                               READY   STATUS    RESTARTS 
test1-deploy-776d6f99f5-669qf      1/1     Running   0          7s
test1-deploy-776d6f99f5-87fvz      1/1     Running   0          10m
test1-deploy-776d6f99f5-fg72r      1/1     Running   0          7s
posted @ 2021-09-18 19:28  小绵  阅读(252)  评论(0编辑  收藏  举报