第6题:Deployment 资源的更新:
题目:按照以下方式创建部署:
- 名称:nginx-app
- 使用1.11.9-alpine版本的容器nginx
- deploymnet应该包含3个副本
- 使用新版本1.12.0-alpine部署应用程序,执行滚动更新并记录更新。
- 将更新回滚到前一个版本1.11.9-alpine
解题思路:
本题考的是滚动更新,从1.11版rolling-update
开始已弃用(请参阅CHANGELOG-1.11.md),改用rollout
。
常用操作:
kubectl set image deployment/frontend www=image:v2 # Rolling update "www" containers of "frontend" deployment, updating the image
kubectl rollout history deployment/frontend # Check the history of deployments including the revision
kubectl rollout undo deployment/frontend # Rollback to the previous deployment
kubectl rollout undo deployment/frontend --to-revision=2 # Rollback to a specific revision
kubectl rollout status -w deployment/frontend # Watch rolling update status of "frontend" deployment until completion
kubectl rollout restart deployment/frontend # Rolling restart of the "frontend" deployment
# deprecated starting version 1.11
kubectl rolling-update frontend-v1 -f frontend-v2.json # (deprecated) Rolling update pods of frontend-v1
kubectl rolling-update frontend-v1 frontend-v2 --image=image:v2 # (deprecated) Change the name of the resource and update the image
kubectl rolling-update frontend --image=image:v2 # (deprecated) Update the pods image of frontend
kubectl rolling-update frontend-v1 frontend-v2 --rollback # (deprecated) Abort existing rollout in progress
cat pod.json | kubectl replace -f - # Replace a pod based on the JSON passed into std
# Force replace, delete and then re-create the resource. Will cause a service outage.
kubectl replace --force -f ./pod.json
# Create a service for a replicated nginx, which serves on port 80 and connects to the containers on port 8000
kubectl expose rc nginx --port=80 --target-port=8000
# Update a single-container pod's image version (tag) to v4
kubectl get pod mypod -o yaml | sed 's/\(image: myimage\):.*$/\1:v4/' | kubectl replace -f -
kubectl label pods my-pod new-label=awesome # Add a Label
kubectl annotate pods my-pod icon-url=http://goo.gl/XXBTWq # Add an annotation
kubectl autoscale deployment foo --min=2 --max=10 # Auto scale a deployment "foo"
具体可参考:https://kubernetes.io/docs/reference/kubectl/cheatsheet/
解题步骤:
- 步骤1:初始化deployment:
sudo kubectl run nginx-app --image=nginx:1.11.9-alpine --generator=run-pod/v1 --dry-run -o yaml > nginx-app.yaml
然后修改yaml文件:
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
run: nginx-app
name: nginx-app
namespace: ns-ehj
spec:
replicas: 3
selector:
matchLabels:
run: nginx-app
template:
metadata:
labels:
run: nginx-app
spec:
containers:
- image: nginx:1.11.9-alpin
name: nginx-app
nginx-app
此时我们来先看下nginx的版本:
运行如下指令进入容器的TTY:
sudo kubectl exec -it nginx-app-5b95d7d76c-6kcjp -n ns-ehj -- /bin/sh
nginx -v
- 步骤2:使用新版本1.12.0-alpine部署应用程序,执行滚动更新并记录更新,我们可以直接运行kubectl set指令:
sudo kubectl set image deployment/nginx-app nginx-app=nginx:1.12.0-alpine -n ns-ehj
此时可以发现,pod name已经全部改变,说明pod重建了。
升级image
我们进入到某个容器下看下nginx的版本:
更新后的nginx版本
- 步骤3:将更新回滚到前一个版本1.11.9-alpine:
sudo kubectl rollout undo deployment/nginx-app -n ns-ehj
我们再来看下nginx的版本:
回滚后的Pod
可以看到nginx回到了以前的版本:
nginx -v
最后可以导出deployment的spec:
sudo kubectl get deployment/nginx-app -n ns-ehj -o=custom-columns=NAME:spec > 7.txt
网友评论