Comments (7)
@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.
from python.
@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!
from python.
/assign @iciclespider
from python.
@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.
from python.
@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
from python.
@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):
- aws eks update-kubeconfig --region us-east-1 --name
- kubectl port-forward service/ : -n
- 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!
from python.
@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
from python.
Related Issues (20)
- Failed to create deployment HOT 2
- Kubernetes Async API Calls with Callbacks Support HOT 9
- How to get access to the slack channel? HOT 2
- Watch stream missing job completion events HOT 1
- `KubeConfigMerger` object poorly structured when `KUBECONFIG` is a list of files. HOT 2
- Cannot change kube config at runtime HOT 4
- k8s_sdk29.0 error:no attribute 'BatchV1beta1Api' HOT 2
- Support new Kubernetes Sidecar Containers HOT 2
- When I call the query deployment interface, it returns 403 forbidden. HOT 1
- Make google-auth an optional dependency
- kubernetes.client.AuthenticationV1TokenRequest() fails HOT 1
- Allow utils.create_from_yaml to use server side apply
- Error with api when set configuration assert_hostname as true
- Azure error being logged as debug message HOT 1
- Credentials appear in debug logs HOT 2
- Update urlib3 package version and use new methods HOT 2
- kubernetes 3.1.0 is failing with python 3.12 HOT 1
- patch_namespaced_secret does not delete keys when passing them via string_data HOT 2
- Unclear Usage of V1ConfigMap HOT 2
- Stream ws_client has a bug related to the time library HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python.