Quick Jsonpath Basics

Some Handy json lookups

kubectl get nodes -o json>/opt/outputs/nodes.json
kubectl get node node01 -o json>/opt/outputs/node01.json
kubectl get nodes -o=jsonpath=’{.items[].metadata.name}’>/opt/outputs/node_names.txt
kubectl get nodes -o=jsonpath=’{.items[
].status.nodeInfo.osImage}’ > /opt/outputs/nodes_os.txt
kubectl config view --kubeconfig=my-kube-config -o jsonpath="{.users[].name}" > /opt/outputs/users.txt
kubectl get pv --sort-by=.spec.capacity.storage > /opt/outputs/storage-capacity-sorted.txt
kubectl get pv --sort-by=.spec.capacity.storage -o=custom-columns=NAME:.metadata.name,CAPACITY:.spec.capacity.storage > /opt/outputs/pv-and-capacity-sorted.txt
kubectl config view --kubeconfig=my-kube-config -o jsonpath="{.contexts[?(@.context.user==‘aws-user’)].name}" > /opt/outputs/aws-context-name
kubectl get nodes -o jsonpath=’{.items[
].status.addresses[?(@.type==“InternalIP”)].address}’>/root/node_ips

kubectl get deploy -n admin2406 -o custom-columns=“DEPLOYMENT:metadata.name,CONTAINER_IMAGE:{…image},READY_REPLICAS:status.readyReplicas,NAMESPACE:metadata.namespace” --sort-by=.metadata.name

kubectl config view --kubeconfig=my-kube-config -o jsonpath="{.contexts[?(@.context.user==‘aws-user’)].name}" > /opt/outputs/aws-context-name

kubectl get pod -o jsonpath=’{.items[].metadata.name}{.items[].status.conditions[?(@.type==”Ready”)].lastTransitionTime}’
kubectl get nodes -o jsonpath=”{range .items[]}{.metadata.name} {.spec.taints[].effect}{\”\n\”}{end}”
kubectl get nodes -o jsonpath=”{range .items[]}{.metadata.name} {.spec.taints[].effect}{\”\n\”}{end}” | grep -v NoSchedule

1 Like