A Deployment controls the number and state of Pods by creating ReplicaSets. This post mainly introduces some common Deployment operations.
Passing the --dry-run flag means the command is not actually executed, only YAML output is generated:
1
| kubectl create deployment nginx --image=nginx --dry-run -o yaml
|
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: nginx
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}
2. Creating
1
| kubectl run nginx --image=nginx
|
kubectl create deployment nginx --image=nginx
- Using a yaml file directly
To make editing more efficient, people usually edit based on an existing Deployment yaml. For example, first run kubectl create deployment nginx --image=nginx --dry-run -o yaml > deployment.yaml, then edit the image, the number of replicas, and other information.
1
| kubectl apply -f deployment.yaml
|
3. Upgrades and Updates
1
| kubectl set image deployment/nginx nginx=nginx:1.9.1
|
- Limit memory and CPU usage
1
| kubectl set resource deployment/nginx -c=nginx --limits=cpu=200m,memory=512Mi
|
- Edit the yaml file directly
The edit command lets you directly edit a Kubernetes object that has already been created; once you save and exit, the change takes effect immediately.
1
| kubectl edit deployment nginx
|
4. Rolling Strategy
In the spec field, you can configure the rolling upgrade strategy:
1
2
3
4
5
6
| spec:
strategy:
rollingUpdate:
maxSurge: 25% # Maximum ratio of extra replicas
maxUnavailable: 25% # Minimum ratio of unavailable replicas
type: RollingUpdate
|
- Pause a Deployment update
1
| kubectl rollout pause deployment/nginx
|
- Resume a Deployment update
1
| kubectl rollout resume deployment/nginx
|
- Roll a Deployment back to a specified revision
1
| kubectl rollout undo deployment/nginx --to-revision=2
|
- Check the Deployment rollout status
1
| kubectl rollout status deployment/nginx
|
- Check the Deployment revision history
1
| kubectl rollout history deployment/nginx
|
5. Elastic Scaling
- Manually change the number of replicas
1
| kubectl scale deployment/nginx --replicas=3
|
- Automatically scale the number of replicas
The cluster’s Horizontal Pod Autoscaler feature depends on the monitoring data provided by an aggregator. Metrics Server is the aggregator for the cluster’s core monitoring data, collecting metric data from the Kubelet.
Create a metrics-server.yaml file with the following content:
1
2
3
4
| args:
- --logtostderr
- --kubelet-insecure-tls
- --kubelet-preferred-address-types=InternalIP
|
Install the Metric Server service
1
2
3
4
| helm install stable/metrics-server \
-n metrics-server \
--namespace kube-system \
-f metrics-server.yaml
|
Check resource usage
1
2
3
| kubectl top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
i-d7uwk2b1 443m 5% 5213Mi 67%
|
Dynamically scale the number of replicas based on the CPU utilization of existing Pods
1
| kubectl autoscale deployment/nginx --min=1 --max=5 --cpu-percent=80
|