blog podcast

Pod Specific Request in Kubernetes

Usually when you want to send a request to a service in kubernetes you use a Service resource. This resource has a name that is registered in the DNS and makes sure to forward your request to an appropriate pod based on a selector. Sometimes you might want to send a request to a specific pod though. In my case I want to do it because the data I want to download is quite big and it’s being saved in a distributed cache. It’s fully possible for the distributed cache to stream the data through whatever pod you are sending your request to, but it’s more efficient if you are able to send a request directly to a pod that already has the data.

To do this you must first be able to identify what pod your data is living on. I am using embedded Infinispan, and in infinispan there are conventient methods that let’s you look up on which node in your infinispan cluster (node in this context to be read as an Infinispan node, ie. one process that is part of the infinispan cluster, in Kubernetes terms this refers to one pod that is part of your cluster.). With the help of this you can identify where your data lives, now you only need to be able to send a request there.

Kubernetes registers DNS names for the specific IP addresses of your pods. But when you are using infinispan you don’t have the IP-address of the pod, instead you have the name of the pod (with some extra post-fix that is added by infinispan that you can clean up). For this name there is per default no DNS name, which means you won’t be able to use it without extra steps to send a request to your pod. One way to fix this is to create a headless service for your deployment. By doing this there are DNS records added in the form of <name>.<headless-service-name>. This helps you doing the request. In my case I wasn’t exposing port 80 on my pods but port 8080. This means I need to add the correct port as well to my host name: <name>.<service-name>:8080. By doing this I’m now finally able to send a request to my desired pod.

In my specific case I’m using a StatefulSet to perform this operation, but from my understanding I think this should also work with a Deployment. And that’s it, now you are able to route your traffic in a more direct way to your pod of choice.