设为首页 收藏本站
查看: 1022|回复: 0

[经验分享] [置顶]kubernetes-kubectl命令说明

[复制链接]

尚未签到

发表于 2018-1-5 12:42:31 | 显示全部楼层 |阅读模式
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>  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 runlykops /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
  
有两种方法可以回退到这个版本:
  

kubectl rollout undo deployment/nginx-deployment  
kubectl rollout undo deployment/nginx-deployment --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>

  

  
缩容Pod的副本数目到1
  

kubectl scale>
  

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=nginx-2,如果在运行过程中,需要把其label改为app=nginx-3,这patch命令如下: kubectl patch pod rc-nginx-2-kpiqt -p '{"metadata":{"labels":{"app":"nginx-3"}}}'

replace
  
replace命令用于对已有资源进行更新、替换。如前面create中创建的nginx,当需要更新resource的一些属性时,如果修改副本数量,增加、修改label,更改image版本,修改端口等。都可以直接修改原yaml文件,然后执行replace命令。
  
注:名字不能被更更新。另外,如果是更新label,原有标签的pod将会与更新label后的rc断开联系,有新label的rc将会创建指定副本数的新的pod,但是默认并不会删除原来的pod。所以此时如果使用get po将会发现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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-431869-1-1.html 上篇帖子: 阿里云ECS安装Kubernetes问题收集与解答 下篇帖子: 一次kubernetes资源文件创建失败的排查
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表