How can I get the IP address of a unit started on a machine and use it in another unit with fleet? How can I get the IP address of a unit started on a machine and use it in another unit with fleet? docker docker

How can I get the IP address of a unit started on a machine and use it in another unit with fleet?


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:

[Unit]Description=PostgresAfter=docker.serviceRequires=flanneld.service docker.service etcd.service[Service]Restart=alwaysExecStartPre=-/usr/bin/env docker kill postgresqlExecStartPre=-/usr/bin/env docker rm postgresqlExecStartPre=/usr/bin/env docker pull sameersbn/postgresql:9.4ExecStart=/usr/bin/docker run --name postgresql sameersbn/postgresql:9.4ExecStop=-/usr/bin/docker stop postgresql

When postgres starts, I get the etcd updated with a postgres entry:

/skydns/net/tacodata/postgresql/aup1:postgresql:5432

And if I fetch that one, I';; see the ip and port:

$ etcdctl get /skydns/net/tacodata/postgresql/aup1:postgresql:5432{"host":"10.1.43.5","port":5432}

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!

root@f7e403be967a:/# host -t srv postgresql.tacodata.net 10.1.45.1Using domain server:Name: 10.1.45.1Address: 10.1.45.1#53Aliases: postgresql.tacodata.net has SRV record 10 100 5432aup1:postgresql:5432.postgresql.tacodata.net.

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


According to Greg I used SkyDNS and Registrator :

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.

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 :)


#cloud-configcoreos:  etcd:    discovery: https://discovery.etcd.io/4c8c3f9a67048e76e9075c97d9d63c0c    addr: 192.168.1.13:4001    peer-addr: 192.168.1.13:7001  fleet:    public-ip: 192.168.1.13  units:    - name: etcd.service      command: start    - name: flanneld.service      drop-ins:        - name: 50-network-config.conf          content: |            [Service]            ExecStartPre=/usr/bin/etcdctl set /coreos.com/network/config '{ "Network": "172.16.0.0/16" }'      command: start    - name: fleet.service      command: start    - name: skydns.service      enable: false      content: |        [Unit]        Description=Skydns        After=docker.service        After=fleet.service        [Service]        ExecStartPre=/usr/bin/docker pull skynetservices/skydns        ExecStart=/usr/bin/env bash -c '/usr/bin/docker run -p 53:53 --name skydns -e ETCD_MACHINES="http://$(ifconfig docker0 | awk \'/\<inet\>/ { print $2}\'):4001" skynetservices/skydns -nameservers="8.8.8.8:53" --addr="0.0.0.0:53"'        ExecStop=/usr/bin/docker stop skydns        Restart=always        [X-Fleet]        Global=true        [Install]        WantedBy=multi-user.target    - name: registrator.service      enable: false      content: |        [Unit]        Description=Registrator        After=docker.service        After=fleet.service        [Service]        ExecStartPre=/usr/bin/docker pull gliderlabs/registrator        ExecStart=/usr/bin/env bash -c '/usr/bin/docker run --net=host -p 8080:8080 -p 8443:8443 -v /var/run/docker.sock:/tmp/docker.sock --name registrator gliderlabs/registrator skydns2://$(ifconfig docker0 | awk \'/\<inet\>/ { print $2}\'):4001/skydns.local'        ExecStop=/usr/bin/docker stop registrator        Restart=always        [X-Fleet]        Global=true    - name: primordial.service      command: start      content: |        [Unit]        Description=Load and start fleet services        After=fleet.service        Requires=fleet.service        After=etcd.service        Requires=etcd.service        After=flanneld.service        Requires=flanneld.service        [Service]        Type=oneshot        RemainAfterExit=yes        ExecStartPre=/usr/bin/systemctl stop docker.service        ExecStartPre=/usr/bin/ifconfig docker0 down        ExecStartPre=/usr/bin/brctl delbr docker0        ExecStartPre=/usr/bin/systemctl start docker.service        ExecStart=/usr/bin/fleetctl start /etc/systemd/system/skydns.service        ExecStart=/usr/bin/fleetctl start /etc/systemd/system/registrator.servicessh_authorized_keys:  - ssh-rsa AAAA....