1. 程式人生 > 實用技巧 >kubectl 常用命令總結

kubectl 常用命令總結

1. help 幫助資訊

# kubectl --help
kubectl controls the Kubernetes cluster manager.
 
Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/
 
Basic Commands (Beginner):
  create         Create a resource from a file or from stdin.
  expose         使用 replication controller, service, deployment 或者 pod 並暴露它作為一個新的Kubernetes Service
  run            在叢集中執行一個指定的映象
  set            為 objects 設定一個指定的特徵
 
Basic Commands (Intermediate):
  explain        檢視資源的文件
  get            顯示一個或更多 resources
  edit           在伺服器上編輯一個資源
  delete         Delete resources by filenames, stdin, resources and names, or by resources and label selector
 
Deploy Commands:
  rollout        Manage the rollout of a resource
  scale          為 Deployment, ReplicaSet, Replication Controller 或者 Job 設定一個新的副本數量
  autoscale      自動調整一個 Deployment, ReplicaSet, 或者 ReplicationController 的副本數量
 
Cluster Management Commands:
  certificate    修改 certificate 資源.
  cluster-info   顯示叢集資訊
  top            Display Resource (CPU/Memory/Storage) usage.
  cordon         標記 node 為 unschedulable
  uncordon       標記 node 為 schedulable
  drain          Drain node in preparation for maintenance
  taint          更新一個或者多個 node 上的 taints
 
Troubleshooting and Debugging Commands:
  describe       顯示一個指定 resource 或者 group 的 resources 詳情
  logs           輸出容器在 pod 中的日誌
  attach         Attach 到一個執行中的 container
  exec           在一個 container 中執行一個命令
  port-forward   Forward one or more local ports to a pod
  proxy          執行一個 proxy 到 Kubernetes API server
  cp             複製 files 和 directories 到 containers 和從容器中複製 files 和 directories.
  auth           Inspect authorization
 
Advanced Commands:
  diff           Diff live version against would-be applied version
  apply          通過檔名或標準輸入流(stdin)對資源進行配置
  patch          使用 strategic merge patch 更新一個資源的 field(s)
  replace        通過 filename 或者 stdin替換一個資源
  wait           Experimental: Wait for a specific condition on one or many resources.
  convert        在不同的 API versions 轉換配置檔案
 
Settings Commands:
  label          更新在這個資源上的 labels
  annotate       更新一個資源的註解
  completion     Output shell completion code for the specified shell (bash or zsh)
 
Other Commands:
  api-resources  Print the supported API resources on the server
  api-versions   Print the supported API versions on the server, in the form of "group/version"
  config         修改 kubeconfig 檔案
  plugin         Provides utilities for interacting with plugins.
  version        輸出 client 和 server 的版本資訊
 
Usage:
  kubectl [flags] [options]
 
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands). 

2. kubect create 建立一個資源從一個檔案或標準輸入

kubectl create deployment nginx --image=nginx:1.14 
kubectl create -f my-nginx.yaml

3. kubectl run 在叢集中執行一個指定的映象

kubectl run nginx --image=nginx:1.16 --port=80 --replicas=1

4. kubectl expose 建立Service物件以將應用程式"暴露"於網路中

kubectl expose deployment/nginx  --type="NodePort" --port=80 --name=nginx

5.kubectl get 顯示一個或更多resources資源

kubectl get cs                          # 檢視叢集狀態
kubectl get nodes                       # 檢視叢集節點資訊
kubectl get ns                          # 檢視叢集名稱空間
kubectl get svc -n kube-system          # 檢視指定名稱空間的服務
kubectl get pod <pod-name> -o wide      # 檢視Pod詳細資訊
kubectl get pod <pod-name> -o yaml      # 以yaml格式檢視Pod詳細資訊
kubectl get pods                        # 檢視資源物件,檢視所有Pod列表
kubectl get rc,service                  # 檢視資源物件,檢視rc和service列表
kubectl get pod,svc,ep --show-labels    # 檢視pod,svc,ep能及標籤資訊
kubectl get all --all-namespaces        # 檢視所有的名稱空間

6.kubectl clster-info 顯示叢集資訊

kubectl cluster-info            # 檢視叢集狀態資訊

7. kubectl describe 描述資源物件

kubectl describe nodes <node-name>  # 顯示Node的詳細資訊
kubectl describe pods/<pod-name>    # 顯示Pod的詳細資訊

8.kubectl scale pod擴容與縮容

kubectl scale deployment nginx --replicas 5    # 擴容
kubectl scale deployment nginx --replicas 3    # 縮容

9.檢視伺服器上支援的API資源

# kubectl api-resources
NAME                              SHORTNAMES   APIGROUP                       NAMESPACED   KIND
bindings                                                                      true         Binding
componentstatuses                 cs                                          false        ComponentStatus
configmaps                        cm                                          true         ConfigMap
endpoints                         ep                                          true         Endpoints
......
```

### 10.生成yaml資源配置檔案方法

```
# 用run命令生成yaml檔案
kubectl create deployment nginx --image=nginx:1.14 -o yaml --dry-run > my.deploy.yaml
 
# 用get命令匯出yaml檔案
kubectl get deploy nginx-deployment -o yaml --export > my.deploy.yaml
 
# Pod容器的欄位拼寫忘記了
kubectl explain pods.spec.containers
```