From dcd671c4546d737f4a984437c979bbdc08a42745 Mon Sep 17 00:00:00 2001 From: "marcoemi.poleggi" <marco-emilio.poleggi@hesge.ch> Date: Fri, 8 Nov 2024 11:12:21 +0100 Subject: [PATCH] Update README.md with socat port-forwarding trick --- README.md | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ff8864c..b69b5c1 100644 --- a/README.md +++ b/README.md @@ -248,6 +248,11 @@ First, check the **External IP** of the load balancer: kubectl get service loadbalancer ``` +:bulb: Thanks to JSON output, you can get directly the IP address with the command: +```bash +kubectl get service loadbalancer -o=jsonpath='{.status.loadBalancer.ingress[0].ip}') +``` + Then, write a shell script that sends some (at least 10) HTTP requests in a loop via `curl`. Run your script: it should show HTTP reponses from two different IP addresses. It might take some time to show output from both instances, as metallb is not a round-robin-style load balancer. @@ -331,4 +336,31 @@ However it is possible to [set up an SSH "tunnel"](https://www.ssh.com/academy/s ```bash workstation$ kubectl get services ``` - \ No newline at end of file + + ### Expose the loadbalancer's external IP address + +As we have seen, with our cluster setup, the loadbalancer's external IP address is not routable outside the KinD host. +Without further complicating the MetalLB configuration, we can expose this address via the `socat` program, +which can do TCP-port forwarding. + +1. Install `socat` on your remote KinD host: + ```bash + kind$ apt install socat + ``` +1. Set up TCP-port-forwarding from the loadbalancer's external IP to the host's public IP: + ```bash + kind$ LB_IP=$(kubectl get service loadbalancer -o=jsonpath='{.status.loadBalancer.ingress[0].ip}') + kind$ nohup sudo socat -ly tcp-listen:80,reuseaddr,fork "tcp:${LB_IP}:80" & + kind$ sudo sh -c "echo $! > /var/run/socat.pid" + ``` + +Now you should be able to connect to your Web service from the outside: +```bash +workstation$ $ curl KIND_HOST_IP:80 +Hello from Kubernetes! My IP is 10.244.1.4 +``` + +To stop the socat port forwarding, issue: +```bash +kind$ sudo sh -c "kill $(cat /var/run/socat.pid)" +``` -- GitLab