Fields

Prints based on Field values

TL;DR

  • Format and print specific fields from Resources
  • Use when scripting with Get

Print Resource Fields

Motivation

Kubectl Get is able to pull out fields from Resources it queries and format them as output.

This may be useful for scripting or gathering data about Resources from a Kubernetes cluster.

Get

The kubectl get reads Resources from the cluster and formats them as output. The examples in this chapter will query for Resources by providing Get the Resource Type with the Version and Group as an argument. For more query options see Queries and Options.

Kubectl can format and print specific fields from Resources using Json Path.

Scripting Pitfalls

By default, if no API group or version is specified, kubectl will use the group and version preferred by the apiserver.

Because the Resource structure may change between API groups and Versions, users should specify the API Group and Version when emitting fields from kubectl get to make sure the command does not break in future releases.

Failure to do this may result in the different API group / version being used after a cluster upgrade, and this group / version may have changed the representation of fields.

Example: Print the JSON representation of the first Deployment in the list on a single line

  1. kubectl get deployment.v1.apps -o=jsonpath='{.items[0]}{"\n"}'

you get:

  1. map[apiVersion:apps/v1 kind:Deployment...replicas:1 updatedReplicas:1]]

This ideology can be extended to query out the specific fields in a yaml resource file.

Command / Examples

Check out the reference for more commands and examples.