Kubernetes之kubectl经常使用命令

最近项目有用到Kubernetes做集群配置,因此学习下相关命令,记录下以备下次使用...java

kubectl help 显示具体的用法node

kubectl controls the Kubernetes cluster manager.

Find more information at https://github.com/kubernetes/kubernetes.

Usage:
  kubectl [flags]
  kubectl [command]

Available Commands:
  get            Display one or many resources
  describe       Show details of a specific resource or group of resources
  create         Create a resource by filename or stdin
  replace        Replace a resource by filename or stdin.
  patch          Update field(s) of a resource using strategic merge patch.
  delete         Delete resources by filenames, stdin, resources and names, or by resources and label selector.
  edit           Edit a resource on the server
  apply          Apply a configuration to a resource by filename or stdin
  namespace      SUPERSEDED: Set and view the current Kubernetes namespace
  logs           Print the logs for a container in a pod.
  rolling-update Perform a rolling update of the given ReplicationController.
  scale          Set a new size for a Replication Controller, Job, or Deployment.
  cordon         Mark node as unschedulable
  drain          Drain node in preparation for maintenance
  uncordon       Mark node as schedulable
  attach         Attach to a running container.
  exec           Execute a command in a container.
  port-forward   Forward one or more local ports to a pod.
  proxy          Run a proxy to the Kubernetes API server
  run            Run a particular image on the cluster.
  expose         Take a replication controller, service or pod and expose it as a new Kubernetes Service
  autoscale      Auto-scale a deployment or replication controller
  rollout        rollout manages a deployment
  label          Update the labels on a resource
  annotate       Update the annotations on a resource
  config         config modifies kubeconfig files
  cluster-info   Display cluster info
  api-versions   Print the supported API versions on the server, in the form of "group/version".
  version        Print the client and server version information.
  explain        Documentation of resources.
  convert        Convert config files between different API versions

Flags:
      --alsologtostderr[=false]: log to standard error as well as files
      --certificate-authority="": Path to a cert. file for the certificate authority.
      --client-certificate="": Path to a client certificate file for TLS.
      --client-key="": Path to a client key file for TLS.
      --cluster="": The name of the kubeconfig cluster to use
      --context="": The name of the kubeconfig context to use
      --insecure-skip-tls-verify[=false]: If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure.
      --kubeconfig="": Path to the kubeconfig file to use for CLI requests.
      --log-backtrace-at=:0: when logging hits line file:N, emit a stack trace
      --log-dir="": If non-empty, write log files in this directory
      --log-flush-frequency=5s: Maximum number of seconds between log flushes
      --logtostderr[=true]: log to standard error instead of files
      --match-server-version[=false]: Require server version to match client version
      --namespace="": If present, the namespace scope for this CLI request.
      --password="": Password for basic authentication to the API server.
  -s, --server="": The address and port of the Kubernetes API server
      --stderrthreshold=2: logs at or above this threshold go to stderr
      --token="": Bearer token for authentication to the API server.
      --user="": The name of the kubeconfig user to use
      --username="": Username for basic authentication to the API server.
      --v=0: log level for V logs
      --vmodule=: comma-separated list of pattern=N settings for file-filtered logging

Use "kubectl [command] --help" for more information about a command.

get命令获取全部资源的详细信息nginx

kubectl get 会显示具体的信息git

kubectl get po -o wide 获取pod运行在哪一个节点上的信息github

 

describe相似于get,一样用于获取resource的相关信息。不一样的是,get得到的是更详细的resource个性的详细信息,describe得到的是resource集群相关的信息。describe命令同get相似,可是describe不支持-o选项,对于同一类型resource,describe输出的信息格式,内容域相同。 web

kubectl describe pod pod名docker

create

ubectl命令用于根据文件或输入建立集群resource。若是已经定义了相应resource的yaml或son文件,直接kubectl create -f filename便可建立文件内定义的resource。也能够直接只用子命令[namespace/secret/configmap/serviceaccount]等直接建立相应的resource。从追踪和维护的角度出发,建议使用json或yaml的方式定义资源。 
     如,前面get中获取的两个nginx pod的replication controller文件内容以下。文件名为:rc-nginx.yaml 
shell

apiVersion: v1
kind: ReplicationController
metadata:
  name: siweb
  labels:
    app: si
    tier: web
spec:
  template:
    metadata:
      labels:
        app: si
        tier: web
    spec:
      nodeName: 10.18.97.152
      containers:
      - imageSiweb
        name: siweb-war
        lifecycle:
          postStart:
            exec:
              command:
                - "cp"
                - "/siweb.war"
                - "/app"
        volumeMounts:
        - mountPath: /app
          name: webapps-volume
      - image: 192.168.1.1/ismp/tomcat
        name: siweb
        volumeMounts:
        - mountPath: /usr/local/tomcat/webapps
          name: webapps-volume
        ports:
        - containerPort: 8080
          hostPort: 8003
      volumes:
      - name: webapps-volume
        emptyDir: {}

直接使用create则能够基于rc-nginx.yaml文件建立出ReplicationController(rc),rc会建立两个副本:json

kubectl create -f rc-nginx.yaml  

建立后,使用“kubectl get rc”能够看到一个名为rc-nginx-2的ReplicationController将被建立,同时“kubectl get po”的结果中会多出两个前缀为“rc-nginx-2-”的podvim

replace

replace命令用于对已有资源进行更新、替换。如前面create中建立的nginx,当咱们须要更新resource的一些属性的时候,若是修改副本数量,增长、修改label,更改image版本,修改端口等。均可以直接修改原yaml文件,而后执行replace命令。 
     注:名字不能被更更新。另外,若是是更新label,原有标签的pod将会与更新label后的rc断开联系,有新label的rc将会建立指定副本数的新的pod,可是默认并不会删除原来的pod。因此此时若是使用get po将会发现pod数翻倍,进一步check会发现原来的pod已经不会被新rc控制.
ubectl replace -f rc-nginx.yaml

  

 patch

若是一个容器已经在运行,这时须要对一些容器属性进行修改,又不想删除容器,或不方便经过replace的方式进行更新。kubernetes还提供了一种在容器运行时,直接对容器进行修改的方式,就是patch命令。 
     如前面建立pod的label是app=nginx-2,若是在运行过程当中,须要把其label改成app=nginx-3,这patch命令以下: 
kubectl patch pod rc-nginx-2-kpiqt -p '{"metadata":{"labels":{"app":"nginx-3"}}}'  

  

7. edit

     edit提供了另外一种更新resource源的操做,经过edit可以灵活的在一个common的resource基础上,发展出更过的significant resource。例如,使用edit直接更新前面建立的pod的命令为: 
kubectl edit po rc-nginx-btv4j 

  

上面命令的效果等效于: 
kubectl get po rc-nginx-btv4j -o yaml >> /tmp/nginx-tmp.yaml   
vim /tmp/nginx-tmp.yaml   
/*do some changes here */   
kubectl replace -f /tmp/nginx-tmp.yaml 

  

8. Delete

kubectl delete -f rc-nginx.yaml   
kubectl delete po rc-nginx-btv4j   
kubectl delete po -lapp=nginx-2   

  

9. apply

     apply命令提供了比patch,edit等更严格的更新resource的方式。经过apply,用户能够将resource的configuration使用source control的方式维护在版本库中。每次有更新时,将配置文件push到server,而后使用kubectl apply将更新应用到resource。kubernetes会在引用更新前将当前配置文件中的配置同已经应用的配置作比较,并只更新更改的部分,而不会主动更改任何用户未指定的部分。 
     apply命令的使用方式同replace相同,不一样的是,apply不会删除原有resource,而后建立新的。apply直接在原有resource的基础上进行更新。同时kubectl apply还会resource中添加一条注释,标记当前的apply。相似于git操做。 
      

10. logs

     logs命令用于显示pod运行中,容器内程序输出到标准输出的内容。跟docker的logs命令相似。若是要得到tail -f 的方式,也可使用-f选项。 
kubectl logs rc-nginx-2-kpiqt 

  

 

11. rolling-update

     rolling-update是一个很是重要的命令,对于已经部署而且正在运行的业务,rolling-update提供了不中断业务的更新方式。rolling-update每次起一个新的pod,等新pod彻底起来后删除一个旧的pod,而后再起一个新的pod替换旧的pod,直到替换掉全部的pod。 
     rolling-update须要确保新的版本有不一样的name,Version和label,不然会报错 。
kubectl rolling-update rc-nginx-2 -f rc-nginx.yaml   

  


 

若是在升级过程当中,发现有问题还能够中途中止update,并回滚到前面版本 
kubectl rolling-update rc-nginx-2 —rollback

  

     rolling-update还有不少其余选项提供丰富的功能,如—update-period指定间隔周期,使用时可使用-h查看help信息 

12. scale 

     scale用于程序在负载加剧或缩小时副本进行扩容或缩小,如前面建立的nginx有两个副本,能够轻松的使用scale命令对副本数进行扩展或缩小。 
扩展副本数到4:
kubectl scale rc rc-nginx-3 —replicas=4   

  

 

从新缩减副本数到2:
kubectl scale rc rc-nginx-3 —replicas=2

   

13. autoscale

     scale虽然可以很方便的对副本数进行扩展或缩小,可是仍然须要人工介入,不能实时自动的根据系统负载对副本数进行扩、缩。autoscale命令提供了自动根据pod负载对其副本进行扩缩的功能。 
     autoscale命令会给一个rc指定一个副本数的范围,在实际运行中根据pod中运行的程序的负载自动在指定的范围内对pod进行扩容或缩容。如前面建立的nginx,能够用以下命令指定副本范围在1~4 
kubectl autoscale rc rc-nginx-3 —min=1 —max=4  

  

14. cordon, drain, uncordon

     这三个命令是正式release的1.2新加入的命令,三个命令一块儿介绍,是由于三个命令配合使用能够实现节点的维护。在1.2以前,由于没有相应的命令支持,若是要维护一个节点,只能stop该节点上的kubelet将该节点退出集群,是集群不在将新的pod调度到该节点上。若是该节点上本生就没有pod在运行,则不会对业务有任何影响。若是该节点上有pod正在运行,kubelet中止后,master会发现该节点不可达,而将该节点标记为notReady状态,不会将新的节点调度到该节点上。同时,会在其余节点上建立新的pod替换该节点上的pod。这种方式虽然可以保证集群的健壮性,可是任然有些暴力,若是业务只有一个副本,并且该副本正好运行在被维护节点上的话,可能仍然会形成业务的短暂中断。 
     1.2中新加入的这3个命令能够保证维护节点时,平滑的将被维护节点上的业务迁移到其余节点上,保证业务不受影响。以下图所示是一个整个的节点维护的流程(为了方便demo增长了一些查看节点信息的操做):1)首先查看当前集群全部节点状态,能够看到共四个节点都处于ready状态;2)查看当前nginx两个副本分别运行在d-node1和k-node2两个节点上;3)使用cordon命令将d-node1标记为不可调度;4)再使用kubectl get nodes查看节点状态,发现d-node1虽然还处于Ready状态,可是同时还被禁能了调度,这意味着新的pod将不会被调度到d-node1上。4)再查看nginx状态,没有任何变化,两个副本仍运行在d-node1和k-node2上;5)执行drain命令,将运行在d-node1上运行的pod平滑的赶到其余节点上;6)再查看nginx的状态发现,d-node1上的副本已经被迁移到k-node1上;这时候就能够对d-node1进行一些节点维护的操做,如升级内核,升级Docker等;7)节点维护完后,使用uncordon命令解锁d-node1,使其从新变得可调度;8)检查节点状态,发现d-node1从新变回Ready状态。 

15. attach

     attach命令相似于docker的attach命令,能够直接查看容器中以daemon形式运行的进程的输出,效果相似于logs -f,退出查看使用ctrl-c。若是一个pod中有多个容器,要查看具体的某个容器的的输出,须要在pod名后使用-c containers name指定运行的容器。以下示例的命令为查看kube-system namespace中的kube-dns-v9-rcfuk pod中的skydns容器的输出。 
kubectl attach kube-dns-v9-rcfuk -c skydns —namespace=kube-system

  

16. exec

     exec命令一样相似于docker的exec命令,为在一个已经运行的容器中执行一条shell命令,若是一个pod容器中,有多个容器,须要使用-c选项指定容器。 

17. port-forward

     转发一个本地端口到容器端口,博主通常都是使用yaml的方式编排容器,因此基本不使用此命令。 

18. proxy

     使用nginx做为kubernetes多master HA方式的代理,没有使用过此命令为kubernetes api server运行过proxy 

19. run

     相似于docker的run命令,直接运行一个image。 

20. label

     为kubernetes集群的resource打标签,如前面实例中提到的为rc打标签对rc分组。还能够对nodes打标签,这样在编排容器时,能够为容器指定nodeSelector将容器调度到指定lable的机器上,如若是集群中有IO密集型,计算密集型的机器分组,能够将不一样的机器打上不一样标签,而后将不一样特征的容器调度到不一样分组上。 
     在1.2以前的版本中,使用kubectl get nodes则能够列出全部节点的信息,包括节点标签,1.2版本中再也不列出节点的标签信息,若是须要查看节点被打了哪些标签,须要使用describe查看节点的信息。 

21. 其余

其余还有如cluster-info信息能够查看当前集群的一些信息,Version查看集群版本信息等,还有一些集群配置相关的命令等。
相关文章
相关标签/搜索