kubernetes-kubectl命令說明

本文基於kubernetes 1.5.2版本編寫

kubectl

kubectl controls the K8S cluster manager. 

Find more information at https://github.com/K8S/K8S.

Basic Commands (Beginner):
 create Create a resource by filename or stdin
 expose Take a replication controller, service, deployment or pod and expose it as a new K8S Service
 run Run a particular image on the cluster
 set Set specific features on objects

Basic Commands (Intermediate):
 get Display one or many resources
 explain Documentation of resources
 edit Edit a resource on the server delete Delete resources by filenames, stdin, resources and names, or by resources and label selector

Deploy Commands:
 rollout Manage a deployment rollout
 rolling-update Perform a rolling update of the given ReplicationController
 scale Set a new size for a Deployment, ReplicaSet, Replication Controller, or Job
 autoscale Auto-scale a Deployment, ReplicaSet, or ReplicationController

Cluster Management Commands:
 certificate Modify certificate resources.
 cluster-info Display cluster info
 top Display Resource (CPU/Memory/Storage) usage
 cordon Mark node as unschedulable
 uncordon Mark node as schedulable
 drain Drain node in preparation for maintenance
 taint Update the taints on one or more nodes

Troubleshooting and Debugging Commands:
 describe Show details of a specific resource or group of resources
 logs Print the logs for a container in a pod
 attach Attach to a running container
 exec Execute a command in a container
 port-forward Forward one or more local ports to a pod
 proxy Run a proxy to the K8S API server
 cp Copy files and directories to and from containers.

Advanced Commands:
 apply Apply a configuration to a resource by filename or stdin
 patch Update field(s) of a resource using strategic merge patch replace Replace a resource by filename or stdin
 convert Convert config files between different API versions Settings Commands:
 label Update the labels on a resource
 annotate Update the annotations on a resource
 completion Output shell completion code for the given shell (bash or zsh)

Other Commands:
 api-versions Print the supported API versions on the server, in the form of "group/version"
 config Modify kubeconfig files
 help Help about any command
 version Print the client and server version information

基本命令

get

get命令用於獲取集羣的一個或一些resource信息。

參數-o格式,輸出對應的格式,yaml、json等

create

用於根據文件或輸入創建資源對象。如果已經定義了相應資源對象的yaml或json文件,直接kubectl create -f filename即可創建文件內定義的資源對象。也可以直接只用子命令[namespace/secret/configmap/serviceaccount]等直接創建相應的資源對象。從追蹤和維護的角度出發,建議使用json或yaml的方式定義資源。

expose

主要是做NAT的

run

類似於docker的run命令,直接運行一個image。

kubectl run -it --image=web:apache run lykops /bin/bash 

也可以運行計劃任務

Start the cron job to compute to 2000 places and print it out every 5 minutes.
kubectl run pi --schedule="0/5 * * * *" --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)' 

edit

edit提供了另一種更新資源對象的操作。例如,使用edit直接更新前面創建的pod的命令爲:

kubectl edit pod rc-nginx-btv4j 

上面命令的效果等效於:

kubectl get pod rc-nginx-btv4j -o yaml >> /tmp/nginx-tmp.yaml 
vim /tmp/nginx-tmp.yaml 
……
kubectl replace -f /tmp/nginx-tmp.yaml 

delete

根據資源對象名或label刪除資源對象。

服務部署

rollout

deployment回退到指定版本

kubectl rollout undo deployment/lykops --to-revision=2 

rolling-update

只能適用於RC

使用配置文件升級

kubectl rolling-update lykops-rc-v1 -f lykops-rc.yaml --update-period=10s 

直接使用images

rolling-update lykops-rc --image=webapache:v3 

scale

擴容Pod的副本數目到10

kubectl scale relicationcontroller lykops --replicas=10 

縮容Pod的副本數目到1

kubectl scale relicationcontroller lykops --replicas=1 

autoscale

scale雖然能夠很方便的對副本數進行擴展或縮小,但是仍然需要人工介入,不能實時自動的根據系統負載對副本數進行擴、縮。autoscale命令提供了自動根據pod負載對其副本進行擴縮的功能。

autoscale命令會給一個rc指定一個副本數的範圍(同HPA,http://blog.csdn.net/liyingke112/article/details/77101673),在實際運行中根據pod中運行的程序的負載自動在指定的範圍內對pod進行擴容或縮容。如前面創建的nginx,可以用如下命令指定副本範圍在1~4

kubectl autoscale rc lykops-rc --min=1 --max=4  

集羣管理

certificate

修改集羣認證信息

cluster-info

顯示集羣api信息,包括URL

top

顯示系統資源(CPU/Memory/Storage)使用情況

cordon、uncordon、drain

cordon,啓用後,該節點處於非活動節點,新建的資源對象不會分配到該機上。

uncordon,將非活動節點轉爲活動節點

drain,讓節點處於維修狀態

這三個命令是正式release的1.2新加入的命令,三個命令一起介紹,是因爲三個命令配合使用可以實現節點的維護。

在1.2之前,因爲沒有相應的命令支持,如果要維護一個節點,只能stop該節點上的kubelet將該節點退出集羣,是集羣不在將新的pod調度到該節點上。如果該節點上本生就沒有pod在運行,則不會對業務有任何影響。如果該節點上有pod正在運行,kubelet停止後,master會發現該節點不可達,而將該節點標記爲notReady狀態,不會將新的節點調度到該節點上。同時,會在其他節點上創建新的pod替換該節點上的pod。這種方式雖然能夠保證集羣的健壯性,但是讓然有些暴力,如果業務只有一個副本,而且該副本正好運行在被維護節點上的話,可能仍然會造成業務的短暫中斷。

1.2中新加入的這3個命令可以保證維護節點時,平滑的將被維護節點上的業務遷移到其他節點上,保證業務不受影響。

taint

調試和bug處理

describe

describe類似於get,同樣用於獲取resource的相關信息。不同的是,get獲得的是更詳細的resource個性的詳細信息,describe獲得的是resource集羣相關的信息。describe命令同get類似,但是describe不支持-o選項,對於同一類型resource,describe輸出的信息格式,內容域相同。

logs

logs命令用於顯示pod運行中,容器內程序輸出到標準輸出的內容。跟docker的logs命令類似。如果要獲得tail -f 的方式,也可以使用-f選項。

attach

attach命令類似於docker的attach命令,可以直接查看容器中以daemon形式運行的進程的輸出,效果類似於logs -f,退出查看使用ctrl-c。如果一個pod中有多個容器,要查看具體的某個容器的的輸出,需要在pod名後使用-c containers name指定運行的容器。如下示例的命令爲查看kube-system namespace中的kube-dns-v9-rcfuk pod中的skydns容器的輸出。 kubectl attach kube-dns-v9-rcfuk -c skydns

exec

exec命令同樣類似於docker的exec命令,爲在一個已經運行的容器中執行一條shell命令,如果一個pod容器中,有多個容器,需要使用-c選項指定容器。

port-forward

轉發一個本地端口到容器端口,一般都是使用yaml的方式編排容器,所以基本不使用此命令。

proxy

cp

拷貝文件到pod的容器中

高級命令

apply

apply命令提供了比patch,edit等更嚴格的更新resource的方式。通過apply,用戶可以將resource的configuration使用source control的方式維護在版本庫中。每次有更新時,將配置文件push到server,然後使用kubectl apply將更新應用到resource。K8S會在引用更新前將當前配置文件中的配置同已經應用的配置做比較,並只更新更改的部分,而不會主動更改任何用戶未指定的部分。

apply命令的使用方式同replace相同,不同的是,apply不會刪除原有resource,然後創建新的。apply直接在原有resource的基礎上進行更新。同時kubectl apply還會resource中添加一條註釋,標記當前的apply。類似於git操作。

patch

如果一個容器已經在運行,這時需要對一些容器屬性進行修改,又不想刪除容器,或不方便通過replace的方式進行更新。K8S還提供了一種在容器運行時,直接對容器進行修改的方式,就是patch命令。類似打補丁。 如前面創建pod的label是app=lykops-1,如果在運行過程中,需要把其label改爲app=lykops-2,這patch命令如下: kubectl patch pod lykops-1-kpiqt -p '{"metadata":{"labels":{"app":"lykops-2"}}}'

replace

replace命令用於對已有資源進行更新、替換。如前面create中創建的lykops,當需要更新resource的一些屬性時,如果修改副本數量,增加、修改label,更改image版本,修改端口等。都可以直接修改原yaml文件,然後執行replace命令。

注:名字不能被更更新。另外,如果是更新label,原有標籤的pod將會與更新label後的rc斷開聯繫,有新label的rc將會創建指定副本數的新的pod,但是默認並不會刪除原來的pod。所以此時如果使用get pod將會發現pod數翻倍,進一步check會發現原來的pod已經不會被新rc控制。

convert

Convert config files between different API versions

配置

label

爲K8S集羣的resource打標籤,如前面實例中提到的爲rc打標籤對rc分組。還可以對nodes打標籤,這樣在編排容器時,可以爲容器指定nodeSelector將容器調度到指定lable的機器上,如如果集羣中有IO密集型,計算密集型的機器分組,可以將不同的機器打上不同標籤,然後將不同特徵的容器調度到不同分組上。

在1.2之前的版本中,使用kubectl get nodes則可以列出所有節點的信息,包括節點標籤,1.2版本中不再列出節點的標籤信息,如果需要查看節點被打了哪些標籤,需要使用describe查看節點的信息。

annotate

Update the annotations on a resource

completion

Output shell completion code for the given shell (bash or zsh)

其他

api-versions

Print the supported API versions on the server, in the form of "group/version" 

config

修改K8S配置文件
本文轉自開源中國-kubernetes-kubectl命令說明
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章