系列目录html
在使用kubectl get
获取资源信息的时候,能够经过-o(--output简写形式)指定信息输出的格式,若是指定的是yaml或者json输出的是资源的完整信息,实际工做中,输出内容过少则得不到咱们想要的信息,输出内容过于详细又不利于快速定位的咱们想要找到的内容,其实-o输出格式能够指定为go-template而后指定一个template,这样咱们就能够经过go-template获取咱们想要的内容.go-template
与kubernetes无关,它是go语言内置的一种模板引擎.这里不对go-template作过多解释,仅介绍在kubernetes中获取资源经常使用的语法,想要获取更多内容,你们能够参考相关资料获取帮助.node
{{}}
来访问变量以以下方式来访问预约义的变量”foo”:docker
{{ foo }}
以以下方式来调用具备输入1,2的add函数:json
{{ add 1 2 }}
以以下方式来访问Foo的参数”bar”:centos
{{ .Foo.bar }}
{{foo}}
{{ $address := "123 Main St."}} {{ $address }}
go template支持很是多的函数,这里再也不详细介绍,仅介绍与获取kubernetes资源对象相关的range
api
就像Go同样,Go模板中大量的使用了range来遍历map,array或者slice。如下内容是使用range的不一样例子。数组
{{ range array }} {{ . }} {{ end }}
例子2:经过声明value变量的名称bash
{{range $element := array}} {{ $element }} {{ end }}
例子3:经过同时声明key和value变量名称函数
{{range $index, $element := array}} {{ $index }} {{ $element }} {{ end }
go template就简单介绍到这里,下面经过两个示例来讲明如何获取对象的某一属性或者遍历对象的集合属性中的某一字段3d
[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
咱们知道,一个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=格式模板