K8S Deployment 命令

时间:2022-10-30 19:13:19

创建 Deployment

kubectl create -f https://kubernetes.io/docs/user-guide/nginx-deployment.yaml --record
deployment "nginx-deployment" created

将kubectl的 --record 的 flag 设置为 true可以在 annotation 中记录当前命令创建或者升级了该资源。这在未来会很有用,例如,查看在每个 Deployment revision 中执行了哪些命令。

然后立即执行 get 将获得如下结果:

kubectl get deployment  

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

更新Deployment

扩容:

kubectl scale deployment nginx-deployment --replicas 10

如果集群支持 horizontal pod autoscaling 的话,还可以为Deployment设置自动扩展:

kubectl autoscale deployment nginx-deployment --min=10 --max=15 --cpu-percent=80 

更新镜像也比较简单:

kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1

回滚到上一个版本:

kubectl rollout undo deployment/nginx-deployment

也可以使用 --revision参数指定某个历史版本:

 kubectl rollout undo deployment/nginx-deployment --to-revision=2
deployment "nginx-deployment" rolled back

历史记录

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

创建 Deployment 的时候使用了--record参数可以记录命令,我们可以很方便的查看每次 revision 的变化。

查看单个revision 的详细信息:

kubectl rollout history deployment alpine-fbgweb --revision=1

[root@master scripts]# 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