Is there a UUID attribute (or something similar) that is either automatically assigned to all K8S pods, or that can be globally configured for a cluster?
I have an application that acts as a database (DB) front-end in my cluster, where some (but not all pods) submit a catalog of digital "assets" they make accessible to the cluster. The pods are expected to "clean up" their entries they created in the DB before shutting down, but there are cases where the pods could crash, preventing the shutdown/cleanup code from executing.
Even if I use a pre-stop hook, the pod has already crashed, and I can no longer access data within it to know what assets it has reported to the DB application.
I was considering having the DB application, upon receiving an incoming connection, query the remote pod for IP, unique ID/UUID of some sort, etc.; and map that data to the DB information submitted by the pod. I can then have the DB application periodically poll the cluster via the K8S REST API to see if a pod with the corresponding UUID still exists, and to clear out individual records if the pod-UUID associated with them is no longer present.
You can access pod's uid by kubectl get pods/$POD_NAME -o yaml
, it is under metadata.uid
.
It is also exposed to pod by env var, but make sure your kubernetes contain this commit.
you should be using Statefulsets for running stateful containers such as databases and not deployment. you will have control over the pod names like db-0, db-1, db-2