I have an idea to implement and i need to create a cache for the pods IPs in a kubernetes cluster and then use an http request to access the cached IPs. I'm using Golang and as i'm new in this field i would be so grateful if anyone have an idea how to implement that. I searched a lot in internet but i didn't find any simple examples to use as a start.
I started with a piece of code to get the podlist what i need is to put he podlist in a cache, like that each time a request arrives it will use the cache insead of using the kubernetes api o get the IPs.
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
fmt.Printf("Error building kubernetes clientset: %v\n", err)
os.Exit(2)}
options := metav1.ListOptions{
LabelSelector: "app=hello",}
podList, _ := kubeClient.CoreV1().Pods("namespace").List(options). What i need is to create a cache for the IPs of hello pods for-example and when an http request arrive to my http server, the request will use directly the cached IPs
I appreciate your help.
Thank you in advance,
There's only one correct answer to such question: Leave it. It's a very, very bad idea. Believe me, with such solution you'll only generate more problems that you will need to solve. What about updating such cache when a
Podgets recreated and both its name and IP changes ?Podin kubernetes is an object of ephemeral nature and it can be destroyed and recreated in totally normal circumstances e.g. as a result of scaling down the cluster and draining the nodePodsare evicted and rescheduled on a different node, with completely different names and IP addresses.The only stable manner of accessing your
Podsis via aServicethat exposes them.It's really reinventing the wheel. Every time a
Serviceis created (except Services without selectors), the correspondingEndpointsobject is created as well. And in fact it acts exactly like the caching mechanism you need. It keeps track of all IP addresses ofPodsand gets updated if aPodgets recreated and its IP changes. This way you have a guarantee that it is always up to date. When implementing any cache mechanism you would need to call the kubernetes API anyway, to make sure that aPodwith such IP still exists and if it doesn't, what was created instead of it, with what name, with what IP address. Quite bothersome, isn't it ?And it is not true that each time you access a
Podyou need to make a call to kubernetes API to get its IP address. In fact,Serviceis implemented as a set of iptables rules on each node. When the request hits the virtual IP of theServiceit actually falls into specific iptables chain and gets routed by the the kernel to the backendPod. Kubernetes networking is really wide topic, so I recommend you to read about it e.g. using the resources I attached in the end, but not going into unnecessary details it's worth mentioning that each time cluster configuration changes (e.g. somePodis recreated and gets a different IP and the respectiveEndpointsobject, that keeps track ofPodsIPs for the specificService, changes),kube-proxywhich runs as a container on every node takes care of updating the above mentioned iptables forwarding rules. If running in iptables mode (which is the most common implementation)kube-proxyconfiguresNetfilterchains so the connection is routed directly to the backend container’s endpoint by the node’s kernel.So API call is made only when the cluster configuration changes so that
kube-proxycan update iptables rules. Normally when you're accessing thePodvia aService, the traffic is routed to backendPodsbased on currentiptablesrules without a need for asking kubenetes API about the IP of suchPod.In fact, Krishna Chaurasia have already answered your question (shortly but 100% correct) by saying:
and
I can only agree with that. And my reasons for "why" have been explained in detail above.
Additional resources:
kube-proxyworks