linux
命令行通过tab
键自动补全的方式
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
kubectl annotate – 更新资源的注解。
kubectl api-versions – 以“组/版本”的格式输出服务端支持的API版本。
kubectl apply – 通过文件名或控制台输入,对资源进行配置。
kubectl attach – 连接到一个正在运行的容器。
kubectl autoscale – 对replication controller进行自动伸缩。
kubectl cluster-info – 输出集群信息。
kubectl config – 修改kubeconfig配置文件。
kubectl create – 通过文件名或控制台输入,创建资源。
kubectl delete – 通过文件名、控制台输入、资源名或者label selector删除资源。
kubectl describe – 输出指定的一个/多个资源的详细信息。
kubectl edit – 编辑服务端的资源。
kubectl exec – 在容器内部执行命令。
kubectl expose – 输入replication controller,service或者pod,并将其暴露为新的kubernetes service。
kubectl get – 输出一个/多个资源。
kubectl label – 更新资源的label。
kubectl logs – 输出pod中一个容器的日志。
kubectl namespace -(已停用)设置或查看当前使用的namespace。
kubectl patch – 通过控制台输入更新资源中的字段。
kubectl port-forward – 将本地端口转发到Pod。
kubectl proxy – 为Kubernetes API server启动代理服务器。
kubectl replace – 通过文件名或控制台输入替换资源。
kubectl rolling-update – 对指定的replication controller执行滚动升级。
kubectl run – 在集群中使用指定镜像启动容器。
kubectl scale – 为replication controller设置新的副本数。
kubectl stop – (已停用)通过资源名或控制台输入安全删除资源。
kubectl version – 输出服务端和客户端的版本信息。
kubectl cordon 设定node不可使用|
kubectl uncordon 设定node可以使用|
kubectl drain 设定node进入维护模式|
kubectl
输出格式
显示Pod
的更多信息
kubectl get pod <pod-name> -o wide
以yaml
格式显示Pod
的详细信息
kubectl get pod <pod-name> -o yaml
kubectl
操作示例
创建资源对象create
kubectl
命令用于根据文件或输入创建集群resource
。如果已经定义了相应resource
的yaml
或son
文件
根据yaml
配置文件一次性创建service
和rc
kubectl create -f my-service.yaml -f my-rc.yaml
根据<directory>
目录下所有.yaml
、.yml
、.json
文件的定义进行创建操作
kubectl create -f <directory>
查看资源对象get
get
命令用于获取集群的一个或一些resource
信息。resource
包括集群节点、运行的pod,ReplicationController,service
等。
查看所有Pod
列表
kubectl get pods
查看rc
和service
列表
kubectl get rc,service
一般情况下使用时,需要你加入namespace
来确定在哪个命名空间下查找
查看safety
空间下所有pod
在哪个节点上:
kubectl get pods -o wide -n safety
描述资源对象describe
describe
类似于get
,同样用于获取resource
的相关信息。不同的是,get
获得的是更详细的resource
个性的详细信息,describe
获得的是resource
集群相关的信息。describe
命令同get
类似,但是describe
不支持-o
选项,对于同一类型resource
,describe
输出的信息格式,内容域相同。
显示Node的详细信息
kubectl describe nodes <node-name>
显示Pod的详细信息
kubectl describe pods/<pod-name>
显示由RC管理的Pod
的信息
kubectl describe pods <rc-name>
更新替换资源replace
replace
命令用于对已有资源进行更新、替换。如前面create
中创建的nginx
,当我们需要更新resource
的一些属性的时候,比如修改副本数量,增加、修改label
,更改image
版本,修改端口等。都可以直接修改原yaml
文件,然后执行replace
命令。
注:名字不能被更新。另外,如果是更新label
,原有标签的pod
将会与更新label
后的rc断开联系,有新label
的rc将会创建指定副本数的新的pod,但是默认并不会删除原来的pod。所以此时如果使用get po
将会发现pod
数翻倍,进一步check会发现原来的pod已经不会被新rc控制,此处只介绍命令不详谈此问题,好奇者可自行实验。
kubectl 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"}}}'
删除资源对象delete
根据resource
名或label
删除resource
。
基于Pod.yaml
定义的名称删除Pod
kubectl delete -f pod.yaml
删除所有包含某个label
的Pod
和service
kubectl delete pods,services -l name=<label-name>
删除所有Pod
kubectl delete pods --all
执行容器的命令exec
exec命令同样类似于docker的exec命令,为在一个已经运行的容器中执行一条shell命令,如果一个pod容器中,有多个容器,需要使用-c选项指定容器。
执行Pod的data命令,默认是用Pod中的第一个容器执行
kubectl exec <pod-name> data
指定Pod中某个容器执行data命令
kubectl exec <pod-name> -c <container-name> data
通过bash获得Pod中某个容器的TTY,相当于登录容器
kubectl exec -it <pod-name> -c <container-name> bash
6.Pod
的扩容与缩容scale
scale
用于程序在负载加重或缩小时副本进行扩容或缩小,如前面创建的nginx
有两个副本,可以轻松的使用scale
命令对副本数进行扩展或缩小。
执行扩容缩容Pod
的操作
kubectl scale rc redis --replicas=3
我们需要确认的是在rc
配置文件中定义的replicas
数量,当我们执行上述命令的结果大于replicas
的数量时,则我们执行的命令相当于扩容操作,反之相反,可以理解为我们填写的数量是我们需要的Pod
数量。需要注意的是,当我们需要进行永久性扩容时,不要忘记修改rc
配置文件中的replicas
数量。
7.Pod
的滚动升级rolling-update
rolling-update
是一个非常重要的命令,对于已经部署并且正在运行的业务,rolling-update
提供了不中断业务的更新方式。rolling-update
每次起一个新的pod
,等新pod
完全起来后删除一个旧的pod
,然后再起一个新的pod
替换旧的pod
,直到替换掉所有的pod
。
执行滚动升级操作
kubectl rolling-update redis -f redis-rc.update.yaml
需要注意的是当我们执行rolling-update
命令前需要准备好新的RC
配置文件以及ConfigMap
配置文件,RC
配置文件中需要指定升级后需要使用的镜像名称,或者可以使用kubeclt rolling-update redis --image=redis-2.0
直接指定镜像名称的方式直接升级。
日志logs
logs
命令用于显示pod
运行中,容器内程序输出到标准输出的内容。跟docker
的logs
命令类似。如果要获得tail -f
的方式,也可以使用-f
选项。
kubectl logs rc-nginx-2-kpiqt
标签label
为kubernetes
集群的resource
打标签,如前面实例中提到的为rc
打标签对rc
分组。还可以对nodes
打标签,这样在编排容器时,可以为容器指定nodeSelector
将容器调度到指定lable
的机器上,如如果集群中有IO
密集型,计算密集型的机器分组,可以将不同的机器打上不同标签,然后将不同特征的容器调度到不同分组上。
在1.2之前的版本中,使用kubectl get nodes
则可以列出所有节点的信息,包括节点标签,1.2版本中不再列出节点的标签信息,如果需要查看节点被打了哪些标签,需要使用describe
查看节点的信息。
英文 简写 |
---|
clusters (仅对federation apiservers有效) |
componentstatuses (缩写 cs) |
configmaps (缩写 cm) |
daemonsets (缩写 ds) |
deployments (缩写 deploy) |
endpoints (缩写 ep) |
events (缩写 ev) |
horizontalpodautoscalers (缩写 hpa) |
ingresses (缩写 ing) |
jobs |
limitranges (缩写 limits) |
namespaces (缩写 ns) |
networkpolicies |
nodes (缩写 no) |
persistentvolumeclaims (缩写 pvc) |
persistentvolumes (缩写 pv) |
pods (缩写 po) |
podsecuritypolicies (缩写 psp) |
podtemplates |
replicasets (缩写 rs) |
replicationcontrollers (缩写 rc) |
resourcequotas (缩写 quota) |
secrets |
serviceaccounts (缩写 sa) |
services (缩写 svc) |
statefulsets |
storageclasses |
thirdpartyresources |
kubectl edit
使用默认编辑器 编辑服务器上定义的资源。
使用命令行工具获取的任何资源都可以使用edit
命令编辑。edit
命令会打开使用KUBE_EDITOR,GIT_EDITOR
或者EDITOR
环境变量定义的编辑器,可以同时编辑多个资源,但所编辑过的资源只会一次性提交。edit
除命令参数外还接受文件名形式。
文件默认输出格式为YAML
。要以JSON
格式编辑,请指定“-o json
”选项。
如果在更新资源时报错,将会在磁盘上创建一个临时文件来记录。在更新资源时最常见的错误是几个用户同时使用编辑器更改服务器上资源,发生这种情况,你需要将你的更改应用到最新版本的资源上,或者更新保存的临时副本。
示例
编辑名为’docker-registry
’的service
:
kubectl edit svc/docker-registry
使用替代的编辑器
KUBE_EDITOR="nano"
kubectl edit svc/docker-registry
编辑名为“myjob
”的service
,输出JSON
格式 V1 API版本
kubectl edit job.v1.batch/myjob -o json
以YAML格式输出编辑deployment“mydeployment”
,并将修改的配置保存在annotation
中:
kubectl edit deployment/mydeployment -o yaml --save-config
Flags
Name | Shorthand | Default | Usage |
---|---|---|---|
filename | f | [] | Filename, directory, or URL to files to use to edit the resource |
include-extended-apis | True | If true, include definitions of new APIs via calls to the API server. [default true] | |
output | o | yaml | Output format. One of: yaml |
record | False | Record current kubectl command in the resource annotation. If set to false, do not record the command. If set to true, record the command. If not set, default to updating the existing annotation value only if one already exists. | |
recursive | R | False | Process the directory used in -f, --filename recursively. Useful when you want to manage related manifests organized within the same directory. |
save-config | False | If true, the configuration of current object will be saved in its annotation. Otherwise, the annotation will be unchanged. This flag is useful when you want to perform kubectl apply on this object in the future. | |
schema-cache-dir | ~/.kube/schema | If non-empty, load/store cached API schemas in this directory, default is ‘$HOME/.kube/schema’ | |
validate | True | If true, use a schema to validate the input before sending it | |
windows-line-endings | False | Use Windows line-ending |