At Find a Player, we’ve been trying to figure out how we want to cluster our Elixir servers - so that they can share state between each other - recently. Because we come from ruby (ie, not a high-availability background), when we want to discover other machines in a network we’d traditionally use something like Consul - of which I wrote a ruby library called Diplomat (which admittedly needs some love).
However, after chatting with Gordon
Guthrie - a guy who’s been working with
Erlang for over 13 years - over twitter, he told us to checkout
:net_adm.world/0. Which does pretty much exactly what we want, straight out
of the box.
So, if you are building an Elixir app with Autoscaling and AWS, here is how to autoconnect your nodes:
Step 1 - ensure all nodes have the correct hostname set up:
On each node, make sure that the hostname is set to the private DNS:
Step 2 - build a
If you have awscli installed (highly recommended) then you can run this at some point before running the app to build the file:
The above script will find matching instances, print out their private DNS, and produce a file in the format below:
Step 3 - Set the hostname in vm.args:
Make sure the hostname is setup correctly in your vm.args. Here’s a one-liner to do it using sed:
Step 4 - Attempt to join the cluster
In your code, add a module which checks if the
.hosts.erlang file exists, and
if so, use it to connect to one of the nodes.