165

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?

Rakesh N
  • 2,450
  • 3
  • 25
  • 32

8 Answers8

311

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.

mszalbach
  • 10,612
  • 1
  • 41
  • 53
  • 1
    This doesn't work for me. I get: `Error: unknown flag: --field-selector` – toddcscar Mar 25 '19 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 Mar 26 '19 at 07:26
  • yeah. i have an older version. :( – toddcscar Mar 26 '19 at 21:43
  • @toddcscar you can still use more updated version of kubectl with an older server version: for example this will work on kubectl v1.14 against a v1.11 API – Francesco Gualazzi Feb 06 '20 at 09:01
  • 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 – froblesmartin Apr 08 '20 at 07:46
  • This command will not work correctly if your cluster contains some additional objects with the same name as your Pod in the specified namespace. To be precise, use the following command: `kubectl get event --namespace abc-namespace --field-selector involvedObject.name=my-pod-zl6m6,involvedObject.kind=Pod` – Mikolaj Sep 11 '22 at 15:40
  • And if you want to watch for changes: ` watch -d 'kubectl get event --field-selector involvedObject.name=MY_POD_NAME | tail' ` – Roman M Jan 04 '23 at 17:42
66

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=[...]`. 
    
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Jess Chen
  • 3,136
  • 1
  • 26
  • 35
30

Why not display all events and grep for your podname:

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

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"
Chandan Agarwal
  • 406
  • 5
  • 12
10

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
Jobin James
  • 916
  • 10
  • 13
  • 1
    Thanks! This also works for a pod name like this `kubectl get events --field-selector involvedObject.name=$POD_NAME -n $NAMESPACE` – morhook Nov 24 '21 at 13:31
7

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).

Bryan
  • 11,398
  • 3
  • 53
  • 78
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]
froblesmartin
  • 1,527
  • 18
  • 25
2

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
Mikolaj
  • 1,231
  • 1
  • 16
  • 34