kubectl get events only for a pod

KubernetesKubectl

Kubernetes Problem Overview


When I run kubectl -n abc-namespace describe pod my-pod-zl6m6, I get a lot of information about the pod along with the Events in the end.

Is there a way to output just the Events of the pod either using kubectl describe or kubectl get commands?

Kubernetes Solutions


Solution 1 - Kubernetes

You can use the event command of kubectl.

To filter for a specific pod you can use a field-selector:

kubectl get event --namespace abc-namespace --field-selector involvedObject.name=my-pod-zl6m6

To see what fields are possible you can use kubectl describe on any event.

Solution 2 - Kubernetes

This answer gives context to @mszalbach's's answer.

  1. You should first understand the data structure of the events object. You can use kubectl get events --output json to check the data structure.

    $ kubectl get events --output json
    {
        "apiVersion": "v1",
        "items": [
            {
                "apiVersion": "v1",
                "count": 259,
                "eventTime": null,
                "firstTimestamp": "2020-04-15T12:00:46Z",
                "involvedObject": {                 <------ **this**
                    "apiVersion": "v1",
                    "fieldPath": "spec.containers{liveness}",
                    "kind": "Pod",               
                    "name": "liveness-exec",        <------ **this**
                    "namespace": "default",
                    "resourceVersion": "725991",
                    "uid": "3f497636-e601-48bc-aec8-72b3edec3d95"
                },
                ...
    
  2. Then, you can do something like this

    kubectl get events --field-selector involvedObject.name=[...]`. 
    

Solution 3 - Kubernetes

Why not display all events and grep for your podname:

kubectl get events --all-namespaces  | grep -i $podname

Solution 4 - Kubernetes

You can describe you pod and then grep the number of lines after your Events. You can add a watch if you want to monitor it.

watch "kubectl describe pod my-pod-zl6m6 | grep -A20 Events"

Solution 5 - Kubernetes

All events specific to Deployment

kubectl get events --field-selector involvedObject.name=$DEPLOYMENT_NAME -n $NAMESPACE

All events except Normal

get events --field-selector type!=Normal -A

Solution 6 - Kubernetes

If you only want the Event Messages in a short and clear view, @mszalbach answer is the best one.

But if you want all Events with all their elements to be completely displayed you can run:

kubectl describe event [POD_NAME] --namespace [POD's_NAMESPACE]

Solution 7 - Kubernetes

There is a new kubectl command which does what you asked for:

kubectl alpha events pod my-pod-zl6m6

(At some point the alpha will be dropped).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRakesh NView Question on Stackoverflow
Solution 1 - KubernetesmszalbachView Answer on Stackoverflow
Solution 2 - KuberneteskyakyaView Answer on Stackoverflow
Solution 3 - KubernetesOneKView Answer on Stackoverflow
Solution 4 - KubernetesChandan AgarwalView Answer on Stackoverflow
Solution 5 - KubernetesJobin JamesView Answer on Stackoverflow
Solution 6 - KubernetesfroblesmartinView Answer on Stackoverflow
Solution 7 - KubernetesBryanView Answer on Stackoverflow