1. Resource allocation list
In order to better solve the problem of service arrangement, Kubernetes introduced the Deployment controller in v1.2. This controller does not directly manage the Pod, but indirectly manages the Pod by managing the ReplicaSet. Therefore, the function of Deployment is more powerful than that of the ReplicaSet;
Parameter query method:
[root@master ~]# kubectl explain deploy
Summary and sorting of parameters:
apiVersion: apps/v1 #Version information kind: Deployment #type metadata: #metadata name: namespace: labels: controller: deploy spec: #Detailed description replicas: 3 #Number of copies revisionHistoryLimit: 30 #Keep the historical version. The default is 10 paused: false #Pause deployment. The default value is false progressDeadlineSeconds: 600 #Deployment timeout, 600s by default strategy: #strategy type: RollingUpdate #Rolling update strategy rollingUpdate: #Set rolling update maxSurge: 30% #The maximum number of additional copies that can exist, which can be a percentage or a number maxUnavailable: 30% #The maximum number of copies in unavailable status, which can be a percentage or a numeric value selector: #Selectors that specify which pods to manage matchLabels: #Matching label rules app: nginx-pod matchExpressions: - key: "app" operator: In values: ["nginx-pod"] template: #Template. When the number of copies is insufficient, create a Pod according to the following configuration metadata: labels: #Pod tag app: nginx-pod spec: containers: - name: nginx image: nginx:1.17.1 ports: - containerPort: 80
2. Create controller
#Create YAML file [root@master ~]# cat pc-deployment.yaml apiVersion: apps/v1 #Version information kind: Deployment #type metadata: #metadata name: pc-deployment namespace: dev spec: #Detailed description replicas: 3 #Number of copies selector: #Selectors that specify which pods to manage matchLabels: #Matching label rules app: nginx-pod template: #Template. When the number of copies is insufficient, create a Pod according to the following configuration metadata: labels: #Pod tag app: nginx-pod spec: containers: - name: nginx image: nginx:1.17.1 ports: - containerPort: 80
#Call YAML file [root@master ~]# kubectl apply -f pc-deployment.yaml deployment.apps/pc-deployment created #View deployment controller [root@master ~]# kubectl get deploy pc-deployment -n dev -o wide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR pc-deployment 3/3 3 3 40s nginx nginx:1.17.1 app=nginx-pod # UP-TO-DATE: the number of copies of the latest version; # AVAILABLE: the number of copies currently AVAILABLE; see RS [root@master ~]# kubectl get rs -n dev NAME DESIRED CURRENT READY AGE pc-deployment-6756f95949 3 3 3 25s #You can find that the RS name is deployment_ Name xxxxxxxxx (ten digit random code) #View the Pod created by the deployment controller [root@master ~]# kubectl get pod -n dev NAME READY STATUS RESTARTS AGE pc-deployment-6756f95949-6vbj4 1/1 Running 0 47s pc-deployment-6756f95949-l99gj 1/1 Running 0 47s pc-deployment-6756f95949-xpj4f 1/1 Running 0 47s # You can find that the Pod name is: rs_ Name xxxxx (five digit random code)
3. Replica expansion
There are many modification methods, including Edit, command modification and Apply (the same as chapter 2.3);
#edit [root@master ~]# kubectl edit deploy pc-deployment -n dev #Command modification [root@master ~]# kubectl scale deploy pc-deployment --replicas=5 -n dev #apply [root@master ~]# kubectl apply -f pc-deployment.yaml
4. Mirror update
Deployment supports two image update strategies: rebuild update and rolling update (default)
- Rebuild update: all existing pods will be closed before creating a new Pod;
- Rolling update: close a part of the Pod (which can be specified by percentage or value) and start the number of pods with the same proportion or value closed. During the update process, there are two versions of Pod;
It can be configured through options;
spec: strategy: #strategy type: RollingUpdate #RollingUpdate rolling update and Recreate rebuild update are supported rollingUpdate: #Set the parameters to be set after rolling update maxSurge: 30% #The maximum number of additional copies that can exist, which can be a percentage or a number maxUnavailable: 30% #The maximum number of copies in unavailable status, which can be a percentage or a numeric value
4.1 reconstruction and update
(in this experiment, it is recommended to use two session terminals. The first window is used for modification, and the second window is used for real-time monitoring and viewing status information)
#Modify YAML file and add the following contents [root@master ~]# cat pc-deployment.yaml spec: ~ strategy: type: Recreate
#Use edit to modify the image version to 1.17.3 [root@master ~]# kubectl edit deploy pc-deployment -n dev deployment.apps/pc-deployment edited #Open the second session terminal and use the parameter w to monitor the creation process of pod [root@master ~]# kubectl get pod -n dev -o wide -w NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pc-deployment-6756f95949-j8bfv 1/1 Running 0 23s 10.244.112.39 node1.k8s <none> <none> pc-deployment-6756f95949-pz7w7 1/1 Running 0 23s 10.244.166.155 node2.k8s <none> <none> pc-deployment-6756f95949-zk96p 1/1 Running 0 23s 10.244.166.154 node2.k8s <none> <none> #Rebuilding and updating will close all pods currently in use; pc-deployment-6756f95949-pz7w7 1/1 Terminating 0 54s 10.244.166.155 node2.k8s <none> <none> pc-deployment-6756f95949-zk96p 1/1 Terminating 0 54s 10.244.166.154 node2.k8s <none> <none> pc-deployment-6756f95949-j8bfv 1/1 Terminating 0 54s 10.244.112.39 node1.k8s <none> <none> pc-deployment-6756f95949-j8bfv 1/1 Terminating 0 54s 10.244.112.39 node1.k8s <none> <none> pc-deployment-6756f95949-pz7w7 1/1 Terminating 0 54s 10.244.166.155 node2.k8s <none> <none> pc-deployment-6756f95949-zk96p 1/1 Terminating 0 54s 10.244.166.154 node2.k8s <none> <none> pc-deployment-6756f95949-zk96p 0/1 Terminating 0 55s 10.244.166.154 node2.k8s <none> <none> pc-deployment-6756f95949-zk96p 0/1 Terminating 0 55s 10.244.166.154 node2.k8s <none> <none> pc-deployment-6756f95949-zk96p 0/1 Terminating 0 55s 10.244.166.154 node2.k8s <none> <none> pc-deployment-6756f95949-pz7w7 0/1 Terminating 0 55s 10.244.166.155 node2.k8s <none> <none> pc-deployment-6756f95949-pz7w7 0/1 Terminating 0 55s 10.244.166.155 node2.k8s <none> <none> pc-deployment-6756f95949-pz7w7 0/1 Terminating 0 55s 10.244.166.155 node2.k8s <none> <none> pc-deployment-6756f95949-j8bfv 0/1 Terminating 0 55s <none> node1.k8s <none> <none> pc-deployment-6756f95949-j8bfv 0/1 Terminating 0 55s <none> node1.k8s <none> <none> pc-deployment-6756f95949-j8bfv 0/1 Terminating 0 55s <none> node1.k8s <none> <none> #Recreate all numbers of new pods to replace the deleted old pods pc-deployment-78dbc7d98c-56vc2 0/1 Pending 0 0s <none> <none> <none> <none> pc-deployment-78dbc7d98c-t5b99 0/1 Pending 0 0s <none> <none> <none> <none> pc-deployment-78dbc7d98c-4cg6f 0/1 Pending 0 0s <none> <none> <none> <none> pc-deployment-78dbc7d98c-56vc2 0/1 Pending 0 0s <none> node2.k8s <none> <none> pc-deployment-78dbc7d98c-t5b99 0/1 Pending 0 0s <none> node1.k8s <none> <none> pc-deployment-78dbc7d98c-4cg6f 0/1 Pending 0 0s <none> node2.k8s <none> <none> pc-deployment-78dbc7d98c-56vc2 0/1 ContainerCreating 0 0s <none> node2.k8s <none> <none> pc-deployment-78dbc7d98c-t5b99 0/1 ContainerCreating 0 0s <none> node1.k8s <none> <none> pc-deployment-78dbc7d98c-4cg6f 0/1 ContainerCreating 0 0s <none> node2.k8s <none> <none> pc-deployment-78dbc7d98c-t5b99 0/1 ContainerCreating 0 1s <none> node1.k8s <none> <none> pc-deployment-78dbc7d98c-4cg6f 0/1 ContainerCreating 0 1s <none> node2.k8s <none> <none> pc-deployment-78dbc7d98c-56vc2 0/1 ContainerCreating 0 1s <none> node2.k8s <none> <none> pc-deployment-78dbc7d98c-t5b99 1/1 Running 0 93s 10.244.112.40 node1.k8s <none> <none> pc-deployment-78dbc7d98c-4cg6f 1/1 Running 0 98s 10.244.166.156 node2.k8s <none> <none> pc-deployment-78dbc7d98c-56vc2 1/1 Running 0 112s 10.244.166.157 node2.k8s <none> <none> #View current mirror version [root@master ~]# kubectl get deploy pc-deployment -n dev -o wide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR pc-deployment 3/3 3 3 3m56s nginx nginx:1.17.3 app=nginx-pod
4.2 rolling update
(it is suggested to use three session terminals in this experiment. The first window is used to modify, the second window is used to monitor the status information of rs controller in real time, and the third window is used to monitor and view the status information of Pod in real time)
#Modify YAML file and add the following contents [root@master ~]# cat pc-deployment.yaml spec: ~ strategy: #strategy type: RollingUpdate #RollingUpdate rollingupdate rollingUpdate: maxSurge: 25% maxUnavailable: 25%
#Turn on the marking function, and record is the marking parameter, which will log the deployment version upgrade and fallback operations for later viewing and fallback operations [root@master ~]# kubectl apply -f pc-deployment.yaml --record Flag --record has been deprecated, --record will be removed in the future deployment.apps/pc-deployment created #Use edit to modify the image version to 1.17.3 [root@master ~]# kubectl edit deploy pc-deployment -n dev deployment.apps/pc-deployment edited #Open the second session terminal and use parameter w to monitor the process of rs controller [root@master ~]# kubectl get rs -n dev -w NAME DESIRED CURRENT READY AGE pc-deployment-6499c8d644 3 3 3 13m #Turn on rolling updates pc-deployment-78dbc7d98c 1 0 0 0s pc-deployment-78dbc7d98c 1 0 0 0s pc-deployment-78dbc7d98c 1 1 0 0s pc-deployment-78dbc7d98c 1 1 1 1s pc-deployment-6499c8d644 2 3 3 14m #The new RS creates the first pod to replace the first pod of the old rs pc-deployment-78dbc7d98c 2 1 1 1s pc-deployment-6499c8d644 2 3 3 14m pc-deployment-6499c8d644 2 2 2 14m pc-deployment-78dbc7d98c 2 1 1 1s pc-deployment-78dbc7d98c 2 2 1 1s pc-deployment-78dbc7d98c 2 2 2 2s pc-deployment-6499c8d644 1 2 2 14m pc-deployment-6499c8d644 1 2 2 14m #The new RS creates the second pod instead of the old rs pc-deployment-78dbc7d98c 3 2 2 2s pc-deployment-6499c8d644 1 1 1 14m pc-deployment-78dbc7d98c 3 2 2 2s pc-deployment-78dbc7d98c 3 3 2 2s pc-deployment-78dbc7d98c 3 3 3 3s pc-deployment-6499c8d644 0 1 1 14m pc-deployment-6499c8d644 0 1 1 14m #The new RS creates the third pod instead of the old rs #Open the third session terminal and use the parameter w to monitor the creation process of pod [root@master ~]# kubectl get pod -n dev -o wide -w NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pc-deployment-6499c8d644-7hbkc 1/1 Running 0 11m 10.244.166.164 node2.k8s <none> <none> pc-deployment-6499c8d644-nfwdt 1/1 Running 0 14m 10.244.166.163 node2.k8s <none> <none> pc-deployment-6499c8d644-sdd89 1/1 Running 0 12m 10.244.112.48 node1.k8s <none> <none> #Start rolling update pc-deployment-78dbc7d98c-d7gv9 0/1 Pending 0 0s <none> <none> <none> <none> pc-deployment-78dbc7d98c-d7gv9 0/1 Pending 0 0s <none> node1.k8s <none> <none> pc-deployment-78dbc7d98c-d7gv9 0/1 ContainerCreating 0 0s <none> node1.k8s <none> <none> pc-deployment-78dbc7d98c-d7gv9 0/1 ContainerCreating 0 1s <none> node1.k8s <none> <none> pc-deployment-78dbc7d98c-d7gv9 1/1 Running 0 1s 10.244.112.49 node1.k8s <none> <none> pc-deployment-6499c8d644-sdd89 1/1 Terminating 0 13m 10.244.112.48 node1.k8s <none> <none> #The new RS creates the first pod to replace the first pod of the old rs pc-deployment-78dbc7d98c-84jpr 0/1 Pending 0 0s <none> <none> <none> <none> pc-deployment-78dbc7d98c-84jpr 0/1 Pending 0 0s <none> node2.k8s <none> <none> pc-deployment-78dbc7d98c-84jpr 0/1 ContainerCreating 0 0s <none> node2.k8s <none> <none> pc-deployment-6499c8d644-sdd89 1/1 Terminating 0 13m 10.244.112.48 node1.k8s <none> <none> pc-deployment-78dbc7d98c-84jpr 0/1 ContainerCreating 0 1s <none> node2.k8s <none> <none> pc-deployment-78dbc7d98c-84jpr 1/1 Running 0 1s 10.244.166.165 node2.k8s <none> <none> pc-deployment-6499c8d644-nfwdt 1/1 Terminating 0 14m 10.244.166.163 node2.k8s <none> <none> #The new RS creates the second pod instead of the old rs pc-deployment-78dbc7d98c-nckcs 0/1 Pending 0 0s <none> <none> <none> <none> pc-deployment-78dbc7d98c-nckcs 0/1 Pending 0 0s <none> node1.k8s <none> <none> pc-deployment-78dbc7d98c-nckcs 0/1 ContainerCreating 0 0s <none> node1.k8s <none> <none> pc-deployment-6499c8d644-sdd89 0/1 Terminating 0 13m 10.244.112.48 node1.k8s <none> <none> pc-deployment-6499c8d644-sdd89 0/1 Terminating 0 13m 10.244.112.48 node1.k8s <none> <none> pc-deployment-6499c8d644-sdd89 0/1 Terminating 0 13m 10.244.112.48 node1.k8s <none> <none> pc-deployment-6499c8d644-nfwdt 1/1 Terminating 0 14m 10.244.166.163 node2.k8s <none> <none> pc-deployment-78dbc7d98c-nckcs 0/1 ContainerCreating 0 1s <none> node1.k8s <none> <none> pc-deployment-6499c8d644-nfwdt 0/1 Terminating 0 14m 10.244.166.163 node2.k8s <none> <none> pc-deployment-6499c8d644-nfwdt 0/1 Terminating 0 14m 10.244.166.163 node2.k8s <none> <none> pc-deployment-6499c8d644-nfwdt 0/1 Terminating 0 14m 10.244.166.163 node2.k8s <none> <none> pc-deployment-78dbc7d98c-nckcs 1/1 Running 0 1s 10.244.112.50 node1.k8s <none> <none> pc-deployment-6499c8d644-7hbkc 1/1 Terminating 0 11m 10.244.166.164 node2.k8s <none> <none> #The new RS creates the third pod instead of the old rs pc-deployment-6499c8d644-7hbkc 1/1 Terminating 0 11m 10.244.166.164 node2.k8s <none> <none> pc-deployment-6499c8d644-7hbkc 0/1 Terminating 0 11m 10.244.166.164 node2.k8s <none> <none> pc-deployment-6499c8d644-7hbkc 0/1 Terminating 0 11m 10.244.166.164 node2.k8s <none> <none> pc-deployment-6499c8d644-7hbkc 0/1 Terminating 0 11m 10.244.166.164 node2.k8s <none> <none> #View current Pod [root@master ~]# kubectl get pod -n dev -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pc-deployment-78dbc7d98c-84jpr 1/1 Running 0 4m57s 10.244.166.165 node2.k8s <none> <none> pc-deployment-78dbc7d98c-d7gv9 1/1 Running 0 4m58s 10.244.112.49 node1.k8s <none> <none> pc-deployment-78dbc7d98c-nckcs 1/1 Running 0 4m56s 10.244.112.50 node1.k8s <none> <none> #View rs controller [root@master ~]# kubectl get rs -n dev NAME DESIRED CURRENT READY AGE pc-deployment-6499c8d644 0 0 0 19m pc-deployment-78dbc7d98c 3 3 3 5m14s #View current mirror version [root@master ~]# kubectl get deploy pc-deployment -n dev -owide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR pc-deployment 3/3 3 3 23m nginx nginx:1.17.3 app=nginx-pod
5. Version fallback
Deployment supports pause, resume and version fallback during version upgrade, as follows:
[root@master ~]# kubectl rollout --help ~ Available Commands: history View rollout history #Display upgrade history pause Mark the provided resource as paused #Pause the version upgrade process restart Restart a resource #Restart the version upgrade process resume Resume a paused resource #Resume the suspended version upgrade process status Show the status of the rollout #Displays the current upgrade status undo Undo a previous rollout #To go back to the previous version, you can use -- to revision to go back to the specified version
5.1 case demonstration
#View the status of the current upgraded version [root@master ~]# kubectl rollout status deploy pc-deployment -n dev deployment "pc-deployment" successfully rolled out #View upgrade history [root@master ~]# kubectl rollout history deploy pc-deployment -n dev deployment.apps/pc-deployment REVISION CHANGE-CAUSE 1 <none> 2 <none> #The specific reason why the log record is displayed as < none > here is that the -- Record flag is not added to record parameters when using apply (experimental chapter 3.4.2) [root@master ~]# kubectl apply -f pc-deployment.yaml --record Flag --record has been deprecated, --record will be removed in the future deployment.apps/pc-deployment created #Logging under normal conditions [root@master ~]# kubectl rollout history deploy pc-deployment -n dev deployment.apps/pc-deployment REVISION CHANGE-CAUSE 1 kubectl apply --filename=pc-deployment.yaml --record=true 2 kubectl apply --filename=pc-deployment.yaml --record=true · #Version fallback [root@master ~]# kubectl rollout undo deploy pc-deployment --to-revision=1 -n dev deployment.apps/pc-deployment rolled back #Confirm whether the version fallback is successful [root@master ~]# kubectl get deploy pc-deployment -n dev -o wide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR pc-deployment 3/3 3 3 30m nginx nginx:1.17.1 app=nginx-pod #View the Pod information contained under the old and new rs [root@master ~]# kubectl get rs -n dev NAME DESIRED CURRENT READY AGE pc-deployment-6499c8d644 3 3 3 19m pc-deployment-78dbc7d98c 0 0 0 5m14s
6. Canary release
The Deployment controller supports user-defined control of the rolling rhythm in the update process, such as pause and continue. For example, the update process is suspended immediately after the creation of the first batch of new Pod resources. At this time, there are only some new version applications, the main part is still the old version applications, and then a small part of user requests are filtered out and routed to the new version applications, Observe whether the new version of the application can run stably in the expected way. After confirming that the operation is normal, continue to complete the upgrade of the remaining old version of the application, otherwise roll back to the version before the update immediately;
#Prepare the environment [root@master ~]# kubectl apply -f pc-deployment.yaml deployment.apps/pc-deployment created [root@master ~]# kubectl get deploy pc-deployment -n dev -owide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR pc-deployment 3/3 3 3 20s nginx nginx:1.17.1 app=nginx-pod #Update nginx version and configure suspend deploy [root@master ~]# kubectl set image deploy pc-deployment nginx=nginx:1.17.3 -n dev && kubectl rollout pause deployment pc-deployment -n dev deployment.apps/pc-deployment image updated deployment.apps/pc-deployment paused #Open the second terminal and observe the update status [root@master ~]# kubectl rollout status deploy pc-deployment -n dev Waiting for deployment "pc-deployment" rollout to finish: 1 out of 3 new replicas have been updated... #View the rs in the current pause state. At this time, the new rs has updated a pod, but the old pod does not delete the original pod (Pause effect) [root@master ~]# kubectl get rs -n dev -owide NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR pc-deployment-6756f95949 3 3 3 2m32s nginx nginx:1.17.1 app=nginx-pod,pod-template-hash=6756f95949 pc-deployment-78dbc7d98c 1 1 1 49s nginx nginx:1.17.3 app=nginx-pod,pod-template-hash=78dbc7d98c #View Pod [root@master ~]# kubectl get pod -n dev NAME READY STATUS RESTARTS AGE pc-deployment-6756f95949-96t2g 1/1 Running 0 3m24s pc-deployment-6756f95949-bqh9b 1/1 Running 0 3m24s pc-deployment-6756f95949-fnhs7 1/1 Running 0 3m24s pc-deployment-78dbc7d98c-wz4r5 1/1 Running 0 101s #After confirming that there is no problem with the update, resume the update [root@master ~]# kubectl rollout resume deploy pc-deployment -n dev deployment.apps/pc-deployment resumed #View the status information after restoring the update on the second terminal [root@master ~]# kubectl rollout status deploy pc-deployment -n dev Waiting for deployment "pc-deployment" rollout to finish: 1 out of 3 new replicas have been updated... Waiting for deployment spec update to be observed... Waiting for deployment spec update to be observed... Waiting for deployment "pc-deployment" rollout to finish: 1 out of 3 new replicas have been updated... Waiting for deployment "pc-deployment" rollout to finish: 1 out of 3 new replicas have been updated... Waiting for deployment "pc-deployment" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "pc-deployment" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "pc-deployment" rollout to finish: 2 out of 3 new replicas have been updated... Waiting for deployment "pc-deployment" rollout to finish: 1 old replicas are pending termination... Waiting for deployment "pc-deployment" rollout to finish: 1 old replicas are pending termination... deployment "pc-deployment" successfully rolled out #View the updated RS information. At this time, the new RS has successfully replaced the old rs [root@master ~]# kubectl get rs -n dev -owide NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR pc-deployment-6756f95949 0 0 0 4m58s nginx nginx:1.17.1 app=nginx-pod,pod-template-hash=6756f95949 pc-deployment-78dbc7d98c 3 3 3 3m15s nginx nginx:1.17.3 app=nginx-pod,pod-template-hash=78dbc7d98c [root@master ~]# kubectl get pod -n dev NAME READY STATUS RESTARTS AGE pc-deployment-78dbc7d98c-n2dcm 1/1 Running 0 80s pc-deployment-78dbc7d98c-wz4r5 1/1 Running 0 3m34s pc-deployment-78dbc7d98c-z6759 1/1 Running 0 82s
7. Delete controller
[root@master ~]# kubectl delete -f pc-deployment.yaml deployment.apps "pc-deployment" deleted