How to connect a Phoenix app to an Elixir cluster

Posted: 11 months ago

It took me forever to get this working, so I hope it will help someone in the future.

Problem Description:

Serendeputy is a good-sized Elixir app, currently running on a dozen or so machines in a node network, with a number of singleton GenServers handling crawl dispatch and other flows.

The web servers are deliberately simple Phoenix servers in the same monolith app, effectively just doing simple reads from the database.

Where I got stuck was getting the web servers to hook into the rest of the app. It was trivial to get the regular nodes to connect to each other, but the web server was hurting.

Here’s how I finally got it working — your mileage may vary, of course:

  • Create a sys.config file in your application’s root directory.

  • Put a version of this in that file:

[{kernel,
  [
    {sync_nodes_optional, ['name@192.168.xxx.xxx']},
    {sync_nodes_timeout, 3000}
  ]}
].

replacing the xxx’s with the name and location of one of the nodes in your main application.

  • Update the command you use to launch the web server. This is what I use:
MIX_ENV=prod PORT=xxx elixir --name xxx@192.168.xxx.xxx --erl "-config sys.config" --cookie xxx -S mix phx.server

Where you replace the xxx with your own server’s details. Make sure the web server can see the node target server and that the cookies match.

This drove me nuts forever. I hope this saves someone some time in the future. (You may also find some interesting stuff in my elixir tag…)