Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes Watch Pod Events with api

Tags:

kubernetes

We are interested in running certain commands as pods and services, as they start or stop. Using the life-cycle hooks in the yml files does not work for us, since these commands are not optional. We have considered running a watcher pod that uses the watch api to run these commands. But we can't figure out how to use the watch api so that it does not keep sending the same events again and again. Is there a way to tell the watch api to only send new events since connection was opened? If expecting a stateful watch api is unreasonable, will it be possible to pass it a timestamp or a monotonically increasing id to avoid getting already seen events?

Basically what we are doing now we are running a pod with a daemon process that communicates with the api. we can find the events as stream. But we are interested to run some task when a pod created or deleted.

like image 647
sadlil Avatar asked Feb 04 '16 04:02

sadlil


People also ask

How can I watch Kubernetes events?

To collect or watch the events, you can run kubectl get events --watch in deployment and collect the output with a third-party logging tool. To watch Kubernetes events, many free and paid third-party tools help provide visibility and reporting of events in a Kubernetes cluster resource.

How do you check event logs in Kubernetes pod?

Access event logs Developers, application and infrastructure operators can use the kubectl describe command against specific resources, or use the more generic kubectl get event command to list events for a specific resource, or for the entire cluster.

How do you interact with Kubernetes API?

The easiest way to get started with the Kubernetes API is by using kubectl. Run kubectl proxy --port=8080, and as long as the command is successful, you should be able to open your browser. Go to http://localhost:8080 and get a response, telling you what paths are available to be queried.


1 Answers

I have found the answer. In case anyone else is watching.

There is a much better system to watch resource and handle events with custom tasks with pkg/controller/framework package

I found the step like this,

1. initiate a framework.NewInFormer
2. Run the controller
3. the NewInFormer loads with your custom event handlers that will call when the events occured.
like image 82
sadlil Avatar answered Oct 14 '22 05:10

sadlil