天天看點

kubernetes-kubectl指令說明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指令說明

繼續閱讀