最近项目有用到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

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命令如下:

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. 其他

其他还有如cluster-info信息可以查看当前集群的一些信息,Version查看集群版本信息等,还有一些集群配置相关的命令等。

Kubernetes之kubectl常用命令的更多相关文章

  1. Kubernetes的kubectl常用命令速记

    文章转载自:https://mp.weixin.qq.com/s/0kqQzeA-MzCOhPMkmiR4_A kubectl是用来管理Kubernetes集群的命令行工具. kubectl默认在&q ...

  2. Kubernetes,kubectl常用命令详解

    kubectl概述 祭出一张图,转载至 kubernetes-handbook/kubectl命令概述 ,可以对命令族有个整体的概念. 环境准备 允许master节点部署pod,使用命令如下: kub ...

  3. kubernetes 知识点及常用命令

    一.附上一个Deployment文件 apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: selec ...

  4. K8s的kubectl常用命令

    一. 设置kubectl输入命令自动补全 依次执行一下命令: yum install -y bash-completion source /usr/share/bash-completion/bash ...

  5. k8s记录-kubectl常用命令

    kubectl kubectl annotate – 更新资源的注解.kubectl api-versions – 以“组/版本”的格式输出服务端支持的API版本.kubectl apply – 通过 ...

  6. kubectl 常用命令一

    1.kubectl logs <options>  <PodName> -f -p, --previous --since= No. --since-time= --tail ...

  7. kubectl 常用命令总结

    # 查看所有 pod 列表, -n 后跟 namespace, 查看指定的命名空间 kubectl get pod kubectl get pod -n kube # 查看 RC 和 service ...

  8. kubectl常用命令

    command kubectl kubectl 输出格式 显示Pod的更多信息 kubectl get pod <pod-name> -o wide 以yaml格式显示Pod的详细信息 k ...

  9. kubectl常用命令(个人记录)

    一.获取pod信息 1.获取当前集群运行的所有的pods的信息 kubectl get pod   2.获取当前集群运行的所有的pod运行在哪个节点 kubectl get pods -owide   ...

随机推荐

  1. LaTeX 中插入图片使其紧跟插入的文字之后

    LaTeX 中插入图片使其不跑到每页的开头而紧跟插入的文字之后. 此次建模过程中,遇到的一个比较棘手的问题是: 当插入图片时,图片的位置总是会自动跑到当页(或下一页)的最上方,而不是紧跟在其对应的说明 ...

  2. sql关于group by之后把每一条记录的详情的某个字段值合并提取的方法

    在利用group by写了统计语句之后,还有一个查看每一个记录详情的需求, 首先想到的是根据group by的条件去拼接查询条件, 但是条件有点多,拼接起来不仅麻烦,还容易出错, 所以想到要在grou ...

  3. 详解Struts1中的struts-config.xml配置文件【一】

    搞清楚struts-config.xml中各项元素的作用,对于我们构建web项目有莫大的好处.<struts-config>是struts的根元素,它主要有8个子元素,DTD定义如下: & ...

  4. ConcurrentBag同线程元素的添加和删除

    https://www.mgenware.com/blog/?p=232 ConcurrentBag<T>对于同一个线程值的添加和删除是非常快的,因为ConcurrentBag内部将数据按 ...

  5. homebrew osx下面最优秀的包管理工具

    官方网站:https://brew.sh/index_zh-cn 安装 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubuserconten ...

  6. 【转载】Oracle数据字典详解

    转自:http://czmmiao.iteye.com/blog/1258462 Oracle数据字典概述 数据库是数据的集合,数据库维护和管理这用户的数据,那么这些用户数据表都存在哪里,用户的信息是 ...

  7. firefox配置

    Firefox23取消了一个很人性化的功能,就是在GUI界面中禁用JavaScript,对于这点我很不能理解!JavaScript是所有网页木马 的源头,防人之心不可无,FireFox就这么确信能防住 ...

  8. Reveal使用教程

    Reveal使用教程 Reveal是用于透视程序整体结构的一个软件,软件收费89美刀,试用期30天,不过好在有破解版,无需担心花钱的问题​ 在然后呢,软件在哪下,可以在我的github上下载到破解版本 ...

  9. 实现DIV居中的几种方法

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. linux学习笔记15--命令locater、slocate

    locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了.在一般的 di ...