[root@k8s-master ~]# 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 fileor 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 Set a new size for a Deployment, ReplicaSet or Replication Controller autoscale 自动调整一个 Deployment, ReplicaSet, 或者 ReplicationController 的副本数量
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 转换配置文件 kustomize Build a kustomization target from a directory or a remote url.
Settings Commands: label 更新在这个资源上的 labels annotate 更新一个资源的注解 completion Output shell completion code for the specified shell (bash or zsh)
Other Commands: alpha Commands for features in alpha 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).
[root@k8s-master ~]# kubectl create namespace dev namespace/dev created
示例:获取namespace
1 2 3 4 5 6 7
[root@k8s-master ~]# kubectl get namespace NAME STATUS AGE default Active 22h dev Active 2m40s kube-node-lease Active 22h kube-public Active 22h kube-system Active 22h
示例:获取namespace(简写)
1 2 3 4 5 6 7
[root@k8s-master ~]# kubectl get ns NAME STATUS AGE default Active 22h dev Active 3m47s kube-node-lease Active 22h kube-public Active 22h kube-system Active 22h
示例:在刚才创建的namespace(dev)下创建并运行一个Nginx的Pod
1 2
[root@k8s-master ~]# kubectl run nginx --image=nginx:1.17.1 -n dev pod/nginx created
[root@k8s-master ~]# kubectl describe pod nginxpod -n dev Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 11m default-scheduler Successfully assigned dev/nginxpod to k8s-node1 Normal Pulled 11m kubelet, k8s-node1 Container image "nginx:1.17.1" already present on machine Normal Killing 116s kubelet, k8s-node1 Container nginx-containers definition changed, will be restarted Normal Pulling 115s kubelet, k8s-node1 Pulling image "nginx:1.17.2" Normal Pulled 83s kubelet, k8s-node1 Successfully pulled image "nginx:1.17.2" Normal Created 82s (x2 over 11m) kubelet, k8s-node1 Created container nginx-containers Normal Started 82s (x2 over 11m) kubelet, k8s-node1 Started container nginx-containers
[root@k8s-master ~]# kubectl create deployment nginx --image=nginx:1.17.1 -n dev deployment.apps/nginx created
使用kubectl get命令导出yaml文件:
1 2
[root@k8s-master ~]# kubectl get deployment nginx -n dev -o yaml --export > test2.yaml Flag --export has been deprecated, This flag isdeprecated and will be removed in future.