kubectl get -o 格式化

kubectl输出格式

 kubectl 命令可以用多种格式对结果进行显示,输出的格式通过-o参数指定:

$ kubectl [command] [TYPE] [NAME] -o=<output_format>

根据不同子命令的输出结果,可选的输出格式如表2.12所示。

输出格式 说明
-o=custom-columns=<spec> 根据自定义列名进行输出,以逗号分隔
-o=custom-colimns-file=<filename> 从文件中获取自定义列名进行输出
-o=json 以JSON格式显示结果
-o=jsonpath=<template> 输出jsonpath表达式定义的字段信息
-o=jsonpath-file=<filename> 输出jsonpath表达式定义的字段信息,来源于文件
-o=name 仅输出资源对象的名称
-o=wide 输出额外信息。对于Pod,将输出Pod所在的Node名
-o=yaml 以yaml格式显示结果

kubectl get -o  custom-columns,custom-columns-file,go-template,go-template-file,json,jsonpath,jsonpath-file,name,template,templatefile,wide,yaml

1、custom-columns:自定义列名   

     kubectl  get  pod coredns-fb8b8dccf-8t6qm  -n kube-system -o yaml > a.yaml

     根据a.yaml结构编写

     kubectl get pod test-pod -o custom-columns=jettech_name:.spec.containers[0].name,jettech_image:.spec.containers[0].image

2、custom-columns-file。根据定的文件列出内容

    vim a.txt    

NAME                RSRC
metadata.name       metadata.resourceVersion
 kubectl get pods chaosfomoney-deployment-64ccd59bdc-h72gh -o=custom-columns-file=a.txt 

另外,还可以将输出结果按某个字段排序,通过--sort-by参数以jsonpath表达式进行指定:

扫描二维码关注公众号,回复: 9289701 查看本文章

例如,按照名字进行排序:

$ kubectl get pods
NAME                                       READY     STATUS    RESTARTS   AGE
chaosfomoney-deployment-64ccd59bdc-h72gh   1/1       Running   0          1d
chaosfomoney-deployment-64ccd59bdc-spnpj   1/1       Running   0          1d
chaosfomoney-deployment-64ccd59bdc-xhnjr   1/1       Running   0          1d

$ kubectl get pods --sort-by=.metadata.name
NAME                                       READY     STATUS    RESTARTS   AGE
chaosfomoney-deployment-64ccd59bdc-h72gh   1/1       Running   0          1d
chaosfomoney-deployment-64ccd59bdc-spnpj   1/1       Running   0          1d
chaosfomoney-deployment-64ccd59bdc-xhnjr   1/1       Running   0          1d

3、jsonpath,jsonpath-file(和2原理一样用文件输出内容),json

首先可以先

kubectl  get  pod coredns-fb8b8dccf-8t6qm  -n kube-system -o json> a.json

根据a.json结构来编写格式化的内容

kubectl  get  pod nginx-pod  -o jsonpath={".spec.volumes"}

4、go-template,go-template-file

kubectl get相关资源,默认输出为kubectl内置,一般我们也可以使用-o json或者-o yaml查看其完整的资源信息。但是很多时候,我们需要关心的信息并不全面,因此我们需要自定义输出的列,那么可以使用go-template来进行实现。

go-template是golang的一种模板,可以参考template的相关说明

比如仅仅想要查看获取的pods中的各个pod的uid,则可以使用以下命令:

[root@node root]# kubectl get pods --all-namespaces -o go-template --template='{{range.items}}{{.metadata.uid}}{{end}}'
0313ffff-f1f4-11e7-9cda-40f2e9b98448
ee49bdcd-f1f2-11e7-9cda-40f2e9b98448
f1e0eb80-f1f2-11e7-9cda-40f2e9b98448
[root@node-106 xuxinkun]# kubectl get pods -o yaml 
apiVersion: v1
items:
- apiVersion: v1
  kind: Pod
  metadata:
    name: nginx-deployment-1751389443-26gbm
    namespace: default
    uid: a911e34b-f445-11e7-9cda-40f2e9b98448
  ...
- apiVersion: v1
  kind: Pod
  metadata:
    name: nginx-deployment-1751389443-rsbkc
    namespace: default
    uid: a911d2d2-f445-11e7-9cda-40f2e9b98448
  ...
- apiVersion: v1
  kind: Pod
  metadata:
    name: nginx-deployment-1751389443-sdbkx
    namespace: default
    uid: a911da1a-f445-11e7-9cda-40f2e9b98448
    ...
kind: List
metadata: {}
resourceVersion: ""

因为get pods的返回结果是List类型,获取的pods都在items这个的value中,因此需要遍历items,也就有了{{range .items}}。而后通过模板选定需要展示的内容,就是items中的每个{{.metadata.uid}}

这里特别注意,要做一个特别的处理,就是要把{{end}}前进行换行,以便在模板中插入换行符。

当然,如果觉得这样处理不优雅的话,也可以使用printf函数,在其中使用\n即可实现换行符的插入

[root@node root]# kubectl get pods --all-namespaces -o go-template --template='{{range.items}}{{printf "%s\n" .metadata.uid}}{{end}}'

其实有了printf,就可以很容易的实现对应字段的输出,且样式可以进行自己控制。比如可以这样

[root@node root]# kubectl get pods --all-namespaces -o go-template --template='{{range.items}}{{printf "|%-20s|%-50s|%-30s|\n" .metadata.namespace .metadata.name.metadata.uid}}{{end}}'
|console             |console-4d2d7eab-1377218307-7lg5v                 |0313ffff-f1f4-11e7-9cda-40f2e9b98448|
|console             |console-4d2d7eab-1377218307-q3bjd                 |ee49bdcd-f1f2-11e7-9cda-40f2e9b98448|
|cxftest             |ipreserve-f15257ec-3788284754-nmp3x               |f1e0eb80-f1f2-11e7-9cda-40f2e9b98448|

除了使用go-template之外,还可以使用go-template-file。则模板不用通过参数传进去,而是写成一个文件,然后需要指定template指向该文件即可。

[root@node root]#  kubectl get pods --all-namespaces -o go-template-file --template=/home/template_file
[root@node root]# cat /home/template_file
{{range .items}}{{printf "%s\n" .metadata.uid}}{{end}}

5、template,templatefile

使用go-template自定义kubectl get输出

原文地址:https://www.cnblogs.com/xuxinkun/p/8304854.html

http://docs.kubernetes.org.cn/626.html

https://www.cnblogs.com/tylerzhou/p/11047456.html

kubectl技巧之通过go-template截取属性

在使用kubectl get获取资源信息的时候,可以通过-o(--output简写形式)指定信息输出的格式,如果指定的是yaml或者json输出的是资源的完整信息,实际工作中,输出内容过少则得不到我们想要的信息,输出内容过于详细又不利于快速定位的我们想要找到的内容,其实-o输出格式可以指定为go-template然后指定一个template,这样我们就可以通过go-template获取我们想要的内容.go-template与kubernetes无关,它是go语言内置的一种模板引擎.这里不对go-template做过多解释,仅介绍在kubernetes中获取资源常用的语法,想要获取更多内容,大家可以参考相关资料获取帮助.

基本语法

  • go-template语法整体风格类似handlebars模板引擎语法,通过{{}}来访问变量

以如下方式来访问预定义的变量”foo”:

{{ foo }}
  • 使用空格来分隔参数

以如下方式来调用具有输入1,2的add函数:

{{ add 1 2 }}
  • 通过.符号来访问方法和字段

以如下方式来访问Foo的参数”bar”:

{{ .Foo.bar }}

变量

  • 通过引用变量名称来访问该变量。
{{foo}}
  • 变量也同样可以被定义和引用。
{{ $address := "123 Main St."}}
{{ $address }}

函数

go template支持非常多的函数,这里不再详细介绍,仅介绍与获取kubernetes资源对象相关的range

就像Go一样,Go模板中大量的使用了range来遍历map,array或者slice。以下内容是使用range的不同例子。

  • 例子1:通过使用上下文
{{ range array }}
    {{ . }}
{{ end }}

例子2:通过声明value变量的名称

{{range $element := array}}
    {{ $element }}
{{ end }}

例子3:通过同时声明key和value变量名称

{{range $index, $element := array}}
    {{ $index }}
    {{ $element }}
{{ end }

go template就简单介绍到这里,下面通过两个示例来说明如何获取对象的某一属性或者遍历对象的集合属性中的某一字段

go template获取资源属性具体信息

  • 示例1 获取pod IP
[centos@k8s-master consul]$ kubectl get pod helloworld-7fdc8d9855-ncfdz -oyaml
apiVersion: v1
kind: Pod
metadata:
 ......

status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2019-03-13T04:34:03Z"
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2019-03-13T04:34:08Z"
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: "2019-03-13T04:34:08Z"
    status: "True"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2019-03-13T04:34:03Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: docker://7d9e68920d0373df278602b976e2757be7c77c5860e32598193cc3d06d635eb5
    image: tutum/hello-world:latest
    imageID: docker-pullable://tutum/hello-world@sha256:0d57def8055178aafb4c7669cbc25ec17f0acdab97cc587f30150802da8f8d85
    lastState: {}
    name: helloworld
    ready: true
    restartCount: 0
    state:
      running:
        startedAt: "2019-03-13T04:34:07Z"
  hostIP: 192.168.122.73
  phase: Running
  podIP: 10.244.1.3
  qosClass: BestEffort
  startTime: "2019-03-13T04:34:03Z"
......

以上是我通过kubectl get pod pod名称获取到的pod的信息,如果仅想要获取关于pod的ip的信息,可以通过如下命令

get pod helloworld-7fdc8d9855-ncfdz -o go-template --template='{{.status.podIP}}'
10.244.1.3

podIP属性在status对象里,因此通过以上语法可获得pod的ip

示例2 获取pod使用镜像的ip

我们知道,一个pod里可能包含多个容器,因此一个pod在创建时可能使用了一个以上的镜像,我们看下资源结构

[centos@k8s-master consul]$ kubectl get po helloworld-7fdc8d9855-ncfdz  -oyaml
apiVersion: v1
kind: Pod
......
spec:
  containers:
  - image: tutum/hello-world
    imagePullPolicy: Always
    name: helloworld
    ports:
    - containerPort: 80
      protocol: TCP
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-4ctj2
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: k8s-node1
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: default-token-4ctj2
    secret:
      defaultMode: 420
      secretName: default-token-4ctj2
......

当然,以上pod里仅使用了一个镜像,但是它包含在containers数组属性里,因此通过.属性名的方式获取获取到结果,我们需要遍历这个数组,然后输出其中的对象.

 kubectl get po helloworld-7fdc8d9855-ncfdz  -o go-template --template='{{range .spec.containers}}{{.image}}{{end}}'
tutum/hello-world[

以上首先通过range获取数组,然后像获取普通属性一样获取数组里对象的image属性.最后加上end标识,表示操作结束.

命令kubectl get po -o go-template --template=xxx可以简写为kubectl get po -o=go-template=格式模板

kubectl技巧之通过jsonpath截取属性

前面一节我们介绍了使用go-template截取属性,go-template功能非常强大,可以定义变量,使用流程控制等,这是jsonpath所不具备的.然而,jsonpth使用的时候更为灵活.通过上一节我们发现,我们想要找到某个具体属性,必须从最外层一层层向内找到具体属性,这对于嵌套层次非常深的yaml对象来说操作是非常繁琐的.而使用jsonpath只需要知道顶层对象,然后可以省略中间的对象,递归查找直接找到我们想要的属性,这在很多时候对我们在不清楚对象的层次但是清楚知道某个属性名称的时候获取这个属性的值是非常有帮助的.并且jsonpath可以使用下标索引数组对象,这在实际工作中也是非常有帮助的(比如虽然pod里可以包含多个containers,但是很多时候一个pod里只有一个container,使用go-template我们为了找到这个对象需要写一个遍历表达式,而使用jsonpath可以直接取第0个对象,省去了写循环的麻烦),还有一点很重要的是jsonpath是一个标准,这对于熟悉jsonpath的开发者来说使用起来方便很多.

jsonpath模板使用一对花括号({})把jsonpath表达式包含在里面(go-template是双花括号).除了标准jsonpath语法外,kubernetes jsonpath模板还额外支持以下语法:

  • 用""双引号来引用JSONPath表达式中的文本

  • 使用rangeend来遍历集合(这点和go-template类似)

  • 使用负数来从尾部索引集合

$操作符是可选的因为表达式默认总是从根节点开始选择
对象通过它的String()函数打印输出出来

假如有以下JSON字符串

{
  "kind": "List",
  "items":[
    {
      "kind":"None",
      "metadata":{"name":"127.0.0.1"},
      "status":{
        "capacity":{"cpu":"4"},
        "addresses":[{"type": "LegacyHostIP", "address":"127.0.0.1"}]
      }
    },
    {
      "kind":"None",
      "metadata":{"name":"127.0.0.2"},
      "status":{
        "capacity":{"cpu":"8"},
        "addresses":[
          {"type": "LegacyHostIP", "address":"127.0.0.2"},
          {"type": "another", "address":"127.0.0.3"}
        ]
      }
    }
  ],
  "users":[
    {
      "name": "myself",
      "user": {}
    },
    {
      "name": "e2e",
      "user": {"username": "admin", "password": "secret"}
    }
  ]
}
Function Description Example Result
text the plain text kind is {.kind} kind is List
@ the current object {@} the same as input
. or [] child operator {.kind} or {[‘kind’]} List
.. recursive descent {..name} 127.0.0.1 127.0.0.2 myself e2e
* wildcard. Get all objects {.items[*].metadata.name} [127.0.0.1 127.0.0.2]
[start:end :step] subscript operator {.users[0].name} myself
[,] union operator {.items[*][‘metadata.name’, ‘status.capacity’]} 127.0.0.1 127.0.0.2 map[cpu:4] map[cpu:8]
?() filter {.users[?(@.name==“e2e”)].user.password} secret
range, end iterate list {range .items[*]}[{.metadata.name}, {.status.capacity}] {end} [127.0.0.1, map[cpu:4]] [127.0.0.2, map[cpu:8]]
quote interpreted string {range .items[*]}{.metadata.name}{’\t’}{end} 127.0.0.1 127.0.0.2

使用jsonpath示例

kubectl get pods -o json
kubectl get pods -o=jsonpath='{@}'
kubectl get pods -o=jsonpath='{.items[0]}'
kubectl get pods -o=jsonpath='{.items[0].metadata.name}'
kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}'

如果对象是集合类型,需要使用range关键字开始,以end关键字结果,同前面一节go-template类似.

我们通过以下示例来看如何通过jsonpath简单地获取到容器所在节点名称

[centos@k8s-master ~]$ kubectl get po consul-0 -ojsonpath='{..nodeName}'
k8s-node1
[centos@k8s-master ~]$

当然以上也可以通过grep来获取到同样的信息,并且对于很多熟悉linux命令的童鞋来说更为方便,如果仅仅是查看.grep确实更为方便,但是通过jsonpath是准确地获取到了一个属性的值,而grep则是截取的包含这个关键字的一行,如果我们要把获取的值作为下一个命令的的输入值时,通过grep获取的结果往往是需要处理的.例如通过grep获取到的结果如下

"k8s-node1",[centos@k8s-master ~]$ kubectl get po consul-0 -ojson|grep nodeName
        "nodeName": "k8s-node1",

这里想要准备的获取结果,产生要截取第二列值,然后再去掉引号,操作起来不如jsonpath方便.尤其在不同环境如果输出的格式不一样的话,通过字符串截取得到的结果可能是错误的.

发布了459 篇原创文章 · 获赞 118 · 访问量 82万+

猜你喜欢

转载自blog.csdn.net/Michaelwubo/article/details/89357788