samtechie

samtechie

How to start a cluster of nodes with Phoenix and Libcluster

Am totally new to Libcluster but I want to start two phoenix app instances on my local machine in dev mode. So this is what I have done so far based on the libcluster documentation.

This is my application.ex.

def start(_type, _args) do
  topologies = Application.get_env(:libcluster, :topologies) || []
  children = [
   # start libcluster
   {Cluster.Supervisor, [topologies, [name: MyApp.ClusterSupervisor]]},
   # ..other children..

And my config/dev.exs

config :libcluster,
topologies: [
  example: [
    # The selected clustering strategy. Required.
    strategy: Cluster.Strategy.Epmd,
    # Configuration for the provided strategy. Optional.
    config: [hosts: [:"a@127.0.0.1", :"b@127.0.0.1"]],
    # The function to use for connecting nodes. The node
    # name will be appended to the argument list. Optional
    connect: {:net_kernel, :connect_node, []},
    # The function to use for disconnecting nodes. The node
    # name will be appended to the argument list. Optional
    disconnect: {:erlang, :disconnect_node, []},
    # The function to use for listing nodes.
    # This function must return a list of node names. Optional
    list_nodes: {:erlang, :nodes, [:connected]},
  ]
]

My first question is what is the example key above? can it be renamed to anything? Should it be the application name?. Secondly, how would I start this in my shell. I tried

PORT=4000 elixir --name a@127.0.0.1 -S mix phx.server  & PORT=4001 elixir --name 
b@127.0.0.1 -S mix phx.server

It just got a warning message unable to connect to :"b@127.0.0.1" and the normal phoenix app was started. I have no idea how to do this properly. Any ideas or guidance are welcome.

Marked As Solved

crova

crova

In case you ever want to fiddle with multiple topologies and don’t want the default behavior of all nodes connecting to everyone else automatically, you’ll want to pass --erl "-connect_all false" when starting your nodes.

Regarding the port being used, make sure that there is no hardcoded “4000” in any of your config. Otherwise it won’t grab the value you’re passing from the command line.

Also Liked

antoine

antoine

In your first post in the command , you used one & so the first instance is running in back ground using the port 4000.

Try to type fg in your terminal , you will go back to the corresponding iex.

samtechie

samtechie

Thanks turns out the problem was the hardcode “4000” in the config.

jarimatti

jarimatti

The docs are not clear on this one so take this with a grain of salt: the example is a name for the topology. You can have multiple topologies in a single application with same or different strategies. I’m not sure if this is very useful in practice, but it’s possible. I usually put application name there.

The code is in the Cluster.Supervisor: libcluster/lib/supervisor.ex at a07ca5605e5cfad3da880ebda1ab6dbd5f635539 · bitwalker/libcluster · GitHub

By default all nodes in an Elixir/Erlang cluster are connected, so when there are multiple topologies it’s still a single cluster.

Where Next?

Popular in Questions Top

ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement