Managing Kubernetes Deployments with kubectl Commands
To create a Deployment, use the kubectl create command with the --record flag to track the command in annotations for future reference. This is useful for reviewing commends executed in each Deployment revision.
kubectl create -f https://kubernetes.io/docs/user-guide/nginx-deployment.yaml --record
deployment "nginx-deployment" created
Immediate after creation, run kubectl get deployment to view the status of Deployments:
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
alpine-fbgweb 1 1 1 1 12h
alpine-lnp 1 1 1 1 3d
demo-deployment 3 3 3 3 15h
jenkins 1 1 1 1 3d
nginx-dm 2 2 2 2 13d
nginx1-7-deployment 1 1 1 1 15h
nginx1-8-deployment 1 1 1 1 15h
To scale a Deployment, adjust the number of replicas:
kubectl scale deployment nginx-deployment --replicas 10
If the cluster suports horizontal pod autosccaling, configure automatic scaling:
kubectl autoscale deployment nginx-deployment --min=10 --max=15 --cpu-percent=80
Updating the container image is straightforward:
kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
To roll back to the previuos version:
kubectl rollout undo deployment/nginx-deployment
Specify a particular historical revision for rollback:
kubectl rollout undo deployment/nginx-deployment --to-revision=2
deployment "nginx-deployment" rolled back
View the revision history of a Deployment:
kubectl rollout history deployment/alpine-fbgweb
REVISION CHANGE-CAUSE
1 kubectl apply --filename=/data/scripts/app/fbgweb.yaml --record=true
2 kubectl apply --filename=/data/scripts/app/fbgweb.yaml --record=true
Using the --record parameter during Deployment creation logs commands, making it easy to inspect changes in each revision.
Examine detailed information for a specific revision:
kubectl rollout history deployment alpine-fbgweb --revision=1
deployments "alpine-fbgweb" with revision #1
Pod Template:
Labels: app=alpine-fbgweb
pod-template-hash=469852024
Annotations: kubernetes.io/change-cause=kubectl apply --filename=/data/scripts/app/fbgweb.yaml --record=true
Containers:
alpine-fbgweb:
Image: 192.168.0.153:5000/fbgweb:2017-11-13-13-49-30
Port: 80/TCP
Environment: <none>
Mounts:
/etc/localtmie from tz-config (rw)
Volumes:
tz-config:
Type: HostPath (bare host directory volume)
Path: /usr/share/zoneinfo/Asia/Shanghai