I'm new to CoreOS and Docker and I'm facing with a problem with fleet.
I have a standard unit launching a POSTGRES container and I would like to know the IP address of the machine where this unit is started. I have actually a cluster of 3 machines and the POSTGRES unit isn't always started on the same machine (which means the IP is not static).
I need it when I start another unit (see below), which needs a POSTGRES.
I'm using at the moment the Unit Parameter called BindsTo :
[Unit]
Description=Test
After=docker.service
After=postgres@1.service
Requires=docker.service
Requires=postgres@1.service
BindsTo=postgres@1.service
[Service]
TimeoutStartSec=0
ExecStartPre=-/usr/bin/docker kill test%i
ExecStartPre=-/usr/bin/docker rm test%i
ExecStart=/usr/bin/docker run -rm --name test%i -e HOST="HereThePostgresIP" sryther/test
ExecStop=/usr/bin/docker stop test%i
N.B. : This unit doesn't need to be started on the same machine as the POSTGRES unit.
I tried with %H variable but it returns the hostname and not the IP address of the machine.
Flannel is also used in my cluster.
Thanks!
I found a workaround :
[Unit]
Description=Test
After=docker.service
Requires=docker.service
[Service]
TimeoutStartSec=0
ExecStartPre=-/usr/bin/docker kill test%i
ExecStartPre=-/usr/bin/docker rm test%i
ExecStart=/usr/bin/docker run -rm --name test%i --link postgres1:postgres sryther/test
ExecStop=/usr/bin/docker stop test%i
And the IP address is set in the container environment as POSTGRES_PORT_5432_TCP_ADDR.
According to Greg I used SkyDNS and Registrator :
Below you can find my working cloud-config, keep in mind that I'm using CoreOS 607 and my tests are performed on VMware. My goal was to set up 3 machines to make a small cluster with one etcd running on each machine like this.
I managed to found this solution thanks to Greg on this topic and with this cloud-config file used with Vagrant.
I hope it'll help :)
I am using registrator for discovery in my fleet environment. When I start up my fleet I modify the cloud-config file to also include registrator (along with etcd, flannel, fleet, etc). Registrator camps on docker and recognizes when new containers are started/killed/stop. Registrator manages a database of discovered containers, it does so in several flavors (like consul, skydns). I configure mine to run in skydns mode. So, here is an example of me starting up my postgres server with a unit file, I don't know where it is going to land:
When postgres starts, I get the etcd updated with a postgres entry:
And if I fetch that one, I';; see the ip and port:
You could modify your application to do that. Or, you can run the container skydns which automatically updates dns for you, so you have an SRV record and an A record. In my installation I use a test domain called tacodata.net, so, after postgresql comes up, I have dns records!
When I start my dependent applications, I make them require postgresql.service, and I simply reference ip address to the application using postgresql.tacodata.net.
-g