Dear @simos, thanks for your detailed explanations on the LXC port-forwarding, which taught me so much and led me to dive deep into this topic. I might have tested all the possibilities of manipulating the forwarding on my 3-node cloud based on Ubuntu 20.04 server, and the LXD/LXC version is 4.0.8.
My nodes use the IP address in the range 10.0.0.0/8, while my LXC instances use the IP address in the range 240.0.0.0/8. By the way, I run a Tomcat 9 instance in the container.
Test 1 - Configure on the very host, the first node with an IP address 10.10.4.86, on which the LXC instance, named mycontainer is running. The IP address of the instance is 240.4.186.19.
Method 1: lxc config device add mycontainer port8082-lxc8080 proxy listen=tcp:0.0.0.0:8082 connect=tcp:127.0.0.1:8080 (Successful as expected)
Note: This is better as my container gets its IP address dynamically.
Method 2: lxc config device add mycontainer port8082-lxc8080 proxy listen=tcp:0.0.0.0:8082 connect=tcp:240.4.186.19:8080 (Successful as expected)
Method 3: lxc config device add mycontainer port8082-lxc8080 proxy listen=tcp:10.10.4.186:8082 connect=tcp:240.4.186.19:8080 (failed)
Error: Failed to start device "port8082-lxc8080": Error occurred when starting proxy device: Error: Failed to listen on 10.10.4.186:8082: listen tcp 10.10.4.186:8082: bind: cannot assign requested address
The following error messages are identical.
Method 4: lxc config device add mycontainer port8082-lxc8080 proxy listen=tcp:10.10.4.186:8082 connect=tcp:127.0.0.1:8080 (failed)
Test 2 - Configure on the second node, whose IP address is 10.10.4.185, rather than the node on which mycontainer is running.
Aim: To test whether we can access the service the other nodes on which the container is NOT running (failed).
Method 1: lxc config device add mycontainer port8082-lxc8080 proxy listen=tcp:0.0.0.0:8082 connect=tcp:240.4.186.19:8080 (Problematic)
Note: The configuration is successful, bu the service is inaccessible.
Method 2: lxc config device add mycontainer port8082-lxc8080 proxy listen=tcp:10.10.4.185:8082 connect=tcp:240.4.186.19:8080 (failed)
Test 3 - Configure on the third node, whose IP address is 10.10.5.113, but specify the host IP address as the very host on which the container is running.
Aim: To test whether we can configure on any node as we did in Test 1 (failed).
Method 1: lxc config device add mycontainer port8082-lxc8080 proxy listen=tcp:10.10.4.186:8082 connect=tcp:240.4.186.19:8080 (failed)
Method 2: lxc config device add mycontainer port8082-lxc8080 proxy listen=tcp:10.10.4.186:8082 connect=tcp:127.0.0.1:8080 (failed)
Test 4 - Manually edit the configuration, but there’s no success when testing the service (failed).
lxc config edit mycontainer
My conclusions:
(1) Port-forwarding can only be configured on the very host that the container is running.
(2) To easily control the request dispatching to the containers, a reverse proxy seems to be the only option as indicated by @simos.