[k8s] Deployment ============== ###### tags: `kubernetes` `workload` ## useful command lines ```shell= kubectl create deployment nginx --image=nginx --replicas=4 ``` ## definition ```yaml= apiVersion: apps/v1 kind: Deployment metadata: name: helloworld spec: replica: 2 strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 maxUnavailable: 1 minReadySeconds: 60 revisionHistoryLimit: 10 selector: matchLabels: app: frontend template: metadata: labels: app: frontend spec: containers: - name: helloworld image: w5151381guy/helloworld imagePullPolicy: IfNotPresent ports: - containerPort: 8080 ``` + ***apiVersion***: `apps/v1` 增加了*RollingUpdate*設定 + ***RollingUpdate***: 先建立新的 ReplicaSet 並控制新內容的 Pod,待新 Pod 也可以正常運作,才會通知 ReplicaSet 將現有的 Pod 移除 + ***maxSurge***: 代表 ReplicaSet 可以產生比 Deployment 設定檔中的 replica 所設定數量還多幾個出來 + ***maxUnavailable***:代表在滾動更新的過程中,可以允許多少的 Pod 無法使用 + ***minReadySeconds***: 代表當新的 Pod 建立好並且在運行的 container 並沒有 crash 的情況下,最少需要多久時間可以開始接受 Request + ***revisionHistoryLimit***: 每次 Deployment 在進行更新的時候都會產生一個新的 ReplicaSet 用來進行版本控制 + ***imagePullPolicy***: 用來決定什麼時候 kubelet 會去pull特定的image. + *Always*: the image is pulled every time the pod is started. + *Never*: the image is assumed to exist locally. No attempt is made to pull the image + *IfNotPresent*: default value, the image is pulled only if it is not already present locally. ## 如何更新內部的 Pod + strategy 為 ==RollingUpdate== 的時候並不會把舊有的 Pod 移除反而會讓新舊 Pod 同時上線,以達到無停機服務的作用,但這樣在網頁中就有可能會同時出現新舊內容 + 如果今天 strategy 設定為 ==Recreate== 就會在更新的過程中出現 503 的錯誤。 ## Deployment 回朔版本 ```shell= kubectl rollout undo deployment/helloworld --to-revision=revisionNumber kubectl rollout history deployment/helloworld ``` ## 捨棄 Replication Controller > NOTE: A Deployment that configures a ReplicaSet is now the recommended way to set up replication. ## useful commands ```yaml= kubectl set image deployments frontend simple-webapp=kodekloud/webapp-color:v2 kubectl rollout status deployment nginx-deployment ```