188

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?

Edit:

This can now (kubernetes 1.29) be achieved via the following command -

kubectl -n abc-namespace events --for pod/my-pod-zl6m6

All the answers below can be ignored as they refer to older versions of kubernetes

10 Answers 10

346
+50

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.

8
  • 1
    This doesn't work for me. I get: Error: unknown flag: --field-selector
    – toddcscar
    Commented Mar 25, 2019 at 22:48
  • The official documentation mentioned field-selector since 1.12. I could not find any hint since when this is supported (found some tickets from 2015). So check your kubectl version and the help output to see what is possible with your kubectl version.
    – mszalbach
    Commented Mar 26, 2019 at 7:26
  • 1
    yeah. i have an older version. :(
    – toddcscar
    Commented Mar 26, 2019 at 21:43
  • 1
    @toddcscar were you using get or describe? That happened to me because I was trying first with describe and there is no such option in that command Commented Apr 8, 2020 at 7:46
  • 1
    And if you want to watch for changes: ` watch -d 'kubectl get event --field-selector involvedObject.name=MY_POD_NAME | tail' `
    – Roman M
    Commented Jan 4, 2023 at 17:42
76

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=[...]`. 
    
2
33

Why not display all events and grep for your podname:

kubectl get events --all-namespaces  | grep -i $podname
3
  • 4
    While this provides the results, I'd prefer the one with field-selector switch :)
    – Rakesh N
    Commented Aug 21, 2018 at 7:26
  • 2
    Of course, no doubt that is the cleaner solution. Learned about the --field-selector switch just now.
    – OneK
    Commented Aug 21, 2018 at 9:24
  • 3
    you couldnt even grep the podname, because it is not printed with the kubectl get events command
    – InsOp
    Commented Sep 10, 2019 at 9:59
14

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"
3
  • is watch a plugin? Commented Oct 27, 2021 at 5:04
  • 2
    no watch is not any kind of plugin even you can use "-w " option and get the same result
    – Aditya
    Commented Dec 28, 2021 at 8:25
  • to be explicit: watch is a stand alone tool. On mac brew install watch
    – rfreytag
    Commented Nov 24, 2022 at 13:49
14

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

kubectl events --for pod/my-pod-zl6m6

https://kubernetes.io/docs/reference/kubectl/generated/kubectl_events/

3
13

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
3
  • 1
    Thanks! This also works for a pod name like this kubectl get events --field-selector involvedObject.name=$POD_NAME -n $NAMESPACE
    – morhook
    Commented Nov 24, 2021 at 13:31
  • i dont think it works for a deployment though Commented Jan 11 at 11:15
  • it works ❯ kubectl get events --field-selector involvedObject.name=nignx 19s Normal ScalingReplicaSet deployment/nignx Scaled up replica set nignx-8f49b6b4b to 1 Commented Jan 11 at 12:24
4

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]
4

For me, the other answers look good. But, It's valuable to understand the under the hood.

Step 1. How to view original data? Because k8s will automatically by default convert json data from etcd into readable.

  • So you should use the command like this to view the both content and structure of each item.
kubectl get events -o json -n default 

Then you will get many items like this enter image description here

Step 2. Using --field-selector combine with the value you want to access from the output above like this

The value you want to access is "involvedObject.name" JSONPATH format

enter image description here

==> Final command

kubectl get events --field-selector=involvedObject.name=demo-deployment-5f4fd5649b-rks6f

Output enter image description here

3

Alternatively, you could use jq tool. With the following command:

kubectl get events -n namespace-name -ojson | jq '.items[] | select ((.involvedObject.name=="pod-name") and (.involvedObject.kind=="Pod"))'

Note that I used an additional condition in the select clause .involvedObject.kind=="Pod", to filter out all non-Pod objects with the same name and namespace as Pod we wanted.

Be aware of that while using the solution included in the-best-answer. You can just add the additional filter-selector to the command as well.

kubectl get event --namespace abc-namespace --field-selector involvedObject.name=my-pod-zl6m6,involvedObject.kind=Pod
3

You can use the kubectl events command for this (not the kubectl get events)

Example:

kubectl events --for pod/nginx

Output:

LAST SEEN   TYPE     REASON      OBJECT      MESSAGE
10m         Normal   Pulling     Pod/nginx   Pulling image "nginx"
10m         Normal   Scheduled   Pod/nginx   Successfully assigned default/nginx to node01
9m58s       Normal   Pulled      Pod/nginx   Successfully pulled image "nginx" in 3.811s (3.811s including waiting)
9m58s       Normal   Created     Pod/nginx   Created container nginx
9m58s       Normal   Started     Pod/nginx   Started container nginx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.