• 1. kubectl命令介绍
  • 2. 操作的常用资源对象
  • 3. kubectl命令分类[command]
    • 3.1 增
    • 3.2 删
    • 3.3 改
    • 3.4 查
  • 4. Pod相关命令
    • 4.1 查询Pod
    • 4.2 进入Pod
    • 4.3 删除Pod
    • 4.4 日志查看
  • 5. Node隔离与恢复
  • 6. kubectl label

    本文个人博客地址:http://www.huweihuang.com/article/kubernetes/kubernetes-commands/

    1. kubectl命令介绍

    kubectl的命令语法

    1. kubectl [command] [TYPE] [NAME] [flags]

    其中command,TYPE,NAME,和flags分别是:

    • command: 指定要在一个或多个资源进行操作,例如creategetdescribedelete

    • TYPE:指定资源类型。资源类型区分大小写,您可以指定单数,复数或缩写形式。例如,以下命令产生相同的输出:

      1. kubectl get pod pod1
      2. kubectl get pods pod1
      3. kubectl get po pod1
    • NAME:指定资源的名称。名称区分大小写。如果省略名称,则会显示所有资源的详细信息,比如$ kubectl get pods

      按类型和名称指定多种资源:

      1. * 要分组资源,如果它们都是相同的类型:`TYPE1 name1 name2 name<#>`.<br/>
      2. 例: `$ kubectl get pod example-pod1 example-pod2`
      3. * 要分别指定多种资源类型: `TYPE1/name1 TYPE1/name2 TYPE2/name3 TYPE<#>/name<#>`.<br/>
      4. 例: `$ kubectl get pod/example-pod1 replicationcontroller/example-rc1`
    • flags:指定可选标志。例如,您可以使用-s--serverflags来指定Kubernetes API服务器的地址和端口。

    更多命令介绍:

    1. [root@node5 ~]# kubectl
    2. kubectl controls the Kubernetes cluster manager.
    3. Find more information at https://github.com/kubernetes/kubernetes.
    4. Basic Commands (Beginner):
    5. create Create a resource from a file or from stdin.
    6. expose Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service
    7. run Run a particular image on the cluster
    8. set Set specific features on objects
    9. run-container Run a particular image on the cluster. This command is deprecated, use "run" instead
    10. Basic Commands (Intermediate):
    11. get Display one or many resources
    12. explain Documentation of resources
    13. edit Edit a resource on the server
    14. delete Delete resources by filenames, stdin, resources and names, or by resources and label selector
    15. Deploy Commands:
    16. rollout Manage the rollout of a resource
    17. rolling-update Perform a rolling update of the given ReplicationController
    18. scale Set a new size for a Deployment, ReplicaSet, Replication Controller, or Job
    19. autoscale Auto-scale a Deployment, ReplicaSet, or ReplicationController
    20. Cluster Management Commands:
    21. certificate Modify certificate resources.
    22. cluster-info Display cluster info
    23. top Display Resource (CPU/Memory/Storage) usage.
    24. cordon Mark node as unschedulable
    25. uncordon Mark node as schedulable
    26. drain Drain node in preparation for maintenance
    27. taint Update the taints on one or more nodes
    28. Troubleshooting and Debugging Commands:
    29. describe Show details of a specific resource or group of resources
    30. logs Print the logs for a container in a pod
    31. attach Attach to a running container
    32. exec Execute a command in a container
    33. port-forward Forward one or more local ports to a pod
    34. proxy Run a proxy to the Kubernetes API server
    35. cp Copy files and directories to and from containers.
    36. auth Inspect authorization
    37. Advanced Commands:
    38. apply Apply a configuration to a resource by filename or stdin
    39. patch Update field(s) of a resource using strategic merge patch
    40. replace Replace a resource by filename or stdin
    41. convert Convert config files between different API versions
    42. Settings Commands:
    43. label Update the labels on a resource
    44. annotate Update the annotations on a resource
    45. completion Output shell completion code for the specified shell (bash or zsh)
    46. Other Commands:
    47. api-versions Print the supported API versions on the server, in the form of "group/version"
    48. config Modify kubeconfig files
    49. help Help about any command
    50. plugin Runs a command-line plugin
    51. version Print the client and server version information
    52. Use "kubectl <command> --help" for more information about a given command.
    53. Use "kubectl options" for a list of global command-line options (applies to all commands).

    2. 操作的常用资源对象

    1. Node
    2. Podes
    3. Replication Controllers
    4. Services
    5. Namespace
    6. Deployment
    7. StatefulSet

    具体对象类型及缩写:

    1. * all
    2. * certificatesigningrequests (aka 'csr')
    3. * clusterrolebindings
    4. * clusterroles
    5. * componentstatuses (aka 'cs')
    6. * configmaps (aka 'cm')
    7. * controllerrevisions
    8. * cronjobs
    9. * customresourcedefinition (aka 'crd')
    10. * daemonsets (aka 'ds')
    11. * deployments (aka 'deploy')
    12. * endpoints (aka 'ep')
    13. * events (aka 'ev')
    14. * horizontalpodautoscalers (aka 'hpa')
    15. * ingresses (aka 'ing')
    16. * jobs
    17. * limitranges (aka 'limits')
    18. * namespaces (aka 'ns')
    19. * networkpolicies (aka 'netpol')
    20. * nodes (aka 'no')
    21. * persistentvolumeclaims (aka 'pvc')
    22. * persistentvolumes (aka 'pv')
    23. * poddisruptionbudgets (aka 'pdb')
    24. * podpreset
    25. * pods (aka 'po')
    26. * podsecuritypolicies (aka 'psp')
    27. * podtemplates
    28. * replicasets (aka 'rs')
    29. * replicationcontrollers (aka 'rc')
    30. * resourcequotas (aka 'quota')
    31. * rolebindings
    32. * roles
    33. * secrets
    34. * serviceaccounts (aka 'sa')
    35. * services (aka 'svc')
    36. * statefulsets (aka 'sts')
    37. * storageclasses (aka 'sc')

    3. kubectl命令分类[command]

    3.1 增

    1)create:[Create a resource by filename or stdin]

    2)run:[ Run a particular image on the cluster]

    3)apply:[Apply a configuration to a resource by filename or stdin]

    4)proxy:[Run a proxy to the Kubernetes API server ]

    3.2 删

    1)delete:[Delete resources ]

    3.3 改

    1)scale:[Set a new size for a Replication Controller]

    2)exec:[Execute a command in a container]

    3)attach:[Attach to a running container]

    4)patch:[Update field(s) of a resource by stdin]

    5)edit:[Edit a resource on the server]

    6) label:[Update the labels on a resource]

    7)annotate:[Auto-scale a replication controller]

    8)replace:[Replace a resource by filename or stdin]

    9)config:[config modifies kubeconfig files]

    3.4 查

    1)get:[Display one or many resources]

    2)describe:[Show details of a specific resource or group of resources]

    3)log:[Print the logs for a container in a pod]

    4)cluster-info:[Display cluster info]

    5) version:[Print the client and server version information]

    6)api-versions:[Print the supported API versions]

    4. Pod相关命令

    4.1 查询Pod

    1. kubectl get pod -o wide --namespace=<NAMESPACE>

    4.2 进入Pod

    1. kubectl exec -it <PodName> /bin/bash --namespace=<NAMESPACE>
    2. # 进入Pod中指定容器
    3. kubectl exec -it <PodName> -c <ContainerName> /bin/bash --namespace=<NAMESPACE>

    4.3 删除Pod

    1. kubectl delete pod <PodName> --namespace=<NAMESPACE>
    2. # 强制删除Pod,当Pod一直处于Terminating状态
    3. kubectl delete pod <PodName> --namespace=<NAMESPACE> --force --grace-period=0

    4.4 日志查看

    1. $ 查看运行容器日志
    2. kubectl logs <PodName> --namespace=<NAMESPACE>
    3. $ 查看上一个挂掉的容器日志
    4. kubectl logs <PodName> -p --namespace=<NAMESPACE>

    5. Node隔离与恢复

    说明:Node设置隔离之后,原先运行在该Node上的Pod不受影响,后续的Pod不会调度到被隔离的Node上。

    1. Node隔离

    1. # cordon命令
    2. kubectl cordon <NodeName>
    3. # 或者
    4. kubectl patch node <NodeName> -p '{"spec":{"unschedulable":true}}'

    2. Node恢复

    1. # uncordon
    2. kubectl uncordon <NodeName>
    3. # 或者
    4. kubectl patch node <NodeName> -p '{"spec":{"unschedulable":false}}'

    6. kubectl label

    1. 固定Pod到指定机器

    1. kubectl label node <NodeName> namespace/<NAMESPACE>=true

    2. 取消Pod固定机器

    1. kubectl label node <NodeName> namespace/<NAMESPACE>-

    参考文章:

    • https://kubernetes.io/docs/reference/kubectl/overview/
    • https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/