1. 程式人生 > 其它 >|NO.Z.00017|——————————|^^ 標準 ^^|——|KuberNetes&標準.V16|---------------------------------------------------------|常用操作.V02|操作引數|

|NO.Z.00017|——————————|^^ 標準 ^^|——|KuberNetes&標準.V16|---------------------------------------------------------|常用操作.V02|操作引數|



[CloudNative:KuberNetes&書籤.V16]                                                                      [Applications.KuberNetes] [|DevOps|kubernetes|k8s.二進位制1.20|網路規劃|常用操作|]








一、常用操作
### --- 常用操作引數
~~~     # 檢視所有 pod 列表,  -n 後跟 namespace, 檢視指定的名稱空間

[root@k8s-master01 ~]#  kubectl get pod
[root@k8s-master01 ~]#  kubectl get pod -n kube-system      // 檢視指定命令空間的Pod  
[root@k8s-master01 ~]#  kubectl get pod -o wide             // 檢視更詳細的資訊,比如Pod所在的節點
[root@k8s-master01 ~]#  kubectl get pod --show-labels       // 獲取pod並檢視pod的標籤
~~~     # 檢視 RC 和 service 列表, -o wide 檢視詳細資訊

[root@k8s-master01 ~]#  kubectl get rc,svc
[root@k8s-master01 ~]#  kubectl get pod,svc -o wide  
[root@k8s-master01 ~]#  kubectl get pod <pod-name> -o yaml
~~~     # 顯示 Node 的詳細資訊
[root@k8s-master01 ~]#  kubectl describe node 192.168.0.212 #可以跟Node IP或者主機名

~~~     # 顯示 Pod 的詳細資訊, 特別是檢視 pod 無法建立的時候的日誌
[root@k8s-master01 ~]#  kubectl describe pod <pod-name>
~~~     # eg:
[root@k8s-master01 ~]#  kubectl describe pod redis-master-tqds9
~~~     # 根據 yaml 建立資源, apply 可以重複執行,create 不行

[root@k8s-master01 ~]#  kubectl create -f pod.yaml
[root@k8s-master01 ~]#  kubectl apply -f pod.yaml
~~~     # 基於 pod.yaml 定義的名稱刪除指定資源
[root@k8s-master01 ~]#  kubectl delete -f pod.yaml 
 
~~~     # 刪除所有包含某個 label 的pod 和 service
[root@k8s-master01 ~]#  kubectl delete pod,svc -l name=<label-name>
~~~     # 刪除預設名稱空間下的所有 Pod
[root@k8s-master01 ~]#  kubectl delete pod --all
 
~~~     # 執行 pod 命令
[root@k8s-master01 ~]#  kubectl exec <pod-name> -- date
[root@k8s-master01 ~]#  kubectl exec <pod-name> -- bash
[root@k8s-master01 ~]#  kubectl exec <pod-name> -- ping 10.24.51.9
~~~     # 通過bash獲得 pod 中某個容器的TTY,相當於登入容器
[root@k8s-master01 ~]#  kubectl exec -it <pod-name> -c <container-name> -- bash
 
~~~     # eg:
[root@k8s-master01 ~]#  kubectl exec -it redis-master-cln81 -- bash
~~~     # 檢視容器的日誌

[root@k8s-master01 ~]#  kubectl logs <pod-name>
[root@k8s-master01 ~]#  kubectl logs -f <pod-name>                      // 實時檢視日誌
[root@k8s-master01 ~]#  kubectl log  <pod-name>  -c <container_name>    // 若 pod 只有一個容器,可以不加 -c 
[root@k8s-master01 ~]#  kubectl logs -l app=frontend                    // 返回所有標記為 app=frontend 的 pod 的合併日誌。
~~~     # 檢視節點 labels
[root@k8s-master01 ~]#  kubectl get node --show-labels
 
~~~     # 重啟 pod
[root@k8s-master01 ~]#  kubectl get pod <POD名稱> -n <NAMESPACE名稱> -o yaml | kubectl replace --force -f -
二、命令補全
### --- 命令補全
~~~     # 在 bash 中設定當前 shell 的自動補全,要先安裝 bash-completion 包。

[root@k8s-master01 ~]#  source <(kubectl completion bash)                           
[root@k8s-master01 ~]#  echo "source <(kubectl completion bash)" >> ~/.bashrc 








===============================END===============================


Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart                                                                                                                                                    ——W.S.Landor



來自為知筆記(Wiz)