Kubernetes之kubectl常用命令
最近项目有用到Kubernetes作集群配置,所以学习下相关命令,记录下以备下次使用...
kubectl help 显示具体的用法
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命令获取所有资源的详细信息
kubectl get 会显示具体的信息
kubectl get po -o wide 获取pod运行在哪个节点上的信息
describe类似于get,同样用于获取resource的相关信息。不同的是,get获得的是更详细的resource个性的详细信息,describe获得的是resource集群相关的信息。describe命令同get类似,但是describe不支持-o选项,对于同一类型resource,describe输出的信息格式,内容域相同。
kubectl describe pod pod名
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
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会创建两个副本:
kubectl create -f rc-nginx.yaml
创建后,使用“kubectl get rc”可以看到一个名为rc-nginx-2的ReplicationController将被创建,同时“kubectl get po”的结果中会多出两个前缀为“rc-nginx-2-”的pod
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命令如下:
7. edit
edit提供了另一种更新resource源的操作,通过edit能够灵活的在一个common的resource基础上,发展出更过的significant resource。例如,使用edit直接更新前面创建的pod的命令为:
上面命令的效果等效于:
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选项。
11. rolling-update
rolling-update是一个非常重要的命令,对于已经部署并且正在运行的业务,rolling-update提供了不中断业务的更新方式。rolling-update每次起一个新的pod,等新pod完全起来后删除一个旧的pod,然后再起一个新的pod替换旧的pod,直到替换掉所有的pod。
rolling-update需要确保新的版本有不同的name,Version和label,否则会报错 。
如果在升级过程中,发现有问题还可以中途停止update,并回滚到前面版本
rolling-update还有很多其他选项提供丰富的功能,如—update-period指定间隔周期,使用时可以使用-h查看help信息
12. scale
scale用于程序在负载加重或缩小时副本进行扩容或缩小,如前面创建的nginx有两个副本,可以轻松的使用scale命令对副本数进行扩展或缩小。
扩展副本数到4:
重新缩减副本数到2:
13. autoscale
scale虽然能够很方便的对副本数进行扩展或缩小,但是仍然需要人工介入,不能实时自动的根据系统负载对副本数进行扩、缩。autoscale命令提供了自动根据pod负载对其副本进行扩缩的功能。
autoscale命令会给一个rc指定一个副本数的范围,在实际运行中根据pod中运行的程序的负载自动在指定的范围内对pod进行扩容或缩容。如前面创建的nginx,可以用如下命令指定副本范围在1~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. 其他
Kubernetes之kubectl常用命令的更多相关文章
- Kubernetes的kubectl常用命令速记
文章转载自:https://mp.weixin.qq.com/s/0kqQzeA-MzCOhPMkmiR4_A kubectl是用来管理Kubernetes集群的命令行工具. kubectl默认在&q ...
- Kubernetes,kubectl常用命令详解
kubectl概述 祭出一张图,转载至 kubernetes-handbook/kubectl命令概述 ,可以对命令族有个整体的概念. 环境准备 允许master节点部署pod,使用命令如下: kub ...
- kubernetes 知识点及常用命令
一.附上一个Deployment文件 apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: selec ...
- K8s的kubectl常用命令
一. 设置kubectl输入命令自动补全 依次执行一下命令: yum install -y bash-completion source /usr/share/bash-completion/bash ...
- k8s记录-kubectl常用命令
kubectl kubectl annotate – 更新资源的注解.kubectl api-versions – 以“组/版本”的格式输出服务端支持的API版本.kubectl apply – 通过 ...
- kubectl 常用命令一
1.kubectl logs <options> <PodName> -f -p, --previous --since= No. --since-time= --tail ...
- kubectl 常用命令总结
# 查看所有 pod 列表, -n 后跟 namespace, 查看指定的命名空间 kubectl get pod kubectl get pod -n kube # 查看 RC 和 service ...
- kubectl常用命令
command kubectl kubectl 输出格式 显示Pod的更多信息 kubectl get pod <pod-name> -o wide 以yaml格式显示Pod的详细信息 k ...
- kubectl常用命令(个人记录)
一.获取pod信息 1.获取当前集群运行的所有的pods的信息 kubectl get pod 2.获取当前集群运行的所有的pod运行在哪个节点 kubectl get pods -owide ...
随机推荐
- Java成员变量与局部变量同名
看到成员变量和局部变量同名这个知识点的时候一开始有点懵逼,想了一下其实特别简单. 先来看一个简单的代码. 首先我定义了一个Person类. public class Person { private ...
- sklearn中xgboost模块中plot_importance函数(特征重要性)
# -*- coding: utf-8 -*- """ ######################################################### ...
- 论Top与ROW_NUMBER读取第一页的效率问题及拼接sql查询条件
http://www.cnblogs.com/Leo_wl/p/4921799.html SELECT TOP * FROM users WHERE nID> And nID< ORDER ...
- MySQL索引长度限制问题
在修改表结构时出现了错误:Specified key was too long;max key length is 1000 bytes. MySQL版本为Server version: 5.1.36 ...
- Makefile 自动变量之 $(@D),$(@F)
参考:http://www.gnu.org/software/make/manual/make.html '$(@D)'The directory part of the file name of t ...
- python学习之os.walk()
os.walk(top,topdown = True,onerror = None,followlinks = False) 参数 top -- 根目录下的每一个文件夹(包含它自己), 产生3-元组 ...
- python学习之range()和xrange()
在python2中,xrange()返回一个xrange对象,注意这个对象并不是生成器,也不是迭代器,但是是迭代对象. 而range()则返回列表对象. >>> range(10) ...
- linux之grub的使用
一.引言 昨天在家里在自己的电脑上安装CentOS7,一切都顺利,结果安装好重启,无法进入win7了,在grub的配置文件也添加了win7的条目,却提示 unkown command chainloa ...
- linux学习笔记9--命令cat
cat命令的用途是连接文件或标准输入并打印.这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用. cat命令连接文件并打印到标准输出设备上, ...
- nginx中,$request_uri和$uri的区别
nginx中,$request_uri和$uri的区别 $request_uri This variable is equal to the *original* request URI as r ...