How to get Rancher virtual IP from within a container?

I’m trying to use Consul with Rancher.
When my services start up, they fetch the IP from the container they run in.
This ends up being something like 170.60.50.40.
So when these services then try to register themselves inside Consul, consul think the services lives on the above IP.
This results in unreachable services as the containers can not use that IP for cross host communication.

Instead, what I want to get, is the virtual IP that is listed in the Rancher GUI, e.g 10.5.4.3.
If I use that IP from within the consul container, I can reach my services.

So, how do I get hold of the IP that Rancher thinks my container have, from within the container itself so that the services can annonce that IP to consul?

Regards
Roger

You can get the container 10.42.x.y IP from metadata, (http://rancher-metadata/2015-12-19/self/container/primary_ip), or with some parsing of ip addr show if installed (for Ubuntu, apt-get install iproute2)

1 Like

Thanks, this works perfectly.