kubectl 命令帮助

# 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:
  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).

另请参见

  • kubectl annotate - 更新资源所关联的注解
  • kubectl api-resources - 打印服务器上所支持的 API 资源
  • kubectl api-versions - 以“组/版本”的格式输出服务端所支持的 API 版本
  • kubectl apply - 基于文件名或标准输入,将新的配置应用到资源上
  • kubectl attach - 连接到一个正在运行的容器
  • kubectl auth - 检查授权信息
  • kubectl autoscale - 对一个资源对象(Deployment、ReplicaSet 或 - ReplicationController )进行扩缩
  • kubectl certificate - 修改证书资源
  • kubectl cluster-info - 显示集群信息
  • kubectl completion - 根据已经给出的 Shell(bash 或 zsh),输出 Shell 补全后的代码
  • kubectl config - 修改 kubeconfig 配置文件
  • kubectl convert - 在不同的 API 版本之间转换配置文件
  • kubectl cordon - 标记节点为不可调度的
  • kubectl cp - 将文件和目录拷入/拷出容器
  • kubectl create - 通过文件或标准输入来创建资源
  • kubectl debug - 创建用于排查工作负载和节点故障的调试会话
  • kubectl delete - 通过文件名、标准输入、资源和名字删除资源,或者通过资源和标签选择器来删除资源
  • kubectl describe - 显示某个资源或某组资源的详细信息
  • kubectl diff - 显示目前版本与将要应用的版本之间的差异
  • kubectl drain - 腾空节点,准备维护
  • kubectl edit - 修改服务器上的某资源
  • kubectl exec - 在容器中执行相关命令
  • kubectl explain - 显示资源文档说明
  • kubectl expose - 给定副本控制器、服务、Deployment 或 Pod,将其暴露为新的 kubernetes Service
  • kubectl get - 显示一个或者多个资源信息
  • kubectl kustomize - 从目录或远程 URL 中构建 kustomization
  • kubectl label - 更新资源的标签
  • kubectl logs - 输出 pod 中某容器的日志
  • kubectl options - 打印所有命令都支持的共有参数列表
  • kubectl patch - 基于策略性合并修补(Stategic Merge Patch)规则更新某资源中的字段
  • kubectl plugin - 运行命令行插件
  • kubectl port-forward - 将一个或者多个本地端口转发到 pod
  • kubectl proxy - 运行一个 kubernetes API 服务器代理
  • kubectl replace - 基于文件名或标准输入替换资源
  • kubectl rollout - 管理资源的上线
  • kubectl run - 在集群中使用指定镜像启动容器
  • kubectl scale - 为一个 Deployment、ReplicaSet 或 - - - - ReplicationController 设置一个新的规模尺寸值
  • kubectl set - 为对象设置功能特性
  • kubectl taint - 在一个或者多个节点上更新污点配置
  • kubectl top - 显示资源(CPU /内存/存储)使用率
  • kubectl uncordon - 标记节点为可调度的
  • kubectl version - 打印客户端和服务器的版本信息
  • kubectl wait - 实验性:等待一个或多个资源达到某种状态

实例

1.kubect create

创建一个资源从一个文件或标准输入

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

2. kubectl run

在集群中运行一个指定的镜像

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

3. kubectl expose

创建Service对象以将应用程序"暴露"于网络中

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

4. 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        # 查看所有的命名空间

5. kubectl clster-info

显示集群信息

kubectl cluster-info            # 查看集群状态信息

6. kubectl describe

描述资源对象

kubectl describe nodes <node-name>  # 显示Node的详细信息
kubectl describe pods/<pod-name>    # 显示Pod的详细信息

7. kubectl scale

pod扩容与缩容

kubectl scale deployment nginx --replicas 5    # 扩容
kubectl scale deployment nginx --replicas 3    # 缩容

8.kubectl api-resources

查看服务器上支持的API资源

# kubectl api-resources
NAME                              SHORTNAMES   APIGROUP                       NAMESPACED   KIND
bindings                                                                      true         Binding
componentstatuses                 cs                                          false        ComponentStatus
configmaps                        cm                                          true         ConfigMap
endpoints                         ep                                          true         Endpoints
events                            ev                                          true         Event
limitranges                       limits                                      true         LimitRange
namespaces                        ns                                          false        Namespace
nodes                             no                                          false        Node
persistentvolumeclaims            pvc                                         true         PersistentVolumeClaim
persistentvolumes                 pv                                          false        PersistentVolume
pods                              po                                          true         Pod
podtemplates                                                                  true         PodTemplate
replicationcontrollers            rc                                          true         ReplicationController
resourcequotas                    quota                                       true         ResourceQuota
secrets                                                                       true         Secret
serviceaccounts                   sa                                          true         ServiceAccount
services                          svc                                         true         Service
mutatingwebhookconfigurations                  admissionregistration.k8s.io   false        MutatingWebhookConfiguration
validatingwebhookconfigurations                admissionregistration.k8s.io   false        ValidatingWebhookConfiguration
customresourcedefinitions         crd,crds     apiextensions.k8s.io           false        CustomResourceDefinition
apiservices                                    apiregistration.k8s.io         false        APIService
controllerrevisions                            apps                           true         ControllerRevision
daemonsets                        ds           apps                           true         DaemonSet
deployments                       deploy       apps                           true         Deployment
replicasets                       rs           apps                           true         ReplicaSet
statefulsets                      sts          apps                           true         StatefulSet
tokenreviews                                   authentication.k8s.io          false        TokenReview
localsubjectaccessreviews                      authorization.k8s.io           true         LocalSubjectAccessReview
selfsubjectaccessreviews                       authorization.k8s.io           false        SelfSubjectAccessReview
selfsubjectrulesreviews                        authorization.k8s.io           false        SelfSubjectRulesReview
subjectaccessreviews                           authorization.k8s.io           false        SubjectAccessReview
horizontalpodautoscalers          hpa          autoscaling                    true         HorizontalPodAutoscaler
cronjobs                          cj           batch                          true         CronJob
jobs                                           batch                          true         Job
certificatesigningrequests        csr          certificates.k8s.io            false        CertificateSigningRequest
leases                                         coordination.k8s.io            true         Lease
events                            ev           events.k8s.io                  true         Event
daemonsets                        ds           extensions                     true         DaemonSet
deployments                       deploy       extensions                     true         Deployment
ingresses                         ing          extensions                     true         Ingress
networkpolicies                   netpol       extensions                     true         NetworkPolicy
podsecuritypolicies               psp          extensions                     false        PodSecurityPolicy
replicasets                       rs           extensions                     true         ReplicaSet
ingresses                         ing          networking.k8s.io              true         Ingress
networkpolicies                   netpol       networking.k8s.io              true         NetworkPolicy
runtimeclasses                                 node.k8s.io                    false        RuntimeClass
poddisruptionbudgets              pdb          policy                         true         PodDisruptionBudget
podsecuritypolicies               psp          policy                         false        PodSecurityPolicy
clusterrolebindings                            rbac.authorization.k8s.io      false        ClusterRoleBinding
clusterroles                                   rbac.authorization.k8s.io      false        ClusterRole
rolebindings                                   rbac.authorization.k8s.io      true         RoleBinding
roles                                          rbac.authorization.k8s.io      true         Role
priorityclasses                   pc           scheduling.k8s.io              false        PriorityClass
csidrivers                                     storage.k8s.io                 false        CSIDriver
csinodes                                       storage.k8s.io                 false        CSINode
storageclasses                    sc           storage.k8s.io                 false        StorageClass
volumeattachments                              storage.k8s.io                 false        VolumeAttachment

9.生成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