Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port forward a service #2195

Closed
AlbertoSoutullo opened this issue Feb 15, 2024 · 7 comments
Closed

Port forward a service #2195

AlbertoSoutullo opened this issue Feb 15, 2024 · 7 comments
Labels
kind/feature Categorizes issue or PR as related to a new feature.

Comments

@AlbertoSoutullo
Copy link

I would like to know if there is a way to get a service, and create a port forward to my local machine. I saw that his can be done pretty easily with external libraries, but I would like to stick with only one K8s library.

My idea is to forward whatever Thanos pod is available through it's service to my local machine, and then make queries to download some metrics to analyze them locally.

Looking at the documentation and other issues, like pod_porforward.py or #2138, but those couldn't really help me, since they are specific to pods, and I would like to do it through a service.

Is there a way to do this? Or I am not completely understanding the documentation?

Regards.

@AlbertoSoutullo AlbertoSoutullo added the kind/feature Categorizes issue or PR as related to a new feature. label Feb 15, 2024
@roycaihw
Copy link
Member

/assign @iciclespider

@k8s-ci-robot
Copy link
Contributor

@roycaihw: GitHub didn't allow me to assign the following users: iciclespider.

Note that only kubernetes-client members with read permissions, repo collaborators and people who have commented on this issue/PR can be assigned. Additionally, issues/PRs can only have 10 assignees at the same time.
For more information please see the contributor guide

In response to this:

/assign @iciclespider

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@iciclespider
Copy link
Contributor

iciclespider commented Feb 26, 2024

@AlbertoSoutullo, use the kubernetes apis to obtain the service and then search for a pod that matches the port you are trying to attach to. This is exactly what the kubectl cli does behind the scenes. The example you linked to also does exactly that here: https://github.com/kubernetes-client/python/blob/master/examples/pod_portforward.py#L143-L171

@AlbertoSoutullo
Copy link
Author

@iciclespider That makes perfect sense now that I think about it. Thank you very much! I will try this and if I find any problem I will reopen this.

@matthewfischl
Copy link

matthewfischl commented May 17, 2024

@AlbertoSoutullo did you find a solution that worked for you? If so, would you mind sharing?

Not sure if our goals are the same, but I am currently using port forwarding to enable local testing of an API.

For example, PRIOR to running a python request to an API, I would need to do the following (I started doing this in a command line then shifted to executing these with a python subprocess):

  1. aws eks update-kubeconfig --region us-east-1 --name
  2. kubectl port-forward service/ : -n
  3. Then make a request in python like: request.get("http://localhost:local port")

However, this approach would require running kubectl for EVERY service that I would want to forward AND doing this with python subprocesses seems undesirable (at least how I implemented it!)

So, ultimately I am trying to replace step #2 with something from the Kubernetes client library. I was hoping that doing so would provide a localhost url that requests could be sent to. However, I'm not exactly sure if that is what the Kubernetes library is designed for?

So, are you trying to do something similar? Any thoughts? Thanks!

@iciclespider
Copy link
Contributor

iciclespider commented May 17, 2024

@matthewfischl The port forwarding ability of the python client does exactly what your step 2 is doing. Here is an example of your step 2, but just in the python code: https://github.com/kubernetes-client/python/blob/master/examples/pod_portforward.py

@AlbertoSoutullo
Copy link
Author

AlbertoSoutullo commented May 17, 2024

@AlbertoSoutullo did you find a solution that worked for you? If so, would you mind sharing?

Hello @matthewfischl ! I wanted to do what you mention in your message (I was hoping that doing so would provide a localhost url that requests could be sent to.).
What I implemented some time ago was something that helped me to keep advancing with what I wanted to do, but I am not still sure it is the “best” approach.

From my understanding, and also something that they comment at the beginning of the pod port forward example, is that the behavior is not exactly the same. I mean, you will not open a port where you can send request command to that address:port.

What you will do is monkey patch the create_connection function from socket. So basically, every time you do a request, this will automatically be called underneath, creating a connection to your Kubernetes cluster without the need of opening ports. Note that I had to use urllib_request.urlopen(request) instead of your request.get().

You can see my code, it is public:

  • Here I am creating the function that will act inside urllib_request.urlopen(request).
  • Here I am doing the monkey patch.
  • Here is the function I use in the previous link, where I am querying Kubernetes.

An example of that request parameter would be something like:

http://thanos-query.svc.thanos.kubernetes:9090/api/v1/query_range?query=libp2p-peers&start=1715769060.0&end=1715770080.0&step=60s

I am using this to basically scrape a lot of metrics through Thanos, for which I have a service that cannot be access from outside the cluster unless you use a port forward, or this kind of approach. Then I just parse the data accordingly with what I want to do.

I hope this can help. Again, I am not sure if this is the optimal approach. Like you, I also had in mind something like "opening a local port where I can query with standard requests", but as far as I know, the port forward of this library doesn't work like that.

Let me know if I can help you with something else!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature.
Projects
None yet
Development

No branches or pull requests

5 participants