Deployments

A Deployment provides declarative updates for Pods and ReplicaSets (in simple terms, it lets you use kubectl apply to update the Pod configuration managed by a ReplicaSet).

A Simple Example

In this example, we will deploy 3 replicas of nginx version 1.16. Example manifest: https://raw.githubusercontent.com/chengqing-su/kubernetes-learning/master/deployment/nginx-deployment.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3 # 可选,用于指定所期望的Pod的数量。默认为1。
selector:
matchLabels:
app: nginx
template: # Pod的模板
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16
ports:
- containerPort: 80

Creating a New Deployment

Use the declarative approach to deploy the example above:

1
kubectl apply -f https://raw.githubusercontent.com/chengqing-su/kubernetes-learning/master/deployment/nginx-deployment.yaml

Process & Result:

create-a-new

As you can see, during the deployment process above, a ReplicaSet named nginx-deployment-79fb9cc9bb was created, along with Pods using nginx-deployment-79fb9cc9bb as their name prefix.

Updating a Deployment

I upgraded the nginx version in the example above to 1.17 and created a new manifest at https://raw.githubusercontent.com/chengqing-su/kubernetes-learning/master/deployment/nginx-deployment-update.yaml.

Use the declarative approach to deploy the updated example:

1
kubectl apply -f https://raw.githubusercontent.com/chengqing-su/kubernetes-learning/master/deployment/nginx-deployment-update.yaml

Process & Result:

update

As you can see, during the deployment process above, a new ReplicaSet named nginx-deployment-6c5bfff4d9 was created. It started with only one Pod, then gradually scaled up to 3, while the number of Pods managed by the old ReplicaSet gradually decreased to 0.

In the manifests above, I did not specify a deployment strategy, so the Deployment defaults to RollingUpdate. Currently, Deployments support two strategies: RollingUpdate and Recreate, which can be selected via .spec.strategy.type.

Q: After completing an update, why does the old ReplicaSet nginx-deployment-79fb9cc9bb still exist?

This depends on the configured cleanup policy. By default, a Deployment retains 10 ReplicaSets. You can use the .spec.revisionHistoryLimit field to specify how many ReplicaSets you want to keep.

Managing a Deployment Imperatively

It is not recommended to manage Kubernetes resources imperatively, as such operations cannot be versioned or stored in a version control system. The following is therefore only a brief introduction.

Rollback

You can also update a Deployment imperatively, for example:

1
kubectl set image deployment.v1.apps/nginx-deployment nginx=nginx:1.17 --record=true

Using --record records this change on the resource.

You can use kubectl rollout history to view the revision history of a resource.

Then use kubectl rollout undo to roll the resource back to the previous version or a specific version.

Scaling

There are two scaling mechanisms:

The first is a simple scale that sets the number of Pods to a specified count. For example, scaling the 3 replicas above up to 10:

1
kubectl scale deployment.v1.apps/nginx-deployment --replicas=10

The other is autoscaling, which dynamically scales a Deployment based on metrics such as CPU utilization:

1
kubectl autoscale deployment.v1.apps/nginx-deployment --min=10 --max=15 --cpu-percent=80

Deleting a Deployment

Run the delete command:

1
kubectl delete deployment nginx-deployment

Process & Result:

delete