Deployment介紹


一、什么是Deployment?

	用於部署無狀態的服務,這個最常用的控制器。一般用於管理維護企業內部無狀態的微服務,比如configserver、zuul、springboot。他可以管理多個副本的Pod實現無縫遷移、自動擴容縮容、自動災難恢復、一鍵回滾等功能。
       用於部署無狀態的服務!!!

二、 創建一個Deployment

2.1、手動創建

[root@k8s-master01 ~]# kubectl create deployment nginx --image=nginx:1.15.2
deployment.apps/nginx created

2.2、使用文件創建

# 查看手動創建的nginx的yaml文件,然后把f開頭的刪了,且刪了最后的status標簽的內容,得到下面的yaml文件
[root@k8s-master01 ~]# kubectl  get deployment nginx -o yaml > nginx-deploy.yaml
cat > nginx-deploy.yaml << EFO
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2020-12-22T00:07:49Z"
  generation: 1
  labels:
    app: nginx
  name: nginx
  namespace: default
  resourceVersion: "73782"
  uid: 6186f4c7-50bc-45d0-9ed4-916b311802eb
spec:
  progressDeadlineSeconds: 600
  replicas: 1               # 副本數
  revisionHistoryLimit: 10  # 歷史記錄保留的個數
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.15.2
        imagePullPolicy: IfNotPresent
        name: nginx
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
EFO
# 使用以下命令去新建一個deployment
[root@k8s-master01 ~]# kubectl replace -f nginx-deploy.yaml 
deployment.apps/nginx replaced

# 在線更改yaml,管理deployment    ---把副本數改為2
[root@k8s-master01 ~]# kubectl edit deploy  nginx 

# 查看是否生成2個副本
[root@k8s-master01 ~]# kubectl get po
NAME                     READY   STATUS             RESTARTS   AGE
nginx-66bbc9fdc5-c6l6t   1/1     Running            0          57s
nginx-66bbc9fdc5-hsv4d   1/1     Running  		    0          34m

2.3、狀態解析

[root@k8s-master01 ~]# kubectl get deploy -owide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
nginx   2/2     2            1           35m   nginx        nginx:1.15.2   app=nginx

 NAME: Deployment名稱
 READY:Pod的狀態,已經Ready的個數
 UP-TO-DATE:已經達到期望狀態的被更新的副本數
 AVAILABLE:已經可以用的副本數
 AGE:顯示應用程序運行的時間
 CONTAINERS:容器名稱
 IMAGES:容器的鏡像
 SELECTOR:管理的Pod的標簽

三、Deployment的更新

3.1、更改deployment的鏡像並記錄

[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:1.15.3 --record
deployment.apps/nginx image updated

3.2、查看更新過程

[root@k8s-master01 ~]# kubectl rollout status deploy nginx
Waiting for deployment "nginx" rollout to finish: 1 out of 3 new replicas have been updated...

# 或者使用describe查看
[root@k8s-master01 ~]# kubectl describe deploy nginx 

四、Deployment的回滾

3.1、回滾到上一個版本(一般都是回滾到上一個版本)

# 例如錯誤的更新到了一個xxx版本
[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:xxx --record   
deployment.apps/nginx image updated

# 查看kubectl更新的歷史命令
[root@k8s-master01 ~]# kubectl rollout history deploy nginx 
deployment.apps/nginx 
REVISION  CHANGE-CAUSE
1         <none>
2         kubectl set image deploy nginx nginx=nginx:1.15.3 --record=true
3         kubectl set image deploy nginx nginx=nginx:xxx --record=true

# 回滾到上一個版本
[root@k8s-master01 ~]# kubectl rollout undo deploy nginx
deployment.apps/nginx rolled back

3.2、回滾到指定版本(較少,但是得掌握)

# 多次更新錯誤版本
[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:aa --record
deployment.apps/nginx image updated
[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:bb --record
deployment.apps/nginx image updated
[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:cc --record
deployment.apps/nginx image updated

# 查看kubectl更新的歷史命令
[root@k8s-master01 ~]# kubectl rollout history deploy nginx 
deployment.apps/nginx 
REVISION  CHANGE-CAUSE
1         <none>
3         kubectl set image deploy nginx nginx=nginx:xxx --record=true
4         kubectl set image deploy nginx nginx=nginx:1.15.3 --record=true
5         kubectl set image deploy nginx nginx=nginx:aa --record=true
6         kubectl set image deploy nginx nginx=nginx:bb --record=true
7         kubectl set image deploy nginx nginx=nginx:cc --record=true

# 查看指定版本的詳細信息 ---看revision對應的數字即可
[root@k8s-master01 ~]# kubectl rollout history deploy nginx --revision=4
deployment.apps/nginx with revision #4
Pod Template:
  Labels:       app=nginx
        pod-template-hash=5dfc8689c6
  Annotations:  kubernetes.io/change-cause: kubectl set image deploy nginx nginx=nginx:1.15.3 --record=true
  Containers:
   nginx:
    Image:      nginx:1.15.3
    Port:       <none>
    Host Port:  <none>
    Environment:        <none>
    Mounts:     <none>
  Volumes:      <none>
  
# 回滾到指定版本
[root@k8s-master01 ~]# kubectl rollout undo deploy nginx --to-revision=4
deployment.apps/nginx rolled back

五、Deployment的擴容與縮容

5.1、Deployment的擴容

# Deployment的擴容與縮容,不會生成新的rs
[root@k8s-master01 ~]# kubectl  scale  --replicas=4  deploy nginx 
deployment.apps/nginx scaled

# --replicas  # 指定副本數
# nginx  	  # pod的名字

# 查看rs
[root@k8s-master01 ~]# kubectl get rs

5.2、Deployment的縮容

# Deployment的擴容與縮容,不會生成新的rs
[root@k8s-master01 ~]# kubectl  scale  --replicas=1  deploy nginx
deployment.apps/nginx scaled

# --replicas  # 指定副本數
# nginx  	  # pod的名字

# 查看rs
[root@k8s-master01 ~]# kubectl get rs

六、Deployment的暫停和恢復

  • deployment可以在線edit更改(可以一次性更改多個)
  • 也可以用kubectl set image更改(也可以一次性更改多個,但是需要使用到Deployment的暫停和恢復功能)

6.1、Deployment 暫停功能

# 暫停
[root@k8s-master01 ~]# kubectl rollout pause deployment nginx
deployment.apps/nginx paused

# 第一次更新
[root@k8s-master01 ~]# kubectl set image deploy nginx nginx=nginx:1.15.4 --record
deployment.apps/nginx image updated

# 第二次更新、添加內存、CPU
[root@k8s-master01 ~]# kubectl set resources deploy nginx -c nginx --limits=cpu=200m,memory=128Mi --requests=cpu=10m,memory=16Mi
deployment.apps/nginx resource requirements updated

# 查看被更改以后的nginx鏡像的deployment
[root@k8s-master01 ~]# kubectl get deploy nginx -oyaml

6.2、Deployment 恢復功能

# 更新完想更新的內容后,然后恢復鏡像
[root@k8s-master01 ~]# kubectl rollout resume deploy nginx 
deployment.apps/nginx resumed

# 查看rs,看到有新的
[root@k8s-master01 ~]# kubectl get rs
NAME                DESIRED   CURRENT   READY   AGE
nginx-5b6bc78b67    1         1         0       41s

七、Deployment注意事項

.spec.revisionHistoryLimit:設置保留RS舊的revision的個數,設置為0的話,不保留歷史數據

.spec.minReadySeconds:可選參數,指定新創建的Pod在沒有任何容器崩潰的情況下視為Ready最小的秒數,默認為0,即一旦被創建就視為可用。

滾動更新的策略:
	.spec.strategy.type:更新deployment的方式,默認是RollingUpdate
		RollingUpdate:滾動更新,可以指定maxSurge和maxUnavailable
			maxUnavailable:指定在回滾或更新時最大不可用的Pod的數量,可選字段,默認25%,可以設置成數字或百分比,如果該值為0,那么maxSurge就不能0
			maxSurge:可以超過期望值的最大Pod數,可選字段,默認為25%,可以設置成數字或百分比,如果該值為0,那么maxUnavailable不能為0
		Recreate:重建,先刪除舊的Pod,在創建新的Pod


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM