系列目录

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

最新文章

  1. (整理) JQuery中的AJAX
  2. eclipse javascript验证报错
  3. eclipse常用快捷键,这个只要新学会的常用的会陆续更新的。
  4. 用bat启动sqlserver服务
  5. 计算机网络之HTTP(上)基础知识点
  6. Android开发小问题集
  7. OpenStack笔记
  8. 20175311 2018-2019-2 《Java程序设计》第1周学习总结
  9. es6三个点运算符
  10. sql,取得当前系统时间,算时间区间
  11. Final发布——视频博客
  12. nginx map使用方法
  13. 《开源框架那些事儿22》:UI框架设计实战
  14. Learn Docker(一)—软件安装与常规操作
  15. QLayout窗口布局
  16. java学习(六)面向对象 final关键字 多态
  17. CDH集群安装配置(四)- mysql 的安装
  18. NYOJ 821 简单求值【简单题】
  19. MySQL配置参数:wait_timeout
  20. iOS UITableViewCell UITableVIewController 纯代码开发

热门文章

  1. mysql经验总结
  2. C#函数多返回值的方法
  3. 【POJ3321】Apple Tree(DFS序,树状数组)
  4. 一款手机端的日历插件ICalendar.js
  5. HDU 6231 (二分+双指针)
  6. linux shmget shmctl
  7. Definition vs declaration
  8. Codeforces 835F Roads in the Kingdom (环套树 + DP)
  9. 济南day6
  10. Codeforces 235 E Number Challenge