官术网_书友最值得收藏!

Deployments

At this point, you already know the purpose of Pods and ReplicaSets. Deployments are Kubernetes objects that provide declarative updates for Pods and ReplicaSets. You can declaratively perform operations such as the following by using them:

  • Perform a rollout of a new ReplicaSet.
  • Change the Pod template and perform a controlled rollout. The old ReplicaSet will be gradually scaled down, whereas the new ReplicaSet will scale up at the same rate.
  • Perform a rollback to an earlier version of the Deployment.
  • Scale the ReplicaSet up or down.

The relationship of Deployment to ReplicaSets and Pods can be seen in the following diagram:

You should avoid managing ReplicaSets created by a Deployment on your own. If you need to make any changes to the ReplicaSet, perform the changes on the owning Deployment Object.

Note that the issue of the accidental acquisition of Pods by ReplicaSets managed by Deployments does not exist. The reason for this is that Pods and ReplicaSets use a special, automatically generated label called pod-template-hash that guarantees the uniqueness of the selection.

Let's take a look at an example Deployment manifest in the nginx-deployment.yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment-example
spec:
replicas: 3
selector:
matchLabels:
environment: test
template:
metadata:
labels:
environment: test
spec:
containers:
- name: nginx
image: nginx:1.17
ports:
- containerPort: 80

As you can see, the basic structure is almost identical to ReplicaSet, but there are significant differences in how Deployment behaves when you perform a declarative update. Let's quickly demonstrate this in the playground:

  1. Create manually the Deployment manifest file or download it using the wget command:
wget https://raw.githubusercontent.com/PacktPublishing/Hands-On-Kubernetes-on-Windows/master/Chapter04/03_deployment-example/nginx-deployment.yaml
  1. Apply the Deployment manifest file using the following command:
kubectl apply -f nginx-deployment.yaml --record
The  --record flag adds a metadata annotation of  kubernetes.io/change-cause to API objects that were created or modified by the preceding command. This feature allows you to easily track changes in your cluster.
  1. Wait for the Deployment to fully roll out (you can observe the number of ready Pods in your deployment using kubectl get deployment -w).
  2. Now, change the Pod Spec in the template in the YAML manifest; for example, change .spec.template.spec.containers[0].image to nginx:1.16 and apply the Deployment manifest again.
  3. Immediately after that, observe how the rollout progresses using the following command:
master $ kubectl rollout status deployment nginx-deployment-example
Waiting for deployment "nginx-deployment-example" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment-example" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment-example" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deployment-example" successfully rolled out

The Spec  of Deployment is much richer than ReplicaSet. You can check the official documentation for more details:  https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#writing-a-deployment-spec . The official documentation contains multiple use cases of Deployments, all of which are described in detail:  https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#use-case .

As you can see, the declarative update to the Deployment template definition caused a smooth rollout of new Pod replicas. The old ReplicaSet was scaled down and, simultaneously, a new ReplicaSet, with a new Pod template, was created and gradually scaled up. You can now try performing the same operation with an image update for an existing bare ReplicaSet and you will see that... actually, nothing happens. This is because ReplicaSet only uses a Pod template to create new Pods. Existing Pods will not be updated or removed by such a change.

A rollout is only triggered when the  .spec.template for Deployment is changed. Other changes to the Deployment manifest will not trigger a rollout.

Next, let's take a look at a concept similar to Deployments: StatefulSets.

主站蜘蛛池模板: 福州市| 仁布县| 浪卡子县| 黄龙县| 阜平县| 五大连池市| 星座| 望都县| 乌拉特前旗| 上思县| 阳东县| 灵宝市| 邵阳市| 罗定市| 平原县| 临澧县| 获嘉县| 分宜县| 邳州市| 宣化县| 徐闻县| 亳州市| 沈阳市| 柏乡县| 安康市| 固始县| 永康市| 镇宁| 涟源市| 淮北市| 卢龙县| 涪陵区| 宜阳县| 马龙县| 赤峰市| 沭阳县| 邢台市| 沈阳市| 泉州市| 道孚县| 鸡泽县|