Within a Kubernetes cluster catch outgoing requests from a Pod and redirect to a different target

12/29/2019

I have a cluster with 3 nodes. In each node i have a frontend application running in a Pod and backend application running in a separate Pod. I send data from the frontend application to the backend application, to do this i utilise the Cluster IP Service and k8 dns resource. I also have a function in my frontend where i send data to a separate service unrelated to my k8s cluster. I send this data using a standard AJAX request to a url with a payload i.e http://my-seperate-service-unrelated-tok8.com. All of this works correctly and the cluster operates as i want. - i have this cluster deployed to GKE. 

I now want to run this cluster local using minikube, which i have been able to do, however, when i am running locally i do not want to send data to my external service - instead i want to forward it to either a new Pod i will create or just not send it.



The problem here is i need a proxy to intercept outgoing network traffic, check if the outgoing request is the request i am looking for and if it is then redirect it.

I understand each node running in a cluster has a kube-proxy service running within the node - which is used to forward traffic to the relevant services in the cluster. 

I would like to either extend this service, or create a new proxy service where i can listen for outgoing traffic to a specific url and redirect it. 

Is this possible to do in a k8 cluster? I assume there is a Service i can create to listen for all outgoing requests and redirect specific requests based on rules i set. 



I wasn’t sure if k8 clusters have a Service already configured i can simply add to - that’s why i thought of the kube-proxy, would anyone be able to advice on this?

I wanted to add this proxy so i don’t have to change my code when its ran locally in minikube or deployed to GKE.



Any help is greatly appreciated. Thanks!

-- FeedMeData
google-kubernetes-engine
kubernetes
proxy

1 Answer

12/30/2019

I did a tool that help you to forward a service to another service,local port, service from other cluster, etc...

This way you can have exactly your same urls, ports and code... but the underlying services gets "replaced", if I understand correctly this is what you are looking for.

Here is a quick example of an stage service being replaced with my local 3000 port link service

This is the repository with more info and examples: linker-tool

If you are interested let me know if you need help or have any question.

-- Victor Jimenez
Source: StackOverflow