kubectl技巧之通过go-template截取属性
2019-06-18 20:30
1716 查看
在使用
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=格式模板
相关文章推荐
- JavaScript中通过prototype属性共享属性和方法的技巧实例
- JavaScript中通过prototype属性共享属性和方法的技巧实例
- apache的一个技巧,通过X-Forwarded-For来封ip
- GOPATH设置和go get替换HTTPS的技巧
- 如何: 通过HTML文档对象模型访问文档中的ActiveX控件的属性
- 项模板选择器属性(ItemTemplateSelector属性)和样式选择器(ItemContainerStyleSelector)
- wp7 >> 通过后台代码给ListBoxItem添加ContextMenu 属性
- java通过反射机制取类的属性和方法
- go语言使用go-sciter创建桌面应用(一) 简单的通过html,css写ui
- springCloud入门学习(七):通过属性自定义Ribbon配置
- jsp通过<img>标签的src属性来调用servlet类生成验证码遇到的问题
- Go语言学习之html/template包(The way to go)
- 通过8个技巧让你成为一个超强的Linux终端用户
- selenium相关:通过location 和 size 获取元素所在像素位置和尺寸,截取图片ROI
- Java通过注解直接格式化data属性
- (四十八)Android TextView中文字通过SpannableString来设置超链接、颜色、字体等属性
- 通过JDBC连接oracle数据库的十大技巧
- js对象属性 通过点(.) 和 方括号([]) 的不同之处
- Flex中如何通过设置toolTipField属性在ButtonBar控件中设定自定义tool tip的例子
- 通过api查看android控件属性