系列目錄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=格式模板