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=格式模板

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wssfss.html