背景说明
通过kubectl进行获取资源对象例如pod、pv时可能需要自定义输出字段,比如默认情况下
student@master01:~$ kubectl get pod
NAME READY STATUS RESTARTS AGE
11-factor-app 1/1 Running 6 (23d ago) 72d
foo 1/1 Running 6 (23d ago) 72d
front-end-94f5bf8fb-hxg4p 1/1 Running 6 (23d ago) 72d
podname1 1/1 Running 1 (112m ago) 113m
podname2 1/1 Running 1 (107m ago) 112m
presentation-5649f596b9-jxfnn 1/1 Running 6 (23d ago) 72d
student@master01:~$
解决方案
官方文档
https://kubernetes.io/zh/docs/reference/kubectl/jsonpath/
中间过程
student@master01:~$ kubectl get pod -o yaml
apiVersion: v1
items:
- apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2022-02-23T16:55:04Z"
name: 11-factor-app
namespace: default
resourceVersion: "57635"
uid: 16578c61-8ab6-4ba3-9157-b76aeef403be
spec:
containers:
- args:
- /bin/sh
- -c
- |
i=0; while true; do
echo "$(date) INFO $i" >> /var/log/11-factor-app.log;
i=$((i+1));
sleep 1;
done
image: busybox
imagePullPolicy: IfNotPresent
name: count
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: kube-api-access-kcjc2
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: node02
preemptionPolicy: PreemptLowerPriority
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: kube-api-access-kcjc2
projected:
defaultMode: 420
sources:
- serviceAccountToken:
expirationSeconds: 3607
path: token
- configMap:
items:
- key: ca.crt
path: ca.crt
name: kube-root-ca.crt
- downwardAPI:
items:
- fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
path: namespace
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2022-02-23T16:55:04Z"
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2022-05-07T06:29:43Z"
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2022-05-07T06:29:43Z"
status: "True"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2022-02-23T16:55:04Z"
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://da36ee8fe4f43e96232f6686a95b27bd8433a6c521addc03c168cb51efca3e82
image: busybox:latest
imageID: docker-pullable://busybox@sha256:5acba83a746c7608ed544dc1533b87c737a0b0fb730301639a0179f9344b1678
lastState:
terminated:
containerID: docker://07e5061731c3e797189812bd7625499422269ec5bbf192e3e50fa90548d49fef
exitCode: 137
finishedAt: "2022-04-14T06:20:41Z"
reason: Error
startedAt: "2022-04-14T06:19:50Z"
name: count
ready: true
restartCount: 6
started: true
state:
running:
startedAt: "2022-05-07T06:29:41Z"
hostIP: 11.0.1.113
phase: Running
podIP: 10.244.2.34
podIPs:
- ip: 10.244.2.34
qosClass: BestEffort
startTime: "2022-02-23T16:55:04Z"
- apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2022-02-23T16:54:17Z"
labels:
name: foo
name: foo
namespace: default
resourceVersion: "57658"
uid: cae12fa2-3c47-48d7-b982-a451f956255c
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: kube-api-access-l969c
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: node01
preemptionPolicy: PreemptLowerPriority
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: kube-api-access-l969c
projected:
defaultMode: 420
sources:
- serviceAccountToken:
expirationSeconds: 3607
path: token
- configMap:
items:
- key: ca.crt
path: ca.crt
name: kube-root-ca.crt
- downwardAPI:
items:
- fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
path: namespace
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2022-02-23T16:54:17Z"
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2022-05-07T06:29:47Z"
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2022-05-07T06:29:47Z"
status: "True"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2022-02-23T16:54:17Z"
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://9302d2be6a478977c3f63332084161eb3ac97c471bd978b84c8e9a973444e377
image: nginx:latest
imageID: docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
lastState:
terminated:
containerID: docker://a83d66b5c42e9107f59439bc6d12a0e9d3235938a9464593f1642b8d3df17e4c
exitCode: 0
finishedAt: "2022-04-14T06:20:31Z"
reason: Completed
startedAt: "2022-04-14T06:19:50Z"
name: nginx
ready: true
restartCount: 6
started: true
state:
running:
startedAt: "2022-05-07T06:29:44Z"
hostIP: 11.0.1.112
phase: Running
podIP: 10.244.1.28
podIPs:
- ip: 10.244.1.28
qosClass: BestEffort
startTime: "2022-02-23T16:54:17Z"
- apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2022-02-23T16:48:28Z"
generateName: front-end-94f5bf8fb-
labels:
app: front-end
pod-template-hash: 94f5bf8fb
name: front-end-94f5bf8fb-hxg4p
namespace: default
ownerReferences:
- apiVersion: apps/v1
blockOwnerDeletion: true
controller: true
kind: ReplicaSet
name: front-end-94f5bf8fb
uid: fe59f286-24df-4169-8842-89b401d78a40
resourceVersion: "57611"
uid: fa6c8aa9-b010-49a7-9cb9-fa41a3c716c0
spec:
containers:
- image: vicuu/nginx:hello
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: kube-api-access-xww5d
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: node02
preemptionPolicy: PreemptLowerPriority
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: kube-api-access-xww5d
projected:
defaultMode: 420
sources:
- serviceAccountToken:
expirationSeconds: 3607
path: token
- configMap:
items:
- key: ca.crt
path: ca.crt
name: kube-root-ca.crt
- downwardAPI:
items:
- fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
path: namespace
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2022-02-23T16:48:28Z"
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2022-05-07T06:29:41Z"
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2022-05-07T06:29:41Z"
status: "True"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2022-02-23T16:48:28Z"
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://3fc52fe7042dc1260318b9d5dd9dd7cab336342b42715495470ed9b6666005c9
image: vicuu/nginx:hello
imageID: docker-pullable://vicuu/nginx@sha256:6ab7771fa243c2773b5f2344f00f70a833a45f4bdfb0b26e93f69b5eedfb51fb
lastState:
terminated:
containerID: docker://b6b3a89101209f6094b784855a846efb6dbcce978bb5bddc1a579522e8998ac1
exitCode: 0
finishedAt: "2022-04-14T06:20:30Z"
reason: Completed
startedAt: "2022-04-14T06:19:50Z"
name: nginx
ready: true
restartCount: 6
started: true
state:
running:
startedAt: "2022-05-07T06:29:40Z"
hostIP: 11.0.1.113
phase: Running
podIP: 10.244.2.32
podIPs:
- ip: 10.244.2.32
qosClass: BestEffort
startTime: "2022-02-23T16:48:28Z"
- apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2022-05-07T07:12:37Z"
labels:
run: podname1
name: podname1
namespace: default
resourceVersion: "61725"
uid: 3db68ff6-72a7-46d9-9992-0fe946865619
spec:
containers:
- image: nginx
imagePullPolicy: Always
name: podname1
resources: {}
stdin: true
stdinOnce: true
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
tty: true
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: kube-api-access-q9g2n
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: node01
preemptionPolicy: PreemptLowerPriority
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: kube-api-access-q9g2n
projected:
defaultMode: 420
sources:
- serviceAccountToken:
expirationSeconds: 3607
path: token
- configMap:
items:
- key: ca.crt
path: ca.crt
name: kube-root-ca.crt
- downwardAPI:
items:
- fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
path: namespace
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2022-05-07T07:12:37Z"
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2022-05-07T07:13:24Z"
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2022-05-07T07:13:24Z"
status: "True"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2022-05-07T07:12:37Z"
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://ed770b4fa71794e842d07622b51cdd68572904cb2df07da3e49e3ef666d6594c
image: nginx:latest
imageID: docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
lastState:
terminated:
containerID: docker://1ed55f27f239effc3668797023f45b35d1e720123960aa93567f4521ef1924fd
exitCode: 0
finishedAt: "2022-05-07T07:13:06Z"
reason: Completed
startedAt: "2022-05-07T07:12:54Z"
name: podname1
ready: true
restartCount: 1
started: true
state:
running:
startedAt: "2022-05-07T07:13:23Z"
hostIP: 11.0.1.112
phase: Running
podIP: 10.244.1.32
podIPs:
- ip: 10.244.1.32
qosClass: BestEffort
startTime: "2022-05-07T07:12:37Z"
- apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2022-05-07T07:13:39Z"
labels:
run: podname2
name: podname2
namespace: default
resourceVersion: "62209"
uid: a919402b-c6f7-4c2b-b441-7254748beb4f
spec:
containers:
- args:
- /bin/bash
image: nginx
imagePullPolicy: Always
name: podname2
resources: {}
stdin: true
stdinOnce: true
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
tty: true
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: kube-api-access-j4prk
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: node02
preemptionPolicy: PreemptLowerPriority
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: kube-api-access-j4prk
projected:
defaultMode: 420
sources:
- serviceAccountToken:
expirationSeconds: 3607
path: token
- configMap:
items:
- key: ca.crt
path: ca.crt
name: kube-root-ca.crt
- downwardAPI:
items:
- fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
path: namespace
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2022-05-07T07:13:39Z"
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2022-05-07T07:18:29Z"
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2022-05-07T07:18:29Z"
status: "True"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2022-05-07T07:13:39Z"
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://eaf617143c82156404e59a986d626805ef6befe65aa5f949ecbec720f5a20135
image: nginx:latest
imageID: docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
lastState:
terminated:
containerID: docker://429f55451268195da3bf7bda2b1b060bafdcc8a5f19892b96cb5748908e0f80f
exitCode: 0
finishedAt: "2022-05-07T07:18:13Z"
reason: Completed
startedAt: "2022-05-07T07:13:56Z"
name: podname2
ready: true
restartCount: 1
started: true
state:
running:
startedAt: "2022-05-07T07:18:29Z"
hostIP: 11.0.1.113
phase: Running
podIP: 10.244.2.37
podIPs:
- ip: 10.244.2.37
qosClass: BestEffort
startTime: "2022-05-07T07:13:39Z"
- apiVersion: v1
kind: Pod
metadata:
creationTimestamp: "2022-02-23T16:49:42Z"
generateName: presentation-5649f596b9-
labels:
app: presentation
pod-template-hash: 5649f596b9
name: presentation-5649f596b9-jxfnn
namespace: default
ownerReferences:
- apiVersion: apps/v1
blockOwnerDeletion: true
controller: true
kind: ReplicaSet
name: presentation-5649f596b9
uid: 627e4117-ed25-4003-8832-b1c00e1b250c
resourceVersion: "57631"
uid: 7aae8380-c712-4e75-8909-2a2593c25b2c
spec:
containers:
- image: vicuu/nginx:hello
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: kube-api-access-ml9qn
readOnly: true
dnsPolicy: ClusterFirst
enableServiceLinks: true
nodeName: node02
preemptionPolicy: PreemptLowerPriority
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: kube-api-access-ml9qn
projected:
defaultMode: 420
sources:
- serviceAccountToken:
expirationSeconds: 3607
path: token
- configMap:
items:
- key: ca.crt
path: ca.crt
name: kube-root-ca.crt
- downwardAPI:
items:
- fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
path: namespace
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2022-02-23T16:49:42Z"
status: "True"
type: Initialized
- lastProbeTime: null
lastTransitionTime: "2022-05-07T06:29:43Z"
status: "True"
type: Ready
- lastProbeTime: null
lastTransitionTime: "2022-05-07T06:29:43Z"
status: "True"
type: ContainersReady
- lastProbeTime: null
lastTransitionTime: "2022-02-23T16:49:42Z"
status: "True"
type: PodScheduled
containerStatuses:
- containerID: docker://f7d274a74bb94eef3c34f27e2b1db9519438697b2c99d4d06419dc7c3a1583d0
image: vicuu/nginx:hello
imageID: docker-pullable://vicuu/nginx@sha256:6ab7771fa243c2773b5f2344f00f70a833a45f4bdfb0b26e93f69b5eedfb51fb
lastState:
terminated:
containerID: docker://f65473c41ca83a04f22429c132a09c8ce5ecf4881a250ae93ab19375cbdd3621
exitCode: 0
finishedAt: "2022-04-14T06:20:30Z"
reason: Completed
startedAt: "2022-04-14T06:19:48Z"
name: nginx
ready: true
restartCount: 6
started: true
state:
running:
startedAt: "2022-05-07T06:29:41Z"
hostIP: 11.0.1.113
phase: Running
podIP: 10.244.2.33
podIPs:
- ip: 10.244.2.33
qosClass: BestEffort
startTime: "2022-02-23T16:49:42Z"
kind: List
metadata:
resourceVersion: ""
selfLink: ""
student@master01:~$
固定字段
student@master01:~$ kubectl get pod -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}'
11-factor-app 2022-02-23T16:55:04Z
foo 2022-02-23T16:54:17Z
front-end-94f5bf8fb-hxg4p 2022-02-23T16:48:28Z
podname1 2022-05-07T07:12:37Z
podname2 2022-05-07T07:13:39Z
presentation-5649f596b9-jxfnn 2022-02-23T16:49:42Z
student@master01:~$