1. 程式人生 > >K8S Deployment 命令

K8S Deployment 命令

dep record tor 版本 nts master ted guide ltm

創建 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 

K8S Deployment 命令