Skip to content
This repository has been archived by the owner on Feb 24, 2020. It is now read-only.

private-net: should forwarded ports work on localhost? #1256

Closed
steveej opened this issue Aug 7, 2015 · 10 comments
Closed

private-net: should forwarded ports work on localhost? #1256

steveej opened this issue Aug 7, 2015 · 10 comments

Comments

@steveej
Copy link
Contributor

steveej commented Aug 7, 2015

Currently ports that are forwarded to containers are not accessible on the host via localhost:forwarded-port. It would be possible to do enable that and the technique is generally called NAT loopback.

It's a matter of choice if we want to change this or not so I want to bring it up for discussion.

Personally I'm unsure if we need it, but it could help in situations when service discovery is not there and the host expects a service on localhost while the container is not supposed to share the host netns.

@alban
Copy link
Member

alban commented Aug 7, 2015

Currently ports that are forwarded to containers are not accessible on the host via localhost:forwarded-port.

Do you have an example of commands to show what is not available? I tried the following:

sudo ./build-rkt-0.7.0+git/bin/rkt --local --insecure-skip-verify run --mds-register=false --volume volume-data,kind=host,source=/tmp/redis docker://redis

It exposes the port 6379:

        "ports": [
            {
                "name": "6379-tcp",
                "protocol": "tcp",
                "port": 6379,
                "count": 1,
                "socketActivated": false
            }
        ]

And then on the host: nc localhost 6379 and I could talk successfully to redis in the pod.

I also tried to setup a ssh forwarded port with ssh -L 6378:localhost:6379 localhost and to connect with nc localhost 6378 and it worked as well.

@alban
Copy link
Member

alban commented Aug 7, 2015

Oh sorry, I should not use the defaut network namespace for testing this. So the full command I tried is:

sudo ./build-rkt-0.7.0+git/bin/rkt --local --insecure-skip-verify run --mds-register=false --volume volume-data,kind=host,source=/tmp/redis --private-net=default --port=6379-tcp:8080 docker://redis

And then I have the behavior you described when trying with nc 192.168.0.13 8080 (it works) and nc localhost 8080 (it does not work).

Do you know if kube-proxy uses localhost to connect to a kubernetes endpoint?

@steveej steveej changed the title should forwarded ports work on localhost? private-net: should forwarded ports work on localhost? Aug 7, 2015
@jonboulle jonboulle added this to the v0.12.0 milestone Nov 6, 2015
@alban
Copy link
Member

alban commented Nov 25, 2015

@eyakubovich @steveej ping?

@steveej
Copy link
Contributor Author

steveej commented Nov 25, 2015

I'm okay with the current behavior as I would not expect it to behave differently. I brought up the question to see what the general assumption would be. We have to make the decision if we leave it as is (my vote) or if we enable NAT loopback.

@jonboulle jonboulle modified the milestones: v0.13.0, v0.12.0 Nov 25, 2015
@jonboulle
Copy link
Contributor

It seems pretty unintuitive to me. Can you help explain your vote?

@blalor
Copy link

blalor commented Dec 11, 2015

I was bitten by this yesterday. I think the principle of least surprise would dictate that the forwarded port is also reachable via localhost. I'd be fine with it being a runtime option (to --port or something). But in the case of CoreOS where everything must run as a container, it really would simplify connecting to services if I didn't first have to look up the local host's IP, or depend on $HOSTNAME or $( hostname ).

@yorickvP
Copy link

yorickvP commented Jan 3, 2016

I was debugging this for a few hours yesterday, trying to reach a specific container from the host is really hard without doing this, I don't think there's any disadvantage to making it work on localhost.

@steveej
Copy link
Contributor Author

steveej commented Jan 6, 2016

@blalor @yorickvP thanks for your feedback on this, supporting @jonboulle's intuition. This topic has come up often enough lately to convince me that we should have NAT loopback.

@iaguis
Copy link
Member

iaguis commented Jan 19, 2016

@steveej Any chance we can get this for the next release?

@alban alban modified the milestones: v1.0.0, v0.16.0 Jan 20, 2016
@steveej
Copy link
Contributor Author

steveej commented Jan 22, 2016

@iaguis it looks like we're having trouble getting this in at all.

Rewriting localhost:forwarded-port -> container-IP:forwarded-port doesn't work by design with iptables.

As an example, the following rule

-A OUTPUT -d 127.0.0.1/32 -p tcp -m tcp --dport 1024 -j DNAT --to-destination 172.16.28.2:1024

Will be matched, but it will leave the package destination unchanged, as can be seen in the TRACE output:

Jan 22 21:47:05 steveej-laptop kernel: TRACE: raw:OUTPUT:policy:2 IN= OUT=lo SRC=127.0.0.1 DST=127.0.0.1 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=55893 DF PROTO=TCP SPT=39992 DPT=1024 SEQ=1195156359 ACK=0 WINDOW=43690 RES=0x00 SYN URGP=0 OPT (0204FFD70402080A029579D20000000001030307) UID=1000 GID=100 
Jan 22 21:47:05 steveej-laptop kernel: TRACE: mangle:OUTPUT:policy:1 IN= OUT=lo SRC=127.0.0.1 DST=127.0.0.1 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=55893 DF PROTO=TCP SPT=39992 DPT=1024 SEQ=1195156359 ACK=0 WINDOW=43690 RES=0x00 SYN URGP=0 OPT (0204FFD70402080A029579D20000000001030307) UID=1000 GID=100 
Jan 22 21:47:05 steveej-laptop kernel: TRACE: nat:OUTPUT:rule:1 IN= OUT=lo SRC=127.0.0.1 DST=127.0.0.1 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=55893 DF PROTO=TCP SPT=39992 DPT=1024 SEQ=1195156359 ACK=0 WINDOW=43690 RES=0x00 SYN URGP=0 OPT (0204FFD70402080A029579D20000000001030307) UID=1000 GID=100

Any suggestions are welcome!


UPDATE: Fortunately I was wrong with my conclusion from last evening. I'll prepare a PR with a proposal.

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

No branches or pull requests

6 participants