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 example not working #297

Closed
gittadesushil opened this issue Aug 9, 2019 · 7 comments · Fixed by #309
Closed

Port Forward example not working #297

gittadesushil opened this issue Aug 9, 2019 · 7 comments · Fixed by #309

Comments

@gittadesushil
Copy link

gittadesushil commented Aug 9, 2019

I am using Port forward example where I need to forward the port of running service. I have service running inside pod on port 3122.
When I redirect it to 8080. The url is localhost:8080 and has UI but I don't see full UI. Only I can see the service name mentioned in Title bar of browser.

Debug:

if (bytesRec == 0 || Encoding.ASCII.GetString(bytes,0,bytesRec).IndexOf("<EOF>") > -1) {  
                            break;  
}

Coming always false Encoding.ASCII.GetString(bytes,0,bytesRec).IndexOf("<EOF>") > -1

But if I execute same using kubectl command then it works perfectly fine.
kubectl port-forward service/dbdashboard 8080:3122 -n my_namespace

Note: k8s cluster is not installed on my system. I am using k8s cluster which is installed on Azure cloud.

@gittadesushil
Copy link
Author

It started working if I close the listener in while loop.
handler = listener.Accept();

while (true)
                    {
                        int bytesRec = handler.Receive(bytes);
                        stream.Write(bytes, 0, bytesRec);
                        if (bytesRec == 0 || Encoding.Default.GetString(bytes, 0, bytesRec).IndexOf("<EOF>", StringComparison.InvariantCultureIgnoreCase) > -1)
                        {
                            Console.WriteLine("We are in brake mode!");
                            break;
                        }
                        else
                        {
                            Console.WriteLine(Encoding.Default.GetString(bytes, 0, bytesRec));
                        }
                        listener.Close(); //This has to be close because if TCP connection is more than 6 then 6th request will in pending state. 
                    }

Can someone please look into my suggestion? I feel example need modification.

@blushingpenguin
Copy link

I encountered this as well, you just need to change

var demux = new StreamDemuxer(webSocket);

to

var demux = new StreamDemuxer(webSocket, StreamType.PortForward);

@gittadesushil
Copy link
Author

@blushingpenguin
After using var demux = new StreamDemuxer(webSocket, StreamType.PortForward);. still I see some request are in pending state. Resulting I don't see all resources get loads.
According to This link

Queueing. The browser queues requests when:
There are higher priority requests.
There are already six TCP connections open for this origin, which is the limit. Applies to HTTP/1.0 and HTTP/1.1 only.
The browser is briefly allocating space in the disk cache

So I think it's necessary to close listener after every request full-fill. Do you have better solution?

@blushingpenguin
Copy link

I don't see what Chrome's behaviour has to do with .NET's HttpClient? As it is the port forward sample is actually broken -- the stream isn't demuxed appropriately and includes random binary garbage which would explain why your browser isn't loading what you are port fowarding correctly. What's wrong with your code apart from that I couldn't say without seeing it.

@gittadesushil
Copy link
Author

gittadesushil commented Aug 30, 2019

@blushingpenguin I am using same example as it is.
UseCase : There is micro-service running on k8s cloud where it cannot be ingress outside. So to do that I need to use port forward so that service should available to testing team. To check this port forward is working I can hit the url in chrome and see weather everything is loaded (Service has UI stuffs in it).
Do you have correct sample of portforward which works well with k8s cluster?

@wbuck
Copy link

wbuck commented May 9, 2024

Just to be clear, is this port-forwarding example the equivalent to running the following from the command line?

kubectl port-forward <pod-name> 80:8080 --namespace "default"

@tg123
Copy link
Member

tg123 commented May 9, 2024

yours is localhost:80 -> pod 8080
the example is localhost:8080 -> pod 80

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants