kubernetes-kubectl命令说明

本文基于kubernetes 1.5.2版本编写

kubectl

kubectl controls the K8S cluster manager. 

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

Basic Commands (Beginner):
  create         Create a resource by filename or stdin
  expose         Take a replication controller, service, deployment or pod and expose it as a new K8S Service
  run            Run a particular image on the cluster
  set            Set specific features on objects

Basic Commands (Intermediate):
  get            Display one or many resources
  explain        Documentation of resources
  edit           Edit a resource on the server
  delete         Delete resources by filenames, stdin, resources and names, or by resources and label selector

Deploy Commands:
  rollout        Manage a deployment rollout
  rolling-update Perform a rolling update of the given ReplicationController
  scale          Set a new size for a Deployment, ReplicaSet, Replication Controller, or Job
  autoscale      Auto-scale a Deployment, ReplicaSet, or ReplicationController

Cluster Management Commands:
  certificate    Modify certificate resources.
  cluster-info   Display cluster info
  top            Display Resource (CPU/Memory/Storage) usage
  cordon         Mark node as unschedulable
  uncordon       Mark node as schedulable
  drain          Drain node in preparation for maintenance
  taint          Update the taints on one or more nodes

Troubleshooting and Debugging Commands:
  describe       Show details of a specific resource or group of resources
  logs           Print the logs for a container in a pod
  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 K8S API server
  cp             Copy files and directories to and from containers.

Advanced Commands:
  apply          Apply a configuration to a resource by filename or stdin
  patch          Update field(s) of a resource using strategic merge patch
  replace        Replace a resource by filename or stdin
  convert        Convert config files between different API versions

Settings Commands:
  label          Update the labels on a resource
  annotate       Update the annotations on a resource
  completion     Output shell completion code for the given shell (bash or zsh)

Other Commands:
  api-versions   Print the supported API versions on the server, in the form of "group/version"
  config         Modify kubeconfig files
  help           Help about any command
  version        Print the client and server version information

基本命令

get

get命令用于获取集群的一个或一些resource信息。

参数-o格式,输出对应的格式,yaml、json等

create

用于根据文件或输入创建资源对象。如果已经定义了相应资源对象的yaml或json文件,直接kubectl create -f filename即可创建文件内定义的资源对象。也可以直接只用子命令[namespace/secret/configmap/serviceaccount]等直接创建相应的资源对象。从追踪和维护的角度出发,建议使用json或yaml的方式定义资源。

expose

主要是做NAT的

run

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

kubectl run -it --image=web:apache run lykops /bin/bash

也可以运行计划任务

Start the cron job to compute to 2000 places and print it out every 5 minutes.
kubectl run pi --schedule="0/5 * * * *" --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'

edit

edit提供了另一种更新资源对象的操作。例如,使用edit直接更新前面创建的pod的命令为:

kubectl edit pod rc-nginx-btv4j   

上面命令的效果等效于:

kubectl get pod rc-nginx-btv4j -o yaml >> /tmp/nginx-tmp.yaml   
vim /tmp/nginx-tmp.yaml   
……
kubectl replace -f /tmp/nginx-tmp.yaml  

delete

根据资源对象名或label删除资源对象。

服务部署

rollout

deployment回退到指定版本

kubectl rollout undo deployment/lykops --to-revision=2

rolling-update

只能适用于RC

使用配置文件升级

kubectl rolling-update lykops-rc-v1 -f lykops-rc.yaml --update-period=10s

直接使用images

rolling-update lykops-rc --image=webapache:v3

scale

扩容Pod的副本数目到10

kubectl scale relicationcontroller lykops --replicas=10

缩容Pod的副本数目到1

kubectl scale relicationcontroller lykops --replicas=1

autoscale

scale虽然能够很方便的对副本数进行扩展或缩小,但是仍然需要人工介入,不能实时自动的根据系统负载对副本数进行扩、缩。autoscale命令提供了自动根据pod负载对其副本进行扩缩的功能。

autoscale命令会给一个rc指定一个副本数的范围(同HPA,http://blog.csdn.net/liyingke112/article/details/77101673),在实际运行中根据pod中运行的程序的负载自动在指定的范围内对pod进行扩容或缩容。如前面创建的nginx,可以用如下命令指定副本范围在1~4

kubectl autoscale rc lykops-rc --min=1 --max=4   

集群管理

certificate

修改集群认证信息

cluster-info

显示集群api信息,包括URL

top

显示系统资源(CPU/Memory/Storage)使用情况

cordon、uncordon、drain

cordon,启用后,该节点处于非活动节点,新建的资源对象不会分配到该机上。

uncordon,将非活动节点转为活动节点

drain,让节点处于维修状态

这三个命令是正式release的1.2新加入的命令,三个命令一起介绍,是因为三个命令配合使用可以实现节点的维护。

在1.2之前,因为没有相应的命令支持,如果要维护一个节点,只能stop该节点上的kubelet将该节点退出集群,是集群不在将新的pod调度到该节点上。如果该节点上本生就没有pod在运行,则不会对业务有任何影响。如果该节点上有pod正在运行,kubelet停止后,master会发现该节点不可达,而将该节点标记为notReady状态,不会将新的节点调度到该节点上。同时,会在其他节点上创建新的pod替换该节点上的pod。这种方式虽然能够保证集群的健壮性,但是让然有些暴力,如果业务只有一个副本,而且该副本正好运行在被维护节点上的话,可能仍然会造成业务的短暂中断。

1.2中新加入的这3个命令可以保证维护节点时,平滑的将被维护节点上的业务迁移到其他节点上,保证业务不受影响。

taint

调试和bug处理

describe

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

logs

logs命令用于显示pod运行中,容器内程序输出到标准输出的内容。跟docker的logs命令类似。如果要获得tail -f 的方式,也可以使用-f选项。

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

exec

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

port-forward

转发一个本地端口到容器端口,一般都是使用yaml的方式编排容器,所以基本不使用此命令。

proxy

cp

拷贝文件到pod的容器中

高级命令

apply

apply命令提供了比patch,edit等更严格的更新resource的方式。通过apply,用户可以将resource的configuration使用source control的方式维护在版本库中。每次有更新时,将配置文件push到server,然后使用kubectl apply将更新应用到resource。K8S会在引用更新前将当前配置文件中的配置同已经应用的配置做比较,并只更新更改的部分,而不会主动更改任何用户未指定的部分。

apply命令的使用方式同replace相同,不同的是,apply不会删除原有resource,然后创建新的。apply直接在原有resource的基础上进行更新。同时kubectl apply还会resource中添加一条注释,标记当前的apply。类似于git操作。

patch

如果一个容器已经在运行,这时需要对一些容器属性进行修改,又不想删除容器,或不方便通过replace的方式进行更新。K8S还提供了一种在容器运行时,直接对容器进行修改的方式,就是patch命令。类似打补丁。 如前面创建pod的label是app=lykops-1,如果在运行过程中,需要把其label改为app=lykops-2,这patch命令如下: kubectl patch pod lykops-1-kpiqt -p '{"metadata":{"labels":{"app":"lykops-2"}}}'

replace

replace命令用于对已有资源进行更新、替换。如前面create中创建的lykops,当需要更新resource的一些属性时,如果修改副本数量,增加、修改label,更改image版本,修改端口等。都可以直接修改原yaml文件,然后执行replace命令。

注:名字不能被更更新。另外,如果是更新label,原有标签的pod将会与更新label后的rc断开联系,有新label的rc将会创建指定副本数的新的pod,但是默认并不会删除原来的pod。所以此时如果使用get pod将会发现pod数翻倍,进一步check会发现原来的pod已经不会被新rc控制。

convert

Convert config files between different API versions

配置

label

为K8S集群的resource打标签,如前面实例中提到的为rc打标签对rc分组。还可以对nodes打标签,这样在编排容器时,可以为容器指定nodeSelector将容器调度到指定lable的机器上,如如果集群中有IO密集型,计算密集型的机器分组,可以将不同的机器打上不同标签,然后将不同特征的容器调度到不同分组上。

在1.2之前的版本中,使用kubectl get nodes则可以列出所有节点的信息,包括节点标签,1.2版本中不再列出节点的标签信息,如果需要查看节点被打了哪些标签,需要使用describe查看节点的信息。

annotate

Update the annotations on a resource

completion

Output shell completion code for the given shell (bash or zsh)

其他

api-versions

Print the supported API versions on the server, in the form of "group/version"

config

修改K8S配置文件
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值