K8S命令的梳理
K8S命令的梳理
kubectl
是一個基礎的K8S叢集管理命令,可以實現對K8S資源的查詢,建立,刪除,更新,回退等各種各樣的操作。由於其複雜的功能體系,命令靈活度又高,因此需要進行常見的一些命令和使用場景的梳理。
1 K8S命令的組成
K8S基礎命令由三段式組成:
kubectl 動作 資源 [選項]
2 K8S命令的基礎動作
常見的K8S命令有8個動作。分別是:
- get:展示一個或者多個資源
- create:通過資源配置檔名或者鍵盤輸入建立資源
- expose:選擇一個RC,Service,Deployment或者Pod,並且暴露為新的K8s服務
- run:在叢集上執行指定映象
- set:在物件上設定指定屬性
- explain:資源的文件
- edit:編輯伺服器上的資源
- delete:通過資源建立的檔名,鍵盤輸入,資源名,或者選擇器標籤等刪除資源
此外,kubectl還提供了一些高階的用法。在日常開發中除非特定的場景,否則一般不會用到。這些命令包括:
-
部署類的命令
- rollout:管理deployment的部署
- rolling-update:實現滾動升級,並最終輸出RC
- scale:為Deployment, ReplicaSet, RC或者Job設定新的大小
- autoscale:自動伸縮Deployment, ReplicaSet, ReplicationController
-
叢集管理類的命令
unschedulable schedulable
-
故障定位和排除類的命令
- describe:顯示指定資源或者資源組的詳情
- logs:列印某個pod中容器的日誌
- attach:附加在一個執行的容器上執行,使用該命令注意不要關閉容器並退出
- exec:在一個容器中執行命令,不影響現在執行的容器中的功能
- port-forward:轉發一個或者多個埠到pod中
- proxy:執行porxy以實現到Kubernetes API server的功能轉發
- cp:與容器之間進行檔案拷貝
-
其他更高階的命令
- 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
-
設定命令
- 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)
-
其他的系統級命令
group/version kubeconfig
2.1 基礎動作 get 的命令
get
是kubectl中最基礎的命令,也是使用最多的命令。
該命令的使用格式如下:
get 資源 [選項]
因此,使用改命令需要首先分清楚,paas上到底有多少中資源型別。那麼,paas上面有多少中資源呢?
2.1.1 get命令中的資源型別
請見下表(需要區分的是,kubectl工具中認為的資源,並不等同於k8s上面可以創建出的與pod相關的資源型別)
序號 | 資源 | 資源縮寫 |
---|---|---|
1 | cloud | cld |
2 | clusters | 只有在聯邦apiservers下才合法 |
3 | componentstatuses | cs |
4 | configmaps | cm |
5 | daemonsets | ds |
6 | deployments | deploy |
7 | endpoints | ep |
8 | events | ev |
9 | horizontalpodautoscalers | hpa |
10 | ingresses | ing |
11 | jobs | - |
12 | limitranges | limits |
13 | namespaces | ns |
14 | networks | net |
15 | networkmappings | nm |
16 | networkpolicies | - |
17 | nodes | no |
18 | persistentvolumeclaims | pvc |
19 | persistentvolumes | pv |
20 | physicalnetworks | pn |
21 | processlifecycles | plc |
22 | pods | po |
23 | podsecuritypolicies | psp |
24 | podtemplates | - |
25 | replicasets | rs |
26 | replicationcontrollers | rc |
27 | resourcequotas | quota |
28 | secrets | - |
29 | serviceaccounts | sa |
30 | services | svc |
31 | statefulsets | - |
32 | storageclasses | - |
33 | thirdpartyresources | - |
34 | clusterversions | - |
2.1.2 幾個常用的get命令組合
使用get
命令需要注意的一點是:該命令會隱藏一些資源資訊,如執行情況等
。要顯示這些資訊,可以通過在命令最後加上--show-all
標記進行實現。
get
的幾個常用命令組合:
- 顯示所有的pod資訊,格式如linux下的ps命令(精簡)
kubectl get pods
-
全面顯示pod 資訊,增加
-o wide
選項
kubectl get pods -o wide
- 檢視單個指定RC名稱的資訊
kubectl get replicationcontroller web
- 使用json格式展示指定的pod資訊
kubectl get -o json pod web-pod-13je7
- 把yaml形式的pod資訊展示位json格式
kubectl get -f pod.yaml -o json
- 檢視指定pod資訊中的某一段
kubectl get -o template pod/web-pod-13je7 --template={{.status.phase}}
- 同時輸出所有的RC和service資源例項列表
kubectl get rc,services
- 通過資源型別和名字獲取具體的例項資訊
kubectl get rc/web service/frontend pods/web-pod-13je7
2.1.3 get中的重要選項
- 跨名稱空間查詢物件
--all-namespaces=false
-
指定配置檔名
使用
-f
或者--filename=[]
-
指定輸出格式
使用
-o
或者--output=''
指定輸出格式,輸出格式必須為下表中的一個
-
json
-
yaml
-
wide
-
name
-
custom-columns=...
,具體資訊可以檢視:http://kubernetes.io/docs/user-guide/kubectl-overview/#custom-columns -
custom-columns-file=...
-
go-template=...
,具體資訊可以檢視http://golang.org/pkg/text/template/#pkg-overview -
go-template-file=...
-
jsonpath=...
,具體資訊可以檢視http://kubernetes.io/docs/user-guide/jsonpath -
jsonpath-file=...
-
顯示所有的輸出資訊
-a
選項可以輸出所有的資訊。