控制器之一

烟雨楼台,行云流水發表於2024-10-17

ReplicaSet 的介紹

ReplicaSet 的作用是維持在任何給定時間執行的一組穩定的副本 Pod。 通常,你會定義一個 Deployment,並用這個 Deployment 自動管理 ReplicaSet。

ReplicaSet 的目的是維護一組在任何時候都處於執行狀態的 Pod 副本的穩定集合。 因此,它通常用來保證給定數量的、完全相同的 Pod 的可用性。

ReplicaSet 是透過一組欄位來定義的,包括一個用來識別可獲得的 Pod 的集合的選擇算符、一個用來標明應該維護的副本個數的數值、一個用來指定應該建立新 Pod 以滿足副本個數條件時要使用的 Pod 模板等等。 每個 ReplicaSet 都透過根據需要建立和刪除 Pod 以使得副本個數達到期望值, 進而實現其存在價值。當 ReplicaSet 需要建立新的 Pod 時,會使用所提供的 Pod 模板。

ReplicaSet 透過 Pod 上的 metadata.ownerReferences 欄位連線到附屬 Pod,該欄位給出當前物件的屬主資源。 ReplicaSet 所獲得的 Pod 都在其 ownerReferences 欄位中包含了屬主 ReplicaSet 的標識資訊。正是透過這一連線,ReplicaSet 知道它所維護的 Pod 集合的狀態, 並據此計劃其操作行為。

ReplicaSet 使用其選擇算符來辨識要獲得的 Pod 集合。如果某個 Pod 沒有 OwnerReference 或者其 OwnerReference 不是一個控制器, 且其匹配到某 ReplicaSet 的選擇算符,則該 Pod 立即被此 ReplicaSet 獲得。

ReplicaSet 確保任何時間都有指定數量的 Pod 副本在執行。 然而,Deployment 是一個更高階的概念,它管理 ReplicaSet,並向 Pod 提供宣告式的更新以及許多其他有用的功能。 因此,我們建議使用 Deployment 而不是直接使用 ReplicaSet, 除非你需要自定義更新業務流程或根本不需要更新。

這實際上意味著,你可能永遠不需要操作 ReplicaSet 物件:而是使用 Deployment,並在 spec 部分定義你的應用。

[root@master replicaset]# cat rs-1.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs1
  namespace: default
spec:
  minReadySeconds: 5 #建立pod 環境準備的時間
  replicas: 4 # pod 個數
  selector: # 標籤選擇
    matchLabels:
      app: rs-1
  template: 
    metadata: # pod 後設資料
      name: cxrs
      labels: # 設定標籤
        app: rs-1
    spec: 
      containers:
      - name: cx
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - name: web
          containerPort: 80
        livenessProbe:
          exec:
            command:
            - /bin/sh
            - "-c"
            - "ls /usr/share/nginx/html/index.html"
          initialDelaySeconds: 3
          periodSeconds: 5
          successThreshold: 1
          failureThreshold: 2
          
[root@master replicaset]# kubectl delete -f rs-1.yaml 
replicaset.apps "rs1" deleted
[root@master replicaset]# kubectl apply -f rs-1.yaml | kubectl get rs ; kubectl get po -w
No resources found in default namespace.
NAME              READY   STATUS              RESTARTS      AGE
liveness-1        1/1     Running             0             22h
liveness-3        1/1     Running             0             21h
lrso              1/1     Running             0             112m
periodseconds-1   1/1     Running             0             21h
periodseconds-2   1/1     Running             0             17h
pod-test-1        1/1     Running             0             24h
pod-test-2        1/1     Running             0             24h
pod-test-cx       1/1     Running             1 (37h ago)   4d23h
rs1-4fhk7         0/1     ContainerCreating   0             0s
rs1-5qz66         0/1     ContainerCreating   0             0s
rs1-p66m8         0/1     ContainerCreating   0             0s
rs1-x4qjm         0/1     Pending             0             0s
stat-1            1/1     Running             0             14h
stat-2            1/1     Running             0             147m
stat-3            1/1     Running             0             142m
rs1-x4qjm         0/1     ContainerCreating   0             0s
rs1-4fhk7         0/1     ContainerCreating   0             1s
rs1-p66m8         0/1     ContainerCreating   0             1s
rs1-5qz66         0/1     ContainerCreating   0             1s
rs1-x4qjm         0/1     ContainerCreating   0             1s
rs1-4fhk7         1/1     Running             0             2s
rs1-5qz66         1/1     Running             0             2s
rs1-p66m8         1/1     Running             0             2s
rs1-x4qjm         1/1     Running             0             3s

  更新

[root@master replicaset]# cat rs-1.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs1
  namespace: default
spec:
  minReadySeconds: 5 #建立pod 環境準備的時間
  replicas: 5 # pod 個數
  selector: # 標籤選擇
    matchLabels:
      app: rs-1
  template: 
    metadata: # pod 後設資料
      name: cxrs
      labels: # 設定標籤
        app: rs-1
    spec: 
      containers:
      - name: cx
        #image: nginx
        image: docker.io/ikubernetes/myapp:v1
        imagePullPolicy: IfNotPresent
        ports:
        - name: web
          containerPort: 80
        livenessProbe:
          exec:
            command:
            - /bin/sh
            - "-c"
            - "ls /"
          initialDelaySeconds: 3
          periodSeconds: 5
          successThreshold: 1
          failureThreshold: 2


[root@master replicaset]# kubectl apply -f rs-1.yaml 
replicaset.apps/rs1 configured

檢視
[root@master replicaset]# kubectl get po -w
NAME              READY   STATUS    RESTARTS      AGE
liveness-1        1/1     Running   0             22h
liveness-3        1/1     Running   0             21h
lrso              1/1     Running   0             150m
periodseconds-1   1/1     Running   0             21h
periodseconds-2   1/1     Running   0             18h
pod-test-1        1/1     Running   0             25h
pod-test-2        1/1     Running   0             25h
pod-test-cx       1/1     Running   1 (38h ago)   4d23h
rs1-4fhk7         1/1     Running   0             38m
rs1-5qz66         1/1     Running   0             38m
rs1-fbfxj         1/1     Running   0             4m4s
rs1-p66m8         1/1     Running   0             38m
rs1-x4qjm         1/1     Running   0             38m
stat-1            1/1     Running   0             15h
stat-2            1/1     Running   0             3h6m
stat-3            1/1     Running   0             3h1m
手動刪除pod,完成更新
[root@master replicaset]# kubectl delete po rs1-4fhk7 
pod "rs1-4fhk7" deleted
rs1-4fhk7         1/1     Terminating   0             41m
rs1-49p8w         0/1     Pending       0             0s
rs1-49p8w         0/1     Pending       0             0s
rs1-49p8w         0/1     ContainerCreating   0             0s
rs1-4fhk7         1/1     Terminating         0             41m
rs1-4fhk7         0/1     Terminating         0             41m
rs1-4fhk7         0/1     Terminating         0             41m
rs1-49p8w         0/1     ContainerCreating   0             1s
rs1-4fhk7         0/1     Terminating         0             41m
rs1-4fhk7         0/1     Terminating         0             41m
rs1-49p8w         1/1     Running             0             11s
    
[root@master replicaset]# kubectl delete po rs1-5qz66 rs1-fbfxj rs1-p66m8 rs1-x4qjm 
pod "rs1-5qz66" deleted
pod "rs1-fbfxj" deleted
pod "rs1-p66m8" deleted
pod "rs1-x4qjm" deleted
rs1-5qz66         1/1     Terminating         0             43m
rs1-fbfxj         1/1     Terminating         0             8m49s
rs1-zwb7p         0/1     Pending             0             0s
rs1-p66m8         1/1     Terminating         0             43m
rs1-x4qjm         1/1     Terminating         0             43m
rs1-zwb7p         0/1     Pending             0             0s
rs1-xhnz2         0/1     Pending             0             0s
rs1-xhnz2         0/1     Pending             0             0s
rs1-zwb7p         0/1     ContainerCreating   0             0s
rs1-g9hlt         0/1     Pending             0             0s
rs1-wg5rt         0/1     Pending             0             0s
rs1-g9hlt         0/1     Pending             0             0s
rs1-wg5rt         0/1     Pending             0             0s
rs1-xhnz2         0/1     ContainerCreating   0             0s
rs1-g9hlt         0/1     ContainerCreating   0             0s
rs1-wg5rt         0/1     ContainerCreating   0             0s
rs1-5qz66         1/1     Terminating         0             43m
rs1-p66m8         1/1     Terminating         0             43m
rs1-fbfxj         1/1     Terminating         0             8m49s
rs1-x4qjm         1/1     Terminating         0             43m
rs1-x4qjm         0/1     Terminating         0             43m
rs1-xhnz2         0/1     ContainerCreating   0             1s
rs1-5qz66         0/1     Terminating         0             43m
rs1-wg5rt         0/1     ContainerCreating   0             1s
rs1-fbfxj         0/1     Terminating         0             8m50s
rs1-p66m8         0/1     Terminating         0             43m
rs1-g9hlt         0/1     ContainerCreating   0             1s
rs1-zwb7p         0/1     ContainerCreating   0             2s
rs1-x4qjm         0/1     Terminating         0             43m
rs1-xhnz2         1/1     Running             0             2s
rs1-x4qjm         0/1     Terminating         0             43m
rs1-x4qjm         0/1     Terminating         0             43m
rs1-p66m8         0/1     Terminating         0             43m
rs1-fbfxj         0/1     Terminating         0             8m51s
rs1-5qz66         0/1     Terminating         0             43m
rs1-fbfxj         0/1     Terminating         0             8m51s
rs1-fbfxj         0/1     Terminating         0             8m51s
rs1-p66m8         0/1     Terminating         0             43m
rs1-p66m8         0/1     Terminating         0             43m
rs1-5qz66         0/1     Terminating         0             43m
rs1-5qz66         0/1     Terminating         0             43m
rs1-wg5rt         1/1     Running             0             3s
rs1-g9hlt         1/1     Running             0             11s
rs1-zwb7p         1/1     Running             0             14s

  deployment 控制器https://kubernetes.io/zh-cn/docs/concepts/workloads/controllers/deployment/

Deployment 用於管理執行一個應用負載的一組 Pod,通常適用於不保持狀態的負載。

一個 Deployment 為 PodReplicaSet 提供宣告式的更新能力。

你負責描述 Deployment 中的目標狀態,而 Deployment 控制器(Controller) 以受控速率更改實際狀態, 使其變為期望狀態。你可以定義 Deployment 以建立新的 ReplicaSet,或刪除現有 Deployment, 並透過新的 Deployment 收養其資源。

更新

[root@master dpment]# vim dpment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
[root@master dpment]# kubectl apply -f dpment.yaml 
deployment.apps/nginx-deployment created
[root@master dpment]# kubectl get deployments.apps  -w
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           40s
更新映象
[root@master dpment]# kubectl set image -n default deployment.apps/nginx-deployment nginx=nginx:1.16.1
deployment.apps/nginx-deployment image updated
[root@master dpment]# kubectl get po -w
NAME                                READY   STATUS              RESTARTS      AGE
liveness-1                          1/1     Running             0             22h
liveness-3                          1/1     Running             0             22h
lrso                                1/1     Running             0             166m
nginx-deployment-848dd6cfb5-5ncfc   0/1     ContainerCreating   0             9s
nginx-deployment-86dcfdf4c6-bxdtm   1/1     Running             0             2m35s
nginx-deployment-86dcfdf4c6-h9l4j   1/1     Running             0             2m35s
nginx-deployment-86dcfdf4c6-swvxg   1/1     Running             0             2m35s
periodseconds-1                     1/1     Running             0             22h
periodseconds-2                     1/1     Running             0             18h
pod-test-1                          1/1     Running             0             25h
pod-test-2                          1/1     Running             0             25h
pod-test-cx                         1/1     Running             1 (38h ago)   4d23h
rs1-49p8w                           1/1     Running             0             12m
rs1-g9hlt                           1/1     Running             0             11m
rs1-wg5rt                           1/1     Running             0             11m
rs1-xhnz2                           1/1     Running             0             11m
rs1-zwb7p                           1/1     Running             0             11m
stat-1                              1/1     Running             0             15h
stat-2                              1/1     Running             0             3h22m
stat-3                              1/1     Running             0             3h17m
nginx-deployment-848dd6cfb5-5ncfc   1/1     Running             0             25s
nginx-deployment-86dcfdf4c6-h9l4j   1/1     Terminating         0             2m51s
nginx-deployment-848dd6cfb5-48dkv   0/1     Pending             0             0s
nginx-deployment-848dd6cfb5-48dkv   0/1     Pending             0             0s
nginx-deployment-848dd6cfb5-48dkv   0/1     ContainerCreating   0             0s
nginx-deployment-848dd6cfb5-48dkv   0/1     ContainerCreating   0             0s
nginx-deployment-86dcfdf4c6-h9l4j   1/1     Terminating         0             2m52s
nginx-deployment-86dcfdf4c6-h9l4j   0/1     Terminating         0             2m52s
nginx-deployment-86dcfdf4c6-h9l4j   0/1     Terminating         0             2m53s
nginx-deployment-86dcfdf4c6-h9l4j   0/1     Terminating         0             2m53s
nginx-deployment-86dcfdf4c6-h9l4j   0/1     Terminating         0             2m53s
nginx-deployment-848dd6cfb5-48dkv   1/1     Running             0             25s
nginx-deployment-86dcfdf4c6-swvxg   1/1     Terminating         0             3m16s
nginx-deployment-848dd6cfb5-c242w   0/1     Pending             0             0s
nginx-deployment-848dd6cfb5-c242w   0/1     Pending             0             0s
nginx-deployment-848dd6cfb5-c242w   0/1     ContainerCreating   0             0s
nginx-deployment-848dd6cfb5-c242w   0/1     ContainerCreating   0             1s
nginx-deployment-86dcfdf4c6-swvxg   1/1     Terminating         0             3m17s
nginx-deployment-86dcfdf4c6-swvxg   0/1     Terminating         0             3m17s
nginx-deployment-86dcfdf4c6-swvxg   0/1     Terminating         0             3m18s
nginx-deployment-86dcfdf4c6-swvxg   0/1     Terminating         0             3m18s
nginx-deployment-86dcfdf4c6-swvxg   0/1     Terminating         0             3m18s
nginx-deployment-848dd6cfb5-c242w   1/1     Running             0             2s
nginx-deployment-86dcfdf4c6-bxdtm   1/1     Terminating         0             3m18s
nginx-deployment-86dcfdf4c6-bxdtm   1/1     Terminating         0             3m18s
nginx-deployment-86dcfdf4c6-bxdtm   0/1     Terminating         0             3m18s
nginx-deployment-86dcfdf4c6-bxdtm   0/1     Terminating         0             3m19s
nginx-deployment-86dcfdf4c6-bxdtm   0/1     Terminating         0             3m19s
nginx-deployment-86dcfdf4c6-bxdtm   0/1     Terminating         0             3m19s

[root@master dpment]# kubectl get rs
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-848dd6cfb5   3         3         3       6m47s
nginx-deployment-86dcfdf4c6   0         0         0       9m13s
rs1                           5         5         5       60m

  更新策略

翻轉(多 Deployment 動態更新)

Deployment 控制器每次注意到新的 Deployment 時,都會建立一個 ReplicaSet 以啟動所需的 Pod。 如果更新了 Deployment,則控制標籤匹配 .spec.selector 但模板不匹配 .spec.template 的 Pod 的現有 ReplicaSet 被縮容。 最終,新的 ReplicaSet 縮放為 .spec.replicas 個副本, 所有舊 ReplicaSet 縮放為 0 個副本。

當 Deployment 正在上線時被更新,Deployment 會針對更新建立一個新的 ReplicaSet 並開始對其擴容,之前正在被擴容的 ReplicaSet 會被翻轉,新增到舊 ReplicaSet 列表 並開始縮容。

回滾 Deployment

有時,你可能想要回滾 Deployment;例如,當 Deployment 不穩定時(例如進入反覆崩潰狀態)。 預設情況下,Deployment 的所有上線記錄都保留在系統中,以便可以隨時回滾 (你可以透過修改修訂歷史記錄限制來更改這一約束)。

假設你在更新 Deployment 時犯了一個拼寫錯誤,將映象名稱命名設定為 nginx:1.161 而不是 nginx:1.16.1

kubectl set image deployment/nginx-deployment nginx=nginx:1.161
輸出類似於:

deployment.apps/nginx-deployment image updated
此上線程序會出現停滯。你可以透過檢查上線狀態來驗證:

kubectl rollout status deployment/nginx-deployment
輸出類似於:

Waiting for rollout to finish: 1 out of 3 new replicas have been updated...
按 Ctrl-C 停止上述上線狀態觀測。有關上線停滯的詳細資訊,參考這裡。
你可以看到舊的副本(算上來自 nginx-deployment-1564180365 和 nginx-deployment-2035384211 的副本)有 3 個, 新的副本(來自 nginx-deployment-3066724191)有 1 個:

kubectl get rs
輸出類似於:

NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-1564180365   3         3         3       25s
nginx-deployment-2035384211   0         0         0       36s
nginx-deployment-3066724191   1         1         0       6s
檢視所建立的 Pod,你會注意到新 ReplicaSet 所建立的 1 個 Pod 卡頓在映象拉取迴圈中。

kubectl get pods
輸出類似於:

NAME                                READY     STATUS             RESTARTS   AGE
nginx-deployment-1564180365-70iae   1/1       Running            0          25s
nginx-deployment-1564180365-jbqqo   1/1       Running            0          25s
nginx-deployment-1564180365-hysrc   1/1       Running            0          25s
nginx-deployment-3066724191-08mng   0/1       ImagePullBackOff   0 

  檢查歷史

按照如下步驟檢查回滾歷史:

首先,檢查 Deployment 修訂歷史:

kubectl rollout history deployment/nginx-deployment
輸出類似於:

deployments "nginx-deployment"
REVISION    CHANGE-CAUSE
1           kubectl apply --filename=https://k8s.io/examples/controllers/nginx-deployment.yaml
2           kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1
3           kubectl set image deployment/nginx-deployment nginx=nginx:1.161
CHANGE-CAUSE 的內容是從 Deployment 的 kubernetes.io/change-cause 註解複製過來的。 複製動作發生在修訂版本建立時。你可以透過以下方式設定 CHANGE-CAUSE 訊息:

使用 kubectl annotate deployment/nginx-deployment kubernetes.io/change-cause="image updated to 1.16.1" 為 Deployment 新增註解。
手動編輯資源的清單。
要檢視修訂歷史的詳細資訊,執行:

kubectl rollout history deployment/nginx-deployment --revision=2
輸出類似於:

deployments "nginx-deployment" revision 2
  Labels:       app=nginx
          pod-template-hash=1159050644
  Annotations:  kubernetes.io/change-cause=kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1
  Containers:
   nginx:
    Image:      nginx:1.16.1
    Port:       80/TCP
     QoS Tier:
        cpu:      BestEffort
        memory:   BestEffort
    Environment Variables:      <none>
  No volumes.
回滾到之前的修訂版本

  回退

按照下面給出的步驟將 Deployment 從當前版本回滾到以前的版本(即版本 2)。

假定現在你已決定撤消當前上線並回滾到以前的修訂版本:

kubectl rollout undo deployment/nginx-deployment
輸出類似於:

deployment.apps/nginx-deployment rolled back
或者,你也可以透過使用 --to-revision 來回滾到特定修訂版本:

kubectl rollout undo deployment/nginx-deployment --to-revision=2
輸出類似於:

deployment.apps/nginx-deployment rolled back
與回滾相關的指令的更詳細資訊,請參考 kubectl rollout。

現在,Deployment 正在回滾到以前的穩定版本。正如你所看到的,Deployment 控制器生成了回滾到修訂版本 2 的 DeploymentRollback 事件。

檢查回滾是否成功以及 Deployment 是否正在執行,執行:

kubectl get deployment nginx-deployment
輸出類似於:

NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           30m

  擴縮容pod

kubectl scale deployment/nginx-deployment --replicas=10
輸出類似於:

deployment.apps/nginx-deployment scaled

  

滾動更新 Deployment

Deployment 會在 .spec.strategy.type==RollingUpdate時,採取 滾動更新的方式更新 Pod。你、可以指定 maxUnavailablemaxSurge 來控制滾動更新過程。

最大不可用

.spec.strategy.rollingUpdate.maxUnavailable 是一個可選欄位, 用來指定更新過程中不可用的 Pod 的個數上限。該值可以是絕對數字(例如,5),也可以是所需 Pod 的百分比(例如,10%)。百分比值會轉換成絕對數並去除小數部分。 如果 .spec.strategy.rollingUpdate.maxSurge 為 0,則此值不能為 0。 預設值為 25%。

例如,當此值設定為 30% 時,滾動更新開始時會立即將舊 ReplicaSet 縮容到期望 Pod 個數的70%。 新 Pod 準備就緒後,可以繼續縮容舊有的 ReplicaSet,然後對新的 ReplicaSet 擴容, 確保在更新期間可用的 Pod 總數在任何時候都至少為所需的 Pod 個數的 70%。

最大峰值(可用)

.spec.strategy.rollingUpdate.maxSurge 是一個可選欄位,用來指定可以建立的超出期望 Pod 個數的 Pod 數量。此值可以是絕對數(例如,5)或所需 Pod 的百分比(例如,10%)。 如果 MaxUnavailable 為 0,則此值不能為 0。百分比值會透過向上取整轉換為絕對數。 此欄位的預設值為 25%。

例如,當此值為 30% 時,啟動滾動更新後,會立即對新的 ReplicaSet 擴容,同時保證新舊 Pod 的總數不超過所需 Pod 總數的 130%。一旦舊 Pod 被殺死,新的 ReplicaSet 可以進一步擴容, 同時確保更新期間的任何時候執行中的 Pod 總數最多為所需 Pod 總數的 130%。

進度期限秒數

.spec.progressDeadlineSeconds 是一個可選欄位,用於指定系統在報告 Deployment 進展失敗之前等待 Deployment 取得進展的秒數。 這類報告會在資源狀態中體現為 type: Progressingstatus: Falsereason: ProgressDeadlineExceeded。Deployment 控制器將在預設 600 毫秒內持續重試 Deployment。 將來,一旦實現了自動回滾,Deployment 控制器將在探測到這樣的條件時立即回滾 Deployment。

如果指定,則此欄位值需要大於 .spec.minReadySeconds 取值。

[root@master dpment]# cat dpment-1.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment-ro
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-ro
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      labels:
        app: nginx-ro
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
[root@master dpment]# kubectl apply -f dpment-1.yaml 
deployment.apps/nginx-deployment-ro created
[root@master dpment]# kubectl get po
NAME                                   READY   STATUS    RESTARTS   AGE
nginx-deployment-ro-56d78cc78d-9l5nh   1/1     Running   0          2s
nginx-deployment-ro-56d78cc78d-b2vj8   1/1     Running   0          2s
nginx-deployment-ro-56d78cc78d-p9wlj   1/1     Running   0          2s
[root@master dpment]# kubectl set image deployment.apps/nginx-deployment-ro nginx=nginx:1.16.1 | kubectl get po -w 
NAME                                   READY   STATUS    RESTARTS   AGE
nginx-deployment-ro-56d78cc78d-9l5nh   1/1     Running   0          108s
nginx-deployment-ro-56d78cc78d-b2vj8   1/1     Running   0          108s
nginx-deployment-ro-56d78cc78d-p9wlj   1/1     Running   0          108s
nginx-deployment-ro-56d78cc78d-p9wlj   1/1     Terminating   0          108s
nginx-deployment-ro-f56978bf-jn8sv     0/1     Pending       0          0s
nginx-deployment-ro-f56978bf-jn8sv     0/1     Pending       0          0s
nginx-deployment-ro-f56978bf-jn8sv     0/1     ContainerCreating   0          0s
nginx-deployment-ro-f56978bf-qknvh     0/1     Pending             0          0s
nginx-deployment-ro-f56978bf-qknvh     0/1     Pending             0          0s
nginx-deployment-ro-f56978bf-qknvh     0/1     ContainerCreating   0          0s
nginx-deployment-ro-56d78cc78d-p9wlj   1/1     Terminating         0          108s
nginx-deployment-ro-56d78cc78d-p9wlj   0/1     Terminating         0          108s
nginx-deployment-ro-f56978bf-jn8sv     0/1     ContainerCreating   0          0s
nginx-deployment-ro-f56978bf-qknvh     0/1     ContainerCreating   0          0s
nginx-deployment-ro-56d78cc78d-p9wlj   0/1     Terminating         0          108s
nginx-deployment-ro-56d78cc78d-p9wlj   0/1     Terminating         0          109s
nginx-deployment-ro-56d78cc78d-p9wlj   0/1     Terminating         0          109s
nginx-deployment-ro-f56978bf-jn8sv     1/1     Running             0          1s
nginx-deployment-ro-56d78cc78d-9l5nh   1/1     Terminating         0          109s
nginx-deployment-ro-f56978bf-5p4f8     0/1     Pending             0          0s
nginx-deployment-ro-f56978bf-qknvh     1/1     Running             0          2s
nginx-deployment-ro-f56978bf-5p4f8     0/1     Pending             0          0s
nginx-deployment-ro-f56978bf-5p4f8     0/1     ContainerCreating   0          0s
nginx-deployment-ro-56d78cc78d-b2vj8   1/1     Terminating         0          110s
nginx-deployment-ro-56d78cc78d-9l5nh   1/1     Terminating         0          110s
nginx-deployment-ro-56d78cc78d-9l5nh   0/1     Terminating         0          110s
nginx-deployment-ro-56d78cc78d-b2vj8   1/1     Terminating         0          110s
nginx-deployment-ro-f56978bf-5p4f8     0/1     ContainerCreating   0          0s
nginx-deployment-ro-56d78cc78d-b2vj8   0/1     Terminating         0          110s
nginx-deployment-ro-56d78cc78d-b2vj8   0/1     Terminating         0          110s
nginx-deployment-ro-56d78cc78d-b2vj8   0/1     Terminating         0          110s
nginx-deployment-ro-56d78cc78d-b2vj8   0/1     Terminating         0          110s
nginx-deployment-ro-56d78cc78d-9l5nh   0/1     Terminating         0          111s
nginx-deployment-ro-56d78cc78d-9l5nh   0/1     Terminating         0          111s
nginx-deployment-ro-56d78cc78d-9l5nh   0/1     Terminating         0          111s
nginx-deployment-ro-f56978bf-5p4f8     1/1     Running             0          1s
[root@master dpment]# kubectl get rs -w
NAME                             DESIRED   CURRENT   READY   AGE
nginx-deployment-848dd6cfb5      0         0         0       45m
nginx-deployment-86dcfdf4c6      0         0         0       47m
nginx-deployment-ro-56d78cc78d   3         3         3       97s
rs1                              0         0         0       99m
nginx-deployment-ro-f56978bf     1         0         0       0s
nginx-deployment-ro-56d78cc78d   2         3         3       109s
nginx-deployment-ro-f56978bf     2         0         0       0s
nginx-deployment-ro-56d78cc78d   2         3         3       109s
nginx-deployment-ro-f56978bf     2         0         0       0s
nginx-deployment-ro-56d78cc78d   2         2         2       109s
nginx-deployment-ro-f56978bf     2         1         0       0s
nginx-deployment-ro-f56978bf     2         2         0       0s
nginx-deployment-ro-f56978bf     2         2         1       1s
nginx-deployment-ro-56d78cc78d   1         2         2       110s
nginx-deployment-ro-f56978bf     3         2         1       2s
nginx-deployment-ro-56d78cc78d   1         2         2       111s
nginx-deployment-ro-56d78cc78d   1         1         1       111s
nginx-deployment-ro-f56978bf     3         2         1       2s
nginx-deployment-ro-f56978bf     3         3         2       2s
nginx-deployment-ro-56d78cc78d   0         1         1       111s
nginx-deployment-ro-56d78cc78d   0         1         1       111s
nginx-deployment-ro-56d78cc78d   0         0         0       111s
nginx-deployment-ro-f56978bf     3         3         3       3s

  檢視歷史版本

[root@master dpment]# kubectl rollout history deployment nginx-deployment-ro 
deployment.apps/nginx-deployment-ro 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

  命令列回滾

[root@master dpment]# kubectl rollout undo deployment nginx-deployment-ro --to-revision=1
deployment.apps/nginx-deployment-ro rolled back

  檢視回滾rs 控制器過程

[root@master dpment]# kubectl get rs -w
NAME                             DESIRED   CURRENT   READY   AGE
nginx-deployment-848dd6cfb5      0         0         0       168m
nginx-deployment-86dcfdf4c6      0         0         0       170m
nginx-deployment-ro-56d78cc78d   0         0         0       124m
nginx-deployment-ro-f56978bf     3         3         3       122m
rs1                              0         0         0       3h42m
nginx-deployment-ro-56d78cc78d   0         0         0       125m
nginx-deployment-ro-56d78cc78d   1         0         0       125m
nginx-deployment-ro-f56978bf     2         3         3       123m
nginx-deployment-ro-56d78cc78d   2         0         0       125m
nginx-deployment-ro-f56978bf     2         3         3       123m
nginx-deployment-ro-f56978bf     2         2         2       123m
nginx-deployment-ro-56d78cc78d   2         0         0       125m
nginx-deployment-ro-56d78cc78d   2         1         0       125m
nginx-deployment-ro-56d78cc78d   2         2         0       125m
nginx-deployment-ro-56d78cc78d   2         2         1       125m
nginx-deployment-ro-f56978bf     1         2         2       123m
nginx-deployment-ro-f56978bf     1         2         2       123m
nginx-deployment-ro-56d78cc78d   3         2         1       125m
nginx-deployment-ro-f56978bf     1         1         1       123m
nginx-deployment-ro-56d78cc78d   3         2         1       125m
nginx-deployment-ro-56d78cc78d   3         3         1       125m
nginx-deployment-ro-56d78cc78d   3         3         2       125m
nginx-deployment-ro-f56978bf     0         1         1       123m
nginx-deployment-ro-f56978bf     0         1         1       123m
nginx-deployment-ro-f56978bf     0         0         0       123m
nginx-deployment-ro-56d78cc78d   3         3         3       125m

  deployment 基於反親和性建立po

[root@master dpment]# cat dpment-2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-cx1
  labels:
    app: nginx
spec:
  replicas: 3
  revisionHistoryLimit: 4
 # paused: true
  selector:
    matchLabels:
      app: nginx-cx
  strategy:
 #   type: rollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      labels:
        app: nginx-cx
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - nginx-cx
            topologyKey: kubernetes.io/hostname
      tolerations: # 汙點容忍度
      - key: node-role.kubernetes.io/control-plane # 汙點KEY
        operator: Exists
        effect: NoSchedule
      containers:
      - name: nginx
        image: nginx:1.10
        ports:
        - containerPort: 80
[root@master dpment]# kubectl apply -f dpment-2.yaml ; kubectl get po -w
deployment.apps/nginx-cx1 created
NAME                                   READY   STATUS              RESTARTS   AGE
nginx-cx1-5c86fddfc5-4wdvg             0/1     ContainerCreating   0          0s
nginx-cx1-5c86fddfc5-8qgqb             0/1     Pending             0          0s
nginx-cx1-5c86fddfc5-jgtt5             0/1     Pending             0          0s
nginx-deployment-ro-56d78cc78d-5sskn   1/1     Running             0          44m
nginx-deployment-ro-56d78cc78d-77m8k   1/1     Running             0          44m
nginx-deployment-ro-56d78cc78d-kvhsw   1/1     Running             0          44m
nginx-cx1-5c86fddfc5-jgtt5             0/1     ContainerCreating   0          0s
nginx-cx1-5c86fddfc5-8qgqb             0/1     ContainerCreating   0          0s
nginx-cx1-5c86fddfc5-4wdvg             0/1     ContainerCreating   0          1s
nginx-cx1-5c86fddfc5-jgtt5             0/1     ContainerCreating   0          1s
nginx-cx1-5c86fddfc5-8qgqb             0/1     ContainerCreating   0          2s
nginx-cx1-5c86fddfc5-4wdvg             1/1     Running             0          2s
nginx-cx1-5c86fddfc5-jgtt5             1/1     Running             0          2s
nginx-cx1-5c86fddfc5-8qgqb             1/1     Running             0          33s

[root@master dpment]# kubectl get rs -w
NAME                             DESIRED   CURRENT   READY   AGE
nginx-deployment-848dd6cfb5      0         0         0       3h33m
nginx-deployment-86dcfdf4c6      0         0         0       3h36m
nginx-deployment-ro-56d78cc78d   3         3         3       169m
nginx-deployment-ro-f56978bf     0         0         0       168m
rs1                              0         0         0       4h27m
nginx-cx1-5c86fddfc5             3         0         0       0s
nginx-cx1-5c86fddfc5             3         0         0       0s
nginx-cx1-5c86fddfc5             3         3         0       0s
nginx-cx1-5c86fddfc5             3         3         1       2s
nginx-cx1-5c86fddfc5             3         3         2       2s
nginx-cx1-5c86fddfc5             3         3         3       33s
[root@master dpment]# kubectl get deployments.apps 
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
nginx-cx1             3/3     3            3           3m13s
nginx-deployment      0/0     0            0           3h39m
nginx-deployment-ro   3/3     3            3           173m

  

相關文章