Blankman
Blankman

Reputation: 267320

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

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?

Upvotes: 3

Views: 1666

Answers (3)

Mcd
Mcd

Reputation: 1

Is there a reason why you would need to get IPs of Pods rather than deploying a StatefulSet and work with stable SRV records ?

StatefulSet looks a better approach to get stable identities. Master-Slaves topologies are typical use cases for StatefulSets.

https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/

Upvotes: 0

Arghya Sadhu
Arghya Sadhu

Reputation: 44687

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.

Upvotes: 1

weibeld
weibeld

Reputation: 15322

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.

Upvotes: 5

Related Questions