kubectl 格式化输出

发布时间 2023-10-17 15:06:05作者: 小糊涂90

1、显示Pod的更多信息,例如Node等:

[root@k8s-master ~]# kubectl get pod nginx-585449566-qkwhc  -o wide
NAME                    READY   STATUS    RESTARTS   AGE    IP               NODE        NOMINATED NODE   READINESS GATES
nginx-585449566-qkwhc   1/1     Running   0          3h7m   10.100.169.136   k8s-node2   <none>           <none>

2、以yaml格式显示pod的详细信息:

[root@k8s-master ~]# kubectl get pod  nginx-585449566-qkwhc -o yaml
apiVersion: v1
kind: Pod
metadata:
  annotations:
    cni.projectcalico.org/podIP: 10.100.169.136/32
    cni.projectcalico.org/podIPs: 10.100.169.136/32
  creationTimestamp: "2023-10-17T03:41:53Z"
  generateName: nginx-585449566-
  labels:
    app: nginx
    pod-template-hash: "585449566"
......

3、以自定义列名显示pod的信息:

[root@k8s-master ~]# kubectl get pod -o custom-columns=NAME:.metadata.selfLink
NAME
/api/v1/namespaces/default/pods/nginx-585449566-qkwhc
/api/v1/namespaces/default/pods/nginx-585449566-skrj8
/api/v1/namespaces/default/pods/nginx-585449566-x9fd8

4、基于自定义列名配置文件林进行输出:

[root@k8s-master ~]# cat d.txt 
NAME	UID
metadata.name	metadata.uid
[root@k8s-master ~]# kubectl get pod -o custom-columns-file=d.txt 
NAME                    UID
nginx-585449566-qkwhc   62235664-c1dd-46f9-8667-ba92ba5eca41
nginx-585449566-skrj8   c0552f14-c7b5-4eb4-8d34-c00b590a15b2
nginx-585449566-x9fd8   c004b08f-2e32-4199-9775-23a0f845fc55

5、关闭服务端列名

[root@k8s-master ~]# kubectl get pod --server-print=false
NAME                    AGE
nginx-585449566-qkwhc   3h11m
nginx-585449566-skrj8   5h25m
nginx-585449566-x9fd8   5h25m

6、将输出结果按某个字段排序

[root@k8s-master ~]# kubectl get pod -o custom-columns=TIME:.status.startTime,NAME:.metadata.name
TIME                   NAME
2023-10-17T03:41:53Z   nginx-585449566-qkwhc
2023-10-17T01:27:58Z   nginx-585449566-skrj8
2023-10-17T01:27:58Z   nginx-585449566-x9fd8

[root@k8s-master ~]# kubectl get pod -o custom-columns=TIME:.status.startTime,NAME:.metadata.name --sort-by=.status.startTime
TIME                   NAME
2023-10-17T01:27:58Z   nginx-585449566-skrj8
2023-10-17T01:27:58Z   nginx-585449566-x9fd8
2023-10-17T03:41:53Z   nginx-585449566-qkwhc