1. 程式人生 > 其它 >|NO.Z.00018|——————————|^^ 標準 ^^|——|KuberNetes&標準.V17|---------------------------------------------------------|常用操作.V03|建立資源|檢視資源|

|NO.Z.00018|——————————|^^ 標準 ^^|——|KuberNetes&標準.V17|---------------------------------------------------------|常用操作.V03|建立資源|檢視資源|



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








一、建立命令
### --- 建立命令
~~~     # 建立資源
[root@k8s-master01 ~]#   kubectl apply -f ./my-manifest.yaml
~~~     # 使用多個檔案建立
[root@k8s-master01 ~]#   kubectl apply -f ./my1.yaml -f ./my2.yaml   
~~~     # 基於目錄下的所有清單檔案建立資源
[root@k8s-master01 ~]#   kubectl apply -f ./dir            
~~~     # 從 URL 中建立資源
[root@k8s-master01 ~]#   kubectl apply -f https://git.io/vPieo      
~~~     # 啟動單例項 nginx
[root@k8s-master01 ~]#   kubectl create deployment nginx --image=nginx 
~~~     # 獲取 pod 清單的文件說明
[root@k8s-master01 ~]#   kubectl explain pods,svc      
~~~     # 從標準輸入建立多個 YAML 物件

[root@k8s-master01 ~]#   cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: busybox-sleep
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - sleep
    - "1000000"
---
apiVersion: v1
kind: Pod
metadata:
  name: busybox-sleep-less
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - sleep
    - "1000"
EOF
~~~     # 建立有多個 key 的 Secret

[root@k8s-master01 ~]#   cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  password: $(echo -n "s33msi4" | base64 -w0)
  username: $(echo -n "jane" | base64 -w0)
EOF

二、檢視和查詢建立的資源
### --- 檢視和查詢建立的資源
~~~     # get 命令的基本輸出

[root@k8s-master01 ~]#   kubectl get services                 // 列出當前名稱空間下的所有 services
[root@k8s-master01 ~]#   kubectl get pods --all-namespaces    // 列出所有名稱空間下的全部的 Pods
[root@k8s-master01 ~]#   kubectl get pods -o wide             // 列出當前名稱空間下的全部 Pods,並顯示更詳細的資訊
[root@k8s-master01 ~]#   kubectl get deployment my-dep        // 列出某個特定的 Deployment
[root@k8s-master01 ~]#   kubectl get pods                     // 列出當前名稱空間下的全部 Pods
[root@k8s-master01 ~]#   kubectl get pod my-pod -o yaml       // 獲取一個 pod 的 YAML
~~~     # describe 命令的詳細輸出

[root@k8s-master01 ~]#  kubectl describe nodes my-node
[root@k8s-master01 ~]#  kubectl describe pods my-pod
~~~     # 列出當前名字空間下所有 Services,按名稱排序
[root@k8s-master01 ~]#  kubectl get services --sort-by=.metadata.name

~~~     # 列出 Pods,按重啟次數排序
[root@k8s-master01 ~]#  kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'
~~~     # 列舉所有 PV 持久卷,按容量排序
[root@k8s-master01 ~]#  kubectl get pv --sort-by=.spec.capacity.storage

~~~     # 獲取包含 app=cassandra 標籤的所有 Pods 的 version 標籤
[root@k8s-master01 ~]#  kubectl get pods --selector=app=cassandra -o \
  jsonpath='{.items[*].metadata.labels.version}'
~~~     # 獲取所有工作節點(使用選擇器以排除標籤名稱為 'node-role.kubernetes.io/master' 的結果)
[root@k8s-master01 ~]#  kubectl get node --selector='!node-role.kubernetes.io/master'

~~~     # 獲取當前名稱空間中正在執行的 Pods
kubectl get pods --field-selector=status.phase=Running
~~~     # 獲取全部節點的 ExternalIP 地址
[root@k8s-master01 ~]#  kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'

~~~     # 列出屬於某個特定 RC 的 Pods 的名稱
~~~     # 在轉換對於 jsonpath 過於複雜的場合,"jq" 命令很有用;
~~~     可以在 https://stedolan.github.io/jq/ 找到它。
[root@k8s-master01 ~]#  sel=${$(kubectl get rc my-rc --output=json | jq -j '.spec.selector | to_entries | .[] | "\(.key)=\(.value),"')%?}
[root@k8s-master01 ~]#  echo $(kubectl get pods --selector=$sel --output=jsonpath={.items..metadata.name})
~~~     # 顯示所有 Pods 的標籤(或任何其他支援標籤的 Kubernetes 物件)
[root@k8s-master01 ~]#  kubectl get pods --show-labels

~~~     # 檢查哪些節點處於就緒狀態
[root@k8s-master01 ~]#  JSONPATH='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}' \
 && kubectl get nodes -o jsonpath="$JSONPATH" | grep "Ready=True"
~~~     # 列出被一個 Pod 使用的全部 Secret
[root@k8s-master01 ~]#  kubectl get pods -o json | jq '.items[].spec.containers[].env[]?.valueFrom.secretKeyRef.name' | grep -v null | sort | uniq

~~~     # 列舉所有 Pods 中初始化容器的容器 ID(containerID)
~~~     Helpful when cleaning up stopped containers, while avoiding removal of initContainers.
[root@k8s-master01 ~]#  kubectl get pods --all-namespaces -o jsonpath='{range .items[*].status.initContainerStatuses[*]}{.containerID}{"\n"}{end}' | cut -d/ -f3
~~~     # 列出事件(Events),按時間戳排序
[root@k8s-master01 ~]#  kubectl get events --sort-by=.metadata.creationTimestamp

~~~     # 比較當前的叢集狀態和假定某清單被應用之後的叢集狀態
[root@k8s-master01 ~]#  kubectl diff -f ./my-manifest.yaml








===============================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)