kubectl技巧之經過go-template截取屬性

系列目錄html

在使用kubectl get獲取資源信息的時候,能夠經過-o(--output簡寫形式)指定信息輸出的格式,若是指定的是yaml或者json輸出的是資源的完整信息,實際工做中,輸出內容過少則得不到咱們想要的信息,輸出內容過於詳細又不利於快速定位的咱們想要找到的內容,其實-o輸出格式能夠指定爲go-template而後指定一個template,這樣咱們就能夠經過go-template獲取咱們想要的內容.go-template與kubernetes無關,它是go語言內置的一種模板引擎.這裏不對go-template作過多解釋,僅介紹在kubernetes中獲取資源經常使用的語法,想要獲取更多內容,你們能夠參考相關資料獲取幫助.node

基本語法

  • go-template語法總體風格相似handlebars模板引擎語法,經過{{}}來訪問變量

以以下方式來訪問預約義的變量」foo」:docker

{{ foo }}
  • 使用空格來分隔參數

以以下方式來調用具備輸入1,2的add函數:json

{{ add 1 2 }}
  • 經過.符號來訪問方法和字段

以以下方式來訪問Foo的參數」bar」:centos

{{ .Foo.bar }}

變量

  • 經過引用變量名稱來訪問該變量。
{{foo}}
  • 變量也一樣能夠被定義和引用。
{{ $address := "123 Main St."}}
{{ $address }}

函數

go template支持很是多的函數,這裏再也不詳細介紹,僅介紹與獲取kubernetes資源對象相關的rangeapi

就像Go同樣,Go模板中大量的使用了range來遍歷map,array或者slice。如下內容是使用range的不一樣例子。數組

  • 例子1:經過使用上下文
{{ range array }}
    {{ . }}
{{ end }}

例子2:經過聲明value變量的名稱bash

{{range $element := array}}
    {{ $element }}
{{ end }}

例子3:經過同時聲明key和value變量名稱函數

{{range $index, $element := array}}
    {{ $index }}
    {{ $element }}
{{ end }

go template就簡單介紹到這裏,下面經過兩個示例來講明如何獲取對象的某一屬性或者遍歷對象的集合屬性中的某一字段3d

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

相關文章
相關標籤/搜索