How to get notified when a pod or service or endpoints change?

Viewed 269

If I have a service, pod etc. that I can query using a selector changes its ip address, is there a way to get notified?

For example, say my application needs to have a list of ip addresses of a pod, or the ip address of a service. Since the container can go down and get recreated using kubernetes, is there a way to get notified when the containers go down and get recreated so I can then use the kubernetes API to get the latest values for the ip addresses?

This would be required for things like primary and slave databases etc.

Does kubernetes have a webhook type functionality that can be used to notify my app?

3 Answers

You can use watch API operations.

To watch all Endpoints objects:

GET /api/v1/namespaces/{namespace}/endpoints?watch=true

To watch a specific Endpoints object:

GET /api/v1/watch/namespaces/{namespace}/endpoints/{name}?watch=true

This creates a hanging HTTP GET request and you get notified whenever any of the watched objects changes.

See the Kubernetes API reference.

There is nothing out of the box. You would have to write a controller which can watch and get notified for change of a resource in kubernetes cluster ETCD store. The endpoint controller within kubernetes is an example of that because it updates the Endpoints object whenever IP of a pod behind a service changes.

Another example is ingress controllers which watches for any change in the Endpoints which holds the Pod IPs behind a service.

The watch API in the standard kubernetes client libraries is pretty efficient and widely used.

Related