两岸军事 發表於 2019-6-18 20:30:00

kubectl技巧之通过go-template截取属性

<blockquote>
<p>系列目录</p>
</blockquote>
<p>在使用<code>kubectl get</code>获取资源信息的时候,可以通过-o(--output简写形式)指定信息输出的格式,如果指定的是yaml或者json输出的是资源的完整信息,实际工作中,输出内容过少则得不到我们想要的信息,输出内容过于详细又不利于快速定位的我们想要找到的内容,其实-o输出格式可以指定为go-template然后指定一个template,这样我们就可以通过go-template获取我们想要的内容.<code>go-template</code>与kubernetes无关,它是go语言内置的一种模板引擎.这里不对go-template做过多解释,仅介绍在kubernetes中获取资源常用的语法,想要获取更多内容,大家可以参考相关资料获取帮助.</p>
<h2 id="基本语法">基本语法</h2>
<ul>
<li>go-template语法整体风格类似handlebars模板引擎语法,通过<code>{{}}</code>来访问变量</li>
</ul>
<p>以如下方式来访问预定义的变量”foo”:</p>
<pre><code class="language-handlebars">{{ foo }}
</code></pre>
<ul>
<li>使用空格来分隔参数</li>
</ul>
<p>以如下方式来调用具有输入1,2的add函数:</p>
<pre><code class="language-handlebars">{{ add 1 2 }}
</code></pre>
<ul>
<li>通过.符号来访问方法和字段</li>
</ul>
<p>以如下方式来访问Foo的参数”bar”:</p>
<pre><code class="language-handlebars">{{ .Foo.bar }}
</code></pre>
<h3 id="变量"><strong>变量</strong></h3>
<ul>
<li>通过引用变量名称来访问该变量。</li>
</ul>
<pre><code class="language-handlebars">{{foo}}
</code></pre>
<ul>
<li>变量也同样可以被定义和引用。</li>
</ul>
<pre><code class="language-handlebars">{{ $address := "123 Main St."}}
{{ $address }}
</code></pre>
<h3 id="函数"><strong>函数</strong></h3>
<p>go template支持非常多的函数,这里不再详细介绍,仅介绍与获取kubernetes资源对象相关的<code>range</code></p>
<p>就像Go一样,Go模板中大量的使用了range来遍历map,array或者slice。以下内容是使用range的不同例子。</p>
<ul>
<li>例子1:通过使用上下文</li>
</ul>
<pre><code class="language-handlebars">{{ range array }}
    {{ . }}
{{ end }}
</code></pre>
<p>例子2:通过声明value变量的名称</p>
<pre><code class="language-handlebars">{{range $element := array}}
    {{ $element }}
{{ end }}
</code></pre>
<p>例子3:通过同时声明key和value变量名称</p>
<pre><code class="language-handlebars">{{range $index, $element := array}}
    {{ $index }}
    {{ $element }}
{{ end }
</code></pre>
<p>go template就简单介绍到这里,下面通过两个示例来说明如何获取对象的某一属性或者遍历对象的集合属性中的某一字段</p>
<h2 id="go-template获取资源属性具体信息">go template获取资源属性具体信息</h2>
<ul>
<li>示例1 获取pod IP</li>
</ul>
<pre><code class="language-yml">$ 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"
......
</code></pre>
<p>以上是我通过<code>kubectl get pod pod名称</code>获取到的pod的信息,如果仅想要获取关于pod的ip的信息,可以通过如下命令</p>
<pre><code class="language-bash">get pod helloworld-7fdc8d9855-ncfdz -o go-template --template='{{.status.podIP}}'
10.244.1.3
</code></pre>
<p>podIP属性在status对象里,因此通过以上语法可获得pod的ip</p>
<h2 id="示例2-获取pod使用镜像的ip">示例2 获取pod使用镜像的ip</h2>
<p>我们知道,一个pod里可能包含多个容器,因此一个pod在创建时可能使用了一个以上的镜像,我们看下资源结构</p>
<pre><code class="language-yaml">$ 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
......

</code></pre>
<p>当然,以上pod里仅使用了一个镜像,但是它包含在<code>containers</code>数组属性里,因此通过<code>.属性名</code>的方式获取获取到结果,我们需要遍历这个数组,然后输出其中的对象.</p>
<pre><code class="language-bash"> kubectl get po helloworld-7fdc8d9855-ncfdz-o go-template --template='{{range .spec.containers}}{{.image}}{{end}}'
tutum/hello-world[
</code></pre>
<p>以上首先通过<code>range</code>获取数组,然后像获取普通属性一样获取数组里对象的<code>image</code>属性.最后加上end标识,表示操作结束.</p>
<blockquote>
<p>命令kubectl get po -o go-template --template=xxx可以简写为<code>kubectl get po -o=go-template=格式模板</code></p>
</blockquote><br><br>
来源:https://www.cnblogs.com/tylerzhou/p/11047456.html
頁: [1]
查看完整版本: kubectl技巧之通过go-template截取属性