一、k8s-kubectl命令大全
Kubectl命令行管理對象
類型 命令 描述 基礎命令 create 通過文件名或標准輸入創建資源。 expose 將一個資源公開為一個新的Kubernetes服務。 run 創建並運行一個特定的鏡像,可能是副本。 創建一個deployment或job管理創建的容器。 set 配置應用資源。 修改現有應用程序資源。 get 顯示一個或多個資源。 explain 文檔參考資料。 edit 使用默認的編輯器編輯一個資源。 delete 通過文件名、標准輸入、資源名稱或標簽選擇器來刪除資源。 部署命令 rollout 管理資源的發布。 rolling-update 執行指定復制控制的滾動更新。 scale 擴容或縮容Pod數量,Deployment、ReplicaSet、RC或Job。 autoscale 創建一個自動選擇擴容或縮容並設置Pod數量。 集群管理命令 certificate 修改證書資源。 cluster-info 顯示集群信息。 top 顯示資源(CPU/Memory/Storage)使用。需要Heapster運行。 cordon 標記節點不可調度。 uncordon 標記節點可調度。 drain 維護期間排除節點。 taint
Kubectl命令行管理對象 類型 命令 描述 故障診斷和調試命令 describe 顯示特定資源或資源組的詳細信息。 logs 在pod或指定的資源中容器打印日志。如果pod只有一個容器,容器名稱是可選的。 attach 附加到一個進程到一個已經運行的容器。 exec 執行命令到容器。 port-forward 轉發一個或多個本地端口到一個pod。 proxy 為kubernetes API Server啟動服務代理。 cp 拷貝文件或目錄到容器中。 auth 檢查授權。 高級命令 apply 通過文件名或標准輸入對資源應用配置。 patch 使用補丁修改、更新資源的字段。 replace 通過文件名或標准輸入替換一個資源。 convert 不同的API版本之間轉換配置文件。YAML和JSON格式都接受。 設置命令 label 更新資源上的標簽。 annotate 在一個或多個資源上更新注釋。 completion 用於實現kubectl工具自動補全。 其他命令 api-versions 打印受支持的API版本。 config 修改kubeconfig文件(用於訪問API,比如配置認證信息)。 help 所有命令幫助。 plugin 運行一個命令行插件。 version 打印客戶端和服務版本信息
Kubectl命令行管理對象 示例: # 運行應用程序 kubectl run hello-world --replicas=3 --labels="app=example" --image=nginx:1.10 --port=80 # 顯示有關Deployments信息 kubectl get deployments hello-world kubectl describe deployments hello-world # 顯示有關ReplicaSet信息 kubectl get replicasets kubectl describe replicasets # 創建一個Service對象暴露Deployment(在88端口負載TCP流量) kubectl expose deployment hello-world --port=88 --type=NodePort --target-port=80 --name=example-service # 創建一個Service對象暴露Deployment(在4100端口負載UDP流量) kubectl expose deployment hello-world --port=4100 --type=NodePort --protocol=udp --target-port=80 -- name=example-service # 顯示有關Service信息 kubectl describe services example-service # 使用節點IP和節點端口訪問應用程序 curl http://<public-node-ip>:<node-port>
Kubectl命令行管理對象 示例: # 列出運行應用程序的pod kubectl get pods --selector="app=example" --output=wide # 查看pods所有標簽 kubectl get pods --show-labels # 根據標簽查看pods kubectl get pods -l app=example # 擴容Pod副本數 kubectl scale deployment --replicas=10 hello-world # 清理應用程序 kubectl delete services example-service kubectl delete deployment hello-world