-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Comments
/assign @iciclespider |
@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. In response to this:
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. |
@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 |
@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. |
@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):
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! |
@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 |
Hello @matthewfischl ! I wanted to do what you mention in your message ( 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 You can see my code, it is public:
An example of that
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! |
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.
The text was updated successfully, but these errors were encountered: