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
```