Finding Kubernetes search terms

Viewed 664

I have problems finding the values used for selecting or displaying objects in kubernetes. I feel like I'm not using the kubernetes docs correctly or these are linked with a command I haven't learned yet.

Examples:

  1. Find all the pods on a certain node? How do you know it's in spec.nodeName

    kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName=<node>

  2. Get all Pods in a running state? How do you know this value is stored in status.phase

    kubectl get pods --field-selector status.phase=Running

  3. Get all pods but only listing the name, How do you know it called metadata.name

    kubectl get pods -o custom-columns:POD:metadata.name

5 Answers

Each resource in Kubernetes is described by some specification. You can find the detailed spec for the resources in the documentation, for example here https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/. If you navigate to the Pod definition (https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#pod-v1-core) you will see all the properties of the Pod type with their underlying types.

For example, your first point, reading the PodSpec (https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/#podspec-v1-core) you can see that the nodeName property holds the name of a node that should be used to schedule the pod.

Same documentation can be reviewed from the kubectl command. To get the documentation for PodSpec you can just type kubectl explain pod.spec

You can find the description of almost any Kubernetes object in the API documentation.
Specifically, the pod object attributes are described here.

Each member of the pod object can be further explored via the direct links found in the same documentation page.

--field-selector allows you to create any filter you need as you already very well described in the question.

Practical answers:

Find all the pods on a certain node?

It's included in the output of

kubectl describe node node-001.example.com

Get all Pods in a running state?

kubectl get pods | grep Running

Get all pods but only listing the name

kubectl get pods -o name

Note that these particular commands don't combine especially well. If you want to get the name only of all of the running pods on a specific node, you can combine the more detailed options in the original question (as others have suggested, using the Pod definition in the API docs as a reference).

In addition to @krizas answer wanna add you can also use --recurcive option of kubectl explain output

Add the --recursive flag to display all of the fields at once without descriptions. Information about each field is retrieved from the server in OpenAPI format.

What I mean is that you can have short list of all available objects and sub-objects and only then have their descriptions. E.g

 kubectl explain pod --recursive
 #will give you all sub-objects of pod

Or you can have all pod.metadata objects like

kubectl explain pod.metadata --recursive
FIELDS:
   annotations  <map[string]string>
   clusterName  <string>
   creationTimestamp    <string>
   deletionGracePeriodSeconds   <integer>
   deletionTimestamp    <string>
   finalizers   <[]string>
   generateName <string>
   generation   <integer>
   initializers <Object>
      pending   <[]Object>
         name   <string>
      result    <Object>
         apiVersion     <string>
         code   <integer>
         details        <Object>
            causes      <[]Object>
               field    <string>
               message  <string>
               reason   <string>
            group       <string>
            kind        <string>
            name        <string>
            retryAfterSeconds   <integer>
            uid <string>
         kind   <string>
         message        <string>
         metadata       <Object>
            continue    <string>
            resourceVersion     <string>
            selfLink    <string>
         reason <string>
         status <string>
   labels       <map[string]string>
   managedFields        <[]Object>
      apiVersion        <string>
      fields    <map[string]>
      manager   <string>
      operation <string>
      time      <string>
   name <string>
   namespace    <string>
   ownerReferences      <[]Object>
      apiVersion        <string>
      blockOwnerDeletion        <boolean>
      controller        <boolean>
      kind      <string>
      name      <string>
      uid       <string>
   resourceVersion      <string>
   selfLink     <string>
   uid  <string>

Then its easy to explain exact object you need. Plus you have good structure in front of your eyes.

Related