Rakesh N
Rakesh N

Reputation: 2850

kubectl get events only for a pod

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

Upvotes: 195

Views: 327549

Answers (10)

Bryan
Bryan

Reputation: 12190

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/

Upvotes: 16

Rumesh Madhusanka
Rumesh Madhusanka

Reputation: 1425

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

Upvotes: 3

Nguyễn Văn Phong
Nguyễn Văn Phong

Reputation: 14218

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

Upvotes: 4

Mikolaj
Mikolaj

Reputation: 1869

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

Upvotes: 3

Jess
Jess

Reputation: 3715

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

Upvotes: 78

Jobin James
Jobin James

Reputation: 1030

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

Upvotes: 13

froblesmartin
froblesmartin

Reputation: 1861

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]

Upvotes: 4

Chandan Agarwal
Chandan Agarwal

Reputation: 466

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"

Upvotes: 14

mszalbach
mszalbach

Reputation: 11440

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.

Upvotes: 356

OneK
OneK

Reputation: 705

Why not display all events and grep for your podname:

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

Upvotes: 34

Related Questions