Deployment
Pod describes pod, which is the smallest and simplest unit in the Kubernetes object model that you create or deploy. However, a pod is designed to be a one-off entity. A pod can be evicted (when node resources are insufficient) and disappears as the cluster node fails. Kubernetes provides controllers to manage pods. Controllers can create and manage multiple pods, and provide replica management, rolling upgrade, and self-healing capabilities. The most commonly used is Deployment.
A Deployment can contain one or more pod replicas. Each pod replica has the same role. Therefore, the system automatically distributes requests to multiple pod replicas of a Deployment.
A Deployment integrates a lot of functions, including online deployment, rolling upgrade, replica creation, and restoration of online jobs. To some extent, Deployments can be used to realize unattended rollout, which greatly reduces communication difficulties and operation risks in the rollout process.
Creating a Deployment
In the following example, a Deployment named nginx is created, and two pod replicas are created from the nginx:latest image. Each pod replica occupies 500m and 1024Mi memory.
apiVersion: apps/v1 # Note the difference from that of pods. It is apps/v1 instead of v1 for a Deployment. kind: Deployment # The resource type is Deployment. metadata: name: nginx # Name of the Deployment spec: replicas: 2 # Number of pod replicas. The Deployment ensures that two pod replicas are running. selector: # Label Selector matchLabels: app: nginx template: # Definition of a pod, which is used to create pods. It is also known as pod template. metadata: labels: app: nginx spec: volumes: - name: cci-sfs-test # SFS volume name persistentVolumeClaim: claimName: cci-sfs-test containers: - image: nginx:latest name: container-0 resources: limits: cpu: 500m memory: 1024Mi requests: cpu: 500m memory: 1024Mi volumeMounts: - name: cci-sfs-test mountPath: /tmp/sfs0/krlp2k8j # SFS volume's mount path in a container imagePullSecrets: # Secret used to pull the image, which must be imagepull-secret. - name: imagepull-secret
In this definition, the name of the Deployment is nginx, and spec.replicas defines the number of pods. That is, the Deployment controls two pods. spec.selector is a label selector, indicating that the Deployment selects the pod whose label is app=nginx. spec.template is the definition of the pod and is the same as that defined in Pod.
Save the definition of the Deployment to deployment.yaml and use kubectl to create the Deployment.
Run the kubectl get command to view the Deployment and the pods. The value of DESIRED is 2, indicating that the Deployment desires two pods. The value of CURRENT is 2, indicating that there are two pods. The value of AVAILABLE is 2, indicating that two pods are available.
$ kubectl create -f deployment.yaml -n $namespace_name $ kubectl get deployment -n $namespace_name NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE nginx 2 2 2 2 8s
How Does the Deployment Control Pods?
Continue to query pods, as shown below.
$ kubectl get pods -n $namespace_name NAME READY STATUS RESTARTS AGE nginx-7f98958cdf-tdmqk 1/1 Running 0 13s nginx-7f98958cdf-txckx 1/1 Running 0 13s
If you delete a pod, a new pod is immediately created, as shown below. As mentioned above, the Deployment ensures that there are two pods running. If a pod is deleted, the Deployment creates another pod. If a pod crashes or is faulty, the Deployment automatically restarts the pod.
$ kubectl delete pod nginx-7f98958cdf-txckx -n $namespace_name $ kubectl get pods -n $namespace_name NAME READY STATUS RESTARTS AGE nginx-7f98958cdf-tdmqk 1/1 Running 0 21s nginx-7f98958cdf-tesqr 1/1 Running 0 21s
There are two pods, nginx-7f98958cdf-tdmqk and nginx-7f98958cdf-tesqr, in which nginx is the name of the Deployment, and -7f98958cdf-tdmqk and -7f98958cdf-tesqr are the suffixes randomly generated by Kubernetes.
You may notice that the first part of the two suffixes is the same, that is, 7f98958cdf. This is because the Deployment does not control the pod directly, but through a controller named ReplicaSet. You can run the following command to query the ReplicaSet, in which rs is the abbreviation of ReplicaSet.
$ kubectl get rs -n $namespace_name NAME DESIRED CURRENT READY AGE nginx-7f98958cdf 3 3 3 1m
The name of the ReplicaSet is nginx-7f98958cdf, and the suffix -7f98958cdf is generated randomly.
Figure 1 shows how the Deployment controls the pod via the ReplicaSet.
If you run the kubectl describe command to view the details of the Deployment, you can see the ReplicaSet. As shown below, you can see a line NewReplicaSet: nginx-7f98958cdf (2/2 replicas created). In events, the number of pods of the ReplicaSet is scaled out to 2. In practice, you may not operate ReplicaSet directly, but understanding that a Deployment controls a pod by controlling a ReplicaSet helps you locate problems.
$ kubectl describe deploy nginx -n $namespace_name Name: nginx Namespace: default CreationTimestamp: Sun, 16 Dec 2018 19:21:58 +0800 Labels: app=nginx ... NewReplicaSet: nginx-7f98958cdf (2/2 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 5m deployment-controller Scaled up replica set nginx-7f98958cdf to 2
Upgrade
In actual applications, upgrade is a common operation. A Deployment can easily support application upgrade.
You can set different upgrade policies for a Deployment:
- RollingUpdate: Gradually create new pods and delete old pods). This is the default policy.
- Recreate: Delete the current pods and then create new pods.
The Deployment upgrade can be in declarative mode. That is, you only need to modify the YAML definition of the Deployment. For example, you can run the kubectl edit command to change the Deployment image to nginx:alpine. After the modification, query the ReplicaSet and pod, a new ReplicaSet is created, and the pod is recreated.
$ kubectl edit deploy nginx -n $namespace_name $ kubectl get rs -n $namespace_name NAME DESIRED CURRENT READY AGE nginx-6f9f58dffd 2 2 2 1m nginx-7f98958cdf 0 0 0 48m $ kubectl get pods -n $namespace_name NAME READY STATUS RESTARTS AGE nginx-6f9f58dffd-tdmqk 1/1 Running 0 21s nginx-6f9f58dffd-tesqr 1/1 Running 0 21s
The Deployment can use the maxSurge and maxUnavailable parameters to control the proportion of pods to be recreated during the upgrade. This is useful in many scenarios. The configuration is as follows:
spec: strategy: rollingUpdate: maxSurge: 1 maxUnavailable: 0 type: RollingUpdate
- maxSurge: specifies the percent of pods that can exist over spec.replicas in the Deployment. The default value is 25%. For example, if spec.replicas is set to 4, no more than five pods can exist during the upgrade, and the upgrade step is 1. The absolute number is calculated from the percentage by rounding up. The value can also be set to an absolute number.
- maxUnavailable: specifies the percent of pods that can be unavailable during the update. The default value is 25%. For example, if spec.replicas is set to 4, there must be at least three pods during the upgrade, so the deleting step is 1. The value can also be set to an absolute number.
In the preceding example, the value of spec.replicas is 2. Suppose both maxSurge and maxUnavailable are the default value 25%, maxSurge allows a maximum of three pods to exist (2 * 1.25 = 2.5, rounded up to 3), and maxUnavailable does not allow unavailable pods (2 * 0.75 = 1.5, rounded up to 2). As a result, two pods are running during the upgrade. Each time a new pod is created, an old pod is deleted, until all pods are new.
Rollback
Rollback is to roll an application back to the earlier version when a fault occurs during the upgrade. A Deployment can be easily rolled back to the earlier version.
For example, if the upgraded image is faulty, you can run the kubectl rollout undo command to roll back.
$ kubectl rollout undo deployment nginx -n $namespace_name deployment "nginx" rolled back
A Deployment can be easily rolled back because a Deployment uses a ReplicaSet to control a pod. After the upgrade, the ReplicaSet still exists. The Deployment is rolled back by recreating the pod using the ReplicaSet. The number of ReplicaSets stored in a Deployment can be restricted by the revisionHistoryLimit parameter. The default value is 10.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot