1. Overview

Kubernetes has become a de facto standard for deploying containerized applications. It allows us to create objects in an imperative as well as declarative way.

The imperative way allows us to create a Kubernetes object directly from the command line, whereas, with the declarative way, we need to define a configuration in a manifest file. Most people prefer the declarative way because it allows us to maintain configuration via the version control system.

Oftentimes, we need to get details of the deployed Kubernetes objects to debug application issues. In such cases, we can use the kubectl get command. Along with this, we can display output in various formats using the –output option of the command.

In this tutorial, we’ll discuss how to get a Kubernetes object’s configuration in YAML and other formats. So, let’s get started.

2. Setting up an Example

First, let’s create a new Kubernetes namespace for the deployment:

$ kubectl create ns nginx-demo
namespace/nginx-demo created

Next, we’ll use the imperative approach to create a Kubernetes deployment object:

$ kubectl create deployment nginx --image=nginx:stable-alpine3.17-slim -n nginx-demo
deployment.apps/nginx created

In this example, we’ve created an NGINX deployment object, which ultimately creates a Pod in a given namespace.

Finally, let’s verify that the deployment has created the NGINX pod:

$ kubectl get pods -n nginx-demo
NAME                    READY   STATUS    RESTARTS   AGE
nginx-9bd595477-dwz45   1/1     Running   0          101s

Now, we’ve created the required setup. The latter section of the tutorial uses these Deployment and Pod objects to display output in different formats.

It’s important to note that the tutorial uses Pod and Deployment objects to demonstrate the usage of the command. However, we can use the same technique with any other Kubernetes object.

3. Show the Output in Wide Format

We can use the get command to display the Kubernetes objects. However, by default, it shows limited information. Let’s understand this with a simple example:

$ kubectl get pods -n nginx-demo
NAME                    READY   STATUS    RESTARTS   AGE
nginx-9bd595477-dwz45   1/1     Running   0          8m33s

In the above output, we can see that the command shows the basic details about the Pod. However, sometimes, this limited information is not sufficient. In such cases, we, use the wide option to display additional information:

$ kubectl get pods -n nginx-demo -o wide
NAME                    READY   STATUS    RESTARTS   AGE   IP           NODE                  NOMINATED NODE   READINESS GATES
nginx-9bd595477-dwz45   1/1     Running   0          15m   10.244.0.5   naren-control-plane   <none>           <none>

Now, we can see that the output shows some additional fields, such as the IP address of the Pod, the Kubernetes node on which the Pod is running, readiness gates, and more.

4. Show the Output in YAML Format

In the previous example, we used the wide option to display additional details about the Kubernetes object. Sometimes, however, we need to get the complete manifest of the deployed Kubernetes object. In such cases, we can specify the yaml argument with the -o option.

To understand this, let’s display the manifest of the nginx Deployment object:

$ kubectl get deploy nginx -n nginx-demo -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2023-05-22T16:36:25Z"
  generation: 1
  labels:
    app: nginx
  name: nginx

Here, we can see that the output now shows much more detailed information.

It’s important to note that the YAML output can be really lengthy hence we’ve skipped some of it for brevity.

In the full output, we can observe that the status section of the output shows the current state of the object:

status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: "2023-05-22T16:36:25Z"
    lastUpdateTime: "2023-05-22T16:36:25Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2023-05-22T16:36:24Z"
    lastUpdateTime: "2023-05-22T16:36:25Z"
    message: ReplicaSet "nginx-9bd595477" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 1
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1

5. Show the Output in JSON Format

Similarly, we can instruct kubectl to display the object manifest in JSON format. To achieve this, we can use the json argument with the -o option:

$ kubectl get deploy nginx -n nginx-demo -o json | head
{
    "apiVersion": "apps/v1",
    "kind": "Deployment",
    "metadata": {
        "annotations": {
            "deployment.kubernetes.io/revision": "1"
        },
        "creationTimestamp": "2023-05-22T16:36:25Z",
        "generation": 1,
        "labels": {

In this example, we can observe that the same output is shown in JSON format.

Both YAML and JSON formats are readable. However, JSON formats are easier to parse. In fact, kubectl allows us to filter specific fields from the JSON object. In the next section, we’ll see a practical example of it.

6. Filter the Specific Field From the JSON Object

In the previous section, we saw how to display the complete manifest in JSON format. However, sometimes, we want to extract a particular field from the JSON object. In such cases, we can use the JSONPath expression enclosed in the curly braces ({}).

To understand this, let’s extract the kind field from the Deployment object:

$ kubectl get deploy nginx -n nginx-demo -o jsonpath='{.kind}'
Deployment

Here, we have used a simple JSONPath expression to illustrate the usage. However, the jsonpath option supports complex JSONPath expressions as well. We can refer to the official Kubernetes documentation to get a complete list of supported expressions.

7. Show Custom Fields in the Output

In addition to this, we can also instruct kubectl to display custom fields. This allows us to rename the default header fields as well.

So, let’s use the custom-columns argument with the -o option to print the object type, its name, and the namespace:

$ kubectl get deploy nginx -n nginx-demo -o custom-columns="Kind:.kind,Name:.metadata.name,Namespace:.metadata.namespace"
Kind         Name    Namespace
Deployment   nginx   nginx-demo

Here, we’ve used a comma-delimited list to specify the multiple custom fields we want to see. Each individual field is then delimited by the colon (:) where the left part represents our custom column header and the right part represents the actual field whose value we want to display.

In this way, kubectl allows us to alter the output fields as per our requirements.

8. Cleaning Up

Just like any other objects, Kubernetes objects also consume system resources, so it’s a good practice to clean them up if they aren’t needed. Removing the wrong Kubernetes object can cause potential issues. Hence, we have to be extra careful before deleting them.

Let’s use the delete command to remove the Deployment and Namespace objects:

$ kubectl delete deployment nginx -n nginx-demo
deployment.apps "nginx" deleted

$ kubectl delete namespace nginx-demo
namespace "nginx-demo" deleted

9. Conclusion

In this article, we saw how to display the YAML manifest of a deployed Kubernetes object.

First, we used the wide output format to show the additional fields. Next, we discussed getting output in the YAML and JSON formats. Then, we saw how to extract a particular field from the JSON object.

Finally, we discussed altering the output field using the custom-columns option.