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