k8s——deployment

代码你敲我不敲發表於2024-05-21

建立deployment

[root@master deploy]# kubectl create deploy nginx-deploy --image=nginx:1.7.9
deployment.apps/nginx-deploy created
// kubectl create 資源型別   資源名稱(deployment的名稱)  指定映象
// 都是在動態的更新pod副本的數量   deployment和replicaSet是巢狀關係,


[root@master deploy]# kubectl get deploy
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   1/1     1            1           66s
[root@master deploy]# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-68c658776b   1         1         1       75s
[root@master deploy]# kubectl get replicaset
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-68c658776b   1         1         1       4m5s
[root@master deploy]# kubectl get po
NAME                            READY   STATUS    RESTARTS      AGE
my-pod                          1/1     Running   1 (19m ago)   4d19h
nginx-demo                      1/1     Running   2 (19m ago)   4d23h
nginx-deploy-68c658776b-z9rc6   1/1     Running   0             4m48s
nginx-po                        1/1     Running   2 (19m ago)   4d23h
[root@master deploy]# 

//先是有deployment,然後有replicaSet,然後有pod

//vim :set paste 貼上模式


[root@master deploy]# vim nginx-deploy.yaml 

piVersion: apps/v1  #deployment api 版本
kind: Deployment # 資源型別為deployment
metadata:      # 後設資料
  labels:      # 標籤
    app: nginx-deploy   # 具體的 k:y配置形式
  name: nginx-deploy     # deployment的名字
  namespace: default    # 所在的名稱空間
spec:
  replicas: 1     # 期望的副本數
  revisionHistoryLimit: 10     # 進行滾動更新後,保留的歷史版本數
  selector:    # 選擇器,用於找到匹配的rs
    matchLabels:  # 按照標籤匹配
      app: nginx-deploy     # 匹配的標籤k:y
  strategy:      # 更新策略
    rollingUpdate:      #  滾動更新配置
      maxSurge: 25%       # 進行滾動更新時候,更新的個數最多可以超過預期副本數的個數/比
例
      maxUnavailable: 25%  #進行滾動更新時,最大不可用更新比例,表述在所有副本數中,最>多可以有多少個不更新成功
    type: RollingUpdate   # 更新型別,採用滾動更新
  template:    # pod模版
    metadata:  # pod原資訊
      labels:  # pod標籤
        app: nginx-deploy    
    spec:      # pod的期望資訊
      containers:   # pod 的容器
      - image: nginx:1.7.9   # 映象
        imagePullPolicy: IfNotPresent   # 拉取策略
        name: nginx   # 容器名稱
      restartPolicy: Always  # 重啟策略
      terminationGracePeriodSeconds: 30    #刪除操作最多寬限多久

滾動更新

[root@master deploy]# kubectl edit deploy nginx-deploy
deployment.apps/nginx-deploy edited


[root@master deploy]# kubectl get deploy
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   3/3     3            3           28m
[root@master deploy]# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-68c658776b   3         3         3       29m
[root@master deploy]# kubectl get po
NAME                            READY   STATUS    RESTARTS      AGE
my-pod                          1/1     Running   1 (44m ago)   4d19h
nginx-demo                      1/1     Running   2 (44m ago)   4d23h
nginx-deploy-68c658776b-hgppq   1/1     Running   0             23s
nginx-deploy-68c658776b-vld7l   1/1     Running   0             23s
nginx-deploy-68c658776b-z9rc6   1/1     Running   0             29m
nginx-po                        1/1     Running   2 (44m ago)   4d23h
[root@master deploy]# 

//開兩個終端
[root@master deploy]# kubectl set image deployment/nginx-deploy nginx=nginx:1.7.9
deployment.apps/nginx-deploy image updated
[root@master deploy]# 

[root@master deploy]# kubectl rollout status deplloy nginx-deploy
Waiting for deployment "nginx-deploy" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deploy" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deploy" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deploy" successfully rolled out
[root@master deploy]# 
# 滾動更新過程
1.建立一個空的rs2
2.在空的rs2中建立pod1,在rs1中關閉原pod1
3. 在rs2中建立pod2,在rs1中關閉原pod2
//整個過程中保持2個pod的存在

# 檢視rs可以看到2個rs
[root@master deploy]# kubectl get rs --show-labels
NAME                      DESIRED   CURRENT   READY   AGE     LABELS
nginx-deploy-68c658776b   3         3         3       40m     app=nginx-deploy,pod-template-hash=68c658776b
nginx-deploy-7c97fc8644   0         0         0       9m21s   app=nginx-deploy,pod-template-hash=7c97fc8644
[root@master deploy]# 

回滾

// 當你的更新存在問題,可以回滾
//可以使用如下命令檢視更新的歷史版本
[root@master deploy]# kubectl rollout history deployment/nginx-deploy
deployment.apps/nginx-deploy 
REVISION  CHANGE-CAUSE
2         <none>
3         <none>

// 檢視歷史版本
[root@master deploy]# kubectl rollout history deployment/nginx-deploy --revision=2
deployment.apps/nginx-deploy with revision #2
Pod Template:
  Labels:	app=nginx-deploy
	pod-template-hash=7c97fc8644
  Containers:
   nginx:
    Image:	nginx:1.9.1
    Port:	<none>
    Host Port:	<none>
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

[root@master deploy]# kubectl rollout history deployment/nginx-deploy --revision=3
deployment.apps/nginx-deploy with revision #3
Pod Template:
  Labels:	app=nginx-deploy
	pod-template-hash=68c658776b
  Containers:
   nginx:
    Image:	nginx:1.7.9
    Port:	<none>
    Host Port:	<none>
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

// 會記錄修改的值
// 回退操作
[root@master deploy]# kubectl rollout undo deployment/nginx-deploy --to-revision=2
deployment.apps/nginx-deploy rolled back
[root@master deploy]# kubectl rollout status deployment/nginx-deploy
deployment "nginx-deploy" successfully rolled out
[root@master deploy]# 

// 回退操作 在.spec.revisionHistoryLimit 中指定deploment保留多少revision,如果設定0,則不允許回滾

擴容和縮容

擴容

[root@master deploy]# kubectl scale --replicas=6 deploy nginx-deploy
deployment.apps/nginx-deploy scaled
[root@master deploy]# kubectl get po
NAME                            READY   STATUS    RESTARTS      AGE
my-pod                          1/1     Running   1 (81m ago)   4d20h
nginx-demo                      1/1     Running   2 (81m ago)   5d
nginx-deploy-7c97fc8644-62dpp   1/1     Running   0             13m
nginx-deploy-7c97fc8644-g48zt   1/1     Running   0             10s
nginx-deploy-7c97fc8644-jlhfn   1/1     Running   0             13m
nginx-deploy-7c97fc8644-lqpxj   1/1     Running   0             13m
nginx-deploy-7c97fc8644-v69f4   1/1     Running   0             10s
nginx-deploy-7c97fc8644-zslj4   1/1     Running   0             10s
nginx-po                        1/1     Running   2 (81m ago)   5d
[root@master deploy]# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-68c658776b   0         0         0       66m
nginx-deploy-7c97fc8644   6         6         6       36m
[root@master deploy]# kubectl get deploy
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   6/6     6            6           67m
[root@master deploy]# 

縮容

[root@master deploy]# kubectl scale --replicas=3 deploy nginx-deploy
deployment.apps/nginx-deploy scaled
[root@master deploy]# kubectl get po
NAME                            READY   STATUS    RESTARTS      AGE
my-pod                          1/1     Running   1 (83m ago)   4d20h
nginx-demo                      1/1     Running   2 (83m ago)   5d
nginx-deploy-7c97fc8644-62dpp   1/1     Running   0             14m
nginx-deploy-7c97fc8644-jlhfn   1/1     Running   0             14m
nginx-deploy-7c97fc8644-lqpxj   1/1     Running   0             14m
nginx-po                        1/1     Running   2 (83m ago)   5d
[root@master deploy]# kubectl get deploy
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   3/3     3            3           68m
[root@master deploy]# 

暫停與恢復

暫停

// 由於每次對pod template的修改都會觸發更新,所以可以透過暫停更新來避免頻繁更新。我們可以先暫停更新,然後修改pod template,最後恢復更新。


[root@master deploy]# kubectl rollout pause deployment nginx-deploy 
deployment.apps/nginx-deploy paused
[root@master deploy]# kubectl edit deploy nginx-deployment
Error from server (NotFound): deployments.apps "nginx-deployment" not found
[root@master deploy]# kubectl edit deploy nginx-deploy
deployment.apps/nginx-deploy edited

[root@master deploy]# kubectl rollout  history deployment  nginx-deploy 
deployment.apps/nginx-deploy 
REVISION  CHANGE-CAUSE
3         <none>
4         <none>

[root@master deploy]# kubectl rollout  history deployment  nginx-deploy  --revision=4
deployment.apps/nginx-deploy with revision #4
Pod Template:
  Labels:	app=nginx-deploy
	pod-template-hash=7c97fc8644
  Containers:
   nginx:
    Image:	nginx:1.9.1
    Port:	<none>
    Host Port:	<none>
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

[root@master deploy]# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-68c658776b   0         0         0       80m
nginx-deploy-7c97fc8644   3         3         3       49m
[root@master deploy]# 

恢復

[root@master deploy]# 
[root@master deploy]# kubectl rollout resume deployment nginx-deploy 
deployment.apps/nginx-deploy resumed
[root@master deploy]# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-68c658776b   0         0         0       82m
nginx-deploy-6cbbdbbf8b   3         3         3       10s
nginx-deploy-7c97fc8644   0         0         0       51m
[root@master deploy]# kubectl rollout  history 
daemonset    deployment   statefulset  
[root@master deploy]# kubectl rollout  history deployment  nginx-deploy 
deployment.apps/nginx-deploy 
REVISION  CHANGE-CAUSE
3         <none>
4         <none>
5         <none>

[root@master deploy]# kubectl rollout  history deployment  nginx-deploy  --revision=5
deployment.apps/nginx-deploy with revision #5
Pod Template:
  Labels:	app=nginx-deploy
	pod-template-hash=6cbbdbbf8b
  Containers:
   nginx:
    Image:	nginx:1.9.1
    Port:	<none>
    Host Port:	<none>
    Limits:
      cpu:	500m
      memory:	512Mi
    Requests:
      cpu:	100m
      memory:	128Mi
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

[root@master deploy]# 

相關文章